Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions memory_per_device/capabilities/collectGarbage-presentation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
id: honestadmin11679.collectgarbage
version: 1
dashboard:
states:
- label: Collect Garbage
alternatives: []
basicPlus: []
actions:
- displayType: pushButton
pushButton:
command: collect
detailView:
- label: Create Target
displayType: pushButton
pushButton:
command: collect
10 changes: 10 additions & 0 deletions memory_per_device/capabilities/collectGarbage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: honestadmin11679.collectgarbage
version: 1
status: proposed
name: collectgarbage
ephemeral: false
attributes: {}
commands:
collect:
name: collect
arguments: []
5 changes: 5 additions & 0 deletions memory_per_device/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'memory_per_device'
packageKey: 'memory_per_device'
permissions:
lan: {}
discovery: {}
6 changes: 6 additions & 0 deletions memory_per_device/profiles/mpd-profile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: mpd_sensor.v1
components:
- id: main
capabilities:
- id: switch
version: 1
8 changes: 8 additions & 0 deletions memory_per_device/profiles/mpd_bridge-profile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: mpd_bridge.v1
components:
- id: main
capabilities:
- id: honestadmin11679.targetcreate
version: 1
- id: honestadmin11679.collectgarbage
version: 1
1 change: 1 addition & 0 deletions memory_per_device/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Memory Per Device
90 changes: 90 additions & 0 deletions memory_per_device/src/disco.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
local json = require 'dkjson'
local log = require 'log'
local utils = require 'st.utils'

--- Add a new device to this driver
---
---@param driver Driver The driver instance to use
---@param device_number number|nil If populated this will be used to generate the device name/label if not, `get_device_list`
--- will be called to provide this value
local function add_sensor_device(driver, device_number, parent_device_id)
log.trace('add_sensor_device')
if device_number == nil then
log.debug('determining current device count')
local device_list = driver.device_api.get_device_list()
device_number = #device_list
end
local device_name = 'MPD ' .. device_number
log.debug('adding device ' .. device_name)
local device_id = utils.generate_uuid_v4() .. tostring(device_number) .. '2'
local device_info = {
type = 'LAN',
deviceNetworkId = device_id,
label = device_name,
parent_device_id = parent_device_id,
profileReference = 'mpd_sensor.v1',
vendorProvidedName = device_name,
}
local device_info_json = json.encode(device_info)
local success, msg = driver.device_api.create_device(device_info_json)
if success then
log.debug('successfully created device')
return device_name, device_id
end
log.error(string.format('unsuccessful create_device (sensor) %s', msg))
end

local function add_bridge_device(driver)
log.trace('add_bridge_device')
local device_id = utils.generate_uuid_v4()
local device_name = "MPD Bridge"
local device_info = {
type = 'LAN',
deviceNetworkId = device_id,
label = device_name,
profileReference = 'mpd_bridge.v1',
vendorProvidedName = device_name,
}
local device_info_json = json.encode(device_info)
local success, msg = driver.device_api.create_device(device_info_json)
if success then
log.debug('successfully created device')
return device_name, device_id
end
log.error(string.format('unsuccessful create_device (bridge) %s', msg))
return nil, nil, msg
end

--- A discovery pass that will discover exactly 1 device
--- for a driver. I any devices are already associated with
--- this driver, no devices will be discovered
---
---@param driver Driver the driver name to use when discovering a device
---@param opts table the discovery options
---@param cont function function to check if discovery should continue
local function disco_handler(driver, opts, cont)
log.trace('disco')

if cont() then
local device_list = driver:get_devices()
log.trace('starting discovery')
for _idx,device in ipairs(device_list or {}) do
if device:supports_capability_by_id("honestadmin11679.targetcreate") then
return
end
end
local device_name, device_id, err = add_bridge_device(driver)
if err ~= nil then
log.error(err)
return
end
log.info('added new device ' .. device_name)
end
end



return {
disco_handler = disco_handler,
add_sensor_device = add_sensor_device,
}
1 change: 1 addition & 0 deletions memory_per_device/src/driver_name.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return "memory_per_device"
66 changes: 66 additions & 0 deletions memory_per_device/src/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
local Driver = require 'st.driver'
local capabilities = require 'st.capabilities'
local discovery = require 'disco'

local createTargetId = "honestadmin11679.targetcreate"
local createTarget = capabilities[createTargetId];
local collectGarbageId = "honestadmin11679.collectgarbage"
local collectGarbage = capabilities[collectGarbageId];

local function is_bridge(device)
return device:supports_capability_by_id(collectGarbageId)
end

local function emit_switch(driver, device, on)
local ev
if on then
ev = capabilities.switch.switch.on()
else
ev = capabilities.switch.switch.off()
end
device:emit_event(ev)
end

local function device_init(driver, device)
if not is_bridge(device) then
emit_switch(driver, device, false)
end
end

local driver = Driver(require("driver_name"), {
lifecycle_handlers = {
init = device_init,
added = device_init,
},
discovery = discovery.disco_handler,
capability_handlers = {
[createTargetId] = {
create = function(driver, device)
discovery.add_sensor_device(driver, nil, driver.bridge_id)
end
},
[collectGarbageId] = {
collect = function(driver, device)
collectgarbage("collect")
collectgarbage("collect")
memory.trim()
end,
create = function(driver, device)
collectgarbage("collect")
collectgarbage("collect")
memory.trim()
end,
},
[capabilities.switch.ID] = {
[capabilities.switch.commands.on.NAME] = function(driver, device)
emit_switch(driver, device, true)
end,
[capabilities.switch.commands.off.NAME] = function(driver, device)
emit_switch(driver, device, false)
end,
},
}
})


driver:run()