-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.go
74 lines (66 loc) · 1.59 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
package internal
import (
"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.SubscribeStruct[model.Notification]("notify/+/+", func(topic string, notify *model.Notification) {
//topics := strings.Split(message.Topic(), "/")
//pid := topics[1]
//id := topics[2]
//var alarm model.Alarm 结构体不取 project,device字段
alarm := make(map[string]any)
has, err := db.Engine.Table("alarm").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").
Where("alarm.id=?", notify.AlarmId).
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 k, v := range alarm {
if val, ok := v.(string); ok {
m[k] = val
}
}
//通知
for _, t := range notify.Channels {
switch t {
case "sms":
//发送短信
err = Send([]string{user.Cellphone}, m)
if err != nil {
log.Info(err)
}
case "voice":
//打电话
}
}
})
}