-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtankmon.lua
More file actions
355 lines (324 loc) · 11.1 KB
/
Copy pathtankmon.lua
File metadata and controls
355 lines (324 loc) · 11.1 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
--tankmon
-- Railcraft tank monitoring by Forgotten_Boy
-- requires OpenPeripherals (OP) at least version 0.1.9, supports new liquid names in OP 0.2.1
-- with thanks to AmigaLink and Kalmor for the updated liquid names.
-- Supports iron and steel Railcraft tanks and 15 common liquids.
--[[
Setup:
- Place an Advanced Computer with wireless modem and with tankmon on it adjacent to a tank valve. Run "tankmon".
- Setup another Advanced Computer with wireless modem and with tankmon on it adjacent to an advanced monitor. Run "tankmon".
- Your monitor should now show the contents of the tank. Add as many tanks as you like and the server will simply add them to the display.
- The size of the monitor or locations of the modems don't matter, place them anywhere on the computer. The monitor can be resized while tankmon is running.
Advanced usage:
- On the client, you can use tankmon to trigger a redstone signal when the tank reaches a certain threshold (specified as 0 to 100, a percentage). For example:
tankmon 100 left
tankmon 0 top
The first example will send redstone output on the left when the tank is full. The second example will send redstone output on the top when the tank is empty.
--]]
-- Variable definitions
local valve, monitor, screenw, screenh
local serverID = nil
local clients = {}
local args = {...}
local redlimit, redside, on
local sides = {"left", "right", "top", "bottom", "front", "back"};
----------------------------------------------------
-- Function definitions
----------------------------------------------------
local liquidColors = {{"Water", colors.blue },
{"tile.oilStill", colors.gray, "Oil"},
{"Creosote Oil", colors.brown},
{"Essence", colors.lime},
{"Steam", colors.lightGray},
{"Honey", colors.yellow},
{"Ethanol", colors.orange},
{"Lava", colors.orange},
{"item.fuel", colors.yellow, "Fuel"},
{"Biomass", colors.green},
{"Fortron", colors.lightBlue},
{"Sludge", colors.black},
{"Liquid DNA", colors.magenta},
{"Fruit Juice", colors.green},
{"Seed Oil", colors.yellow},
{"Liquid Force", colors.yellow},
{"Oil", colors.gray, "Oil"},
{"Fuel", colors.yellow, "Fuel"},
{"uumatter", colors.purple, "UUMatter"},
{"vegetable", colors.magenta, "Veg"},
{"deuterium", colors.lightBlue, "Deuterium"},
--liquid names for OpenPeripherals 0.2.1 by AmigaLink
{"creosote", colors.brown, "Creosote Oil"},
{"essence", colors.lime, "Essence"},
{"steam", colors.lightGray, "Steam"},
{"honey", colors.yellow, "Honey"},
{"bioethanol", colors.orange, "Ethanol"},
{"lava", colors.orange, "Lava"},
{"biomass", colors.green, "Biomass"},
{"fortron", colors.lightBlue, "Fortron"},
{"sludge", colors.black, "Sludge"},
{"liquiddna", colors.magenta, "Liquid DNA"},
{"fruitjuice", colors.green, "Fruit Juice"},
{"seedoil", colors.yellow, "Seed Oil"},
{"xpjuice", colors.lime, "XP Juice"},
{"liquidforce", colors.yellow, "Liquid Force"},
{"oil", colors.gray, "Oil"},
{"fuel", colors.yellow, "Fuel"},
{"milk", colors.white, "Milk"},
-- Life Essence suggested by Fyrhtu
{"life essence", colors.red, "Life Essence"}
}
local function getLiquidColor(liquid)
for c, color in pairs (liquidColors) do
if (liquid == color[1]) then
return color[2],color[3] or liquid
end
end
return colors.white, liquid;
end
local function getDeviceSide(deviceType)
for i,side in pairs(sides) do
if (peripheral.isPresent(side)) then
if (peripheral.getType(side)) == string.lower(deviceType) then
return side;
end
end
end
end
local function showLevel(count,max,filled,color,label, amt, threshold, signal)
local screenw, screenh = monitor.getSize()
max = max + 1
if (not screenw) then
return nil;
-- monitor has been broken
end
local starty = screenh - math.floor((screenh * filled))
local width = math.ceil(screenw / max + .5)
local offset = math.ceil(width * (count - 1))
local amtw = string.len(amt)
local thresholdy = (threshold and ( screenh - ((threshold / 100) * screenh)))
if (count == max) then
-- the final column should use up the remaining space. A hack!
width = screenw - offset
end
--truncate the label to the width of the bar.
label = string.sub(label, 1, math.max((width - 1), 0))
if (thresholdy and thresholdy < 1) then
thresholdy = 1
else
if (thresholdy and thresholdy > screenh) then
thresholdy = screenh
end
end
term.redirect(monitor)
for c=starty, screenh + 1, 1 do
for line=0, width, 1 do
paintutils.drawPixel(line + offset, c, color)
end
end
if (thresholdy) then
local thresholdColor = color
for line=0, width, 1 do
thresholdColor = color
if (signal) then
thresholdColor = colors.red
else
-- makes a dotted line when there is no redstone signal
if (line % 2 == 0) then
thresholdColor = colors.red
end
end
paintutils.drawPixel(line + offset, thresholdy, thresholdColor)
end
end
monitor.setBackgroundColor(color)
if (color == colors.white) then
monitor.setTextColor(colors.black)
end
labely = math.min((starty + 1), screenh - 1)
monitor.setCursorPos(offset + 1, labely)
write(label)
if (amtw <= width) then
amty = math.min(labely + 1, screenh)
monitor.setCursorPos(offset + 1, amty)
write(amt)
end
monitor.setTextColor(colors.white)
-- term.restore()
end
local function tankStats(tank)
if(tank) then
local amt = tank["contents"]["amount"]
local size = tank["capacity"]
local filled = (amt and 1 / (size / amt)) or 0
return amt, size, filled
else
return nil;
end
end
local function tableCount(t)
local total=0
for k,v in pairs (t) do
total = total + 1
end
return total
end
local function updateDisplay()
local total = tableCount(clients)
local count = 1
monitor.setBackgroundColor(colors.black)
monitor.setTextScale(.5)
monitor.clear()
for ix,client in pairs (clients) do
local tank = client[1]
local threshold = client[2]
local signalOn = client[3]
local amt,size,filled = tankStats(tank)
local kind = tank["contents"]["name"]
local color,name = getLiquidColor(kind)
local unit = ""
local amount = math.max(amt or 0, 0)
if (amount > 1000000) then
unit="M"
amount=string.format("%.2f", math.floor(amt / 1000) / 1000)
else
if(amount > 0) then
unit="K"
amount=string.format("%.2f", amt / 1000)
else
amount = ""
end
end
amount = amount..unit
showLevel(count, total, filled, color, name or "Empty", amount, threshold, signalOn)
count = count + 1
end
return nil;
end
local function broadcast ()
term.clear()
term.setCursorPos(1,1)
print("_____________ tankmon Server started __________")
print("Broadcasting that tank display is available...")
print("Hold Ctrl+T to Terminate.")
while true do
rednet.broadcast(os.getComputerID())
term.setCursorPos(1, 5)
term.clearLine()
write("Connected tankmon clients: " .. tostring(tableCount(clients)))
sleep(7)
end
end
local function receive()
while true do
local senderID, message, distance = rednet.receive()
if (message) then
local data = textutils.unserialize(message)
clients[senderID] = data
end
end
end
local function display()
while true do
updateDisplay()
sleep(1.5)
end
end
local function connect()
print("Looking for a tankmon server in wireless Rednet range...")
while true do
local senderID, message, distance = rednet.receive()
serverID = senderID
print("Connected to server " .. tostring(serverID))
sleep(3)
end
end
local function publishTank()
while true do
if serverID then
term.clear()
term.setCursorPos(1,1)
print("** Sending out tank information **")
local tank = valve.getTankInfo()[1]
-- establish whether redstone signal should be sent
local amt,size,pctFilled = tankStats(tank)
on = false
local filled = pctFilled * 100
if (filled and redlimit and redlimit==0 and filled==0) then
on = true
else
if(filled and redlimit and filled <= redlimit) then
on=true
end
end
if(redside) then
rs.setOutput(redside, on)
end
-- use rednet to update the server with this tank's info.
local info = {tank, redlimit, on}
if (redlimit and redside) then
print("Redstone threshold: " .. tostring(redlimit))
print("Redstone output side: " .. redside)
print("Redstone signal on: " .. tostring(on))
print("")
end
term.clearLine()
write("** Tank contains: " .. tostring(amt))
rednet.send(serverID, textutils.serialize(info), false)
end
sleep(math.random(1,5))
end
end
---------------------------------------
--the Main
---------------------------------------
local modemSide = getDeviceSide("modem");
if (modemSide) then
local modem = peripheral.wrap(modemSide)
else
error("A wireless modem must be attached to this computer.")
end
local tankSide = getDeviceSide("iron_tank_valve");
local tankSide2 = getDeviceSide("steel_tank_valve");
local tankSide3 = getDeviceSide("rcsteeltankvalvetile");
local tankSide4 = getDeviceSide("rcirontankvalvetile");
local finalside = tankSide or tankSide2 or tankSide3 or tankSide4
local screenSide = getDeviceSide("monitor");
if (finalside and screenSide) then
error("Either a screen or a tank valve can be connected, not both.")
end
if finalside then
valve = peripheral.wrap(finalside )
end
if (screenSide) then
monitor = peripheral.wrap(screenSide)
if(not monitor.isColor()) then
error("The attached monitor must be Advanced. Get some gold!")
end
screenw, screenh = monitor.getSize()
monitor.clear()
end
rednet.open(modemSide)
if (valve) then
-- client mode
redlimit = args[1]
redside = args[2]
if (redlimit and not redside) then
print("A threshold and redstone side must both be present.")
print("e.g. tankmon 100 top")
error()
end
if (redlimit) then
redlimit = tonumber(redlimit)
print("")
print("Tank will send redstone signal at or below " .. tostring(redlimit) .. "% on side " .. redside)
end
-- clear outstanding redstone signals.
for i,side in pairs(sides) do
rs.setOutput(side, false)
end
parallel.waitForAll(connect, publishTank)
else
-- server mode
parallel.waitForAll(broadcast, receive, display)
end
rednet.close(modemSide)