0
0
mirror of https://github.com/Doodle3D/doodle3d-firmware.git synced 2024-12-22 11:03:48 +01:00

better network connect feedback

control access managment
start of api:info/status
This commit is contained in:
peteruithoven 2013-10-10 12:11:16 +02:00
parent 8991845427
commit d49b1bbd94
9 changed files with 276 additions and 69 deletions

View File

@ -201,7 +201,7 @@ local function main(environment)
if err ~= nil then log:error(err) end if err ~= nil then log:error(err) end
response:send() response:send()
response:executePostResponseQueue() response:executePostResponseQueue()
else else
log:info("Nothing to do...bye.\n") log:info("Nothing to do...bye.\n")
end end

View File

@ -3,6 +3,7 @@ local log = require('util.logger')
local settings = require('util.settings') local settings = require('util.settings')
local wifi = require('network.wlanconfig') local wifi = require('network.wlanconfig')
local uci = require('uci').cursor() local uci = require('uci').cursor()
local signin = require('network.signin')
local M = {} local M = {}
local reconf = {} local reconf = {}
@ -13,7 +14,12 @@ M.WWW_CAPTIVE_PATH = '/usr/share/lua/wifibox/www'
M.WWW_CAPTIVE_INDICATOR = '/www/.wifibox-inplace' M.WWW_CAPTIVE_INDICATOR = '/www/.wifibox-inplace'
M.WWW_RENAME_NAME = '/www-regular' M.WWW_RENAME_NAME = '/www-regular'
M.CONNECTING_FAILED = -1
M.NOT_CONNECTED = 0
M.CONNECTING = 1
M.CONNECTED = 2
M.CREATING = 3
M.CREATED = 4
local function reloadBit(dlist, itemname) local function reloadBit(dlist, itemname)
if dlist[itemname] == nil then dlist[itemname] = '' end if dlist[itemname] == nil then dlist[itemname] = '' end
@ -250,11 +256,16 @@ end
-- @tparam string ssid The SSID to use for the access point. -- @tparam string ssid The SSID to use for the access point.
-- @return True on success or nil+msg on error. -- @return True on success or nil+msg on error.
function M.setupAccessPoint(ssid) function M.setupAccessPoint(ssid)
M.setStatus(M.CREATING,"Creating access point...");
M.switchConfiguration{apnet="add_noreload"} M.switchConfiguration{apnet="add_noreload"}
wifi.activateConfig(ssid) wifi.activateConfig(ssid)
-- NOTE: dnsmasq must be reloaded after network or it will be unable to serve IP addresses -- NOTE: dnsmasq must be reloaded after network or it will be unable to serve IP addresses
M.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add_noreload", wwwredir="add", dnsredir="add" } M.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add_noreload", wwwredir="add", dnsredir="add" }
M.switchConfiguration{dhcp="reload"} M.switchConfiguration{dhcp="reload"}
M.setStatus(M.CREATED,"Access point created");
return true return true
end end
@ -268,6 +279,9 @@ end
-- @return True on success or nil+msg on error. -- @return True on success or nil+msg on error.
function M.associateSsid(ssid, passphrase, recreate) function M.associateSsid(ssid, passphrase, recreate)
log:info("netconfig:associateSsid: "..(ssid or "<nil>")..", "..(passphrase or "<nil>")..", "..(recreate or "<nil>")) log:info("netconfig:associateSsid: "..(ssid or "<nil>")..", "..(passphrase or "<nil>")..", "..(recreate or "<nil>"))
M.setStatus(M.CONNECTING,"Connecting...");
-- see if previously configured network for given ssid exists -- see if previously configured network for given ssid exists
local cfg = nil local cfg = nil
for _, net in ipairs(wifi.getConfigs()) do for _, net in ipairs(wifi.getConfigs()) do
@ -284,7 +298,9 @@ function M.associateSsid(ssid, passphrase, recreate)
wifi.createConfigFromScanInfo(scanResult, passphrase) wifi.createConfigFromScanInfo(scanResult, passphrase)
else else
--check for error --check for error
return nil,"no wireless network with requested SSID is available" local msg = "Wireless network "..ssid.." is not available"
M.setStatus(M.CONNECTING_FAILED,msg);
return nil,msg
end end
end end
@ -297,10 +313,59 @@ function M.associateSsid(ssid, passphrase, recreate)
-- check if we are actually associated -- check if we are actually associated
local status = wifi.getDeviceState() local status = wifi.getDeviceState()
if not status.ssid or status.ssid ~= ssid then if not status.ssid or status.ssid ~= ssid then
return nil,"could not associate with network (incorrect passphrase?)" local msg = "Could not associate with network (incorrect password?)"
M.setStatus(M.CONNECTING_FAILED,msg);
return nil,msg
end
M.setStatus(M.CONNECTED,"Connected");
-- signin to connect.doodle3d.com
local success, output = signin.signin()
if success then
log:info("API:Network:signed in")
else
log:info("API:Network:Signing in failed")
end end
return true return true
end end
--- Disassociate wlan device as client from all SSID's.
-- Note: this function might belong in the wlanconfig module but that would introduce
-- a circular dependency, easiest solution is to place the function here.
-- @return True on success or nil+msg on error.
function M.disassociate()
M.setStatus(M.NOT_CONNECTED,"Not connected");
wifi.activateConfig()
return wifi.restart()
end
function M.getStatus()
log:info("getStatus")
local file, error = io.open('/tmp/networkstatus.txt','r')
if file == nil then
--log:error("Util:Access:Can't read controller file. Error: "..error)
return "",""
else
local status = file:read('*a')
--log:info(" status: "..utils.dump(status))
file:close()
local parts = {}
local code, msg = string.match(status, "([^|]+)|+(.*)")
--log:info(" code: "..utils.dump(code))
--log:info(" msg: "..utils.dump(msg))
return code,msg
end
end
function M.setStatus(code,msg)
log:info("setStatus: "..code.." | "..msg)
local file = io.open('/tmp/networkstatus.txt','w')
file:write(code.."|"..msg)
file:flush()
file:close()
end
return M return M

