-
Notifications
You must be signed in to change notification settings - Fork 72
/
events.py
232 lines (206 loc) · 10.9 KB
/
events.py
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import _thread
from utils import *
from connection import DahuaConnect
class DahuaEvents(DahuaConnect):
def __init__(self):
super(DahuaEvents, self).__init__()
def internal_event_manager(self, dh_data):
""" JSON fixing part, then feed 'local_event_handler()' """
try:
events = fix_json(dh_data)
for event in events:
self.local_event_handler(event)
except Exception as e:
log.failure('[internal_event_manager] {}'.format(repr(e)))
def local_event_handler(self, dh_data):
""" Local event handler """
try:
host = dh_data.get('host')
event_list = dh_data.get('params').get('eventList')
for events in event_list:
if events.get('Action') == 'Start':
"""
Reboot event, remote device is already rebooting and we cannot make clean exit,
so just close instance and reschedule connection
"""
if events.get('Code') == 'Reboot':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), LYELLOW),
color(host, GREEN),
color('Reboot', RED),
))
tmp = False
session = None
for session in self.dhConsole:
if self.dhConsole.get(session).get('host') == host:
log.warning(
"{}: {} ({})".format(
session,
self.dhConsole.get(session).get('device'),
self.dhConsole.get(session).get('host')))
tmp = self.dhConsole.get(session).get('instance')
tmp.terminate = True
tmp.logout()
break
if tmp:
if tmp == self.dh:
del self.dh
self.dhConsole.pop(session)
if len(self.dhConsole):
for session in self.dhConsole:
self.dh = self.dhConsole.get(session).get('instance')
break
else:
del tmp
self.dhConsole.pop(session)
# _thread.start_new_thread(self.restart_connection, ("restart_connection", host,))
_thread.start_new_thread(self.restart_connection, (host,))
elif events.get('Code') == 'Exit':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color('Exit App', RED)
))
elif events.get('Code') == 'ShutDown':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color('ShutDown App', RED)
))
# VTO
elif events.get('Code') == 'AlarmLocal':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color('AlarmLocal [Start]', RED)
))
# VTO
elif events.get('Code') == 'ProfileAlarmTransmit':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color(
'ProfileAlarmTransmit [Start]\n'
'AlarmType: {}, DevSrcType: {}, SenseMethod: {}, UserID: {}'.format(
events.get('Data').get('AlarmType'),
events.get('Data').get('DevSrcType'),
events.get('Data').get('SenseMethod'),
events.get('Data').get('UserID'),
), RED)
))
elif events.get('Code') == 'SafetyAbnormal':
log.warning('[{} ({}) Start ] {}'.format(
color(
events.get('Data').get('AbnormalTime')
if events.get('Data').get('AbnormalTime') else events.get('Data').get('LocaleTime'),
YELLOW
),
color(host, GREEN),
color('{} {}'.format(
events.get('Data').get('ExceptionType'),
events.get('Data').get('Address')
), RED),
))
elif events.get('Action') == 'Stop':
# VTO
if events.get('Code') == 'AlarmLocal':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color('AlarmLocal [Stop]', GREEN)
))
# VTO
elif events.get('Code') == 'ProfileAlarmTransmit':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color(
'ProfileAlarmTransmit [Stop]\n'
'AlarmType: {}, DevSrcType: {}, SenseMethod: {}, UserID: {}'.format(
events.get('Data').get('AlarmType'),
events.get('Data').get('DevSrcType'),
events.get('Data').get('SenseMethod'),
events.get('Data').get('UserID'),
), GREEN)
))
elif events.get('Code') == 'SafetyAbnormal':
log.warning('[{} ({}) Stop ] {}'.format(
color(
events.get('Data').get('AbnormalTime')
if events.get('Data').get('AbnormalTime') else events.get('Data').get('LocaleTime'),
YELLOW
),
color(host, GREEN),
color('{} {}'.format(
events.get('Data').get('ExceptionType'),
events.get('Data').get('Address')
), RED),
))
elif events.get('Action') == 'Pulse':
if events.get('Code') == 'SafetyAbnormal':
log.warning('[{} ({}) ] {}'.format(
color(
events.get('Data').get('AbnormalTime')
if events.get('Data').get('AbnormalTime') else events.get('Data').get('LocaleTime'),
YELLOW
),
color(host, GREEN),
color('{} {}'.format(
events.get('Data').get('ExceptionType'),
events.get('Data').get('Address')
), RED),
))
elif events.get('Code') == 'LoginFailure':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color('Login Failure: {} {} ({})'.format(
events.get('Data').get('Name'),
events.get('Data').get('Address'),
events.get('Data').get('Type')
), RED),
))
elif events.get('Code') == 'RemoteIPModified':
log.warning('[{} ({}) ] {}\n{}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color('DHDiscover.setConfig', YELLOW),
events.get('Data'),
))
elif events.get('Code') == 'Reset':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color('Factory default reset', RED),
))
# VTH
elif events.get('Code') == 'InfoTip':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color('InfoTip', YELLOW),
))
# VTH
elif events.get('Code') == 'KeepLightOn':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color('KeepLightOn: {}'.format(events.get('Data').get('Status')), YELLOW),
))
# VTH
elif events.get('Code') == 'ScreenOff':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color('ScreenOff', YELLOW),
))
# VTH
elif events.get('Code') == 'VthAlarm':
log.warning('[{} ({}) ] {}'.format(
color(events.get('Data').get('LocaleTime'), YELLOW),
color(host, GREEN),
color('VTH Alarm', RED),
))
except Exception as e:
log.failure('[local_event_handler] {}'.format(repr(e)))
pass