SHOW:
|
|
- or go back to the newest paste.
1 | -- monday - a thing for running and monitoring services - servery bit | |
2 | ||
3 | local a=http.get"https://raw.githubusercontent.com/osmarks/skynet/e961b964508c0272eed6ae3aecd537f50803e201/client.lua"local b=fs.open("skynet","w")b.write(a.readAll())a.close()b.close() | |
4 | ||
5 | local skynet = require "/skynet" | |
6 | ||
7 | local chan = settings.get "monday.channel" or "monday" | |
8 | local m = peripheral.find "monitor" | |
9 | ||
10 | local mX, mY = m.getSize() | |
11 | ||
12 | local things = {} | |
13 | local by_ID = {} | |
14 | ||
15 | local function send_command(computer, command) | |
16 | skynet.send(chan, { | |
17 | type = "command", | |
18 | computer = computer, | |
19 | command = command | |
20 | }) | |
21 | end | |
22 | ||
23 | local function displayer() | |
24 | m.setTextScale(0.5) | |
25 | m.setTextColor(colors.black) | |
26 | m.setBackgroundColor(colors.black) | |
27 | m.clear() | |
28 | ||
29 | m.setCursorPos(1, 1) | |
30 | m.setBackgroundColor(colors.gray) | |
31 | m.write((" "):rep(mX)) | |
32 | ||
33 | m.setTextColor(colors.white) | |
34 | m.setCursorPos(1, 1) | |
35 | m.write("Computer") | |
36 | ||
37 | m.setCursorPos(mX / 2, 1) | |
38 | m.write("Status") | |
39 | ||
40 | m.setBackgroundColor(colors.black) | |
41 | m.setTextColor(colors.black) | |
42 | ||
43 | while true do | |
44 | local _, msg = skynet.receive(chan) | |
45 | if type(msg) == "table" and msg.type == "update" then | |
46 | things[msg.computer] = { | |
47 | state = msg.state, | |
48 | message = msg.message | |
49 | } | |
50 | end | |
51 | ||
52 | local i = 2 | |
53 | for k, v in pairs(things) do | |
54 | m.setCursorPos(1, i) | |
55 | if v.state == "running" then | |
56 | m.setBackgroundColor(colors.green) | |
57 | elseif v.state == "restarting" then | |
58 | m.setBackgroundColor(colors.yellow) | |
59 | else | |
60 | m.setBackgroundColor(colors.red) | |
61 | end | |
62 | m.clearLine() | |
63 | m.write(("%s"):format(k)) | |
64 | m.setCursorPos(mX / 2, i) | |
65 | m.write(("%s"):format(v.message or "Online")) | |
66 | by_ID[i] = k | |
67 | i = i + 1 | |
68 | end | |
69 | end | |
70 | end | |
71 | ||
72 | local function touch_handler() | |
73 | local event, peripheral_name, x, y = os.pullEvent "monitor_touch" | |
74 | local name = by_ID[y] | |
75 | if name then | |
76 | print("Restarting", name) | |
77 | send_command(name, "restart") | |
78 | end | |
79 | end | |
80 | ||
81 | local function split_at_spaces(s) | |
82 | local t = {} | |
83 | for i in string.gmatch(s, "%S+") do | |
84 | table.insert(t, i) | |
85 | end | |
86 | return t | |
87 | end | |
88 | ||
89 | local function command_reader() | |
90 | local hist = {} | |
91 | while true do | |
92 | write "|> " | |
93 | local text = read(nil, hist) | |
94 | table.insert(hist, text) | |
95 | local command = split_at_spaces(text) | |
96 | send_command(command[2], command[1]) | |
97 | end | |
98 | end | |
99 | ||
100 | parallel.waitForAll(displayer, touch_handler, command_reader) |