123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package handler
- import (
- "GtDataStore/app/cmd/events/internal/logic/job"
- "GtDataStore/app/cmd/events/internal/svc"
- "GtDataStore/app/model"
- "context"
- "github.com/zeromicro/go-zero/core/logx"
- "metawant.greentech.com.cn/gaoyagang/gt-common/envitem"
- "metawant.greentech.com.cn/gaoyagang/gt-common/identify"
- "sync"
- "time"
- )
- func EventMulti(ctx context.Context, task *job.Task, technologyName string) error {
- // 1. 查询所有的设备
- binds, err := findMultiRecord(task.Job.SvcCtx, int64(task.Id))
- if err != nil {
- return err
- }
- // 2. 识别事件
- var wg sync.WaitGroup
- wg.Add(len(binds))
- for _, bind := range binds {
- bind := bind
- go func() {
- defer wg.Done()
- multiEnvItem := extractEventItems(bind)
- //fmt.Printf("extractEventItems: %+v\n", multiEnvItem)
- err := multiEnvItem.FillCurrentValue()
- if err != nil {
- return
- }
- ovs := multiEnvItem.FindPrevString()
- nvs := multiEnvItem.FindString()
- rules := make(map[string]identify.MultiRule)
- rules[bind.Name] = bind.Config.Multi
- checker := identify.NewMultiCheck(rules)
- name := checker.Check(ovs, nvs)
- //fmt.Printf("ovs: %+v\n", ovs)
- //fmt.Printf("nvs: %+v\n", nvs)
- //fmt.Printf("rules: %+v\n", rules)
- //fmt.Printf("name: %s\n", name)
- if name != "" {
- items := make([]string, 0)
- times := make(map[string]time.Time)
- for itemName, _ := range nvs {
- items = append(items, itemName)
- times[itemName] = *multiEnvItem.GetItemHtime(itemName)
- }
- multiEventChan <- multiEventInfo{
- ProjectId: bind.ProjectId,
- DeviceCode: bind.DeviceCode,
- Items: items,
- EventId: bind.Id,
- EventName: name,
- OldValues: ovs,
- NewValues: nvs,
- Times: times,
- }
- }
- }()
- }
- wg.Wait()
- return nil
- }
- func findMultiRecord(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcEventBind, error) {
- if data := eventBindCacheTable.GetCache(EVENT_MULTI_ITEM); data != nil {
- return data, nil
- }
- if binds, err := svcCtx.DcEventBind.FindByProjectIdRuleFlag(context.Background(), projectId, EVENT_MULTI_ITEM); err != nil {
- logx.Error("findSingleRecord not found record")
- return nil, err
- } else {
- eventBindCacheTable.SetCache(EVENT_MULTI_ITEM, binds, time.Now().Add(300*time.Second))
- return binds, nil
- }
- }
- func extractEventItems(bind model.DcEventBind) envitem.MultiEnvItem {
- me := make(envitem.MultiEnvItem)
- for itemName, rule := range bind.Config.Multi {
- _ = rule
- me[itemName] = &envitem.EnvItem{
- ProjectId: bind.ProjectId,
- Item: itemName,
- }
- }
- return me
- }
|