eventmgr.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. time.Sleep(time.Second)
  46. gopool.Add(o.Handler, args)
  47. logrus.Errorf("EventMgr.Handler发生异常:%s", string(debug.Stack()))
  48. }
  49. }()
  50. for {
  51. msg, ok, _ := o.EventQueue.Get()
  52. if !ok {
  53. time.Sleep(200 * time.Millisecond)
  54. continue
  55. }
  56. eo, ok := msg.(*EventObject)
  57. if !ok {
  58. continue
  59. }
  60. //事件保存
  61. o.SaveEventObject(eo)
  62. //在线离线告警处理
  63. if eo.EventType == models.ET_ONLINE || eo.EventType == models.ET_OFFLINE {
  64. o.CheckOffLine(eo)
  65. } else if eo.EventType == models.ET_ONLAMP || eo.EventType == models.ET_OFFLAMP {
  66. o.CheckLampOnOff(eo)
  67. }
  68. }
  69. }
  70. func (o *EventMgr) SaveEventObject(eo *EventObject) {
  71. oo := models.DeviceEvents{DID: eo.ID, EType: eo.EventType, Time: eo.Time}
  72. if err := models.G_db.Create(&oo).Error; err != nil {
  73. logrus.Errorf("设备[%s]事件入库失败:%s", eo.ID, err.Error())
  74. }
  75. }
  76. func (o *EventMgr) CheckOffLine(eo *EventObject) {
  77. if eo.EventType == models.ET_ONLINE { //上线事件
  78. ai, ok := o.MapAlarm[eo.ID]
  79. if !ok {
  80. if str, err := redisCltRawData.HGet(DeviceAlarmId, eo.ID).Result(); err == nil {
  81. if id, err := strconv.Atoi(str); err == nil {
  82. ai = int64(id)
  83. }
  84. }
  85. }
  86. if ai > 0 {
  87. oo := models.DeviceAlarm{ID: ai, TEnd: eo.Time, EValue: float32(0)}
  88. if err := oo.Update(); err != nil {
  89. logrus.Errorf("更新告警[%d]信息失败:%s", ai, err.Error())
  90. }
  91. delete(o.MapAlarm, eo.ID)
  92. if err := redisCltRawData.HDel(DeviceAlarmId, eo.ID).Err(); err != nil {
  93. logrus.Errorf("更新告警[%d]信息失败:%s", ai, err.Error())
  94. }
  95. }
  96. } else if eo.EventType == models.ET_OFFLINE { //掉线事件
  97. oo := models.DeviceAlarm{
  98. DID: eo.ID,
  99. TStart: eo.Time,
  100. Threshold: 0,
  101. SValue: 0,
  102. Content: "离线",
  103. AlarmType: 0,
  104. Level: 2, //设备离线为严重告警
  105. }
  106. if err := models.G_db.Create(&oo).Error; err != nil {
  107. logrus.Errorf("告警信息[%v]入库失败:%s", eo, err.Error())
  108. } else {
  109. o.MapAlarm[eo.ID] = oo.ID
  110. if err := redisCltRawData.HSet(DeviceAlarmId, eo.ID, oo.ID).Err(); err != nil {
  111. logrus.Errorf("设备[%s]告警数据[%d]缓存失败:%s", eo.ID, oo.ID, err.Error())
  112. }
  113. }
  114. }
  115. }
  116. func (o *EventMgr) CheckLampOnOff(eo *EventObject) {
  117. if eo.EventType == models.ET_OFFLAMP { //关灯事件
  118. RID, ok := o.MapLampEvent[eo.ID]
  119. if !ok {
  120. if str, err := redisCltRawData.HGet(LampEventId, eo.ID).Result(); err == nil {
  121. if id, err := strconv.Atoi(str); err == nil {
  122. RID = int64(id)
  123. }
  124. }
  125. }
  126. if RID > 0 {
  127. oo := models.DeviceLampEvents{
  128. ID: uint(RID),
  129. DID: eo.ID,
  130. TEnd: eo.Time,
  131. }
  132. if err := oo.Update(); err != nil {
  133. logrus.Errorf("灯控[%s]关灯数据[ID=%d]入库失败:%s", eo.ID, RID, err.Error())
  134. }
  135. delete(o.MapLampEvent, eo.ID)
  136. if err := oo.Split(); err != nil {
  137. logrus.Errorf("分割开灯事件[%d]失败:%s", oo.ID, err.Error())
  138. }
  139. if err := redisCltRawData.HDel(LampEventId, eo.ID).Err(); err != nil {
  140. logrus.Errorf("灯控[%s]关灯数据[ID=%d]删除失败:%s", eo.ID, RID, err.Error())
  141. }
  142. }
  143. } else if eo.EventType == models.ET_ONLAMP { //开灯事件
  144. oo := models.DeviceLampEvents{
  145. DID: eo.ID,
  146. TStart: eo.Time,
  147. Brightness: uint8(eo.Value),
  148. }
  149. if err := models.G_db.Create(&oo).Error; err != nil {
  150. logrus.Errorf("灯控[%s]开灯数据入库失败:%s", eo.ID, err.Error())
  151. } else {
  152. o.MapLampEvent[eo.ID] = int64(oo.ID)
  153. if err := redisCltRawData.HSet(LampEventId, eo.ID, oo.ID).Err(); err != nil {
  154. logrus.Errorf("灯控[%s]开灯数据[ID=%d]缓存失败:%s", eo.ID, oo.ID, err.Error())
  155. }
  156. }
  157. }
  158. }