diff --git a/Makefile b/Makefile index c89fe98..98ef1fb 100644 --- a/Makefile +++ b/Makefile @@ -126,6 +126,8 @@ 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 + $(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/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index 194ad82..2048dd8 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -185,6 +185,14 @@ 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 socket = printer:getId() + io.popen("print-fetch.lua " .. socket) +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..577639e --- /dev/null +++ b/src/script/print-fetch.lua @@ -0,0 +1,56 @@ +#!/usr/bin/lua +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]) + +local remote = "10.0.0.212:8080" + +local finished = false + +local info = JSON:decode(io.popen("wget -qO - " .. remote .. "/info/"):read("*a")) + +local current_line = 0 +local total_lines = tonumber(info["lines"]) +local started = false + +while(not finished) +do + local f = io.popen("wget -qO - " .. remote .. "/fetch/" .. current_line) + local line = f:read() + while line ~= nil do + printer:appendGcode(line) + current_line = current_line + 1 + line = f:read() + end + if current_line > total_lines then + finished = true + break + end + + if not started then + started = true + print("send print start command") + printer:startPrint() + end + + local accepts_new_gcode = false + + while (not accepts_new_gcode) + do + local current,buffer,total,bufferSize,maxBufferSize = printer:getProgress() + + print("current: " .. current .. " total:" .. total .. " buffer: " .. buffer .. " bufferSize: " .. bufferSize .. " maxBufferSize: " .. maxBufferSize) + 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