qixiaoning
2025-07-08 fe724b50b3f1b3dfe2219eb9af4bcca96c89a158
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package service
 
import (
    "encoding/json"
    "net/http"
 
    "basic.com/valib/go-aiot.git/aiotProto/aiot"
    "basic.com/valib/go-aiot.git/client"
    "basic.com/valib/logger.git"
    "go.uber.org/zap"
)
 
type Client struct {
}
 
var cliCon *Client
 
// 心跳callback
func (c *Client) OnHeartBeat(cli *client.Client, msg *aiot.Protocol) error {
    logger.Debug("OnHeartBeat...")
    return nil
}
 
// 设备注册callback 初始化操作
func (c *Client) OnRegister(cli *client.Client, msg *aiot.Protocol) error {
    logger.Debug("OnRequest...", zap.Any("msg", msg))
    // 服务注册完成后执行其他操作
    // 启动数据同步
    startSync(cli)
    return nil
}
 
// 收到请求
func (c *Client) OnRequest(cli *client.Client, msg *aiot.Protocol) error {
    logger.Debug("OnRequest...", zap.Any("msg", msg))
    reply := &aiot.BusinessReply{}
    replyMsg := &aiot.Protocol{
        Receiver: aiot.RECEIVER_TO_SAAS,
        SenderId: cli.GetDeviceId(),
        MsgType:  aiot.MSG_TYPE_BUSINESS,
        ReqType:  aiot.REQ_TYPE_RESPONSE,
        MsgProto: client.GetMsgProto(msg.MsgProto.MsgId),
    }
    switch msg.Receiver {
    case aiot.RECEIVER_TO_NODE:
        // 单节点请求
        logger.Debug("to node")
 
        // 解码数据
        req := &aiot.NodeReq{}
        err := json.Unmarshal(msg.Data, req)
        if err != nil {
            reply.Success = false
            reply.Code = http.StatusBadRequest
            reply.Msg = "can not Unmarshal req," + err.Error()
            replyMsg.Data, _ = json.Marshal(reply)
            _ = cli.WriteBody(replyMsg)
            return err
        }
 
        // 处理请求
        err = NodeReq(msg, req, reply)
        if err != nil {
            reply.Success = false
            reply.Code = http.StatusBadRequest
            reply.Msg = "execute, " + err.Error()
            replyMsg.Data, _ = json.Marshal(reply)
            _ = cli.WriteBody(replyMsg)
            return err
        }
 
        // 回写结果
        logger.Debug("to node -- results ", reply)
        replyMsg.Data, _ = json.Marshal(reply)
        _ = cli.WriteBody(replyMsg)
        break
    case aiot.RECEIVER_TO_MASTER:
        // 主节点请求
        logger.Debug("to master")
 
        // 解码数据
        req := &aiot.NodeReq{}
        err := json.Unmarshal(msg.Data, req)
        if err != nil {
            reply.Success = false
            reply.Code = http.StatusBadRequest
            reply.Msg = "can not Unmarshal req," + err.Error()
            replyMsg.Data, _ = json.Marshal(reply)
            _ = cli.WriteBody(replyMsg)
            return err
        }
 
        // 处理请求
        err = NodeReq(msg, req, reply)
        if err != nil {
            reply.Success = false
            reply.Code = http.StatusBadRequest
            reply.Msg = "execute, " + err.Error()
            replyMsg.Data, _ = json.Marshal(reply)
            _ = cli.WriteBody(replyMsg)
            return err
        }
 
        // 回写结果
        logger.Debug("to master -- results ", reply)
        replyMsg.Data, _ = json.Marshal(reply)
        _ = cli.WriteBody(replyMsg)
        break
    case aiot.RECEIVER_TO_NODE_LIST:
        logger.Debug("to node list")
        // todo 多节点请求 待完成
        break
    case aiot.RECEIVER_TO_CLUSTER:
        logger.Debug("to cluster")
        // todo 集群全部节点请求 待完成
        break
    default:
        // 未知的请求类型
        logger.Debug("unknown receiver...")
    }
    return nil
}
 
// 收到回复
func (c *Client) OnResponse(cli *client.Client, msg *aiot.Protocol) error {
    logger.Debug("OnResponse...", zap.Any("msg", msg))
    return nil
}
 
// 连接关闭
func (c *Client) OnClose(cli *client.Client) {
    logger.Debug("OnClose...")
    return
}
 
// 数据上报
func (c *Client) OnDataReport(cli *client.Client, msg *aiot.Protocol) error {
    logger.Debug("OnDataReport...", zap.Any("msg", msg))
    return nil
}
 
// 设备控制
func (c *Client) OnDeviceControl(cli *client.Client, msg *aiot.Protocol) error {
    logger.Debug("OnDeviceControl...", zap.Any("msg", msg))
    return nil
}