View File

@ -1,4 +1,10 @@
local lfs = require('lfs') local lfs = require('lfs')
local log = require('util.logger')
local utils = require('util.utils')
local accessManager = require('util.access')
local printDriver = require('print3d')
local printerUtils = require('util.printer')
--local printerAPI = require('rest.api.api_printer')
local TMP_DIR = '/tmp' local TMP_DIR = '/tmp'
local LOG_COLLECT_DIRNAME = 'wifibox-logs' local LOG_COLLECT_DIRNAME = 'wifibox-logs'
@ -15,7 +21,6 @@ local function redirectedExecute(cmd)
return os.execute(cmd .. " > /dev/null 2>&1") return os.execute(cmd .. " > /dev/null 2>&1")
end end
local M = { local M = {
isApi = true isApi = true
} }
@ -90,4 +95,90 @@ function M.logfiles(request, response)
local rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_ARCHIVE_FILE) local rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_ARCHIVE_FILE)
end end
function M.access(request, response)
--log:info(" remoteAddress: |"..utils.dump(request.remoteAddress).."|");
--log:info(" controller: |"..utils.dump(accessManager.getController()).."|");
-- when there is a controller we check if the printer is idle,
-- if so, it should be done printing and we can clear the controller
if accessManager.getController() ~= "" then
local argId = request:get("id")
local printer,msg = printerUtils.createPrinterOrFail(argId, response)
local rv,msg = printer:getState()
if rv then
response:setSuccess()
if(state == "idle") then -- TODO: define in constants somewhere
accessManager.setController("") -- clear controller
end
else
response:setError(msg)
return
end
end
local hasControl = accessManager.hasControl(request.remoteAddress)
response:setSuccess()
response:addData('hasControl', hasControl)
return
end
function M.status(request, response)
--[[
local argId = request:get("id")
local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer then return end
response:addData('id', argId)
-- Temperature
local temperatures,msg = printer:getTemperatures()
if temperatures then
response:setSuccess()
response:addData('hotend', temperatures.hotend)
response:addData('hotend_target', temperatures.hotend_target)
response:addData('bed', temperatures.bed)
response:addData('bed_target', temperatures.bed_target)
else
response:setError(msg)
return
end
--TODO: reuse printer/temperature api
-- Progress
-- NOTE: despite their names, `currentLine` is still the error indicator and `numLines` the message in such case.
local currentLine,numLines = printer:getProgress()
if currentLine then
response:setSuccess()
response:addData('current_line', currentLine)
response:addData('num_lines', numLines)
else
response:setError(numLines)
return
end
--TODO: reuse printer/progress api
-- State
local rv,msg = printer:getState()
if rv then
response:setSuccess()
response:addData('state', rv)
else
response:setError(msg)
return
end
]]---
----TODO: reuse printer/state api
--printerAPI.temperature(request, response)
--printerAPI.progress(request, response)
--printerAPI.state(request, response)
-- access
M.access(request, response)
response:addData('v', 8)
end
return M return M

