tower_status.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package timetask
  4. import (
  5. "context"
  6. "lift-monitor/consts"
  7. "lift-monitor/pb"
  8. "lift-monitor/pb/v1"
  9. "lift-monitor/util"
  10. "encoding/json"
  11. "fmt"
  12. "sync"
  13. "time"
  14. "github.com/jaryhe/gopkgs/logger"
  15. "go.uber.org/zap"
  16. )
  17. var status map[string]int64
  18. var mutex sync.Mutex
  19. func handleOfflineAlarm(sn string) error {
  20. deviceInfo, err := util.GetDeviceInfo(sn)
  21. if err != nil {
  22. return err
  23. }
  24. alram := &v1.AlarmAddRequest{
  25. Date: time.Now().Format(consts.TimeSecondLayOut),
  26. ProjectId: deviceInfo.ProjectId,
  27. Sn: sn,
  28. DeviceName: deviceInfo.Name,
  29. DeviceType: consts.LiftDeviceName,
  30. DeviceCode: consts.LiftDeviceCode,
  31. }
  32. alram.AlarmReason = "离线"
  33. alram.AlarmCode = consts.DeviceOfflineCode
  34. // 添加告警
  35. _, err = pb.SmartAlarm.AlarmAdd(context.Background(), alram)
  36. if err != nil {
  37. reqByte, _ := json.Marshal(*alram)
  38. logger.Error("rpc",
  39. zap.String("call", "pb.SmartAlarm.AlarmAdd"),
  40. zap.String("args", string(reqByte)),
  41. zap.String("error", err.Error()))
  42. return err
  43. }
  44. return nil
  45. }
  46. func setLiftStatus(sn string, status int32) error {
  47. if status == consts.DeviceOffline {
  48. handleOfflineAlarm(sn)
  49. }
  50. rpcReq := &v1.DeviceStatusUpdateRequest{Sn: sn, Status: status, DeviceCode: consts.LiftDeviceCode}
  51. _, err := pb.SmartAuth.DeviceStatusUpdate(context.Background(), rpcReq)
  52. if err != nil {
  53. reqByte, _ := json.Marshal(*rpcReq)
  54. logger.Error("rpc",
  55. zap.String("call", "pb.SmartAuth.DeviceStatusUpdate"),
  56. zap.String("args", string(reqByte)),
  57. zap.String("error", err.Error()))
  58. }
  59. return err
  60. }
  61. func liftStatusTaskImpl() {
  62. defer func() {
  63. if r := recover(); r != nil {
  64. err := fmt.Errorf("%+v", r)
  65. logger.Error("err",
  66. zap.String("system_err", err.Error()),
  67. zap.Stack("stacktrace"))
  68. }
  69. }()
  70. timeNow := time.Now().Unix()
  71. for k, v := range status {
  72. // 超过3分钟没有连接认为超时
  73. if (timeNow - v) > consts.MinSecond*3 {
  74. mutex.Lock()
  75. // 设置状态成功后清除缓存
  76. err := setLiftStatus(k, consts.DeviceOffline)
  77. if err == nil {
  78. delete(status, k)
  79. }
  80. mutex.Unlock()
  81. }
  82. }
  83. }
  84. func liftStatusTask() {
  85. status = make(map[string]int64)
  86. for {
  87. // 任务实现
  88. liftStatusTaskImpl()
  89. // 休眠60s
  90. time.Sleep(consts.MinSecond * time.Second)
  91. }
  92. }
  93. func UpdateLiftTime(sn string) {
  94. timeNow := time.Now().Unix()
  95. mutex.Lock()
  96. defer mutex.Unlock()
  97. // 重新启动或设备上线
  98. if _, ok := status[sn]; !ok {
  99. // 设置为在线
  100. err := setLiftStatus(sn, consts.DeviceOnline)
  101. if err != nil {
  102. // 设置失败不更新本地缓存
  103. return
  104. }
  105. }
  106. // 更新时间为当前时间搓
  107. status[sn] = timeNow
  108. }