niujiuru 3 недель назад
Родитель
Сommit
e49cbb0407
2 измененных файлов с 95 добавлено и 37 удалено
  1. 60 9
      mcu_ctrl_board/bridge.go
  2. 35 28
      utils/jsonrpc2/rpc.go

+ 60 - 9
mcu_ctrl_board/bridge.go

@@ -7,6 +7,7 @@ import "C"
 
 import (
 	"fmt"
+	"sync"
 
 	"hnyfkj.com.cn/rtu/xy_v/utils/jsonrpc2"
 )
@@ -29,26 +30,56 @@ func mcuCtrlBoard_ComExit() error {
 	return nil
 }
 
+type WorkState string
+
+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" // 环境数据-上传中
+)
+
+type WorkStateMgr struct {
+	mu    sync.RWMutex
+	state WorkState
+}
+
+var WSMgr = &WorkStateMgr{state: Idle}
+
+func (mgr *WorkStateMgr) Set(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
+}
+
 //export RTU_ProcessCommand
 func RTU_ProcessCommand(request *C.char) *C.char {
 	r, err := jsonrpc2.ParseRequest(C.GoString(request))
-	var w *jsonrpc2.RpcResponse
+	var w *jsonrpc2.Response
 	if err != nil {
 		w = jsonrpc2.BuildParseError(r.ID)
 	} else {
 		switch r.Method {
-		// Todo: 控制板查询数据板状态
+		// 控制板查询数据板状态
 		case "get_mcu_status":
-			fallthrough
-		// Todo: 控制板发送传感器数据
+			w = handleGetMCUStatus(r)
+		// 控制板发送传感器数据
 		case "send_sensor_data":
-			fallthrough
-		// Todo: 控制板请求数据板拍照
+			w = handleSendSensorData(r)
+		// 控制板请求数据板拍照
 		case "take_photo":
-			fallthrough
-		// Todo: 控制板发送掉电预通知
+			w = handleTakePhoto(r)
+		// 控制板发送掉电通知
 		case "power_down":
-			fallthrough
+			w = handlePowerDown(r)
 		default:
 			w = jsonrpc2.BuildMethodNotFound(r.ID)
 		}
@@ -59,3 +90,23 @@ func RTU_ProcessCommand(request *C.char) *C.char {
 	}
 	return C.CString(b)
 }
+
+// 控制板查询数据板状态
+func handleGetMCUStatus(r *jsonrpc2.Request) *jsonrpc2.Response {
+	return jsonrpc2.BuildInternalError(r.ID)
+}
+
+// 控制板发送传感器数据
+func handleSendSensorData(r *jsonrpc2.Request) *jsonrpc2.Response {
+	return jsonrpc2.BuildInternalError(r.ID)
+}
+
+// 控制板请求数据板拍照
+func handleTakePhoto(r *jsonrpc2.Request) *jsonrpc2.Response {
+	return jsonrpc2.BuildInternalError(r.ID)
+}
+
+// 控制板发送预掉电通知
+func handlePowerDown(r *jsonrpc2.Request) *jsonrpc2.Response {
+	return jsonrpc2.BuildInternalError(r.ID)
+}

+ 35 - 28
utils/jsonrpc2/rpc.go

@@ -3,40 +3,47 @@
 
 package jsonrpc2
 
-import "encoding/json"
+import (
+	"encoding/json"
+	"fmt"
+)
 
-type RpcErrorCode int
+type ErrorCode int
 
 const (
-	ErrParse          RpcErrorCode = -32700 // Parse error			(解析错误)
-	ErrInvalidRequest RpcErrorCode = -32600 // Invalid Request	(无效请求)
-	ErrMethodNotFound RpcErrorCode = -32601 // Method not found	(无效方法)
-	ErrInvalidParams  RpcErrorCode = -32602 // Invalid params		(无效参数)
-	ErrInternal       RpcErrorCode = -32603 // Internal error		(内部错误)
+	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 RpcRequest struct {
+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 RpcError struct {
-	Code    RpcErrorCode `json:"code"`    // 错误码
-	Message string       `json:"message"` // 错误信息
+type Error struct {
+	Code    ErrorCode `json:"code"`    // 错误码
+	Message string    `json:"message"` // 错误信息
+}
+
+func (e Error) Error() string {
+	return e.Message
 }
 
-type RpcResponse struct {
+type Response struct {
 	JSONRPC string          `json:"jsonrpc"`          // 版本号, 固定: "2.0"
 	Result  json.RawMessage `json:"result,omitempty"` // 响应结果, 可选可为空
-	Error   *RpcError       `json:"error,omitempty"`  // 错误信息, 可选可为空
+	Error   *Error          `json:"error,omitempty"`  // 错误信息, 可选可为空
 	ID      int             `json:"id"`               // 应答ID, 响应匹配请求
 }
 
 // 解析请求数据
-func ParseRequest(jsonStr string) (*RpcRequest, error) {
-	var req RpcRequest
+func ParseRequest(jsonStr string) (*Request, error) {
+	var req Request
 	if err := json.Unmarshal([]byte(jsonStr), &req); err != nil {
 		return nil, err
 	}
@@ -44,12 +51,12 @@ func ParseRequest(jsonStr string) (*RpcRequest, error) {
 }
 
 // 构建响应数据
-func BuildResult(id int, result any) (*RpcResponse, error) {
+func BuildResult(id int, result any) (*Response, error) {
 	b, err := json.Marshal(result)
 	if err != nil {
 		return nil, err
 	}
-	return &RpcResponse{
+	return &Response{
 		JSONRPC: "2.0",
 		Result:  b,
 		ID:      id,
@@ -57,10 +64,10 @@ func BuildResult(id int, result any) (*RpcResponse, error) {
 }
 
 // 构建错误响应
-func BuildError(id int, code RpcErrorCode, message string) *RpcResponse {
-	return &RpcResponse{
+func BuildError(id int, code ErrorCode, message string) *Response {
+	return &Response{
 		JSONRPC: "2.0",
-		Error: &RpcError{
+		Error: &Error{
 			Code:    code,
 			Message: message,
 		},
@@ -69,35 +76,35 @@ func BuildError(id int, code RpcErrorCode, message string) *RpcResponse {
 }
 
 // 构建解析错误
-func BuildParseError(id int) *RpcResponse {
+func BuildParseError(id int) *Response {
 	return BuildError(id, ErrParse, "Parse error")
 }
 
 // 构建无效请求
-func BuildInvalidRequest(id int) *RpcResponse {
-	return BuildError(id, ErrInvalidRequest, "Invalid Request")
+func BuildInvalidRequest(id int) *Response {
+	return BuildError(id, ErrInvalidRequest, "Invalid request")
 }
 
 // 构建无效方法
-func BuildMethodNotFound(id int) *RpcResponse {
+func BuildMethodNotFound(id int) *Response {
 	return BuildError(id, ErrMethodNotFound, "Method not found")
 }
 
 // 构建无效参数
-func BuildInvalidParams(id int) *RpcResponse {
+func BuildInvalidParams(id int) *Response {
 	return BuildError(id, ErrInvalidParams, "Invalid params")
 }
 
 // 构建内部错误
-func BuildInternalError(id int) *RpcResponse {
+func BuildInternalError(id int) *Response {
 	return BuildError(id, ErrInternal, "Internal error")
 }
 
 // 应答转字符串
-func (resp *RpcResponse) String() (string, error) {
+func (resp *Response) String() (string, error) {
 	b, err := json.Marshal(resp)
 	if err != nil {
-		return "", err
+		return "", fmt.Errorf("marshal response: %w", err)
 	}
 	return string(b), nil
 }