From 937040944ffdf7819b7ce7bee23a3d346718c0e4 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 19 Nov 2015 19:33:05 +0100 Subject: Major refactoring, splitting stuff into separate files. --- diller/main.lua | 151 +++----------------------------------------------------- 1 file changed, 6 insertions(+), 145 deletions(-) (limited to 'diller/main.lua') diff --git a/diller/main.lua b/diller/main.lua index 3450bec..98e28b0 100644 --- a/diller/main.lua +++ b/diller/main.lua @@ -4,53 +4,16 @@ function panic(reason) node.restart() end -info = function(msg) - print("info "..msg) -end - -local P = {} - local inter = require('inter') local mq = require('mq') -local properties = {} - -local function read_cfg(name, required) - local filename = "cfg-"..name - if not file.open(filename, "r") then - if required then - panic("Could not read configuration file: "..filename) - else - return nil - end - end - local value = file.readline() - file.close() - if value == nil or #value == 0 then - if required then - panic("Empty configuration file: "..filename) - else - return nil - end - end - return string.sub(value, 1, -2) -end - -local function write_cfg(name, value) - local filename = "cfg-"..name - file.open(filename, "w+") - local ok = file.writeline(value) - file.flush() - file.close() - return ok -end - -local function configure_wlan() +function configure_wlan() local wlan_ssid = read_cfg("wlan-ssid", false) local wlan_password = read_cfg("wlan-password", false) if not wlan_ssid or not wlan_password then info("Missing SSID and/or password configuration, use 'wlan' to configure") + wifi.sta.config('', '') return end @@ -59,99 +22,12 @@ local function configure_wlan() wifi.sta.config(wlan_ssid, wlan_password) end -local function on_cmd(cmd, args) - if not cmd then - return - end - - info("on_cmd: '"..cmd.."', #args="..tostring(table.getn(args))) - for k, v in pairs(args) do - info(k.."="..tostring(v)) - end - - if cmd == "reset" then - print("ok reset") - panic("Reset requested") - elseif cmd == "wlan" then - local ssid = args.ssid password = args.password - - local reconfigured = false - if args.ssid then - write_cfg("wlan-ssid", args.ssid) - end - if args.password then - write_cfg("wlan-password", args.password) - end - if args.ssid and args.password then - configure_wlan() - end - ssid, password, bssid_set, bssid = wifi.sta.getconfig() - print("ok ssid="..(ssid or '')) - elseif cmd == "network" then - local ip = args.ip nm = args.netmask gw = args.gateway - - if args.ip then - wifi.sta.setip({ip=ip, netmask=nm, gateway=gw}) - end - - ip, nm, gw = wifi.sta.getip() - print("ok ip="..(ip or '').." netmask="..(nm or '').." gateway="..(gw or '')) - elseif cmd == "set-property" then - local id = args.id - if not id then - print("fail status=missing-id") - else - property = properties[id] - - local value_path = "property/"..id.."/value" - local name_path = "property/"..id.."/name" - local description_path = "property/"..id.."/description" - - if not property then - info("new property: "..id) - property = {} - properties[id] = property - mq.subscribe(name_path, function() info('message on '..path) end) - mq.subscribe(description_path, function() info('message on '..path) end) - end - - if args.value then - mq.publish(value_path, args.value) - end - if args.name and property.name ~= args.name then - info("publishing name") - mq.publish(name_path, args.name) - property.name = args.name - end - if args.description and property.description ~= args.description then - info("publishing description") - mq.publish(description_path, args.description) - property.description = args.description - end - end - elseif cmd == "publish" then --- print("Publishing, topic="..tostring(cmd.topic)..", payload="..tostring(cmd.payload)) - ok, msg = mq.publish(cmd.topic, cmd.payload) - if ok then - print("ok status="..msg) - else - print("failed status="..msg) - end - else - print("failed status=unknown-command") - end -end - local function print_status() ip, nm, gw = wifi.sta.getip() print("status uptime="..tmr.time().." heap-left="..node.heap().." ip="..tostring(ip).." netmask="..tostring(nm).." gateway="..tostring(gw)) end --- uart.setup(id, baud, databits, parity, stopbits, echo) --- uart.setup(0, 115200, 8, 0, 1, 0) --- uart.setup(0, 9600, 8, 0, 1, 0) - -function P.main() +function main() local timers = { status = 0, inter = 1, @@ -166,23 +42,8 @@ function P.main() inter.init(on_cmd) - tmr.alarm(timers.status, 10 * 1000, 1, print_status) - --- local majorVer, minorVer, devVer, chipId, flashId, flashSize, flashMode, flashSpeed, buildDate = node.info() --- payload = '{"version": "'..majorVer..'.'..minorVer..'.'..devVer..'", "chipId":'..chipId..', "flashId":'..flashId..', "flashSize":'..flashSize..', "flashMode":'..flashMode..', "flashSpeed":'..flashSpeed --- --- if buildDate then --- payload = payload..', "buildDate": "'..buildDate..'"' --- end --- --- if node.info_versions then --- major, minor, dev, buildDate, sdkVersion = node.info_versions() --- payload = payload..'", versions": {"major": '..major..', "minor": '..minor..', "dev": '..dev..', "buildDate": "'..buildDate..'", "sdk": "'..sdkVersion..'"}' --- end --- payload = payload.."}" --- P.publish("firmware", payload) - + load_properties() info("init done") -end -return P + tmr.alarm(timers.status, 10 * 1000, 1, print_status) +end -- cgit v1.2.3