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-08-16 20:03:53 +02:00
|
|
|
local lfs = require('lfs')
|
2013-10-10 12:11:16 +02:00
|
|
|
local log = require('util.logger')
|
|
|
|
local utils = require('util.utils')
|
|
|
|
local accessManager = require('util.access')
|
|
|
|
local printDriver = require('print3d')
|
|
|
|
local printerUtils = require('util.printer')
|
2013-10-10 15:03:42 +02:00
|
|
|
local printerAPI = require('rest.api.api_printer')
|
2013-10-26 01:31:06 +02:00
|
|
|
local wifi = require('network.wlanconfig')
|
2014-02-28 11:05:15 +01:00
|
|
|
local settings = require('util.settings')
|
2013-08-16 20:03:53 +02:00
|
|
|
|
|
|
|
local TMP_DIR = '/tmp'
|
|
|
|
local LOG_COLLECT_DIRNAME = 'wifibox-logs'
|
|
|
|
local LOG_COLLECT_DIR = TMP_DIR .. '/' .. LOG_COLLECT_DIRNAME
|
2014-06-04 23:19:59 +02:00
|
|
|
local DEFAULT_WIFIBOX_LOG_FILENAME = 'wifibox.log'
|
|
|
|
local DEFAULT_WIFIBOX_LOG_FILE = TMP_DIR .. '/' .. DEFAULT_WIFIBOX_LOG_FILENAME
|
2014-04-25 14:51:29 +02:00
|
|
|
local MOD_ABBR = "AINF"
|
2013-11-29 19:38:26 +01:00
|
|
|
|
2013-08-16 20:03:53 +02:00
|
|
|
local SYSLOG_FILENAME = 'syslog'
|
2013-11-29 19:38:26 +01:00
|
|
|
local PROCESS_LIST_FILENAME = 'processes'
|
|
|
|
local MEMINFO_FILENAME = 'meminfo'
|
|
|
|
local MOUNTS_FILENAME = 'mounts'
|
|
|
|
local DISKFREE_FILENAME = 'diskfree'
|
|
|
|
|
|
|
|
local UCI_CONFIG_FILES_TO_SAVE = { 'dhcp', 'firewall', 'network', 'system', 'wifibox', 'wireless' }
|
|
|
|
|
|
|
|
local USB_DIRTREE_COMMAND = "ls -R /sys/devices/platform/ehci-platform/usb1 | grep \":$\" | sed -e 's/:$//' -e 's/[^-][^\\/]*\\//--/g' -e 's/^/ /' -e 's/-/|/'"
|
|
|
|
local USB_DIRTREE_FILENAME = 'sys_devices_platform_ehci-platform_usb1.tree'
|
|
|
|
|
2013-10-10 17:00:16 +02:00
|
|
|
local PRINT3D_BASEPATH = '/tmp'
|
2014-06-04 23:19:59 +02:00
|
|
|
local PRINT3D_LOG_FILENAME_PREFIX = 'print3d.'
|
2013-10-10 17:00:16 +02:00
|
|
|
local PRINT3D_LOG_FILENAME_SUFFIX = '.log'
|
2013-08-16 20:03:53 +02:00
|
|
|
local LOG_COLLECT_ARCHIVE_FILENAME = LOG_COLLECT_DIRNAME .. '.tgz'
|
|
|
|
local LOG_COLLECT_ARCHIVE_FILE = TMP_DIR .. '/' .. LOG_COLLECT_ARCHIVE_FILENAME
|
|
|
|
|
|
|
|
local function redirectedExecute(cmd)
|
|
|
|
return os.execute(cmd .. " > /dev/null 2>&1")
|
|
|
|
end
|
|
|
|
|
2013-07-24 16:36:27 +02:00
|
|
|
local M = {
|
|
|
|
isApi = true
|
|
|
|
}
|
|
|
|
|
2014-04-28 16:39:27 +02:00
|
|
|
-- returns wifiboxid (since version 0.10.2)
|
2013-07-24 16:36:27 +02:00
|
|
|
function M._global(request, response)
|
|
|
|
response:setSuccess()
|
2014-04-25 14:51:29 +02:00
|
|
|
|
2014-02-28 11:05:15 +01:00
|
|
|
local wifiboxid = wifi.getSubstitutedSsid(settings.get('network.cl.wifiboxid'))
|
|
|
|
response:addData('wifiboxid', wifiboxid)
|
2014-04-25 14:51:29 +02:00
|
|
|
|
2013-07-24 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
2013-08-16 20:03:53 +02:00
|
|
|
-- TODO: redirect stdout+stderr; handle errors
|
|
|
|
function M.logfiles(request, response)
|
2013-11-29 19:38:26 +01:00
|
|
|
local rv,sig,code,msg = nil,nil,nil,nil
|
|
|
|
|
|
|
|
rv,msg = lfs.mkdir(LOG_COLLECT_DIR)
|
|
|
|
rv,msg = lfs.chdir(TMP_DIR)
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2014-06-04 23:19:59 +02:00
|
|
|
local wifiboxLogFilePath = log:getLogFilePath()
|
|
|
|
local wifiboxLogFileName = wifiboxLogFilePath and wifiboxLogFilePath:match('.*/(.*)')
|
|
|
|
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2013-08-16 20:03:53 +02:00
|
|
|
--[[ create temporary files ]]--
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2013-11-29 19:38:26 +01:00
|
|
|
-- copy wifibox API-script log
|
2014-06-04 23:19:59 +02:00
|
|
|
rv,sig,code = redirectedExecute('cp ' .. wifiboxLogFilePath .. ' ' .. LOG_COLLECT_DIR)
|
2013-11-29 19:38:26 +01:00
|
|
|
|
|
|
|
-- capture syslog
|
|
|
|
rv,sig,code = os.execute('logread > ' .. LOG_COLLECT_DIR .. '/' .. SYSLOG_FILENAME)
|
|
|
|
|
|
|
|
-- capture running processes
|
|
|
|
rv,sig,code = os.execute('ps -w > ' .. LOG_COLLECT_DIR .. '/' .. PROCESS_LIST_FILENAME)
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2013-11-29 19:38:26 +01:00
|
|
|
-- capture info on RAM memory
|
|
|
|
rv,sig,code = os.execute('cat /proc/meminfo > ' .. LOG_COLLECT_DIR .. '/' .. MEMINFO_FILENAME)
|
|
|
|
|
|
|
|
-- capture info on mounted file systems
|
|
|
|
rv,sig,code = os.execute('mount > ' .. LOG_COLLECT_DIR .. '/' .. MOUNTS_FILENAME)
|
|
|
|
|
|
|
|
-- capture info on free disk space
|
|
|
|
rv,sig,code = os.execute('df -h > ' .. LOG_COLLECT_DIR .. '/' .. DISKFREE_FILENAME)
|
|
|
|
|
|
|
|
-- list directory structure for primary USB controller
|
|
|
|
rv,sig,code = os.execute(USB_DIRTREE_COMMAND .. ' > ' .. LOG_COLLECT_DIR .. '/' .. USB_DIRTREE_FILENAME)
|
|
|
|
|
|
|
|
-- copy relevant openwrt configuration files
|
|
|
|
rv,msg = lfs.mkdir(LOG_COLLECT_DIR .. '/config')
|
|
|
|
for _,v in pairs(UCI_CONFIG_FILES_TO_SAVE) do
|
2013-11-29 20:47:17 +01:00
|
|
|
local srcFile = '/etc/config/' .. v
|
2013-11-29 19:38:26 +01:00
|
|
|
local tgtFile = LOG_COLLECT_DIR .. '/config/' .. v
|
2013-11-29 20:47:17 +01:00
|
|
|
if v ~= 'wireless' then
|
|
|
|
rv,sig,code = redirectedExecute('cp ' .. srcFile .. ' ' .. tgtFile)
|
|
|
|
else
|
|
|
|
rv,sig,code = os.execute("sed \"s/option key '.*'/option key '...'/g\" " .. srcFile .. " > " .. tgtFile)
|
2013-11-29 19:38:26 +01:00
|
|
|
end
|
|
|
|
end
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2013-11-29 19:38:26 +01:00
|
|
|
-- collect and copy print3d server logs
|
2013-10-10 17:00:16 +02:00
|
|
|
for file in lfs.dir(PRINT3D_BASEPATH) do
|
|
|
|
if file:find(PRINT3D_LOG_FILENAME_PREFIX) == 1 and file:find(PRINT3D_LOG_FILENAME_SUFFIX) ~= nil then
|
|
|
|
local srcLogFile = PRINT3D_BASEPATH .. '/' .. file
|
|
|
|
local tgtLogFile = LOG_COLLECT_DIR .. '/' .. file
|
2013-11-29 19:38:26 +01:00
|
|
|
rv,sig,code = redirectedExecute('cp ' .. srcLogFile .. ' ' .. tgtLogFile)
|
2013-08-21 13:39:45 +02:00
|
|
|
end
|
2013-08-16 20:03:53 +02:00
|
|
|
end
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2013-11-29 19:38:26 +01:00
|
|
|
rv,sig,code = redirectedExecute('tar czf ' .. LOG_COLLECT_ARCHIVE_FILE .. ' ' .. LOG_COLLECT_DIRNAME) --returns 0 success, 1 error
|
2013-10-10 17:17:23 +02:00
|
|
|
|
|
|
|
|
2013-08-16 20:03:53 +02:00
|
|
|
--[[ add response content ]]--
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2013-11-29 19:38:26 +01:00
|
|
|
rv,msg = response:setBinaryFileData(LOG_COLLECT_ARCHIVE_FILE, LOG_COLLECT_ARCHIVE_FILENAME, 'application/x-compressed')
|
2013-08-16 20:03:53 +02:00
|
|
|
if not rv then
|
|
|
|
response:setError("could not set binary data from file '" .. LOG_COLLECT_ARCHIVE_FILE .. "' (" .. msg .. ")")
|
|
|
|
else
|
|
|
|
response:setSuccess()
|
|
|
|
end
|
2013-10-10 17:17:23 +02:00
|
|
|
|
|
|
|
|
2013-08-16 20:03:53 +02:00
|
|
|
--[[ remove temporary files ]]--
|
2013-10-10 17:17:23 +02:00
|
|
|
|
|
|
|
for file in lfs.dir(LOG_COLLECT_DIR) do
|
|
|
|
if file:find(PRINT3D_LOG_FILENAME_PREFIX) == 1 and file:find(PRINT3D_LOG_FILENAME_SUFFIX) ~= nil then
|
|
|
|
local tgtLogFile = LOG_COLLECT_DIR .. '/' .. file
|
2013-11-29 19:38:26 +01:00
|
|
|
rv,sig,code = redirectedExecute('rm ' .. tgtLogFile)
|
2013-08-16 20:03:53 +02:00
|
|
|
end
|
|
|
|
end
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2013-11-29 19:38:26 +01:00
|
|
|
rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/config/*')
|
|
|
|
rv,msg = lfs.rmdir(LOG_COLLECT_DIR .. '/config')
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2013-11-29 19:38:26 +01:00
|
|
|
rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. USB_DIRTREE_FILENAME)
|
|
|
|
rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. DISKFREE_FILENAME)
|
|
|
|
rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. MOUNTS_FILENAME)
|
|
|
|
rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. MEMINFO_FILENAME)
|
|
|
|
rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. PROCESS_LIST_FILENAME)
|
|
|
|
rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. SYSLOG_FILENAME)
|
2014-06-04 23:19:59 +02:00
|
|
|
rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. wifiboxLogFileName)
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2013-11-29 19:38:26 +01:00
|
|
|
rv,msg = lfs.rmdir(LOG_COLLECT_DIR)
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2013-11-29 19:38:26 +01:00
|
|
|
rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_ARCHIVE_FILE)
|
2013-08-16 20:03:53 +02:00
|
|
|
end
|
|
|
|
|
2013-10-10 12:11:16 +02:00
|
|
|
function M.access(request, response)
|
2014-04-25 14:51:29 +02:00
|
|
|
--log:info(MOD_ABBR, " remoteAddress: |"..utils.dump(request.remoteAddress).."|");
|
|
|
|
--log:info(MOD_ABBR, " controller: |"..utils.dump(accessManager.getController()).."|");
|
2013-10-10 17:17:23 +02:00
|
|
|
|
2013-10-10 12:11:16 +02:00
|
|
|
local hasControl = accessManager.hasControl(request.remoteAddress)
|
2014-04-25 14:51:29 +02:00
|
|
|
-- if hasControl then log:info(MOD_ABBR, " hasControl: true")
|
|
|
|
-- else log:info(MOD_ABBR, " hasControl: false") end
|
2013-10-10 12:11:16 +02:00
|
|
|
response:setSuccess()
|
2013-10-10 17:51:14 +02:00
|
|
|
response:addData('has_control', hasControl)
|
2013-10-18 21:46:41 +02:00
|
|
|
|
2014-05-19 21:37:17 +02:00
|
|
|
return
|
2013-10-10 12:11:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.status(request, response)
|
2013-10-18 21:46:41 +02:00
|
|
|
|
2014-02-28 10:44:02 +01:00
|
|
|
local rv, state = printerAPI.state(request, response)
|
2013-10-10 17:51:14 +02:00
|
|
|
if(rv == false) then return end
|
2014-03-05 17:52:46 +01:00
|
|
|
|
|
|
|
if state ~= "disconnected" and state ~= "connecting" then
|
2013-10-10 17:51:14 +02:00
|
|
|
rv = printerAPI.temperature(request, response)
|
|
|
|
if(rv == false) then return end
|
|
|
|
rv = printerAPI.progress(request, response)
|
|
|
|
if(rv == false) then return end
|
|
|
|
rv = M.access(request, response)
|
|
|
|
if(rv == false) then return end
|
|
|
|
end
|
2013-10-10 12:11:16 +02:00
|
|
|
end
|
|
|
|
|
2013-07-24 16:36:27 +02:00
|
|
|
return M
|