forked from PoroCoco/myaenetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webAux.lua
291 lines (268 loc) · 9.61 KB
/
webAux.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
local computer = require("computer")
local component = require("component")
local internet = require("internet")
local filesystem = require("filesystem")
local shell = require("shell")
local me
if component.isAvailable("me_controller") then
me = component.me_controller
elseif component.isAvailable("me_interface") then
me = component.me_interface
else
print("You need to connect the adapter to either a me controller or a me interface")
os.exit()
end
local version = "0.13"
local working = true
local webIdPath = "home/myaenetwork/webIdentification.txt"
local workingDirectory = "home/myaenetwork/"
local urlSendItemData = "http://192.168.3.30:5000/inputItemData"
local pingUrl = "http://192.168.3.30:5000/toPing"
local urlSendCraftingStatus = "http://192.168.3.30:5000/inputCraftingStatus"
local issuedCraftingRequest = {}
local maxPing = 1000
local followedPing = 0
local serverTimeoutReconnect = 300
local bs = { [0] =
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/',
}
function architectureCheck()
if computer.getArchitecture() ~= "Lua 5.3" then
print("Your computer isn't running on the requiered architecture")
print("The computer will change from Lua 5.2 to Lua 5.3. Please restart the program after the reboot :)")
os.sleep(5)
computer.setArchitecture("Lua 5.3")
computer.shutdown(true)
end
end
architectureCheck()
function encode(s)
local byte, rep = string.byte, string.rep
local pad = 2 - ((#s-1) % 3)
s = (s..rep('\0', pad)):gsub("...", function(cs)
local a, b, c = byte(cs, 1, 3)
return bs[a>>2] .. bs[(a&3)<<4|b>>4] .. bs[(b&15)<<2|c>>6] .. bs[c&63]
end)
return s:sub(1, #s-pad) .. rep('=', pad)
end
function isConfigCorrect(rid, rusername,rpassword)
if rid == nil or rusername == nil or rpassword == nil then
return false
end
if rid == "" or rusername == "" or rpassword == "" then
return false
else
return true
end
end
function getItemDataString()
local string = ""
local isModpackGTNH, storedItems = pcall(me.allItems) --tries the allItems method only available on the GTNH modpack.
if isModpackGTNH then
for item in storedItems do
if type(item) == 'table' then
string = string .. item['label'] .. "~" .. item["size"] .. "~".. tostring(item["isCraftable"])..";"
end
end
return string
else
for k,v in pairs(me.getItemsInNetwork()) do
if type(v) == 'table' then
string = string .. v['label'] .. "~" .. v["size"] .. "~".. tostring(v["isCraftable"])..";"
end
end
return string
end
end
function requestItem(name, number)
local craftables = me.getCraftables()
for k,v in pairs(craftables) do
if type(v) == 'table' then
item = v.getItemStack()
if item['label'] == name then
local craft = v.request(number)
return craft
end
end
end
return "existe pas"
end
function craftingStatusDataToString(table)
local craftingStrig = ""
for k,v in pairs(table) do
craftingStrig = craftingStrig .. v[1] ..";"..v[2]..";"
local itemStatus = tostring(v[3].isDone())
if itemStatus == "true" then
itemStatus = "Done"
elseif itemStatus == "false" then
itemStatus = "Crafting"
end
if v[3].isCanceled() then
itemStatus = "Canceled"
end
craftingStrig = craftingStrig .. itemStatus .."}"
end
return craftingStrig
end
function printCraftingStatus(table)
for k,v in pairs(table) do
io.write(v[1])
io.write(" ")
io.write(v[2])
io.write(" ")
local itemStatus = tostring(v[3].isDone())
if itemStatus == "true" then
itemStatus = "Done"
elseif itemStatus == "false" then
itemStatus = "Crafting"
end
if v[3].isCanceled() then
itemStatus = "Canceled -> Missing ressources"
end
io.write(itemStatus)
io.write("\n")
end
end
function processPing(string)
local t ={}
for i=1,#string do
t[i] = string:sub(i, i)
end
local itemRequested = ""
local numberRequested = ""
local code = ""
local indexStopped = 1
for i=1,#string do
if string:sub(i, i) == ";" then
indexStopped = i
break
end
code = code .. string:sub(i, i)
end
for i=indexStopped+1,#string do
if string:sub(i, i) == ";" then
indexStopped = i
break
end
itemRequested = itemRequested..string:sub(i, i)
end
for i=indexStopped+1,#string do
if string:sub(i, i) == ";" then
indexStopped = i
break
end
numberRequested = numberRequested..string:sub(i, i)
end
-- print(code)
-- print(itemRequested)
-- print(numberRequested)
tab = {code,itemRequested,numberRequested}
return tab
end
function getStringCpus()
local string = ""
for k,v in pairs(me.getCpus()) do
if type(v) == 'table' then
string = string .. v['name'] .. "~" .. tostring(v["storage"]) .. "~".. tostring(v["coprocessors"]).."~".. tostring(v["busy"])..";"
end
end
return string
end
function webRequest(url,string)
local isServerOnline, result = pcall(internet.request(url,string))
if not isServerOnline then
print("Couldn't connect. The Web server is likely offline. Retrying connection in "..serverTimeoutReconnect.." seconds.")
os.sleep(serverTimeoutReconnect)
return false
else
return result
end
end
function updateProgram()
print("You are using an outdated version !")
print("Do you want to update ? Yes/No")
local acceptedUpdate = io.read()
if acceptedUpdate == "Yes" or acceptedUpdate == "yes" then
os.execute("myaenetwork/MaenUpdater.lua")
else
print("You didn't accept the update. You cannot use the program with an outdated version")
os.sleep(5)
computer.shutdown(true)
end
end
if filesystem.exists(webIdPath) then
shell.setWorkingDirectory(workingDirectory)
local f = io.open("webIdentification.txt","r")
local rid = f:read('*l')
local rusername = f:read('*l')
local rpassword = f:read('*l')
local computer_id
if rid ~= nil then
computer_id = string.sub(rid,6)
end
f:close()
shell.setWorkingDirectory("/home/")
if isConfigCorrect(rid,rusername,rpassword) then
print("Started")
while working do
::restart::
followedPing = followedPing + 1
local pingResult = webRequest(pingUrl,version..";"..tostring(computer_id))
if not pingResult then goto restart end
pingResult = processPing(pingResult)
local needUpdate = pingResult[1]
local itemRequested = pingResult[2]
local numberRequested = pingResult[3]
if needUpdate == "Outdated" then
updateProgram()
end
if needUpdate == "True" then
followedPing = 0
print("Server is requesting data")
dataResult = webRequest(urlSendItemData,encode(getItemDataString().."|"..tostring(me.getAvgPowerUsage())..";"..tostring(me.getMaxStoredPower())..";"..tostring(me.getStoredPower()).."|"..getStringCpus()..";"..tostring(computer_id))) if not pingResult then goto restart end
if dataResult == "OK" then
print("Data sent")
end
if itemRequested ~= "EMPTY" then
local subTable ={}
subTable[1] = itemRequested
subTable[2] = numberRequested
subTable[3] = requestItem(itemRequested,tonumber(numberRequested))
if subTable[3] == 'existe pas' then
print("requested item doesn't exist in the craftables")
goto restart
end
issuedCraftingRequest[#issuedCraftingRequest+1] = subTable
webCraftingResult = webRequest(urlSendCraftingStatus, encode(craftingStatusDataToString(issuedCraftingRequest)..";"..tostring(computer_id)))
if not webCraftingResult then goto restart end
if webCraftingResult == "OK" then
print(subTable[1],subTable[2])
print("Crafting status sent")
else
print("Couldn't send crafting status")
end
else
webCraftingResult = webRequest(urlSendCraftingStatus, encode(craftingStatusDataToString(issuedCraftingRequest)..";"..tostring(computer_id)))
if not webCraftingResult then goto restart end
-- print("Crafing status updated")
end
end
if #issuedCraftingRequest > 30 then
table.remove(issuedCraftingRequest,1)
end
os.sleep(1)
if followedPing >= maxPing then
os.sleep(10)
end
end
else
print("No account created or account invalid.")
print("Launch the 'account' file to create your account.")
end
else
print("No account created or account invalid.")
print("Launch the 'account' file to create your account.")
end