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 }