diff --git a/Makefile b/Makefile index c89fe98..d13b89a 100644 --- a/Makefile +++ b/Makefile @@ -126,6 +126,10 @@ define Package/wifibox/install $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/signin.sh $(1)/$(TGT_LUA_DIR_SUFFIX)/script $(CP) $(WIFIBOX_BASE_DIR)/script/logrotate-wifibox.conf $(1)/etc/logrotate.d/wifibox.conf + + $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/print-fetch.lua $(1)/$(TGT_LUA_DIR_SUFFIX)/script + $(LN) -s /$(TGT_LUA_DIR_SUFFIX)/script/print-fetch.lua $(1)/bin/print-fetch + $(CP) $(WIFIBOX_BASE_DIR)/script/wifibox.uci.config $(1)/etc/config/wifibox # copy base configuration to uci config dir $(CP) $(WIFIBOX_BASE_DIR)/FIRMWARE-VERSION $(1)/etc/wifibox-version diff --git a/src/conf_defaults.lua b/src/conf_defaults.lua index b439ad9..0214fae 100644 --- a/src/conf_defaults.lua +++ b/src/conf_defaults.lua @@ -418,4 +418,10 @@ M.doodle3d_update_baseUrl = { description = '' } +M.gcode_server = { + default = 'http://gcodeserver.doodle3d.com', + type = 'string', + description ='' +} + return M diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index 194ad82..5bf04bc 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -185,6 +185,69 @@ local function addSequenceNumbering(printer, response) end end +function M.fetch_POST(request, response) + local printer,msg = printerUtils.createPrinterOrFail(argId, response) + if not printer or not printer:hasSocket() then return end + + local controllerIP = accessManager.getController() + local hasControl = false + if controllerIP == "" then + accessManager.setController(request.remoteAddress) + hasControl = true + elseif controllerIP == request.remoteAddress then + hasControl = true + end + + if not hasControl then + response:setFail("No control access") + return + end + + + log:verbose(MOD_ABBR, " clearing all gcode for " .. printer:getId()) + response:addData('gcode_clear',true) + local rv,msg = printer:clearGcode() + + if rv == false then + response:addData('status', msg) + response:setFail("could not clear gcode (" .. msg .. ")") + elseif rv == nil then + response:setError(msg) + return + end + + local gcodeFiles = " " + local startCode = request:get("start_code") + if startCode ~= nil then + gcodeFiles = gcodeFiles .. '/tmp/startcode ' + io.open('/tmp/startcode', 'w+').write(startCode) + end + + local endCode = request:get("end_code") + if endCode ~= nil then + gcodeFiles = gcodeFiles .. '/tmp/endcode ' + io.open('/tmp/endcode', 'w+').write(endCode) + end + + local socket = printer:getId() + if socket == nil then + response:setError("no socket found") + return + end + local remote = settings.get('gcode.server') + if remote == nil then + response:setError("no gcode server configured") + return + end + local id = request:get("id") + if id == nil then + response:setError("no id supplied") + return + end + io.popen("print-fetch " .. socket .. " " .. remote .. " " .. id .. gcodeFiles) + response:setSuccess() +end + --requires: gcode(string) (the gcode to be appended) --accepts: id(string) (the printer ID to append to) --accepts: clear(bool) (chunks will be concatenated but output file will be cleared first if this argument is true) diff --git a/src/script/print-fetch.lua b/src/script/print-fetch.lua new file mode 100755 index 0000000..5ca7329 --- /dev/null +++ b/src/script/print-fetch.lua @@ -0,0 +1,108 @@ +#!/usr/bin/lua + +local function log(message) + os.execute("logger " .. message) + print(message) +end + +if (table.getn(arg) == 0) then + print("Usage: ./print-fetch {printerSocket} {remoteURL} {id} [startGcode] [endGCode]") + return +end + +log("starting gcode fetch program") + +package.cpath = package.cpath .. '/usr/lib/lua/?.so' +JSON = (loadfile "/usr/share/lua/wifibox/util/JSON.lua")() + +local p3d = require("print3d") + +local printer = p3d.getPrinter(arg[1]) +if printer == nil then + log("error connecting to printer") + return +end + +local remote = arg[2] + +local finished = false + +local id = arg[3] + +log("gcode file id: " .. id) +log("gcode server: " .. remote) + +local info = JSON:decode(io.popen("wget -qO - " .. remote .. "/info/" .. id):read("*a")) + +local current_line = 0 +local total_lines = tonumber(info["lines"]) +local started = false + +log("total lines: " .. total_lines) + +local startCode = nil +local endCode = nil + +function countlines(file) + return tonumber(io.popen("wc -l < " .. file):read('*a')) +end + +function readGCodeArg(argi) + local gcodeFile = arg[argi] + total_lines = total_lines + countlines(gcodeFile) + return io.open(gcodeFile):read('*a') +end + +if table.getn(arg) >= 5 then + startCode = readGCodeArg(4) + endCode = readGCodeArg(5) +end + +if startCode ~= nil then + log("appending start gcode") + printer:appendGcode(startCode) +end + +while(not finished) +do + local f = io.popen("wget -qO - " .. remote .. "/fetch/" .. id .. "/" .. current_line) + local line = f:read() + while line ~= nil do + printer:appendGcode(line, total_lines, { seq_number = -1, seq_total = -1, source = id }) + current_line = current_line + 1 + line = f:read() + end + + if not started then + started = true + print("send print start command") + printer:startPrint() + end + + if current_line >= total_lines then + log("finished fetching gcode") + if endCode ~= nil then + log("appending end gcode") + printer:appendGcode(endCode) + end + finished = true + break + end + + + local accepts_new_gcode = false + + while (not accepts_new_gcode) + do + local current,buffer,total,bufferSize,maxBufferSize = printer:getProgress() + local percentageBufferSize = bufferSize / maxBufferSize + + if percentageBufferSize < 0.8 then + print("buffer below 80% capacity, sending new gcode") + accepts_new_gcode = true + else + print("buffer above 80% capacity") + os.execute("sleep 10") + end + end +end diff --git a/src/script/wifibox.uci.config b/src/script/wifibox.uci.config index dd8146e..98e444d 100644 --- a/src/script/wifibox.uci.config +++ b/src/script/wifibox.uci.config @@ -10,3 +10,4 @@ config settings 'system' config settings 'general' option printer_type 'ultimaker' + option gcode_server 'http://gcodeserver.doodle3d.com'