12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package Handle
- import (
- "AIOTCOER/lib"
- "AIOTCOER/logs"
- "AIOTCOER/models/Device"
- "encoding/json"
- "go.mongodb.org/mongo-driver/bson"
- "io/ioutil"
- "net/http"
- "strings"
- "time"
- )
- // 转发 匹配TAB
- func Relay(Msid string, Device_r *Device.Device, JointTab string, ArticleSlide *bson.M) {
- // 将bson.M转换为map[string]interface{}
- // fmt.Println("Relay:",JointTab,*ArticleSlide)
- jsonData, err := json.Marshal(*ArticleSlide)
- if err != nil {
- logs.Println(Msid, "Relay json.Marshal!")
- return
- }
- //fmt.Println("Relay-:",JointTab,*ArticleSlide)
- for _, value := range Device_r.T_RelayDataJson {
- //value = {
- // "T_tabs": "AAAA.AAAA",
- // "T_name": "推送报警",
- // "T_pushid": 0,
- // "T_pub": "/aaa/{$sn}/AAAA"
- //},
- if value["T_tabs"] == JointTab || value["T_tabs"] == "*" {
- // 替换发布号变量
- T_pub := strings.Replace(lib.To_string(value["T_pub"]), "{$sn}", Device_r.T_sn, -1) // {$sn} :代表当前数据设备编号SN
- if len(T_pub) == 0 {
- logs.Println(Msid, "订阅号错误!")
- return
- }
- // 推送 数据
- go HTTPPush(T_pub, jsonData)
- //// 流量统计
- //sd, sdis := StatisticalData.FlowCountMap[Device_r.T_ProductID]
- //if sdis {
- // sd = &StatisticalData.FlowCount_T{}
- // StatisticalData.FlowCountMap[Device_r.T_ProductID] = sd
- //}
- //sd.T_relay += 1
- }
- }
- }
- // 推送
- func HTTPPush(url string, data []byte) {
- payload := strings.NewReader(string(data))
- client := &http.Client{
- Timeout: 3 * time.Second, // 设置超时时间为5秒
- }
- req, err := http.NewRequest("POST", url, payload)
- if err != nil {
- logs.Println("Relay -> API!!!:", url, any(err))
- return
- }
- req.Header.Add("Content-Type", "application/json")
- res, err := client.Do(req)
- if err != nil {
- logs.Println("Relay -> API!!!:", url, any(err))
- return
- }
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- if err != nil {
- logs.Println("Relay -> API!!!:", url, any(err))
- return
- }
- logs.Println("Relay -> API Body:", string(body))
- return
- }
|