12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package gate_command
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- dbmodel "property-device/model"
- pb_v1 "property-device/pb/v1"
- "property-device/utils/gate_utils"
- "time"
- )
- func checkGateCommandResultParam(req *pb_v1.GateCommandResultRequest) error {
- switch {
- case req.Sn == "" || req.Protocol == 0:
- return status.Error(10003, "设备id不能为空")
- }
- return nil
- }
- func GateCommandResult(ctx context.Context, req *pb_v1.GateCommandResultRequest) (reply *pb_v1.GateCommandResultReply, err error) {
- reply = &pb_v1.GateCommandResultReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- err = checkGateCommandResultParam(req)
- if err != nil {
- return nil, err
- }
- gateInfo, err := getGateInfo(0, req.Sn, req.Protocol)
- if err != nil {
- return nil, err
- }
- switch req.Protocol {
- case gate_utils.GateProtocolSaiboHttpV1:
- err = CommandResultSaiboHttpV1(req, gateInfo)
- case gate_utils.GateProtocolSaiboMqttV1:
- err = CommandResultSaiboMqttV1(req, gateInfo)
- case gate_utils.GateProtocolYufanHttpV1:
- err = CommandResultYufanHttpV1(req, gateInfo)
- if gate_utils.HasCommand(gateInfo.Sn, gateInfo.Protocol) {
- reply.HasTask = true
- }
- }
- // 删除7天以前的数据
- if err != nil {
- return nil, err
- }
- now := time.Now()
- t := now.AddDate(0, 0, -7)
- p := &dbmodel.TGateCommand{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "id", req.Id)
- where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
- where = dbmodel.WhereAdd(where, "updated_at <", t.Format("2006-01-02:15:04:05"))
- p.Delete(database.DB(), where)
- return reply, nil
- }
|