Skip to content

Commit

Permalink
Added usage of datastore to save discovery cache (#1130)
Browse files Browse the repository at this point in the history
* fix for driver crash issue #46
apply patches:
SolutionsEngineering/stse-edge-network-audio-example#63
SolutionsEngineering/stse-edge-network-audio-example#64

* Fix luacheck issue

---------

Co-authored-by: ADHARMAN\ChCai <[email protected]>
Co-authored-by: lelandblue <[email protected]>
  • Loading branch information
3 people authored Feb 8, 2024
1 parent 500c873 commit 7299001
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
20 changes: 12 additions & 8 deletions drivers/SmartThings/jbl/src/discovery.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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))
Expand Down
9 changes: 9 additions & 0 deletions drivers/SmartThings/jbl/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
17 changes: 16 additions & 1 deletion drivers/SmartThings/jbl/src/jbl/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7299001

Please sign in to comment.