saibo_mqttv1_commander.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package gate_command
  2. import (
  3. "context"
  4. "fmt"
  5. "git.getensh.com/common/gopkgs/database"
  6. "property-device/errors"
  7. dbmodel "property-device/model"
  8. "property-device/pb"
  9. pb_v1 "property-device/pb/v1"
  10. "property-device/utils/gate_utils"
  11. "time"
  12. )
  13. type SaiboMqttv1Commander struct {
  14. GateInfo *dbmodel.TGate
  15. }
  16. func (p *SaiboMqttv1Commander) Open() error {
  17. now := time.Now()
  18. tgc := &dbmodel.TGateCommand{
  19. CreatedAt: now,
  20. UpdatedAt: now,
  21. DeviceId: p.GateInfo.ID,
  22. //Param: req.CmdParams,
  23. Desc: gate_utils.CommandCodeMap[gate_utils.OpenCommand],
  24. Status: gate_utils.CommandStatusRuning,
  25. ResultStatus: 0,
  26. ResultStatusDesc: "",
  27. Code: gate_utils.OpenCommand,
  28. }
  29. db := database.DB().Begin()
  30. err := tgc.Insert(db)
  31. if err != nil {
  32. db.Rollback()
  33. return errors.DataBaseError
  34. }
  35. mreq := pb_v1.FaceOpenRequest{Sn: p.GateInfo.Sn, CmdId: fmt.Sprintf("%d", tgc.ID)}
  36. _, err = pb.Mqtt.FaceOpen(context.Background(), &mreq)
  37. if err != nil {
  38. db.Rollback()
  39. return err
  40. }
  41. db.Commit()
  42. return nil
  43. }
  44. func (p *SaiboMqttv1Commander) Reboot() error {
  45. now := time.Now()
  46. tgc := &dbmodel.TGateCommand{
  47. CreatedAt: now,
  48. UpdatedAt: now,
  49. DeviceId: p.GateInfo.ID,
  50. //Param: req.CmdParams,
  51. Desc: gate_utils.CommandCodeMap[gate_utils.RebootCommand],
  52. Status: gate_utils.CommandStatusRuning,
  53. ResultStatus: 0,
  54. ResultStatusDesc: "",
  55. Code: gate_utils.RebootCommand,
  56. }
  57. db := database.DB().Begin()
  58. err := tgc.Insert(db)
  59. if err != nil {
  60. db.Rollback()
  61. return errors.DataBaseError
  62. }
  63. mreq := pb_v1.FaceRebootRequest{Sn: p.GateInfo.Sn, CmdId: fmt.Sprintf("%d", tgc.ID)}
  64. _, err = pb.Mqtt.FaceReboot(context.Background(), &mreq)
  65. if err != nil {
  66. db.Rollback()
  67. return err
  68. }
  69. db.Commit()
  70. return nil
  71. }
  72. func (p *SaiboMqttv1Commander) Command() bool {
  73. return false
  74. }