message.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package gb28181
  4. import (
  5. "encoding/json"
  6. "gitlab.sydip.com/repo/gopkgs/cache"
  7. "time"
  8. . "m7s.live/engine/v4"
  9. "go.uber.org/zap"
  10. "fmt"
  11. )
  12. type PushMessageBody struct {
  13. Id int64 `form:"id" json:"id"`
  14. Sn string `form:"sn" json:"sn" description:"sn"`
  15. RecongnitionKey string `form:"recongnition_key" json:"recongnition_key"`
  16. Color string `form:"color" json:"color"`
  17. Img string `form:"img" json:"img"`
  18. Location string `form:"location" json:"location"`
  19. CameraLocation string `form:"camera_location" json:"camera_location"`
  20. Time int64 `form:"time" json:"time"`
  21. RecongnitionType int `form:"recongnition_type" json:"recongnition_type"`
  22. AccessType int `form:"access_type" json:"access_type"`
  23. }
  24. type PushStateBody struct {
  25. Sn string `form:"sn" json:"sn"`
  26. State int `form:"state" json:"state"`
  27. Time int64 `json:"time"`
  28. }
  29. type Message struct{
  30. Code string `json:"code"` // 1 车辆消息,2 人脸消息
  31. Sn string `json:"sn"`
  32. ChannelId string `json:"channel_id"`
  33. Body []byte `json:"body"`
  34. }
  35. const(
  36. PushStateCode = "200"
  37. PushMessageCode = "201"
  38. PushMessageKey = "push-message-list"
  39. RebootDevicePre ="reboot:"
  40. )
  41. func getRebootKey(channelId string) string {
  42. return fmt.Sprintf("%s:%s",RebootDevicePre,channelId)
  43. }
  44. func SendMessageToRedis(channelId string,state int) error {
  45. newMessage := Message{}
  46. newMessage.Code = PushStateCode
  47. newMessage.ChannelId = channelId
  48. //newMessage.ChannelId = message.channelId
  49. body := PushStateBody{}
  50. body.State = state
  51. body.Time = time.Now().Unix()
  52. sdata ,_:= json.Marshal(body)
  53. newMessage.Body = sdata
  54. ndata ,_ := json.Marshal(newMessage)
  55. _,err := cache.Redis().RPush(PushMessageKey,string(ndata))
  56. if err != nil{
  57. GB28181Plugin.Error("发送设备状态更新失败:",zap.String("deviceId", channelId),zap.String("error", err.Error()))
  58. }else{
  59. GB28181Plugin.Info("发送设备状态更新成功",zap.String("deviceId", channelId))
  60. }
  61. //fmt.Println("发送设备状态更新:",channelId,state)
  62. return err
  63. }
  64. func SendRebootMessage(channelId string){
  65. if channelId == ""{
  66. return
  67. }
  68. key := getRebootKey(channelId)
  69. //fmt.Println(EngineConfig.Engine.RebootCacheTime)
  70. if EngineConfig.Engine.RebootCacheTime == 0 {
  71. EngineConfig.Engine.RebootCacheTime = 360 // 6分钟
  72. }else{
  73. EngineConfig.Engine.RebootCacheTime = EngineConfig.Engine.RebootCacheTime + 60
  74. }
  75. // 10分钟
  76. _,err := cache.Redis().SetNxEx(key,"reboot",EngineConfig.Engine.RebootCacheTime)
  77. if err != nil{
  78. GB28181Plugin.Info("重启设备发送失败",zap.String("channelId", channelId),zap.String("error",err.Error()))
  79. }
  80. }
  81. func DeleteRebootMessage(channelId string){
  82. if channelId == ""{
  83. return
  84. }
  85. key := getRebootKey(channelId)
  86. _,err := cache.Redis().Del(key)
  87. if err != nil{
  88. GB28181Plugin.Info("删除重启设备失败",zap.String("channelId", channelId),zap.String("error",err.Error()))
  89. }
  90. }