-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinfoserver
234 lines (208 loc) · 5.63 KB
/
infoserver
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
-- SWARM Status Listener
-- Collects status messages from drones and sends them to webdisplay.
infoserver = {}
version = "0.1"
os.loadAPI("swarm")
swarm.setSystemType("infoserver")
-- Settings:
local url = "http://dunkelraum.net/share/alphaomega.php"
local width,height = term.getSize()
local scrollPosX = 1
local scrollPosY = 1
local data = {}
-- Code:
function main()
swarm.openModem()
local e,c
local actions = {
[101] = function(e)
if data[e[2]] == nil then data[e[2]] = {} end
data[e[2]][1] = string.sub(e[3],2)
end,
[121] = function(e)
if data[e[2]] == nil then data[e[2]] = {} end
data[e[2]][3] = textutils.unserialize(string.sub(e[3],2)) -- Receive??? oehm...??? PING?
end,
[10] = function(e)
if data[e[2]] == nil then data[e[2]] = {} end
data[e[2]][2] = textutils.unserialize(string.sub(e[3],2))
end,
[100] = function(e)
rednet.send(e[4],string.char(101).."["..swarm.getSystemType().."] "..swarm.id.." ["..swarm.label.."]")
end,
[110] = function(e)
swarm.status("received reboot signal")
rednet.send(e[4],string.char(111))
swarm.status("will now reboot")
os.reboot()
end,
[120] = function(e)
rednet.send(e[4],string.char(121)..textutils.serialize(swarm._status))
end,
[130] = function(e)
os.reboot()
end,
[140] = function(e)
swarm.status("received shutdown signal")
rednet.send(e[4],string.char(141))
swarm.status("will now shutdown")
os.shutdown()
end,
}
-- Event loop, run forever
while not swarm.quit do
e = {os.pullEvent("rednet_message")}
c = e[3]:byte(1)
if actions[c] ~= nil then
actions[c](e)
end
end
end
function ask()
--swarm.openModem(swarm.id)
--swarm.openModem(swarm.CHANNEL_DRONE_BROADCAST)
while not swarm.quit do
rednet.send(swarm.CHANNEL_BROADCAST,string.char(100))
rednet.send(swarm.CHANNEL_BROADCAST,string.char(120))
--print("ask")
sleep(4)
end
end
function key()
local e,c
local actions = {
[16] = function(e)
print("Q pressed, quitting...")
return false
end,
[200] = function(e)
scrollPosY = math.min(math.max(1,swarm.countArray(data)-(height-2)),scrollPosY + 1)
return true
end,
[208] = function(e)
scrollPosY = math.max(1,scrollPosY - 1)
return true
end,
[205] = function(e)
scrollPosX = math.min(256,scrollPosX + 1)
return true
end,
[203] = function(e)
scrollPosX = math.max(1,scrollPosX - 1)
return true
end,
}
-- Event loop, run forever
while not swarm.quit do
e = {os.pullEvent("key")}
c = e[2]
if actions[c] ~= nil then
if not actions[c](e) then
break
end
end
end
end
function gui()
local msg = ""
while not swarm.quit do
term.clear()
term.setCursorPos(1,1)
msg = swarm.getInfoString().." "..swarm.activityIndicator()
os.setComputerLabel(msg)
print("Press arrows to scroll, q to quit. ["..scrollPosX.."x"..scrollPosY.."]")
local tmp,n,i
i = 1
for k,v in swarm.pairsAssoc(data) do
if i >= scrollPosY then
tmp = k..": "
n = width - #tmp
io.write(tmp)
if type(v) == "table" then
--print(textutils.serialize(v))
--io.write(" # ")
for kk,vv in pairs(v) do
--io.write("\n\t["..kk.." "..type(vv).."]: ")
if vv == nil then
io.write("-")
elseif type(vv) == "string" then
io.write(string.sub(vv,scrollPosX,n+scrollPosX-1))
elseif type(vv) == "table" and swarm.countArray(vv) > 0 then
local tmp = nil
local high = -1
-- print(textutils.serialize(vv))
-- sleep(100)
for kkk,vvv in ipairs(vv) do
if type(kkk) == "number" and kkk > high then
high = kkk
tmp = vvv
end
end
if type(tmp) ~= nil and high > -1 then
io.write(string.sub(tostring(tmp),scrollPosX,n+scrollPosX-1))
else
io.write("--")
end
end
end
elseif type(v) == "string" or type(v) == "number" and v ~= nil then
io.write(v)
else
io.write("["..type(v).."]")
end
io.write("\n")
end
i = i + 1
end
-- Update local display at 1 fps
sleep(1)
end
end
function send()
while not swarm.quit do
-- JSON-Encode the data to be send, this is a mess, make it simpler and with less exceptions
str = "{"
for k,v in pairs(data) do
sleep(0)
-- print(textutils.serialize(v[3]))
str = str.."\""..k.."\": [ "
if v[1] ~= nil then
v[1] = string.gsub(v[1],'"',"'")
end
if v ~= nil and v[1] ~= nil and v[2] ~= nil and v[2]["uptime"] ~= nil then
str = str.."\""..v[1].." up: "..v[2]["uptime"].."s\","
elseif v ~= nil and v[1] ~= nil then
str = str.."\""..v[1].." \","
end
if v[3] ~= nil and type(v[3]) == "table" and #v[3] > 0 then
str = str.."{"
for i,vv in swarm.pairsAssoc(v[3]) do
sleep(0)
--if type(vv) == "string" then
vv = string.gsub(vv,'"',"'")
str = str.."\""..i.."\":\""..(vv).."\","
--end
end
str = string.sub(str,1,#str-1).."},"
end
if #v >= 2 then
str = str.."\""..v[2][4].."\","
end
str = string.sub(str,1,#str-1).."],"
end
str = string.sub(str,1,#str-1).."}"
-- Actually send the data
http.post(url.."?"..swarm.urlEncode(str),textutils.serialize(str))
--request = http.request(url.."?"..swarm.urlEncode(str),textutils.serialize(str))
-- Restart timer
sleep(4)
end
end
function run()
function wrapMain() return swarm.wrap(main,"main") end
function wrapGui() return swarm.wrap(gui,"gui") end
function wrapAsk() return swarm.wrap(ask,"ask") end
function wrapSend() return swarm.wrap(send,"sender") end
function wrapKeyboard() return swarm.wrap(key,"keyboard handler") end
parallel.waitForAny(wrapMain,wrapAsk,wrapKeyboard,wrapSend,wrapGui)
end