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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package service
 
import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "time"
    "vamicro/config"
    "vamicro/saas-service/service/nodeService"
 
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/bhomeclient.git"
    "basic.com/valib/logger.git"
)
 
func StartSubTopic(ms *bhomeclient.MicroNode, f func(protomsg.DbAction, string)) {
    logger.Info("StartSubTopic...")
    defer func() {
        if r := recover(); r != nil {
            logger.Errorf("StartSubTopic recover r=%v", r)
        }
    }()
 
    for {
        select {
        case msg := <-ms.SubCh:
            if msg == nil {
                return
            }
 
            // 订阅timerule消息
            topic := string(msg.Topic)
            if topic == "scene-service" {
                var data protomsg.DbChangeMessage
                logger.Infof("StartSubTopic, topic=scene-service, data=%v, len(ms.SubCh)=%v", string(msg.Data), len(ms.SubCh))
                if err := json.Unmarshal(msg.Data, &data); err != nil {
                    return
                }
                if data.Table == protomsg.TableChanged_T_TimeRule {
                    f(data.Action, data.Info)
                }
            }
        }
    }
}
 
func SubTopicCallback(dType protomsg.DbAction, info string) {
    type CameraTimerule struct {
        Id       string `json:"id"`
        Name     string `json:"name"`
        TimeRule string `json:"time_rule"`
    }
    var timerule CameraTimerule
    if err := json.Unmarshal([]byte(info), &timerule); err != nil {
        logger.Errorf("notifyTimeRule to saas SubTopicCallback Unmarshal, err=%v", err)
        return
    }
    notifyTimeRule(dType, timerule.Id, timerule.Name, timerule.TimeRule)
}
 
// 通知 saas 平台 timerule 变更
func notifyTimeRule(dType protomsg.DbAction, id, name, tr string) {
    // 非集群信息,不通知saas
    if nodeService.Node.ClusterId == "" {
        logger.Info("notifyTimeRule to saas, is not cluster, no notify saas")
        return
    }
 
    var url string
    var srcData []byte
    if dType == protomsg.DbAction_Delete {
        url = fmt.Sprintf("http://%v/saas/api-s/cameraTimerule/delete", config.SaasConf.Url)
        srcData = []byte(fmt.Sprintf("{\"id\":\"%v\",\"from\":\"vamicro\"}", id))
    } else if dType == protomsg.DbAction_Insert || dType == protomsg.DbAction_Update {
        url = fmt.Sprintf("http://%v/saas/api-s/cameraTimerule/save", config.SaasConf.Url)
        type TimeRange struct {
            Start string `json:"start"`
            End   string `json:"end"`
        }
 
        type DayCtl struct {
            Day       int32        `json:"day"`
            TimeRange []*TimeRange `json:"time_range"`
        }
        type CameraTimerule struct {
            Id        string    `json:"id"`
            Name      string    `json:"name"`
            TimeRule  []*DayCtl `json:"time_rule"`
            ClusterId string    `json:"clusterId"`
            DevId     string    `json:"devId"`
            From      string    `json:"from"`
        }
        timerule := CameraTimerule{
            Id:        id,
            Name:      name,
            ClusterId: nodeService.Node.ClusterId,
            DevId:     nodeService.Node.NodeId,
            From:      "vamicro",
        }
        var dayC []*DayCtl
        if err := json.Unmarshal([]byte(tr), &dayC); err != nil {
            logger.Errorf("notifyTimeRule to saas Unmarshal, err=%v", err)
            return
        }
        timerule.TimeRule = dayC
        srcData, _ = json.Marshal(timerule)
    } else {
        logger.Errorf("notifyTimeRule to saas dType=%v undefine", dType)
        return
    }
 
    retBuf, err := HttpRCT("POST", url, srcData, 30*time.Second)
    logger.Debugf("notifyTimeRule to saas HttpRCT url=%v, request=%v, response=%v, err=%v", url, string(srcData), string(retBuf), err)
    if err != nil {
        logger.Errorf("notifyTimeRule to saas HttpRCT err=%v", err)
        return
    }
}
 
func HttpRCT(method string, url string, parama []byte, timeout time.Duration) (buf []byte, err error) {
    client := http.Client{
        Timeout: timeout,
    }
    request, err := http.NewRequest(method, url, bytes.NewBuffer(parama))
    request.Header.Set("Content-type", "application/json")
    request.Header.Add("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5NTIxNTcwNjEsInVzZXIiOiJ7XCJpZFwiOlwiNjI0ZjY4ZGYtNTA1NS00OTMzLTkyNTktZGQ1YTZkNzY4YWE0XCIsXCJwYXJlbnRJZFwiOlwiXCIsXCJwZXJtaXNzaW9uc1wiOm51bGwsXCJ1c2VybmFtZVwiOlwiQWRtaW5pc3RyYXRvclwifSJ9.9hCLlblpvxd6R21uxeNF0ppURnv1Pbu9Mhi4yRiK8_s")
 
    if err != nil {
        logger.Debug("build request err:", err)
        return nil, err
    }
 
    resp, err := client.Do(request)
    if err != nil {
        logger.Debug("request error: ", err)
        return nil, err
    }
 
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        logger.Debug("read body err:", err)
        return nil, err
    }
    return body, nil
}