command.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package mqtt_utils
  2. import (
  3. "fmt"
  4. "git.getensh.com/common/gopkgs/cache"
  5. "property-mqtt/utils"
  6. "time"
  7. )
  8. const (
  9. OpenCommand = 1
  10. RebootCommand = 2
  11. DownCommand = 3
  12. DelCommand = 4
  13. QueryCommand = 5
  14. UpdatePicCommand = 6
  15. AddPicCommand = 7
  16. )
  17. const (
  18. GateProtocolSaiboHttpV1 = 1
  19. GateProtocolSaiboMqttV1 = 2
  20. GateProtocolYufanHttpV1 = 3
  21. )
  22. type CommandHandle func(commond string, res string)
  23. var CommandHandleMap = map[string]CommandHandle{
  24. "EditPersonsNew-Ack": DownPersonCommandHandle,
  25. "RebootDevice-Ack": RebootCommandHandle,
  26. "Unlock-Ack": OpenCommandHandle,
  27. "DeletePersons-Ack": DelPersonCommandHandle,
  28. }
  29. const (
  30. WaitMessagePrefix = "WaitFaceDevice_"
  31. // 部分命令需要得到响应后才能再次执行
  32. WaitPrefex = "wait_face_common"
  33. )
  34. func GenerateMsgId(faceId string) string {
  35. return fmt.Sprintf("%s-%d-%s", faceId, time.Now().Unix(), utils.GenerateRandomStr(10, "mix"))
  36. }
  37. func GenerateWaitKey(faceId string) string {
  38. return fmt.Sprintf("%s-%s", WaitPrefex, faceId)
  39. }
  40. //
  41. func Wait(faceid string) bool {
  42. key := GenerateWaitKey(faceid)
  43. for i := 0; i < 15; i++ {
  44. ret, err := cache.Redis().SetNxEx(key, 1, 5)
  45. if err != nil {
  46. return false
  47. }
  48. if ret {
  49. return ret
  50. }
  51. time.Sleep(1 * time.Second)
  52. }
  53. return false
  54. }
  55. func DelWait(faceid string) {
  56. key := GenerateWaitKey(faceid)
  57. cache.Redis().Del(key)
  58. }