-
Notifications
You must be signed in to change notification settings - Fork 3
/
octoprint.lua
106 lines (100 loc) · 4.03 KB
/
octoprint.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
-- Use Lua -> Time
-- For displaying debug messages
debug=false
-- IP address of octoprint
OctoIP='OctoprintIPAddress'
-- Port of Octoprint - Default 80
OctoPort='80'
-- Octoprint API Key (found in Settings -> API)
OctoAPI='OctoprintAPIKey'
-- JSON.lua path
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
-- Curl path
curl = '/usr/bin/curl'
-- IDX of your Dummy devices
OctoStatusIDX='387' -- type: Text
OctoBebIDX='388' -- type: Temperature
OctoHeadIDX='389' -- type: Temperature
OctoTotalTimeIDX='390' -- type: Text
OctoCompleteIDX='391' -- type: Percentage
OctoPrintTimeIDX='392' -- type: Text
OctoTimeLeftIDX='393' -- type: Text
-- If you don't have a heated bed, put this to false
HeatedBed=true
OctoPrinter='http://'..OctoIP..':'..OctoPort..'/api/printer'
OctoJob='http://'..OctoIP..':'..OctoPort..'/api/job'
local function update(idx, value1)
local cmd = idx..'|0|'..value1
table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
local function ping(OctoIP)
ping_success=os.execute('ping -W2 -c1 '..OctoIP)
return ping_success
end
local function online()
DataOctoTemp = assert(io.popen(curl..' -s --max-time 8 -H "X-Api-Key: '..OctoAPI..'" "'..OctoPrinter..'"'))
BlocOctoTemp = DataOctoTemp:read('*all')
DataOctoTemp:close()
JsonOctoTemp = json:decode(BlocOctoTemp)
end
local function round(num, n)
local mult = 10^(n or 0)
return math.floor(num * mult + 0.5) / mult
end
function SecondsToClock(seconds)
local seconds = tonumber(seconds)
if seconds <= 0 then
return "00:00:00";
else
hours = string.format("%02.f", math.floor(seconds/3600));
mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60));
return hours..":"..mins..":"..secs
end
end
local function isempty(s)
return s == nil or s == ''
end
commandArray = {}
local status, retval = pcall(online,10);
if(ping(OctoIP)) then
if (status) then
DataOctoTemp = assert(io.popen(curl..' -s --max-time 8 -H "X-Api-Key: '..OctoAPI..'" "'..OctoPrinter..'"'))
BlocOctoTemp = DataOctoTemp:read('*all')
DataOctoTemp:close()
JsonOctoTemp = json:decode(BlocOctoTemp)
OctoBeb = JsonOctoTemp.temperature.bed.actual
OctoHead = JsonOctoTemp.temperature.tool0.actual
OctoStatus = JsonOctoTemp.state.text
OctoState = JsonOctoTemp.state.flags.printing
if (HeatedBed) then update(OctoBebIDX, OctoBeb) end
update(OctoHeadIDX, OctoHead)
update(OctoStatusIDX, OctoStatus)
if (OctoState) then
DataOctoTime = assert(io.popen(curl..' -s --max-time 8 -H "X-Api-Key: '..OctoAPI..'" "'..OctoJob..'"'))
BlocOctoTime = DataOctoTime:read('*all')
DataOctoTime:close()
JsonOctoTime = json:decode(BlocOctoTime)
OctoTotalTime = JsonOctoTime.job.estimatedPrintTime
OctoComplete = JsonOctoTime.progress.completion
OctoPrintTime = JsonOctoTime.progress.printTime
OctoTimeLeft = JsonOctoTime.progress.printTimeLeft
if isempty(OctoTotalTime) then update(OctoTotalTimeIDX, SecondsToClock(0)) else update(OctoTotalTimeIDX, SecondsToClock(OctoTotalTime)) end
if isempty(OctoComplete) then update(OctoCompleteIDX, round(0)) else update(OctoCompleteIDX, round(OctoComplete)) end
if isempty(OctoPrintTime) then update(OctoPrintTimeIDX, SecondsToClock(0)) else update(OctoPrintTimeIDX, SecondsToClock(OctoPrintTime)) end
if isempty(OctoTimeLeft) then update(OctoTimeLeftIDX, SecondsToClock(0)) else update(OctoTimeLeftIDX, SecondsToClock(OctoTimeLeft)) end
end
else
if (debug) then print("Printer not connected") end
update(OctoStatusIDX, "Printer not connected")
if (HeatedBed) then update(OctoBebIDX, 0) end
update(OctoHeadIDX, 0)
update(OctoTotalTimeIDX, SecondsToClock(0))
update(OctoCompleteIDX, 0)
update(OctoPrintTimeIDX, SecondsToClock(0))
update(OctoTimeLeftIDX, SecondsToClock(0))
end
else
update(OctoStatusIDX, "Octoprint offline.")
end
return commandArray