-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsip_handler.go
163 lines (128 loc) · 3.82 KB
/
sip_handler.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"strings"
"time"
)
// Handler 处理下级设备的消息
type Handler interface {
OnUnregister(id string)
OnRegister(id, transport, addr string) (int, GBDevice, bool)
OnKeepAlive(id string) bool
OnCatalog(device GBDevice, response *CatalogResponse)
OnRecord(device GBDevice, response *QueryRecordInfoResponse)
OnDeviceInfo(device GBDevice, response *DeviceInfoResponse)
OnNotifyPosition(notify *MobilePositionNotify)
}
type EventHandler struct {
}
func (e *EventHandler) OnUnregister(id string) {
device := DeviceManager.Find(id)
if device != nil {
device.(*Device).Status = OFF
}
if DB != nil {
_ = DB.SaveDevice(device.(*Device))
}
}
func (e *EventHandler) OnRegister(id, transport, addr string) (int, GBDevice, bool) {
// 不能和级联设备的上级ID冲突
if PlatformManager.FindPlatform(id) != nil {
Sugar.Errorf("注册失败, ID与级联设备冲突. device: %s", id)
return -1, nil, false
}
var device *Device
old := DeviceManager.Find(id)
if old != nil {
old.(*Device).ID = id
old.(*Device).Transport = transport
old.(*Device).RemoteAddr = addr
device = old.(*Device)
} else {
device = &Device{
ID: id,
Transport: transport,
RemoteAddr: addr,
}
DeviceManager.Add(device)
}
device.Status = ON
device.RegisterTime = time.Now().UnixMilli()
if DB != nil {
if err := DB.SaveDevice(device); err != nil {
Sugar.Errorf("保存设备信息到数据库失败 device: %s err: %s", id, err.Error())
}
}
return 3600, device, device.ChannelsTotal < 1
}
func (e *EventHandler) OnKeepAlive(id string) bool {
device := DeviceManager.Find(id)
if device == nil {
Sugar.Errorf("更新心跳失败, 设备不存在. device: %s", id)
return false
}
if !device.(*Device).Online() {
Sugar.Errorf("更新心跳失败, 设备离线. device: %s", id)
}
if DB != nil {
if err := DB.RefreshHeartbeat(id); err != nil {
Sugar.Errorf("更新有效期失败. device: %s err: %s", id, err.Error())
}
}
return true
}
func (e *EventHandler) OnCatalog(device GBDevice, response *CatalogResponse) {
if DB == nil {
return
}
id := device.GetID()
for _, channel := range response.DeviceList.Devices {
// 状态转为大写
channel.Status = OnlineStatus(strings.ToUpper(channel.Status.String()))
// 默认在线
if OFF != channel.Status {
channel.Status = ON
}
// 判断之前是否已经存在通道, 如果不存在累加总数
old, _ := DB.QueryChannel(id, channel.DeviceID)
if err := DB.SaveChannel(id, channel); err != nil {
Sugar.Infof("保存通道到数据库失败 err: %s", err.Error())
}
if old == nil {
device.(*Device).ChannelsTotal++
device.(*Device).ChannelsOnline++
} else if old.Status != channel.Status {
// 保留处理其他状态
if ON == channel.Status {
device.(*Device).ChannelsOnline++
} else if OFF == channel.Status {
device.(*Device).ChannelsOnline--
} else {
return
}
}
if err := DB.SaveDevice(device.(*Device)); err != nil {
Sugar.Errorf("更新设备在线数失败 err: %s", err.Error())
}
}
}
func (e *EventHandler) OnRecord(device GBDevice, response *QueryRecordInfoResponse) {
event := SNManager.FindEvent(response.SN)
if event == nil {
Sugar.Errorf("处理录像查询响应失败 SN: %d", response.SN)
return
}
event(response)
}
func (e *EventHandler) OnDeviceInfo(device GBDevice, response *DeviceInfoResponse) {
device.(*Device).Manufacturer = response.Manufacturer
device.(*Device).Model = response.Model
device.(*Device).Firmware = response.Firmware
device.(*Device).Name = response.DeviceName
if DB != nil {
if err := DB.SaveDevice(device.(*Device)); err != nil {
Sugar.Errorf("保存设备信息到数据库失败 device: %s err: %s", device.GetID(), err.Error())
}
}
}
func (e *EventHandler) OnNotifyPosition(notify *MobilePositionNotify) {
}