niujiuru 3 тижнів тому
батько
коміт
16e1e499b9
2 змінених файлів з 33 додано та 128 видалено
  1. 33 18
      mcu_ctrl_board/bridge.go
  2. 0 110
      utils/jsonrpc2/rpc.go

+ 33 - 18
mcu_ctrl_board/bridge.go

@@ -10,8 +10,8 @@ import (
 	"sync"
 	"time"
 
-	netmgrd "hnyfkj.com.cn/rtu/linux/netmgrd"
-	"hnyfkj.com.cn/rtu/xy_v/utils/jsonrpc2"
+	"hnyfkj.com.cn/rtu/linux/netmgrd"
+	"hnyfkj.com.cn/rtu/linux/utils/jsonrpc2"
 )
 
 // 打开与MCU控制板的串口通讯
@@ -33,15 +33,15 @@ func mcuCtrlBoard_ComExit() error {
 }
 
 // 定义_RTU_数据板的工作状态
-type WorkState string
+type WorkState uint32
 
 const (
-	Idle                WorkState = "idle"                  // 空闲(默认)
-	AppUpgrading        WorkState = "app_upgrading"         // 应用程序-升级中
-	PhotoCapturing      WorkState = "photo_capturing"       // 相机数据-拍照中
-	PhotoUploading      WorkState = "photo_uploading"       // 相机数据-上传中
-	SensorDataReceiving WorkState = "sensor_data_receiving" // 环境数据-收集中
-	SensorDataUploading WorkState = "sensor_data_uploading" // 环境数据-上传中
+	Idle                WorkState = 0      // 空闲(默认)
+	AppUpgrading        WorkState = 1 << 0 // 应用程序-升级中
+	PhotoCapturing      WorkState = 1 << 1 // 相机数据-拍照中
+	PhotoUploading      WorkState = 1 << 2 // 相机数据-上传中
+	SensorDataReceiving WorkState = 1 << 3 // 环境数据-收集中
+	SensorDataUploading WorkState = 1 << 4 // 环境数据-上传中
 )
 
 // RTU数据板的工作状态管理器
@@ -50,22 +50,37 @@ type WorkStateMgr struct {
 	state WorkState
 }
 
-var WSMgr = &WorkStateMgr{state: Idle}
+var GlobalWorkState = &WorkStateMgr{state: Idle}
 
