-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.go
76 lines (68 loc) · 1.58 KB
/
subscribe.go
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
package internal
import (
"encoding/json"
paho "github.com/eclipse/paho.mqtt.golang"
"github.com/zgwit/iot-master/v3/model"
"github.com/zgwit/iot-master/v3/pkg/db"
"github.com/zgwit/iot-master/v3/pkg/log"
"github.com/zgwit/iot-master/v3/pkg/mqtt"
)
func Subscribe() {
mqtt.Client.Subscribe("notify/+/+", 0, func(client paho.Client, message paho.Message) {
//topics := strings.Split(message.Topic(), "/")
//pid := topics[1]
//id := topics[2]
//解析数据
var notify model.Notification
err := json.Unmarshal(message.Payload(), ¬ify)
if err != nil {
log.Error(err)
return
}
var alarm model.Alarm
has, err := db.Engine.ID(notify.AlarmId).Select("alarm.*, product.name as product, device.name as device").
Join("INNER", "product", "product.id=alarm.product_id").
Join("INNER", "device", "device.id=alarm.device_id").
Get(&alarm)
if err != nil {
log.Error(err)
return
}
if !has {
log.Error("找不到记录")
return
}
var user model.User
has, err = db.Engine.ID(notify.UserId).Get(&user)
if err != nil {
log.Error(err)
return
}
if !has {
log.Error("找不到用户")
return
}
//只取字符串参数
m := map[string]string{
"user": user.Name,
"product": alarm.Product,
"device": alarm.Device,
"type": alarm.Type,
"title": alarm.Title,
"message": alarm.Message,
}
//通知
for _, t := range notify.Channels {
switch t {
case "sms":
//发送短信
err = Send([]string{user.Cellphone}, m)
if err != nil {
log.Info(err)
}
case "voice":
//打电话
}
}
})
}