2013-12-20 16:29:46 +01:00
|
|
|
--
|
|
|
|
-- This file is part of the Doodle3D project (http://doodle3d.com).
|
|
|
|
--
|
|
|
|
-- @copyright 2013, Doodle3D
|
|
|
|
-- @license This software is licensed under the terms of the GNU GPL v2 or later.
|
|
|
|
-- See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
|
|
|
|
|
|
|
|
|
2013-10-10 12:11:16 +02:00
|
|
|
local log = require('util.logger')
|
|
|
|
local utils = require('util.utils')
|
2014-03-12 16:36:16 +01:00
|
|
|
local printerUtils = require('util.printer')
|
2013-10-10 12:11:16 +02:00
|
|
|
|
2014-04-25 14:51:29 +02:00
|
|
|
local MOD_ABBR = "UACS"
|
|
|
|
|
2013-10-10 12:11:16 +02:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
function M.hasControl(ip)
|
|
|
|
local controllerIP = M.getController()
|
2014-04-25 14:51:29 +02:00
|
|
|
|
2014-03-12 16:36:16 +01:00
|
|
|
-- no controller stored? we have control
|
|
|
|
if controllerIP == "" then return true end;
|
2014-04-25 14:51:29 +02:00
|
|
|
|
2014-03-12 16:36:16 +01:00
|
|
|
-- controller stored is same as our (requesting) ip? we have control
|
|
|
|
if(controllerIP == ip) then return true end;
|
2014-04-25 14:51:29 +02:00
|
|
|
|
2014-03-12 16:36:16 +01:00
|
|
|
-- no printer connected? we have control
|
|
|
|
local printer,msg = printerUtils.createPrinterOrFail()
|
2014-04-25 14:51:29 +02:00
|
|
|
if not printer or not printer:hasSocket() then
|
2014-03-12 16:36:16 +01:00
|
|
|
M.setController("") -- clear the controller
|
|
|
|
return true
|
|
|
|
end
|
2014-04-25 14:51:29 +02:00
|
|
|
|
2014-03-12 16:36:16 +01:00
|
|
|
-- printer is idle (done printing)? we have control
|
|
|
|
local state = printer:getState()
|
|
|
|
if state == "idle" then -- TODO: define in constants somewhere
|
|
|
|
M.setController("") -- clear controller
|
|
|
|
return true
|
|
|
|
end
|
2014-04-25 14:51:29 +02:00
|
|
|
|
2014-03-12 16:36:16 +01:00
|
|
|
return false
|
2013-10-10 12:11:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.getController()
|
|
|
|
local file, error = io.open('/tmp/controller.txt','r')
|
|
|
|
if file == nil then
|
2014-05-12 13:46:38 +02:00
|
|
|
--log:error(MOD_ABBR, "Util:Access: Can't read controller file. Error: "..error)
|
2013-10-10 12:11:16 +02:00
|
|
|
return ""
|
|
|
|
else
|
|
|
|
controllerIP = file:read('*a')
|
|
|
|
file:close()
|
2013-10-23 16:12:19 +02:00
|
|
|
--strip trailing newline (useful when manually editing controller.txt)
|
|
|
|
if controllerIP:find('\n') == controllerIP:len() then controllerIP = controllerIP:sub(0, -2) end
|
2013-10-10 12:11:16 +02:00
|
|
|
return controllerIP
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.setController(ip)
|
|
|
|
local file = io.open('/tmp/controller.txt','w')
|
|
|
|
file:write(ip)
|
|
|
|
file:flush()
|
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
|
2013-10-23 16:12:19 +02:00
|
|
|
return M
|