local inter = require('inter') local mq = require('mq') local properties = {} function load_properties() local l = file.list() info("loading properties") for k, v in pairs(l) do local key = string.match(k, '^cfg%-property%-(.+)%-key$') if key then -- info('property..'..key) local name = read_cfg('property-'..key..'-name') local description = read_cfg('property-'..key..'-description') local property = { key = key, name = name, description = description } properties[key] = property else -- info('bad name: '..k) end end if true then return end; -- load_properties = nil end 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 == "mqtt" then local status = mq.status() print("ok status="..(status or '')) elseif cmd == "list-properties" then print("ok count="..tostring(table.getn(properties))) for key, property in pairs(properties) do line = "property key="..key if property.name then line = line..' name="'..property.name..'"' end if property.description then line = line..' description="'..property.description..'"' end print(line) end elseif cmd == "set-property" then local key = args.key -- Compatability if not key then key = args.id end if not key then print("fail status=missing-key") else property = properties[key] local value_path = "property/"..key.."/value" local name_path = "property/"..key.."/name" local description_path = "property/"..key.."/description" if not property then write_cfg('property-'..key..'-key', key) info("new property: "..key) property = {key = key} properties[key] = 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