| | |
| | | } |
| | | |
| | | // 发送消息 |
| | | func (c *Client) WriteMsg(senderId string, msgType aiot.MSG_TYPE, reqType aiot.REQ_TYPE, data []byte, msgProto *aiot.MsgIdProto) (*aiot.Protocol, error) { |
| | | func (c *Client) writeMsg(senderId string, msgType aiot.MSG_TYPE, reqType aiot.REQ_TYPE, data []byte, msgProto *aiot.MsgIdProto) (*aiot.Protocol, error) { |
| | | // 关闭的连接不能写入 |
| | | if c.IsClosed() { |
| | | c.Logger.Error("Can not write msg on the closed chan", zap.Any("msgType", msgType), zap.Any("reqType", reqType), zap.Any("data", string(data))) |
| | |
| | | |
| | | // 拼装并发送消息 |
| | | body := &aiot.Protocol{ |
| | | Receiver: aiot.RECEIVER_TO_SAAS, |
| | | SenderId: senderId, |
| | | MsgType: msgType, |
| | | ReqType: reqType, |
| | |
| | | } |
| | | }() |
| | | |
| | | // 通道已关闭,不能写入 |
| | | if c.IsClosed() { |
| | | errMsg := "Can not write msg into closed chain" |
| | | c.Logger.Warn(errMsg, zap.Any("msg",body)) |
| | | return errors.New(errMsg) |
| | | } |
| | | body.Receiver = aiot.RECEIVER_TO_SAAS |
| | | |
| | | // 消息ID默认处理 |
| | | if body.MsgProto == nil { |
| | | body.MsgProto = GetMsgProto("") |
| | | } |
| | | msgData, err := json.Marshal(body) |
| | | if err != nil { |
| | | c.Logger.Error("Fail to Marshal send data", zap.Error(err)) |
| | |
| | | c.Logger.Debug("registering...") |
| | | data := c.deviceRegister |
| | | msgData, _ := json.Marshal(data) |
| | | _, err := c.WriteMsg(c.deviceId, aiot.MSG_TYPE_REGISTER, aiot.REQ_TYPE_REQUEST, msgData, GetMsgProto("")) |
| | | _, err := c.writeMsg(c.deviceId, aiot.MSG_TYPE_REGISTER, aiot.REQ_TYPE_REQUEST, msgData, GetMsgProto("")) |
| | | if err != nil { |
| | | c.Logger.Error("Fail to send device register", zap.Any("msg", msgData)) |
| | | } |
| | |
| | | t.Stop() |
| | | return |
| | | } |
| | | go c.WriteMsg(c.deviceId, aiot.MSG_TYPE_HEART_BEAT, aiot.REQ_TYPE_REQUEST, pingData, GetMsgProto("")) |
| | | go c.writeMsg(c.deviceId, aiot.MSG_TYPE_HEART_BEAT, aiot.REQ_TYPE_REQUEST, pingData, GetMsgProto("")) |
| | | } |
| | | } |
| | | } |