package mqtt_utils import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/cache" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "property-mqtt/pb" pb_v1 "property-mqtt/pb/v1" "strconv" "strings" ) /* { "operator": "Unlock", "messageId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "info": { "uid":"00021", "openDoor":"1", "showInfo":"请通行" } } 8.2 开门确认信息回复 { "messageId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "operator": "Unlock-Ack", info:{ "uid":"00001", "facesluiceId":"0001", "result":"ok" } } */ type UnlockInfo struct { OpenDoor int `json:"openDoor"` } type UnlockCmd struct { Operator string `json:"operator"` MessageId string `json:"messageId"` Info UnlockInfo `json:"info"` } type UnlockCmdResInfo struct { Result string `json:"result"` FaceId string `json:"facesluiceId"` Detail string `json:"detail"` } type UnlockCmdRes struct { Operator string `json:"operator"` MessageId string `json:"messageId"` Info UnlockCmdResInfo `json:"info"` } func OpenCommandHandle(command string, res string) { fmt.Printf("远程开门响应处理:command:%s\n res:%s\n", command, res) cmdRes := UnlockCmdRes{} json.Unmarshal([]byte(res), &cmdRes) result := 2 if cmdRes.Info.Result == "ok" { result = 1 } // 二维码开门或卡号开门结果单独处理 if strings.Contains(cmdRes.MessageId, "qcodeopen") || strings.Contains(cmdRes.MessageId, "cardopen") { defer func() { cache.Redis().Del(cmdRes.MessageId) }() if result != 1 { logger.Error("qcode open", zap.String("error", cmdRes.Info.Detail)) return } str, _ := cache.Redis().Get(cmdRes.MessageId) if str == "" { return } mreq := pb_v1.GateRecordAddRequest{} json.Unmarshal([]byte(str), &mreq) _, err := pb.Device.GateRecordAdd(context.Background(), &mreq) if err != nil { logger.Error("func", zap.String("call", "pb.Device.GateRecordAdd"), zap.String("error", err.Error())) } return } cmdId, _ := strconv.ParseInt(cmdRes.MessageId, 10, 64) mreq := pb_v1.GateCommandResultRequest{Sn: cmdRes.Info.FaceId, CmdCode: OpenCommand, Content: res, Protocol: GateProtocolSaiboMqttV1, Id: cmdId} mreq.ResultStatus = int32(result) mreq.ResultDesc = cmdRes.Info.Detail _, err := pb.Device.GateCommandResult(context.Background(), &mreq) if err != nil { logger.Error("func", zap.String("call", "pb.Device.GateCommandResult"), zap.String("error", err.Error())) } }