Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- BatteryHost v1.1 by Masserio
- ____ _ _ _ _ _
- | _ \ | | | | | | | | | |
- | |_) | __ _| |_| |_ ___ _ __ _ _| |__| | ___ ___| |_
- | _ < / _` | __| __/ _ \ '__| | | | __ |/ _ \/ __| __|
- | |_) | (_| | |_| || __/ | | |_| | | | | (_) \__ \ |_
- |____/ \__,_|\__|\__\___|_| \__, |_| |_|\___/|___/\__|
- __/ |
- |___/
- This is a program that collects information from several computers
- running BatteryOS (see list below), analyses the data, and sends
- status updates and information to a computer running either
- EngineOS or MonitorOS (again, list below).
- This is one part in a series of programs, and this will not work as
- intended on it's own. If you're planning on using this program please
- check out the following programs as well, as they all communicate:
- -BatteryOS v1.1 Pastebin: 3xja4QFK (Stable)
- Monitors Redstone Energy Cells and sends status messages
- -MonitorOS v1.0 Pastebin: HpFunt8W (Stable)
- Updating statuses on a big screen
- -EngineOS v1.0 Pastebin: PYRX7G2t (Stable)
- Simple engine control which also sends operational status
- Feel free to copy parts or this whole code, and change it as you like!
- --]]
- -- Set up some variables to be used throughout the programme --
- computers = {} -- Table with all known computers
- modem = nil -- Modem side
- cellStatus = {} -- Cell statuses
- powerOutput = 0 -- Maximum power output from Cells
- powerEstimate = 0 -- Estimate of how much power is stored in million MJ
- monitorID = nil -- ID of the computer that runs MonitorOS
- engineID = nil -- ID of the computer that runs EngineOS
- statusUpdateTimer = 0 -- To keep track of time between status updates
- computerAmount = 0 -- Amount of computers connected
- -- Function to detect peripherals --
- function detectModem()
- for i, side in pairs(rs.getSides()) do
- if peripheral.isPresent(side) then
- if peripheral.getType(side) == "modem" then
- modem = side
- end
- end
- end
- if modem == nil then
- print("No modem connected, attach a modem and try again")
- error()
- end
- end
- -- Add a computer to the table --
- function addComputer(ID, status, cell)
- comp = {ID, status, cell}
- -- Computers are stores in the table based on cell nr
- table.insert(computers, cell, comp)
- computerAmount = computerAmount + 1
- end
- -- Get new computer info
- function connectComputer(compID)
- cell = nil
- status = nil
- rednet.send(compID, "Cell")
- ID, msg = rednet.receive(1)
- if ID == compID then
- cell = msg
- rednet.send(compID, "Status")
- ID, msg = rednet.receive(1)
- if ID == compID then
- status = msg
- rednet.send(compID, "Thanks")
- else
- print("Wrong ID or timeout while getting status")
- end
- else
- print("Wrong ID or timeout while getting Cell")
- end
- if cell ~= nil and status ~= nil then
- addComputer(compID, status, cell)
- print("Computer added")
- else
- print("Did not receive all the information, aborting process")
- end
- end
- -- Check for more computers that are not connected
- function checkForNew(times)
- if computerAmount < 16 then
- rednet.broadcast("Calling")
- ID, msg = rednet.receive(1)
- if msg == "Here" then
- print("New computer found with ID: "..ID)
- sleep(0.5) -- Give it time to avoid interference
- rednet.send(ID, "Standby")
- connectComputer(ID)
- checkForNew(1)
- elseif times == 10 then
- print("Giving up, found "..computerAmount.." computers total")
- else
- checkForNew(times + 1)
- end
- else
- print("Computers reached "..computerAmount)
- end
- end
- -- Request status from a ccomputer --
- function requestStatus(compID)
- status = nil
- rednet.send(compID, "Status")
- ID, msg = rednet.receive(1)
- if ID ~= compID and ID ~= nil then
- print("Wrong computer answered, trying again")
- requestStatus(compID)
- elseif ID == nil then
- -- No answer, do nothing
- else
- status = msg
- rednet.send(compID, "Thanks")
- end
- return status
- end
- -- Update Cell statuses --
- function updateCellStatus()
- output = 0
- estimate = 0
- for i=1, 16 do
- comp = computers[i]
- if comp ~= nil then
- status = comp[2]
- cellStatus[comp[3]] = status
- if status == 100 then
- output = output + 100
- estimate = estimate + 1.8
- elseif status == 80 then
- output = output + 100
- estimate = estimate + 1.5
- elseif status == 60 then
- output = output + 100
- estimate = estimate + 1.0
- elseif status == 40 then
- output = output + 100
- estimate = estimate + 0.8
- elseif status == 20 then
- output = output + 100
- estimate = estimate + 0.4
- elseif status == 10 then
- estimate = estimate + 0.1
- else
- -- Both output and estimate 0, add nothing
- end
- end
- end
- powerOutput = output
- powerEstimate = estimate
- print("Updated cell statuses")
- end
- -- Get statuses from all computers --
- function getStatuses()
- -- Go trough all computers
- for i=1, 16 do
- -- Get the computers status
- comp = computers[i]
- if comp ~= nil then
- status = requestStatus(comp[1])
- -- Update the status, and the table of computers
- comp[2] = status
- computers[i] = comp
- end
- end
- updateCellStatus()
- print("Status gathering complete")
- end
- -- Setup modem etc --
- function setup()
- detectModem()
- rednet.open(modem)
- checkForNew(1)
- getStatuses()
- print("Max Power Output: "..powerOutput)
- print("Estimated power storage: "..powerEstimate.."million MJ")
- print("Setup complete")
- end
- -- Send individual cell status (can't send table via modem) --
- function sendOneCellStatus(status)
- done = false
- rednet.send(monitorID, status)
- ID, msg = rednet.receive(1)
- if ID == monitorID and msg == "Thanks" then
- done = true
- rednet.send(monitorID, "Next") -- Prepare the pc for the next status
- end
- return done
- end
- -- Send status of the cells --
- function sendCellStatuses()
- print("Request for cell statuses received")
- for i=1, 16 do
- comp = computers[i]
- status = nil
- if comp ~= nil then
- status = comp[2]
- else
- -- No cell registered
- status = "Err"
- end
- if not sendOneCellStatus(status) then
- print("Error in sending cell status, abort")
- break
- end
- end
- rednet.send(monitorID, "Done")
- print("All cell statuses sent")
- end
- -- Send current max power output --
- function sendPowerOutput(compID, times)
- print("Request for max power output received by "..compID)
- rednet.send(compID, powerOutput)
- ID, msg = rednet.receive(1)
- if ID == nil then
- print("No answer, abort")
- elseif ID ~= compID then
- print("Wrong pc answered, try again")
- sendPowerOutput(times + 1)
- end
- return times
- end
- -- Send the estimated amount of power stored --
- function sendPowerEstimate(compID, times)
- print("Request for power estimate received by "..compID)
- rednet.send(compID, powerEstimate)
- ID, msg = rednet.receive(1)
- if ID == nil then
- print("No answer, abort")
- elseif ID ~= compID then
- print("Wrong pc answered, try again")
- sendPowerEstimate(compID, times + 1)
- end
- return times
- end
- -- Connect to monitor pc or engine pc --
- function connect(compID)
- rednet.send(ID, "Here")
- ID, msg = rednet.receive(1)
- if ID == compID and msg == "Monitor" then
- monitorID = ID
- rednet.send(ID, "Thanks")
- -- reset the update timer to synch with MonitorOS
- statusUpdateTimer = 0
- print("Monitor computer found with ID: "..ID)
- elseif ID == compID and msg == "Engine" then
- engineID = ID
- rednet.send(ID, "Thanks")
- print("Engine computer found with ID: "..ID)
- else
- print("Wrong or no answer during connection, abort")
- end
- end
- -- Do some stuff before the main loop starts --
- term.clear()
- term.setCursorPos(1,1)
- print("Running BatteryHost v1.0")
- setup()
- -- Main loop --
- while true do
- ID, msg = rednet.receive(10)
- if msg == "Host" then
- connect(ID)
- elseif ID == monitorID and msg == "Status" then
- sendCellStatuses()
- statusUpdateTimer = 0
- elseif (ID == monitorID or ID == engineID) and msg == "Output" then
- sendPowerOutput(ID, 1)
- if ID == monitorID then statusUpdateTimer = 0 end
- elseif (ID == monitorID or ID == engineID) and msg == "Power" then
- sendPowerEstimate(ID, 1)
- if ID == monitorID then statusUpdateTimer = 0 end
- elseif ID ~= nil then
- if msg == "Reboot" then
- rednet.close(modem)
- os.reboot()
- else
- print("Unknown message from ID"..ID..":")
- print(msg)
- end
- end
- if statusUpdateTimer == 8 then -- This makes it about 3 minutes between status updates
- getStatuses()
- statusUpdateTimer = 0
- print("Max Power Output: "..powerOutput)
- print("Estimated power storage: "..powerEstimate.."million MJ")
- else
- statusUpdateTimer = statusUpdateTimer + 1
- print("Idle "..statusUpdateTimer)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement