diff --git a/drivers/SmartThings/jbl/src/discovery.lua b/drivers/SmartThings/jbl/src/discovery.lua index 92150d6efa..b698ca66b1 100644 --- a/drivers/SmartThings/jbl/src/discovery.lua +++ b/drivers/SmartThings/jbl/src/discovery.lua @@ -9,23 +9,24 @@ local st_utils = require "st.utils" local discovery = {} -- mapping from device DNI to info needed at discovery/init time -local device_discovery_cache = {} +local joined_device = {} -local function set_device_field(driver, device) +function discovery.set_device_field(driver, device) log.info(string.format("set_device_field : %s", device.device_network_id)) - local device_cache_value = device_discovery_cache[device.device_network_id] + local device_cache_value = driver.datastore.discovery_cache[device.device_network_id] -- persistent fields device:set_field(fields.DEVICE_IPV4, device_cache_value.ip, {persist = true}) - device:set_field(fields.DEVICE_INFO, device_cache_value.device_info , {persist = true}) device:set_field(fields.CREDENTIAL, device_cache_value.credential , {persist = true}) + device:set_field(fields.DEVICE_INFO, device_cache_value.device_info , {persist = true}) + driver.datastore.discovery_cache[device.device_network_id] = nil end local function update_device_discovery_cache(driver, dni, ip, credential) log.info(string.format("update_device_discovery_cache for device dni: %s, %s", dni, ip)) local device_info = driver.discovery_helper.get_device_info(driver, dni, ip) - device_discovery_cache[dni] = { + driver.datastore.discovery_cache[dni] = { ip = ip, device_info = device_info, credential = credential, @@ -39,6 +40,7 @@ local function try_add_device(driver, device_dni, device_ip) if not credential then log.error(string.format("failed to get credential. dni=%s, ip=%s", device_dni, device_ip)) + joined_device[device_dni] = nil return end @@ -49,8 +51,9 @@ end function discovery.device_added(driver, device) log.info("device_added : dni = " .. tostring(device.device_network_id)) - set_device_field(driver, device) - device_discovery_cache[device.device_network_id] = nil + + discovery.set_device_field(driver, device) + joined_device[device.device_network_id] = nil driver.lifecycle_handlers.init(driver, device) end @@ -75,8 +78,9 @@ local function discovery_device(driver) log.info(string.format("discovery_device dni, ip = %s, %s", dni, ip)) if not known_devices or not known_devices[dni] then log.trace(string.format("unknown dni= %s, ip= %s", dni, ip)) - if not device_discovery_cache[dni] then + if not joined_device[dni] then try_add_device(driver, dni, ip) + joined_device[dni] = true end else log.trace(string.format("known dni= %s, ip= %s", dni, ip)) diff --git a/drivers/SmartThings/jbl/src/init.lua b/drivers/SmartThings/jbl/src/init.lua index a80874086e..d707c27376 100644 --- a/drivers/SmartThings/jbl/src/init.lua +++ b/drivers/SmartThings/jbl/src/init.lua @@ -160,6 +160,11 @@ local function device_init(driver, device) driver.controlled_devices[device_dni] = device + if driver.datastore.discovery_cache[device_dni] then + log.warn("set unsaved device field") + discovery.set_device_field(driver, device) + end + local device_ip = device:get_field(fields.DEVICE_IPV4) local device_info = device:get_field(fields.DEVICE_INFO) local credential = device:get_field(fields.CREDENTIAL) @@ -214,6 +219,10 @@ local lan_driver = Driver("jbl", } ) +if lan_driver.datastore.discovery_cache == nil then + lan_driver.datastore.discovery_cache = {} +end + lan_driver:call_on_schedule(CONNECTION_MONITORING_INTERVAL, connection_monitoring, "JBL Connection monitoring thread") log.info("Starting lan driver") diff --git a/drivers/SmartThings/jbl/src/jbl/api.lua b/drivers/SmartThings/jbl/src/jbl/api.lua index 2c24869e89..3cebabce55 100644 --- a/drivers/SmartThings/jbl/src/jbl/api.lua +++ b/drivers/SmartThings/jbl/src/jbl/api.lua @@ -104,7 +104,22 @@ function jbl_api.get_credential(bridge_ip, socket_builder) end function jbl_api.get_info(device_ip, socket_builder) - return process_rest_response(RestClient.one_shot_get(get_base_url(device_ip) .. "/info", ADDITIONAL_HEADERS, socket_builder)) + local raw_device_info = process_rest_response(RestClient.one_shot_get(get_base_url(device_ip) .. "/info", ADDITIONAL_HEADERS, socket_builder)) + if raw_device_info == nil then + log.error("failed to get device info") + return nil + end + + local device_info = { + deviceType = raw_device_info.deviceType or "", + firmwareVersion = raw_device_info.firmwareVersion or "", + label = raw_device_info.label or "", + manufacturerName = raw_device_info.manufacturerName or "", + modelName = raw_device_info.modelName or "", + serialNumber = raw_device_info.serialNumber or "", + } + + return device_info end function jbl_api:get_status()