-// 设置_RTU_数据板的工作状态
-func (mgr *WorkStateMgr) Set(s WorkState) {
+// 工作状态管理-添加一个状态
+func (mgr *WorkStateMgr) Add(s WorkState) {
 	mgr.mu.Lock()
 	defer mgr.mu.Unlock()
-	mgr.state = s
+	mgr.state |= s
 }
 
-// 获取_RTU_数据板的工作状态
+// 工作状态管理-清除一个状态
+func (mgr *WorkStateMgr) Remove(s WorkState) {
+	mgr.mu.Lock()
+	defer mgr.mu.Unlock()
+	mgr.state &^= s
+}
+
+// 工作状态管理-获取当前状态
+// 判断某个状态是否存在
 func (mgr *WorkStateMgr) Get() WorkState {
 	mgr.mu.RLock()
 	defer mgr.mu.RUnlock()
 	return mgr.state
 }
 
+// 工作状态管理-状态是否存在
+func (mgr *WorkStateMgr) Has(s WorkState) bool {
+	mgr.mu.RLock()
+	defer mgr.mu.RUnlock()
+	return mgr.state&s != 0
+}
+
 //export RTU_ProcessCommand
 func RTU_ProcessCommand(request *C.char) *C.char {
 	r, err := jsonrpc2.ParseRequest(C.GoString(request))
@@ -84,8 +99,8 @@ func RTU_ProcessCommand(request *C.char) *C.char {
 	} else {
 		switch r.Method {
 		// 控制板查询数据板状态
-		case "get_mcu_status":
-			w = call(handleGetMCUStatus)
+		case "get_rtu_status":
+			w = call(handleGetRTUStatus)
 		// 控制板发送传感器数据
 		case "send_sensor_data":
 			w = call(handleSendSensorData)
@@ -108,7 +123,7 @@ func RTU_ProcessCommand(request *C.char) *C.char {
 }
 
 // 控制板查询数据板状态
-func handleGetMCUStatus(r *jsonrpc2.Request) (*jsonrpc2.Response, error) {
+func handleGetRTUStatus(r *jsonrpc2.Request) (*jsonrpc2.Response, error) {
 	netst := "offline"
 	if netmgrd.IsInetAvailable() {
 		netst = "online"
@@ -121,7 +136,7 @@ func handleGetMCUStatus(r *jsonrpc2.Request) (*jsonrpc2.Response, error) {
 	}
 
 	wrkst := "idle"
-	if WSMgr.Get() != Idle {
+	if GlobalWorkState.Get() != Idle {
 		wrkst = "busy"
 	}
 

+ 0 - 110
utils/jsonrpc2/rpc.go

@@ -1,110 +0,0 @@
-// Author: NiuJiuRu
-// Email: niujiuru@qq.com
-
-package jsonrpc2
-
-import (
-	"encoding/json"
-	"fmt"
-)
-
-type ErrorCode int
-
-const (
-	ErrParse          ErrorCode = -32700 // 解析错误: "Parse error"
-	ErrInvalidRequest ErrorCode = -32600 // 无效请求: "Invalid request"
-	ErrMethodNotFound ErrorCode = -32601 // 无效方法: "Method not found"
-	ErrInvalidParams  ErrorCode = -32602 // 无效参数: "Invalid params"
-	ErrInternal       ErrorCode = -32603 // 内部错误: "Internal error"
-)
-
-type Request struct {
-	JSONRPC string          `json:"jsonrpc"`          // 版本号, 固定: "2.0"
-	Method  string          `json:"method"`           // 调用方法, 执行函数名
-	Params  json.RawMessage `json:"params,omitempty"` // 请求参数, 可选可为空
-	ID      int             `json:"id"`               // 请求ID, 用于标识请求
-}
-
-type Error struct {
-	Code    ErrorCode `json:"code"`    // 错误码
-	Message string    `json:"message"` // 错误信息
-}
-
-func (e Error) Error() string {
-	return e.Message
-}
-
-type Response struct {
-	JSONRPC string          `json:"jsonrpc"`          // 版本号, 固定: "2.0"
-	Result  json.RawMessage `json:"result,omitempty"` // 响应结果, 可选可为空
-	Error   *Error          `json:"error,omitempty"`  // 错误信息, 可选可为空
-	ID      int             `json:"id"`               // 应答ID, 响应匹配请求
-}
-
-// 解析请求数据
-func ParseRequest(jsonStr string) (*Request, error) {
-	var req Request
-	if err := json.Unmarshal([]byte(jsonStr), &req); err != nil {
-		return nil, err
-	}
-	return &req, nil
-}
-
-// 构建响应数据
-func BuildResult(id int, result any) (*Response, error) {
-	b, err := json.Marshal(result)
-	if err != nil {
-		return nil, err
-	}
-	return &Response{
-		JSONRPC: "2.0",
-		Result:  b,
-		ID:      id,
-	}, nil
-}
-
-// 构建错误响应
-func BuildError(id int, code ErrorCode, message string) *Response {
-	return &Response{
-		JSONRPC: "2.0",
-		Error: &Error{
-			Code:    code,
-			Message: message,
-		},
-		ID: id,
-	}
-}
-
-// 构建解析错误
-func BuildParseError(id int) *Response {
-	return BuildError(id, ErrParse, "Parse error")
-}
-
-// 构建无效请求
-func BuildInvalidRequest(id int) *Response {
-	return BuildError(id, ErrInvalidRequest, "Invalid request")
-}
-
-// 构建无效方法
-func BuildMethodNotFound(id int) *Response {
-	return BuildError(id, ErrMethodNotFound, "Method not found")
-}
-
-// 构建无效参数
-func BuildInvalidParams(id int) *Response {
-	return BuildError(id, ErrInvalidParams, "Invalid params")
-}
-
-// 构建内部错误
-func BuildInternalError(id int) *Response {
-	return BuildError(id, ErrInternal, "Internal error")
-}
-
-// 应答转字符串
-func (resp *Response) String() (string, error) {
-	b, err := json.Marshal(resp)
-	if err != nil {
-		return "", fmt.Errorf("marshal response: %w", err)
-	}
-	return string(b), nil
-}