View File

@ -78,11 +78,10 @@ function M.status(request, response)
local withRaw = utils.toboolean(request:get("with_raw")) local withRaw = utils.toboolean(request:get("with_raw"))
local ds = wifi.getDeviceState() local ds = wifi.getDeviceState()
if ds.ssid == nil then local statusCode, statusMessage = netconf.getStatus()
response:setFail("Not connected") response:addData("status",statusCode)
else response:addData("statusMessage",statusMessage)
response:setSuccess() response:setSuccess(statusMessage)
end
response:addData("ssid", ds.ssid or "") response:addData("ssid", ds.ssid or "")
response:addData("bssid", ds.bssid or "") response:addData("bssid", ds.bssid or "")
@ -94,6 +93,9 @@ function M.status(request, response)
response:addData("txpower", ds.txpower) response:addData("txpower", ds.txpower)
response:addData("signal", ds.signal) response:addData("signal", ds.signal)
response:addData("noise", ds.noise) response:addData("noise", ds.noise)
if withRaw then response:addData("_raw", utils.dump(ds)) end if withRaw then response:addData("_raw", utils.dump(ds)) end
local localip = wifi.getLocalIP() local localip = wifi.getLocalIP()
@ -102,10 +104,6 @@ end
--requires ssid(string), accepts phrase(string), recreate(bool) --requires ssid(string), accepts phrase(string), recreate(bool)
function M.associate_POST(request, response) function M.associate_POST(request, response)
local utils = require('util.utils')
local log = require('util.logger')
log:info("API:Network:associate")
local argSsid = request:get("ssid") local argSsid = request:get("ssid")
local argPhrase = request:get("phrase") local argPhrase = request:get("phrase")
local argRecreate = request:get("recreate") local argRecreate = request:get("recreate")
@ -114,38 +112,22 @@ function M.associate_POST(request, response)
response:setError("missing ssid argument") response:setError("missing ssid argument")
return return
end end
local associate = function()
local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate)
end
response:addPostResponseFunction(associate)
response:setSuccess("wlan is trying to associate")
--[[local helloA = function() local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate)
local log = require('util.logger')
log:info("HELLO A")
end
response:addPostResponseFunction(helloA)
local helloB = function()
local log = require('util.logger')
log:info("HELLO B")
end
response:addPostResponseFunction(helloB)]]--
--[[response:addData("ssid", argSsid)
if rv then if rv then
response:setSuccess("wlan associated") log:info("associated to wifi: "..utils.dump(argSsid))
else else
response:setFail(msg) log:info("failed to associate to wifi: "..utils.dump(argSsid).." ("..utils.dump(msg)..")")
end]]-- end
response:setSuccess("wlan is trying to associate")
end end
function M.disassociate_POST(request, response) function M.disassociate_POST(request, response)
wifi.activateConfig()
local rv = wifi.restart() local rv = netconf.disassociate()
response:setSuccess("all wireless networks deactivated") response:setSuccess("all wireless networks deactivated")
response:addData("wifi_restart_result", rv) response:addData("wifi_restart_result", rv)
end end

View File

