Skip to content

Commit

Permalink
Fixing on/off and moving device event handler to module.
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicletz committed May 26, 2017
1 parent bb21af9 commit d85727c
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 122 deletions.
3 changes: 2 additions & 1 deletion Solutionfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"custom_api": "sample_api.lua",
"custom_api_hook": "_init",
"modules": {
"auth": "modules/util.lua",
"device": "modules/device.lua",
"util": "modules/util.lua",
"debug": "modules/debug.lua",
"listen": "modules/listen.lua"
},
Expand Down
2 changes: 1 addition & 1 deletion app/api/lightbulbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function addLightbulb(name, serialnumber) {
}

function setLightbulbState(serialNumber, state) {
return service.post(`/lightbulb/${serialNumber}`, { state: state.toString() });
return service.post(`/lightbulb/${serialNumber}`, { state: state });
}

export default {
Expand Down
82 changes: 1 addition & 81 deletions event_handler/product.lua
Original file line number Diff line number Diff line change
@@ -1,81 +1 @@
-- Check event type (could be: "data_in" | "connect" | "disconnect" )
if event.type == "data_in" then
data_timestamp = event.payload[1].timestamp
full_payload = event.payload[1].values

data = {
["alias"] = "",
["value"] = {},
["device_sn"] = "",
["source_ip"] = ""
}

for key, value in pairs(full_payload) do
-- For each resource alias included in this event's payload:
-- build-out the old "data" object from the new "event" object
data.alias = key
data.value[1] = data_timestamp
data.value[2] = value
data.device_sn = event.identity
data.source_ip = event.ip

-- Example approach for creating historical log of data using TSDB Service
local metrics = {
[data.alias] = tostring(data.value[2])
}
local tags = {
pid = data.pid,
sn = data.device_sn
}
Tsdb.write({
metrics = metrics,
tags = tags
})

local value = kv_read(data.device_sn)
if value == nil then
value = {
humidity = nil,
temperature = nil,
state = nil
}
end
value[data.alias] = data.value[2]
-- store the last timestamp from this device
value["timestamp"] = data.value[1]/1000
value["ip"] = data.source_ip

if value.alerts ~= nil and data.alias == "state" then
local timerid = data.device_sn .. "_state"
for _ ,alert in ipairs(value.alerts) do
if alert.state == value.state then --condition true
if alert.active and not alert.timer_running then --enabled, not running
trigger(alert, timerid)
end
end
if not alert.active and alert.timer_running then --disabled, running
cancel_trigger(alert)
end
end
end

local listen = value.listen
if listen ~= nil and listen.sn ~= nil and listen.socket_id ~= nil and listen.server_ip then
if data.device_sn == listen.sn then
local msg = {
sn = listen.sn,
alias = data.alias,
timestamp = data.value[1],
value = data.value[2]
}
Websocket.send({
socket_id = listen.socket_id,
server_ip = listen.server_ip,
message = to_json(msg),
type="data-text"
})
end
end
kv_write(data.device_sn, value)
end
end
device_event(event)
101 changes: 101 additions & 0 deletions modules/device.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
-- INSTRUCTIONS
-- Ensure at least one Product is aliased as 'Lightbulb'.
-- Also call 'device_event(event)' from the 'Lightbulb' event handler.

function device_write(sn, alias, value)
local device = kv_read(sn)
device.alias = value

-- save to keystore
kv_write(sn, device)

-- push to device
return Lightbulb.setIdentityState({
identity=sn,
[alias]=value
})
end

function device_event(event)
-- Check event type (could be: "data_in" | "connect" | "disconnect" )
if event.type == "data_in" then
data_timestamp = event.payload[1].timestamp
full_payload = event.payload[1].values

data = {
["alias"] = "",
["value"] = {},
["device_sn"] = "",
["source_ip"] = ""
}

for key, value in pairs(full_payload) do
-- For each resource alias included in this event's payload:
-- build-out the old "data" object from the new "event" object
data.alias = key
data.value[1] = data_timestamp
data.value[2] = value
data.device_sn = event.identity
data.source_ip = event.ip

-- Example approach for creating historical log of data using TSDB Service
local metrics = {
[data.alias] = tostring(data.value[2])
}
local tags = {
pid = data.pid,
sn = data.device_sn
}
Tsdb.write({
metrics = metrics,
tags = tags
})

local value = kv_read(data.device_sn)
if value == nil then
value = {
humidity = nil,
temperature = nil,
state = nil
}
end
value[data.alias] = data.value[2]
-- store the last timestamp from this device
value["timestamp"] = data.value[1]/1000
value["ip"] = data.source_ip

if value.alerts ~= nil and data.alias == "state" then
local timerid = data.device_sn .. "_state"
for _ ,alert in ipairs(value.alerts) do
if alert.state == value.state then --condition true
if alert.active and not alert.timer_running then --enabled, not running
trigger(alert, timerid)
end
end
if not alert.active and alert.timer_running then --disabled, running
cancel_trigger(alert)
end
end
end

local listen = value.listen
if listen ~= nil and listen.sn ~= nil and listen.socket_id ~= nil and listen.server_ip then
if data.device_sn == listen.sn then
local msg = {
sn = listen.sn,
alias = data.alias,
timestamp = data.value[1],
value = data.value[2]
}
Websocket.send({
socket_id = listen.socket_id,
server_ip = listen.server_ip,
message = to_json(msg),
type="data-text"
})
end
end
kv_write(data.device_sn, value)
end
end
end
16 changes: 0 additions & 16 deletions modules/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,6 @@ function kv_write(sn, values)
Keystore.set({key = "sn_" .. sn, value = to_json(values)})
end

function device_write(sn, alias, value)
local device = kv_read(sn)
if device.pid == nil then
return {status="ERROR", reason="device needs to send data first"}
end

-- save to keystore
kv_write(sn, {[alias]=value})

-- push to device
return Lightbulb.setIdentityState({
identity=sn,
[alias]=value
})
end

http_error_codes = {
[400] = {
code = 400,
Expand Down
49 changes: 27 additions & 22 deletions public/builds/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/builds/bundle.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d85727c

Please sign in to comment.