1
1
local log = require " log"
2
+ local socket = require " cosock.socket"
2
3
local capabilities = require " st.capabilities"
3
4
local Driver = require " st.driver"
4
5
local discovery = require " discovery"
@@ -54,13 +55,22 @@ local function create_sse(driver, device, credential)
54
55
end
55
56
56
57
eventsource .onerror = function ()
57
- log .error (string.format (" Eventsource error: dni= %s" , device .device_network_id ))
58
+ local DISCONNECTED_STATUS = " disconnected"
59
+ local connection_status = device :get_field (fields .CONNECTION_STATUS )
60
+ if connection_status and connection_status == DISCONNECTED_STATUS then
61
+ log .error (string.format (" Eventsource error: dni= %s" , device .device_network_id ))
62
+ else
63
+ log .error_with ({ hub_logs = true }, string.format (" Eventsource error: disconnected, dni= %s" , device .device_network_id ))
64
+ device :set_field (fields .CONNECTION_STATUS , DISCONNECTED_STATUS )
65
+ end
58
66
device :offline ()
67
+
59
68
end
60
69
61
70
eventsource .onopen = function ()
62
71
log .info_with ({ hub_logs = true }, string.format (" Eventsource open: dni= %s" , device .device_network_id ))
63
72
device :online ()
73
+ device :set_field (fields .CONNECTION_STATUS , " connected" )
64
74
local success , err = status_update (driver , device )
65
75
if not success then
66
76
log .warn (string.format (" Failed to status_update during eventsource.onopen, err = %s dni= %s" , err , device .device_network_id ))
@@ -125,7 +135,6 @@ local function create_monitoring_thread(driver, device, device_info)
125
135
local monitoring_interval = DEFAULT_MONITORING_INTERVAL
126
136
local new_timer = device .thread :call_on_schedule (monitoring_interval , function ()
127
137
check_and_update_connection (driver , device )
128
- driver .device_manager .device_monitor (driver , device , device_info )
129
138
end , " monitor_timer" )
130
139
device :set_field (fields .MONITORING_TIMER , new_timer )
131
140
end
@@ -145,22 +154,27 @@ end
145
154
146
155
local function device_removed (driver , device )
147
156
local conn_info = device :get_field (fields .CONN_INFO )
157
+ driver .removing_devices = driver .removing_devices + 1
158
+ log .info_with ({ hub_logs = true }, string.format (" Device removed: dni= %s" , device .device_network_id ))
148
159
if not conn_info then
149
- log .warn ( string.format (" remove : failed to find conn_info, dni = %s" , device .device_network_id ))
160
+ log .warn_with ({ hub_logs = true }, string.format (" remove : failed to find conn_info, dni = %s" , device .device_network_id ))
150
161
else
151
162
local _ , err , status = conn_info :get_remove ()
152
163
153
164
if err or status ~= 200 then
154
- log .error ( string.format (" remove : failed to get remove, dni= %s, err= %s, status= %s" , device .device_network_id ,
165
+ log .error_with ({ hub_logs = true }, string.format (" remove : failed to get remove, dni= %s, err= %s, status= %s" , device .device_network_id ,
155
166
err ,
156
167
status ))
168
+ else
169
+ log .info_with ({ hub_logs = true }, string.format (" Device removed: token reset success. dni= %s" , device .device_network_id ))
157
170
end
158
171
end
159
172
160
173
local eventsource = device :get_field (fields .EVENT_SOURCE )
161
174
if eventsource then
162
175
eventsource :close ()
163
176
end
177
+ driver .removing_devices = driver .removing_devices - 1
164
178
end
165
179
166
180
local function device_init (driver , device )
@@ -194,13 +208,41 @@ local function device_init(driver, device)
194
208
195
209
update_connection (driver , device , device_ip , device_info )
196
210
211
+ local eventsource = device :get_field (fields .EVENT_SOURCE )
212
+ if not eventsource then
213
+ log .error_with ({ hub_logs = true }, " failed to create EVENT_SOURCE." )
214
+ device :offline ()
215
+ return
216
+ end
217
+
197
218
do_refresh (driver , device , nil )
198
219
end
199
220
200
221
local function device_info_changed (driver , device , event , args )
201
222
do_refresh (driver , device , nil )
202
223
end
203
224
225
+ local function driver_lifecycle_handler (driver , event_name )
226
+ log .info (string.format (" driver lifecycle event :'%s'" , event_name ))
227
+ if event_name == " shutdown" then
228
+ local MAXIMUM_WAITING_TIME = 30
229
+ local WAITING_TIME = 1
230
+ local total_waiting_time = 0
231
+
232
+ while (driver .removing_devices > 0 ) do
233
+ log .info (" waiting for all devices to be removed" )
234
+ total_waiting_time = total_waiting_time + WAITING_TIME
235
+ socket .sleep (WAITING_TIME ) -- wait for 1 seconds
236
+
237
+ if total_waiting_time > MAXIMUM_WAITING_TIME then
238
+ log .error_with ({ hub_logs = true }, " maximum waiting time exceeded" )
239
+ end
240
+ end
241
+ log .info (string.format (" forced exit" ))
242
+ os.exit (0 )
243
+ end
244
+ end
245
+
204
246
local lan_driver = Driver (" aqara-fp2" ,
205
247
{
206
248
discovery = discovery .do_network_discovery ,
@@ -210,6 +252,7 @@ local lan_driver = Driver("aqara-fp2",
210
252
infoChanged = device_info_changed ,
211
253
removed = device_removed
212
254
},
255
+ driver_lifecycle = driver_lifecycle_handler ,
213
256
capability_handlers = {
214
257
[capabilities .refresh .ID ] = {
215
258
[capabilities .refresh .commands .refresh .NAME ] = do_refresh ,
@@ -221,6 +264,7 @@ local lan_driver = Driver("aqara-fp2",
221
264
discovery_helper = fp2_discovery_helper ,
222
265
device_manager = fp2_device_manager ,
223
266
controlled_devices = {},
267
+ removing_devices = 0
224
268
}
225
269
)
226
270
0 commit comments