|
@@ -6,23 +6,96 @@ import (
|
|
|
"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. 查询所有的设备
|
|
|
- _, err := findMultiRecord(task.Job.SvcCtx, int64(task.Id))
|
|
|
+ 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 records, err := svcCtx.DcEventBind.FindByProjectIdRuleFlag(context.Background(), projectId, EVENT_MULTI_ITEM); err != nil {
|
|
|
- logx.Infof("findMultiRecord not found record")
|
|
|
+ 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 {
|
|
|
- return records, nil
|
|
|
+ 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
|
|
|
+}
|