vars.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package handler
  2. import (
  3. "GtDataStore/app/cmd/events/internal/logic/job"
  4. "GtDataStore/app/model"
  5. "context"
  6. "metawant.greentech.com.cn/gaoyagang/gt-common/rabbitMQ"
  7. "time"
  8. )
  9. const (
  10. EVENT_NOTIFY = "EVENT_NOTIFY"
  11. EVENT_SINGLE_ITEM = "SINGLE_ITEM_EVENT" // 单点位值事件
  12. EVENT_MULTI_ITEM = "MULTI_ITEM_EVENT" // 多点位值事件
  13. EVTNE_NOTIFY_KEY_FORMAT = "EVENT:NOFITY:%d:%s" // 事件通知通道 %d: 项目名称 %s: 事件名称
  14. )
  15. type (
  16. // 事件信息, 单点位
  17. eventInfo struct {
  18. ProjectId int64 // 项目ID
  19. DeviceCode string // 设备位号
  20. Item string // 点位名称
  21. EventId int64 // 事件ID
  22. EventName string // 事件名称
  23. OldValue string // 点位旧值
  24. NewValue string // 新值
  25. Time time.Time // 新值时间
  26. MsgTime time.Time // 消息时间
  27. }
  28. // 事件信息, 多点位
  29. multiEventInfo struct {
  30. ProjectId int64 // 项目ID
  31. DeviceCode string // 设备位号
  32. Items []string // 点位名称
  33. EventId int64 // 事件ID
  34. EventName string // 事件名称
  35. OldValues map[string]string // 点位旧值
  36. NewValues map[string]string // 新值
  37. Times map[string]time.Time // 新值时间
  38. MsgTime time.Time // 消息时间
  39. }
  40. EventBindCache struct {
  41. Expire time.Time
  42. Data []model.DcEventBind
  43. }
  44. EventBindCacheMap map[string]EventBindCache
  45. )
  46. var (
  47. eventChan chan eventInfo
  48. multiEventChan chan multiEventInfo
  49. producter *rabbitMQ.Producter
  50. EventIntervalTable = map[string]time.Duration{
  51. EVENT_SINGLE_ITEM: 2 * time.Second,
  52. EVENT_MULTI_ITEM: 2 * time.Second,
  53. }
  54. EventHandlerTable = map[string]func(ctx context.Context, task *job.Task, technologyName string) error{
  55. EVENT_SINGLE_ITEM: EventSingle,
  56. EVENT_MULTI_ITEM: EventMulti,
  57. }
  58. eventBindCacheTable = EventBindCacheMap{
  59. EVENT_SINGLE_ITEM: EventBindCache{},
  60. EVENT_MULTI_ITEM: EventBindCache{},
  61. }
  62. )
  63. func (t EventBindCacheMap) GetCache(technologyName string) []model.DcEventBind {
  64. if cache, ok := t[technologyName]; ok {
  65. if time.Now().After(cache.Expire) {
  66. t[technologyName] = EventBindCache{}
  67. return nil
  68. }
  69. return cache.Data
  70. } else {
  71. return nil
  72. }
  73. }
  74. func (t EventBindCacheMap) SetCache(technologyName string, data []model.DcEventBind, expire time.Time) {
  75. t[technologyName] = EventBindCache{
  76. Expire: expire,
  77. Data: data,
  78. }
  79. }
  80. func init() {
  81. eventChan = make(chan eventInfo, 50)
  82. multiEventChan = make(chan multiEventInfo, 50)
  83. }