eventmgr.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package main
  2. import (
  3. "runtime/debug"
  4. "strconv"
  5. "sync"
  6. "time"
  7. "github.com/sirupsen/logrus"
  8. "lc/common/models"
  9. "lc/common/util"
  10. )
  11. // DeviceAlarmId 开关灯,离线,上线事件
  12. var DeviceAlarmId = "device_alarm_id"
  13. var _EventMgrOnce sync.Once
  14. var _EventMgrSingle *EventMgr
  15. func GetEventMgr() *EventMgr {
  16. _EventMgrOnce.Do(func() {
  17. _EventMgrSingle = &EventMgr{
  18. EventQueue: util.NewQueue(10000),
  19. MapAlarm: make(map[string]int64),
  20. MapLampEvent: make(map[string]int64),
  21. }
  22. })
  23. return _EventMgrSingle
  24. }
  25. type EventObject struct {
  26. ID string
  27. EventType models.EventType
  28. Time time.Time
  29. Value float64
  30. }
  31. type EventMgr struct {
  32. EventQueue *util.MlQueue
  33. MapAlarm map[string]int64
  34. MapLampEvent map[string]int64
  35. }
  36. func (o *EventMgr) PushEvent(eo *EventObject) {
  37. if eo.ID == "" {
  38. logrus.Debugf("事件的设备ID为空:%s", string(debug.Stack()))
  39. }
  40. o.EventQueue.Put(eo)
  41. }
  42. func (o *EventMgr) Handler(args ...interface{}) interface{} {
  43. defer func() {
  44. if err := recover(); err != nil {
  45. gopool.Add(o.Handler, args)
  46. logrus.Errorf("EventMgr.Handler发生异常:%s", string(debug.Stack()))
  47. }
  48. }()
  49. for {
  50. msg, ok, _ := o.EventQueue.Get()
  51. if !ok {
  52. time.Sleep(200 * time.Millisecond)
  53. continue
  54. }
  55. eo, ok := msg.(*EventObject)
  56. if !ok {
  57. continue
  58. }
  59. //事件保存
  60. o.SaveEventObject(eo)
  61. //在线离线告警处理
  62. if eo.EventType == models.ET_ONLINE || eo.EventType == models.ET_OFFLINE {
  63. o.CheckOffLine(eo)
  64. } else if eo.EventType == models.ET_ONLAMP || eo.EventType == models.ET_OFFLAMP {
  65. o.CheckLampOnOff(eo)
  66. }
  67. }
  68. }
  69. func (o *EventMgr) SaveEventObject(eo *EventObject) {
  70. oo := models.DeviceEvents{DID: eo.ID, EType: eo.EventType, Time: eo.Time}
  71. if err := models.G_db.Create(&oo).Error; err != nil {
  72. logrus.Errorf("设备[%s]事件入库失败:%s", eo.ID, err.Error())
  73. }
  74. }
  75. func (o *EventMgr) CheckOffLine(eo *EventObject) {
  76. if eo.EventType == models.ET_ONLINE { //上线事件
  77. ai, ok := o.MapAlarm[eo.ID]
  78. if !ok {
  79. if str, err := redisCltRawData.HGet(DeviceAlarmId, eo.ID).Result(); err == nil {
  80. if id, err := strconv.Atoi(str); err == nil {
  81. ai = int64(id)
  82. }
  83. }
  84. }
  85. if ai > 0 {
  86. oo := models.DeviceAlarm{ID: ai, TEnd: eo.Time, EValue: float32(0)}
  87. if err := oo.Update(); err != nil {
  88. logrus.Errorf("更新告警[%d]信息失败:%s", ai, err.Error())
  89. }
  90. delete(o.MapAlarm, eo.ID)
  91. if err := redisCltRawData.HDel(DeviceAlarmId, eo.ID).Err(); err != nil {
  92. logrus.Errorf("更新告警[%d]信息失败:%s", ai, err.Error())
  93. }
  94. }
  95. } else if eo.EventType == models.ET_OFFLINE { //掉线事件
  96. oo := models.DeviceAlarm{
  97. DID: eo.ID,
  98. TStart: eo.Time,
  99. Threshold: 0,
  100. SValue: 0,
  101. Content: "离线",
  102. AlarmType: 0,
  103. Level: 2, //设备离线为严重告警
  104. }
  105. if err := models.G_db.Create(&oo).Error; err != nil {
  106. logrus.Errorf("告警信息[%v]入库失败:%s", eo, err.Error())
  107. } else {
  108. o.MapAlarm[eo.ID] = oo.ID
  109. if err := redisCltRawData.HSet(DeviceAlarmId, eo.ID, oo.ID).Err(); err != nil {
  110. logrus.Errorf("设备[%s]告警数据[%d]缓存失败:%s", eo.ID, oo.ID, err.Error())
  111. }
  112. }
  113. }
  114. }
  115. func (o *EventMgr) CheckLampOnOff(eo *EventObject) {
  116. if eo.EventType == models.ET_OFFLAMP { //关灯事件
  117. RID, ok := o.MapLampEvent[eo.ID]
  118. if !ok {
  119. if str, err := redisCltRawData.HGet(LampEventId, eo.ID).Result(); err == nil {
  120. if id, err := strconv.Atoi(str); err == nil {
  121. RID = int64(id)
  122. }
  123. }
  124. }
  125. if RID > 0 {
  126. oo := models.DeviceLampEvents{
  127. ID: uint(RID),
  128. DID: eo.ID,
  129. TEnd: eo.Time,
  130. }
  131. if err := oo.Update(); err != nil {
  132. logrus.Errorf("灯控[%s]关灯数据[ID=%d]入库失败:%s", eo.ID, RID, err.Error())
  133. }
  134. delete(o.MapLampEvent, eo.ID)
  135. if err := oo.Split(); err != nil {
  136. logrus.Errorf("分割开灯事件[%d]失败:%s", oo.ID, err.Error())
  137. }
  138. if err := redisCltRawData.HDel(LampEventId, eo.ID).Err(); err != nil {
  139. logrus.Errorf("灯控[%s]关灯数据[ID=%d]删除失败:%s", eo.ID, RID, err.Error())
  140. }
  141. }
  142. } else if eo.EventType == models.ET_ONLAMP { //开灯事件
  143. oo := models.DeviceLampEvents{
  144. DID: eo.ID,
  145. TStart: eo.Time,
  146. Brightness: uint8(eo.Value),
  147. }
  148. if err := models.G_db.Create(&oo).Error; err != nil {
  149. logrus.Errorf("灯控[%s]开灯数据入库失败:%s", eo.ID, err.Error())
  150. } else {
  151. o.MapLampEvent[eo.ID] = int64(oo.ID)
  152. if err := redisCltRawData.HSet(LampEventId, eo.ID, oo.ID).Err(); err != nil {
  153. logrus.Errorf("灯控[%s]开灯数据[ID=%d]缓存失败:%s", eo.ID, oo.ID, err.Error())
  154. }
  155. }
  156. }
  157. }