test
This commit is contained in:
parent
cf2eda8553
commit
21f08b3041
@ -1,35 +1,71 @@
|
|||||||
local monitor = peripheral.find("monitor")
|
local logFileName = "turtle_status.log"
|
||||||
local turtleStates = {}
|
local turtleStates = {}
|
||||||
|
local monitor = peripheral.find("monitor")
|
||||||
|
|
||||||
-- Set monitor display
|
-- Try to open modem on any side
|
||||||
monitor.setTextScale(0.5)
|
local modemOpened = false
|
||||||
monitor.clear()
|
for _, side in ipairs({"top", "bottom", "left", "right", "front", "back"}) do
|
||||||
monitor.setCursorPos(1,1)
|
if peripheral.getType(side) == "modem" then
|
||||||
|
rednet.open(side)
|
||||||
|
modemOpened = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- Pretty print function
|
if not modemOpened then
|
||||||
local function draw()
|
print("No modem found. Cannot continue.")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if monitor then
|
||||||
|
monitor.setTextScale(0.5)
|
||||||
monitor.clear()
|
monitor.clear()
|
||||||
monitor.setCursorPos(1,1)
|
end
|
||||||
monitor.write("Turtle Monitor")
|
|
||||||
local row = 2
|
-- Logging function
|
||||||
|
local function logToFile(line)
|
||||||
|
local file = fs.open(logFileName, "a")
|
||||||
|
file.writeLine(("[%s] %s"):format(os.date("%H:%M:%S"), line))
|
||||||
|
file.close()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Draws to monitor or terminal
|
||||||
|
local function draw()
|
||||||
|
local output = {}
|
||||||
|
|
||||||
|
table.insert(output, "Turtle Monitor")
|
||||||
for id, data in pairs(turtleStates) do
|
for id, data in pairs(turtleStates) do
|
||||||
monitor.setCursorPos(1, row)
|
|
||||||
local age = os.clock() - data.time
|
local age = os.clock() - data.time
|
||||||
local status = (age > 10) and "offline" or data.status
|
local status = (age > 10) and "offline" or data.status
|
||||||
monitor.write(string.format("%s (ID %d): %s", data.label, id, status))
|
local line = string.format("%s (ID %d): %s", data.label, id, status)
|
||||||
row = row + 1
|
table.insert(output, line)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Output to monitor if present
|
||||||
|
if monitor then
|
||||||
|
monitor.clear()
|
||||||
|
monitor.setCursorPos(1,1)
|
||||||
|
for i, line in ipairs(output) do
|
||||||
|
monitor.setCursorPos(1, i)
|
||||||
|
monitor.write(line)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
term.clear()
|
||||||
|
term.setCursorPos(1,1)
|
||||||
|
for _, line in ipairs(output) do
|
||||||
|
print(line)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Main loop
|
-- Main loop
|
||||||
rednet.open("back") -- or side where modem is
|
|
||||||
while true do
|
while true do
|
||||||
local msg, proto
|
|
||||||
local id, message, protocol = rednet.receive("turtle_status", 2)
|
local id, message, protocol = rednet.receive("turtle_status", 2)
|
||||||
if id then
|
if id then
|
||||||
local ok, data = pcall(textutils.unserialize, message)
|
local ok, data = pcall(textutils.unserialize, message)
|
||||||
if ok and data and data.id then
|
if ok and data and data.id then
|
||||||
turtleStates[data.id] = data
|
turtleStates[data.id] = data
|
||||||
|
logToFile(string.format("Received from %s (ID %d): %s", data.label, data.id, data.status))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
draw()
|
draw()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user