single.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. "GtDataStore/common/envitem"
  7. "GtDataStore/common/identify"
  8. "context"
  9. "errors"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. "sync"
  12. )
  13. func EventSingle(ctx context.Context, task *job.Task, technologyName string) error {
  14. // 1. 查询所有的设备
  15. binds, err := findSingleRecord(task.Job.SvcCtx, int64(task.Id))
  16. if err != nil {
  17. return err
  18. }
  19. // 2. 从记录中提取点位,并合并成MultiEventItems
  20. MultiEvents := mergeToMultiEventItems(binds)
  21. if len(MultiEvents) == 0 {
  22. return errors.New("mergeToMultiEventItems not found envitems")
  23. }
  24. // 2. 获得点位信息
  25. err = MultiEvents.FillCurrentValue()
  26. if err != nil {
  27. return err
  28. }
  29. // 3. 识别事件
  30. var wg sync.WaitGroup
  31. wg.Add(len(binds))
  32. for _, bind := range binds {
  33. item := MultiEvents[bind.Item]
  34. ov := item.GetItemPrevStringVal()
  35. nv := item.GetItemStringVal()
  36. checker := identify.NewSingleCheck(map[string]identify.Rule{bind.Name: bind.Config})
  37. if name := checker.Check(ov, nv); name != "" {
  38. eventChan <- eventInfo{
  39. ProjectId: bind.ProjectId,
  40. DeviceCode: bind.DeviceCode,
  41. Item: bind.Item,
  42. EventName: name,
  43. OldValue: ov,
  44. NewValue: nv,
  45. Time: *item.GetItemHtime(),
  46. }
  47. }
  48. }
  49. wg.Wait()
  50. return nil
  51. }
  52. func findSingleRecord(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcEventBind, error) {
  53. if binds, err := svcCtx.DcEventBind.FindByProjectIdRuleFlag(context.Background(), projectId, EVENT_SINGLE_ITEM); err != nil {
  54. logx.Infof("findSingleRecord not found record")
  55. return nil, err
  56. } else {
  57. return binds, nil
  58. }
  59. }
  60. func mergeToMultiEventItems(binds []model.DcEventBind) envitem.MultiEnvItem {
  61. me := make(envitem.MultiEnvItem)
  62. for _, bind := range binds {
  63. me[bind.Item] = &envitem.EnvItem{
  64. ProjectId: bind.ProjectId,
  65. Item: bind.Item,
  66. }
  67. }
  68. return me
  69. }