multi.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package handler
  2. import (
  3. "GtDataStore/app/cmd/events/internal/logic/job"
  4. "GtDataStore/app/cmd/events/internal/svc"
  5. "GtDataStore/app/model"
  6. "context"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. "metawant.greentech.com.cn/gaoyagang/gt-common/envitem"
  9. "metawant.greentech.com.cn/gaoyagang/gt-common/identify"
  10. "sync"
  11. "time"
  12. )
  13. func EventMulti(ctx context.Context, task *job.Task, technologyName string) error {
  14. // 1. 查询所有的设备
  15. binds, err := findMultiRecord(task.Job.SvcCtx, int64(task.Id))
  16. if err != nil {
  17. return err
  18. }
  19. // 2. 识别事件
  20. var wg sync.WaitGroup
  21. wg.Add(len(binds))
  22. for _, bind := range binds {
  23. bind := bind
  24. go func() {
  25. defer wg.Done()
  26. multiEnvItem := extractEventItems(bind)
  27. //fmt.Printf("extractEventItems: %+v\n", multiEnvItem)
  28. err := multiEnvItem.FillCurrentValue()
  29. if err != nil {
  30. return
  31. }
  32. ovs := multiEnvItem.FindPrevString()
  33. nvs := multiEnvItem.FindString()
  34. rules := make(map[string]identify.MultiRule)
  35. rules[bind.Name] = bind.Config.Multi
  36. checker := identify.NewMultiCheck(rules)
  37. name := checker.Check(ovs, nvs)
  38. //fmt.Printf("ovs: %+v\n", ovs)
  39. //fmt.Printf("nvs: %+v\n", nvs)
  40. //fmt.Printf("rules: %+v\n", rules)
  41. //fmt.Printf("name: %s\n", name)
  42. if name != "" {
  43. items := make([]string, 0)
  44. times := make(map[string]time.Time)
  45. for itemName, _ := range nvs {
  46. items = append(items, itemName)
  47. times[itemName] = *multiEnvItem.GetItemHtime(itemName)
  48. }
  49. multiEventChan <- multiEventInfo{
  50. ProjectId: bind.ProjectId,
  51. DeviceCode: bind.DeviceCode,
  52. Items: items,
  53. EventId: bind.Id,
  54. EventName: name,
  55. OldValues: ovs,
  56. NewValues: nvs,
  57. Times: times,
  58. }
  59. }
  60. }()
  61. }
  62. wg.Wait()
  63. return nil
  64. }
  65. func findMultiRecord(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcEventBind, error) {
  66. if data := eventBindCacheTable.GetCache(EVENT_MULTI_ITEM); data != nil {
  67. return data, nil
  68. }
  69. if binds, err := svcCtx.DcEventBind.FindByProjectIdRuleFlag(context.Background(), projectId, EVENT_MULTI_ITEM); err != nil {
  70. logx.Error("findSingleRecord not found record")
  71. return nil, err
  72. } else {
  73. eventBindCacheTable.SetCache(EVENT_MULTI_ITEM, binds, time.Now().Add(300*time.Second))
  74. return binds, nil
  75. }
  76. }
  77. func extractEventItems(bind model.DcEventBind) envitem.MultiEnvItem {
  78. me := make(envitem.MultiEnvItem)
  79. for itemName, rule := range bind.Config.Multi {
  80. _ = rule
  81. me[itemName] = &envitem.EnvItem{
  82. ProjectId: bind.ProjectId,
  83. Item: itemName,
  84. }
  85. }
  86. return me
  87. }