|
1 | 1 | local log = require "log"
|
2 | 2 | local Fields = require "fields"
|
| 3 | +local cosock = require "cosock" |
3 | 4 |
|
4 | 5 | --- Room or zone with the children translated from their hue device id or light resource id to
|
5 | 6 | --- their SmartThings represented device object. The grouped light resource id is also moved into
|
@@ -271,4 +272,46 @@ function grouped_utils.set_field_on_group_devices(group, field, v)
|
271 | 272 | end
|
272 | 273 |
|
273 | 274 |
|
| 275 | +function grouped_utils.queue_group_scan(driver, bridge_device) |
| 276 | + local queue = bridge_device:get_field(Fields.GROUPS_SCAN_QUEUE) |
| 277 | + if queue == nil then |
| 278 | + local tx, rx = cosock.channel.new() |
| 279 | + -- Set timeout to 30 seconds to allow for other queued scans to come in. |
| 280 | + rx:settimeout(30) |
| 281 | + cosock.spawn(function() |
| 282 | + while true do |
| 283 | + -- The goal here is to timeout on the receive. If we receive a message then another request |
| 284 | + -- to scan came in. This queuing is to prevent a bunch of added devices from causing to have |
| 285 | + -- to keep scanning over and over. |
| 286 | + local _, err = rx:receive() |
| 287 | + if err then |
| 288 | + -- err is most likely a timeout but break for all errs |
| 289 | + break |
| 290 | + end |
| 291 | + end |
| 292 | + |
| 293 | + -- Finally do the scan |
| 294 | + local hue_device_table = build_hue_id_to_device_map(bridge_device) |
| 295 | + local api = bridge_device:get_field(Fields.BRIDGE_API) |
| 296 | + |
| 297 | + if api == nil then |
| 298 | + log.warn("Bridge api is nil, unable to do group scan") |
| 299 | + -- Clear out tx for the next request |
| 300 | + bridge_device:set_field(Fields.GROUPS_SCAN_QUEUE, nil) |
| 301 | + return |
| 302 | + end |
| 303 | + |
| 304 | + grouped_utils.scan_groups(driver, bridge_device, api, hue_device_table) |
| 305 | + |
| 306 | + -- Clear out tx for the next request |
| 307 | + bridge_device:set_field(Fields.GROUPS_SCAN_QUEUE, nil) |
| 308 | + end) |
| 309 | + -- Set the queue to the tx |
| 310 | + bridge_device:set_field(Fields.GROUPS_SCAN_QUEUE, tx) |
| 311 | + else |
| 312 | + -- Send on the tx that another request has come in. |
| 313 | + queue:send(nil) |
| 314 | + end |
| 315 | +end |
| 316 | + |
274 | 317 | return grouped_utils
|
0 commit comments