@ -3,6 +3,8 @@ local log = require('util.logger')
local utils = require('util.utils') local utils = require('util.utils')
local settings = require('util.settings') local settings = require('util.settings')
local printDriver = require('print3d') local printDriver = require('print3d')
local printerUtils = require('util.printer')
local accessManager = require('util.access')
local M = { local M = {
isApi = true isApi = true
@ -15,27 +17,6 @@ local PROGRESS_FILE = 'progress2.out'
local COMMAND_FILE = 'command.in' local COMMAND_FILE = 'command.in'
local GCODE_TMP_FILE = 'combined.gc' local GCODE_TMP_FILE = 'combined.gc'
--returns a printer instance or nil (and sets error state on response in the latter case)
local function createPrinterOrFail(deviceId, response)
local msg,printer = nil, nil
if deviceId and deviceId ~= "" then
printer,msg = printDriver.getPrinter(deviceId)
else
msg = "missing device ID"
end
if not printer then
response:setError("could not open printer driver (" .. msg .. ")")
response:addData('id', deviceId)
return nil
end
return printer
end
function M._global(request, response) function M._global(request, response)
-- TODO: list all printers (based on /dev/ttyACM* and /dev/ttyUSB*) -- TODO: list all printers (based on /dev/ttyACM* and /dev/ttyUSB*)
response:setSuccess() response:setSuccess()
@ -44,7 +25,7 @@ end
--requires id(string) --requires id(string)
function M.temperature(request, response) function M.temperature(request, response)
local argId = request:get("id") local argId = request:get("id")
local printer,msg = createPrinterOrFail(argId, response) local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer then return end if not printer then return end
local temperatures,msg = printer:getTemperatures() local temperatures,msg = printer:getTemperatures()
@ -64,7 +45,7 @@ end
--requires id(string) --requires id(string)
function M.progress(request, response) function M.progress(request, response)
local argId = request:get("id") local argId = request:get("id")
local printer,msg = createPrinterOrFail(argId, response) local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer then return end if not printer then return end
-- NOTE: despite their names, `currentLine` is still the error indicator and `numLines` the message in such case. -- NOTE: despite their names, `currentLine` is still the error indicator and `numLines` the message in such case.
@ -84,7 +65,7 @@ end
--requires id(string) --requires id(string)
function M.busy(request, response) function M.busy(request, response)
local argId = request:get("id") local argId = request:get("id")
local printer,msg = createPrinterOrFail(argId, response) local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer then return end if not printer then return end
local rv,msg = printer:getState() local rv,msg = printer:getState()
@ -101,7 +82,7 @@ end
--requires id(string) --requires id(string)
function M.state(request, response) function M.state(request, response)
local argId = request:get("id") local argId = request:get("id")
local printer,msg = createPrinterOrFail(argId, response) local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer then return end if not printer then return end
local rv,msg = printer:getState() local rv,msg = printer:getState()
@ -115,10 +96,18 @@ function M.state(request, response)
end end
end end
--requires id(string) --requires id(string)
function M.heatup_POST(request, response) function M.heatup_POST(request, response)
if not accessManager.hasControl(request.remoteAddress) then
response:setFail("No control access")
return
end
local argId = request:get("id") local argId = request:get("id")
local printer,msg = createPrinterOrFail(argId, response) local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer then return end if not printer then return end
local temperature = settings.get('printer.heatup.temperature') local temperature = settings.get('printer.heatup.temperature')
@ -132,8 +121,14 @@ end
--requires id(string) --requires id(string)
function M.stop_POST(request, response) function M.stop_POST(request, response)
if not accessManager.hasControl(request.remoteAddress) then
response:setFail("No control access")
return
end
local argId = request:get("id") local argId = request:get("id")
local printer,msg = createPrinterOrFail(argId, response) local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer then return end if not printer then return end
local endGcode = settings.get('printer.endgcode') local endGcode = settings.get('printer.endgcode')
@ -149,12 +144,28 @@ end
--accepts: first(bool) (chunks will be concatenated but output file will be cleared first if this argument is true) --accepts: first(bool) (chunks will be concatenated but output file will be cleared first if this argument is true)
--accepts: last(bool) (chunks will be concatenated and only when this argument is true will printing be started) --accepts: last(bool) (chunks will be concatenated and only when this argument is true will printing be started)
function M.print_POST(request, response) function M.print_POST(request, response)
local controllerIP = accessManager.getController()
local hasControl = false
if controllerIP == "" then
accessManager.setController(request.remoteAddress)
hasControl = true
elseif controllerIP == request.remoteAddress then
hasControl = true
end
log:info(" hasControl: "..utils.dump(hasControl))
if not hasControl then
response:setFail("No control access")
return
end
local argId = request:get("id") local argId = request:get("id")
local argGcode = request:get("gcode") local argGcode = request:get("gcode")
local argIsFirst = utils.toboolean(request:get("first")) local argIsFirst = utils.toboolean(request:get("first"))
local argIsLast = utils.toboolean(request:get("last")) local argIsLast = utils.toboolean(request:get("last"))
local printer,msg = createPrinterOrFail(argId, response) local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer then return end if not printer then return end
response:addData('id', argId) response:addData('id', argId)

View File

@ -3,6 +3,7 @@ local urlcode = require('util.urlcode')
local confDefaults = require('conf_defaults') local confDefaults = require('conf_defaults')
local s = require('util.settings') local s = require('util.settings')
local ResponseClass = require('rest.response') local ResponseClass = require('rest.response')
local log = require('util.logger')
local M = {} local M = {}
M.__index = M M.__index = M
@ -165,6 +166,7 @@ function M.new(environment, postData, debugEnabled)
self.remoteHost = environment['REMOTE_HOST'] self.remoteHost = environment['REMOTE_HOST']
self.remotePort = environment['REMOTE_PORT'] self.remotePort = environment['REMOTE_PORT']
self.userAgent = environment['HTTP_USER_AGENT'] self.userAgent = environment['HTTP_USER_AGENT']
self.remoteAddress = environment['REMOTE_ADDR']
else else
self.requestMethod = 'CMDLINE' self.requestMethod = 'CMDLINE'
end end

30
src/util/access.lua Normal file
View File

@ -0,0 +1,30 @@
local log = require('util.logger')
local utils = require('util.utils')
local M = {}
function M.hasControl(ip)
local controllerIP = M.getController()
return (controllerIP == "" or (controllerIP ~= "" and controllerIP == ip))
end
function M.getController()
local file, error = io.open('/tmp/controller.txt','r')
if file == nil then
--log:error("Util:Access:Can't read controller file. Error: "..error)
return ""
else
controllerIP = file:read('*a')
file:close()
return controllerIP
end
end
function M.setController(ip)
local file = io.open('/tmp/controller.txt','w')
file:write(ip)
file:flush()
file:close()
end
return M

View File

@ -8,7 +8,6 @@ local utils = require('util.utils')
local M = {} local M = {}
local logLevel, logVerbose, logStream local logLevel, logVerbose, logStream
--- Available log levels. --- Available log levels.

View File

@ -1,3 +1,7 @@
local log = require('util.logger')
local utils = require('util.utils')
local printDriver = require('print3d')
local SUPPORTED_PRINTERS = { local SUPPORTED_PRINTERS = {
rigidbot = "Rigidbot", rigidbot = "Rigidbot",
ultimaker = "Ultimaker", ultimaker = "Ultimaker",
@ -46,4 +50,27 @@ function M.supportedBaudRates()
return SUPPORTED_BAUDRATES return SUPPORTED_BAUDRATES
end end
--returns a printer instance or nil (and sets error state on response in the latter case)
function M.createPrinterOrFail(deviceId, response)
log:info("API:printer:createPrinterOrFail: "..utils.dump(deviceId))
local msg,printer = nil, nil
if deviceId == nil or deviceId == "" then
printer,msg = printDriver.getPrinter()
else
printer,msg = printDriver.getPrinter(deviceId)
end
if not printer then
response:setError("could not open printer driver (" .. msg .. ")")
response:addData('id', deviceId)
return nil
end
return printer
end
return M return M