lift_alarm.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package v1
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/jaryhe/gopkgs/logger"
  6. "go.uber.org/zap"
  7. "lift-monitor/consts"
  8. "lift-monitor/pb"
  9. "lift-monitor/pb/v1"
  10. "time"
  11. )
  12. const (
  13. LiftAlarmHigh = "L01"
  14. LiftAlarmWeight = "L02"
  15. LiftAlarmPeople = "L03"
  16. LiftAlarmSpeed = "L04"
  17. LiftAlarmWind = "L05"
  18. LiftAlarmTilt = "L06"
  19. LiftWarningHigh = "L07"
  20. LiftWarningWeight = "L08"
  21. LiftWarningPeople = "L09"
  22. LiftWarningSpeed = "L10"
  23. LiftWarningWind = "L11"
  24. LiftWarningTilt = "L12"
  25. LiftAlarmFrontDoor = "L13"
  26. LiftAlarmBackDoor = "L14"
  27. )
  28. var LiftAlarmCodeReasonMap = map[string]string{
  29. "L01":"高度报警",
  30. "L02":"重量报警",
  31. "L03":"人数报警",
  32. "L04":"速度报警",
  33. "L05":"风速报警",
  34. "L06":"倾角报警",
  35. "L07":"高度预警",
  36. "L08":"重量预警",
  37. "L09":"人数预警",
  38. "L10":"速度预警",
  39. "L11":"风速预警",
  40. "L12":"倾角预警",
  41. "L13":"前门异常",
  42. "L14":"后门异常",
  43. }
  44. func judgeAlarmCode(value int, codes ...string) string {
  45. if value == 0 {
  46. return ""
  47. }
  48. if value == 1 && len(codes) > 0 {
  49. return codes[0]
  50. }
  51. if value == 2 && len(codes) > 1 {
  52. return codes[1]
  53. }
  54. return ""
  55. }
  56. func getAlarmCodes(record *LiftRealtimeData) []string {
  57. ret := []string{}
  58. if code := judgeAlarmCode(record.HighAlarmState, LiftWarningHigh, LiftAlarmHigh); code != "" {
  59. ret = append(ret, code)
  60. }
  61. if code := judgeAlarmCode(record.PeopleAlarmState, LiftWarningPeople, LiftAlarmPeople); code != "" {
  62. ret = append(ret, code)
  63. }
  64. if code := judgeAlarmCode(record.WeightAlarmState, LiftWarningWeight, LiftAlarmWeight); code != "" {
  65. ret = append(ret, code)
  66. }
  67. if code := judgeAlarmCode(record.WindSpeedAlarmState, LiftWarningWind, LiftAlarmWind); code != "" {
  68. ret = append(ret, code)
  69. }
  70. if code := judgeAlarmCode(record.TiltAlarmState, LiftWarningTilt, LiftAlarmTilt); code != "" {
  71. ret = append(ret, code)
  72. }
  73. if code := judgeAlarmCode(record.SpeedAlarmState, LiftWarningSpeed, LiftAlarmSpeed); code != "" {
  74. ret = append(ret, code)
  75. }
  76. if code := judgeAlarmCode(record.FrontDoorAlarmState, LiftAlarmFrontDoor); code != "" {
  77. ret = append(ret, code)
  78. }
  79. if code := judgeAlarmCode(record.BackDoorAlarmState, LiftAlarmBackDoor); code != "" {
  80. ret = append(ret, code)
  81. }
  82. return ret
  83. }
  84. func handleAlarms(record *LiftRealtimeData, sn string, liftno int64, timestamp int64, projectId int64, deviceName string) bool {
  85. codes := getAlarmCodes(record)
  86. for _, code := range codes {
  87. handleAlarm(code, sn, liftno, timestamp, projectId, deviceName)
  88. }
  89. if len(codes) > 0 {
  90. return true
  91. }
  92. return false
  93. }
  94. func handleAlarm(code string, sn string, liftno int64, timestamp int64, projectId int64, deviceName string) {
  95. timeStr := time.Unix(timestamp, 0).Format("2006-01-02 15:04:05")
  96. //reason := fmt.Sprintf("%d号%s", sn, liftno, timeStr, LiftAlarmValueReasonMap[alarmValue])
  97. alarmData := &v1.AlarmAddRequest{
  98. AlarmReason: LiftAlarmCodeReasonMap[code],
  99. AlarmCode: code,
  100. Date: timeStr,
  101. Sn: sn,
  102. ProjectId: projectId,
  103. DeviceCode: consts.LiftDeviceCode,
  104. DeviceType: consts.LiftDeviceName,
  105. DeviceName: deviceName,
  106. }
  107. // 添加告警
  108. _, err := pb.SmartAlarm.AlarmAdd(context.Background(), alarmData)
  109. if err != nil {
  110. reqByte, _ := json.Marshal(alarmData)
  111. logger.Error("rpc",
  112. zap.String("call", "pb.SmartAlarm.AlarmAdd"),
  113. zap.String("args", string(reqByte)),
  114. zap.String("error", err.Error()))
  115. }
  116. }
  117. func LiftFrameAlarmRequestHandle(sn string, liftNo byte, version byte, data []byte) (res []byte, err error) {
  118. // 不管,因为实时包里包含了报警数据
  119. return makeResponse(sn, liftNo, LiftFrameAlarmResponse, version, []byte{0x00}), nil
  120. }