qixiaoning
2025-07-25 94f3085afd10d76fa6e0640b5eed1d615b11ecea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package tasks
 
import (
    "context"
    "encoding/json"
    "time"
    "vamicro/report-service/models"
 
    "vamicro/report-service/util"
 
    "basic.com/valib/logger.git"
)
 
var (
    devCtx, statCtx       context.Context
    devCancel, statCancel context.CancelFunc
)
 
func Start(chMsg chan []byte) {
    // 启动设备信息上报
    go deviceInfoReport()
 
    // 启动设备状态信息上报
    go deviceStateReport()
 
    // 实时报警数据上报
    go aiResultReport(chMsg)
}
 
func ResetDeviceInfoReport() {
    devCancel()
}
 
func ResetDeviceStateReport() {
    statCancel()
}
 
func deviceInfoReport() {
    logger.Info("start device info report task")
    devCtx, devCancel = context.WithCancel(context.Background())
 
    for {
        select {
        case <-devCtx.Done():
            logger.Info("deviceInfoReport exit")
            go deviceInfoReport()
 
            return
        default:
            logger.Info("deviceInfoReport")
 
            time.Sleep(time.Duration(models.IntervalConfig.DevInfo) * time.Second)
        }
    }
}
 
func deviceStateReport() {
    logger.Info("start device state report task")
    statCtx, statCancel = context.WithCancel(context.Background())
 
    for {
        select {
        case <-statCtx.Done():
            logger.Info("deviceStateReport exit")
            go deviceStateReport()
 
            return
        default:
            logger.Info("deviceStateReport")
            time.Sleep(time.Duration(models.IntervalConfig.DevState) * time.Second)
        }
    }
}
 
func aiResultReport(chMsg chan []byte) {
    for {
        select {
        case msg := <-chMsg:
            go handleRuleMsg(msg)
        default:
            time.Sleep(time.Millisecond * 100)
        }
    }
}
 
func handleRuleMsg(msg []byte) {
    var ruleMsg = util.RuleMessage{}
    err := json.Unmarshal(msg, &ruleMsg)
    if err != nil {
        logger.Warn("Unmarshal rule msg err", err)
        return
    }
 
    logger.Info("Recive rule message:", ruleMsg.BaseInfo.CamName, " ", ruleMsg.BaseInfo.TaskName)
}