qixiaoning
2025-07-08 84d2ef9760af0a4a4aa933937294400b3caa291d
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package service
 
import (
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/bhomeclient.git"
    "basic.com/valib/bhomedbapi.git"
    "basic.com/valib/logger.git"
    "encoding/json"
    "time"
    "vamicro/cameraCompose-service/models"
    "context"
)
 
type CameraComposeDetail struct {
    Id         string `json:"id"`
    CameraId   string `json:"cameraId"`
    Status     int    `json:"status"`
    Rtsp       string `json:"rtsp"`
    CameraType int    `json:"cameraType"`
    IsAI       bool   `json:"isAI"`
    IsRunning  bool   `json:"isRunning"`
    Name       string `json:"name"`
    Type       string `json:"type"`
}
 
type CameraComposeMsg struct {
    CameraId string  `json:"cameraId"`
    Status   int     `json:"status"`
    Ids      []int64 `json:"ids"`
}
 
type CameraComposeService struct {
}
 
func Init(c context.Context, ms *bhomeclient.MicroNode) {
    tick := time.Tick(15 * time.Second)
    for {
        select {
        case <-c.Done():
            logger.Info("proc close, self update exit")
            return
        case <-tick:
            var compsoeModel models.CameraCompose
            api := bhomedbapi.CameraApi{}
            cameras := api.FindAll("", "", "", "")
            logger.Debug(cameras)
            var cameraIds = make([]string, 0)
            for _, camera := range cameras {
                compsoeModel.Status = 0
                compsoeModel.CameraId = camera.Id
                compsoeModel.Save(false)
                cameraIds = append(cameraIds, camera.Id)
            }
            compsoeModel.DeleteNoExist(cameraIds)
        }
    }
}
 
func (sv CameraComposeService) GetAll() ([]CameraComposeDetail, error) {
    var compsoeModel models.CameraCompose
    composeList, err := compsoeModel.FindAll()
    if nil != err {
        return nil, err
    }
    api := bhomedbapi.CameraApi{}
    var details []CameraComposeDetail
 
    var apiRule bhomedbapi.CameraRuleApi
    b, allCamRules := apiRule.FindAll()
 
    ruleM := make(map[string]protomsg.CameraAndRules)
    if b && allCamRules != nil {
        for _, r := range allCamRules {
            if r.CameraInfo != nil {
                ruleM[r.CameraInfo.Id] = r
            }
        }
    }
    for _, item := range composeList {
        cam, err := api.GetCameraById(item.CameraId)
        if nil == err {
            isAi := false
            if crInfo, ok := ruleM[item.CameraId]; ok && crInfo.Rules != nil && len(crInfo.Rules) > 0 && (cam.RunType == models.TYPE_RUNTYPE_POLL || cam.RunType == models.TYPE_RUNTYPE_REALTIME) {
                isAi = true
            }
            details = append(details, CameraComposeDetail{CameraId: item.CameraId, Status: item.Status, Rtsp: cam.Rtsp, CameraType: int(cam.Type), IsRunning: cam.IsRunning, IsAI: isAi, Name: cam.Name, Type: "4", Id: item.CameraId})
        }
    }
    return details, nil
}
 
//同步更新设置
func PersistentWrapper(topic string, payloads []byte) {
    var cameraComposeList []CameraComposeMsg
    if err := json.Unmarshal(payloads, &cameraComposeList); nil != err {
        logger.Error("handleSubMsg failed to persistent:", topic, string(payloads))
    }
    var cameraCompose models.CameraCompose
    var cameraPerson models.CameraComposePerson
    //cameraCompose.Clean()
 
    for _, item := range cameraComposeList {
        cameraCompose.CameraId = item.CameraId
        cameraCompose.Status = item.Status
        cameraCompose.Save(true)
        cameraPerson.CameraId = item.CameraId
        cameraPerson.Clean()
        for _, personId := range item.Ids {
            cameraPerson.Status = item.Status
            cameraPerson.PersonId = int(personId)
            cameraPerson.Save()
        }
    }
}