Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --====== Brum's System Monitor Client Program ======--
- -- Author: OzyBrum
- -- Pastebin:
- -- Updater/Autorun pastebin:
- -- Licence: GPL v3
- -- Network Peripheral Function
- local sides = {
- [1] = "front",
- [2] = "back",
- [3] = "left",
- [4] = "right",
- [5] = "top",
- [6] = "bottom"
- }
- function peripherals()
- for i = 1, 6 do
- if peripheral.getType(sides[i]) == "modem" then
- modem = peripheral.wrap(sides[i])
- rednet.open(sides[i])
- modemSide = (sides[i])
- end
- if peripheral.getType(sides[i]) == "monitor" then
- monitor = peripheral.wrap(sides[i])
- monitorConnected = true
- end
- end
- end
- peripherals()
- --========== USER VARIABLES ==========--
- local myName = "System Monitor Client"
- local alert = 5 --Power level (%) at which you will revive an alert.
- local xpos = 5 --X and Y position of the display on your screen.
- local ypos = 30 --Note increasing the Y value will move the display down.
- local textBack = colors.blue --Set the text highLighter color
- local backGround = colors.blue --Set the background color of the monitor
- monitor.setTextColor(colors.white) --Set the monitors text color
- --====================================--
- -- System Variables
- local devList = modem.getNamesRemote()
- local w, h = 0
- local w, h = monitor.getSize()
- local myID = os.getComputerID()
- local version = 0.7
- local connected = false
- local messageReceived = false
- local rednetColours = {
- [1] = "colors.white",
- [2] = "colors.orange",
- [3] = "colors.magenta",
- [4] = "colors.lightBlue",
- [5] = "colors.yellow",
- [6] = "colors.lime",
- [7] = "colors.pink",
- [8] = "colors.gray",
- [9] = "colors.lightGray",
- [10] = "colors.cyan",
- [11] = "colors.purple",
- [12] = "colors.blue",
- [13] = "colors.brown",
- [14] = "colors.green",
- [15] = "colors.red",
- [16] = "colors.black"
- }
- local capEu = 0
- local storedEu = 0
- local capMj = 0
- local storedMj = 0
- local percent = 0
- local percent2 = 0
- local MFSUs = {}
- local rsCells = {}
- local tanks = {}
- local mfsuCount = 0
- local cellCount = 0
- local tankCount = 0
- local tankData = {}
- local tankInfo = {}
- local timer = 0
- local updateSpeed = 5
- monitor.setTextScale(1)
- -- Miscellaneous Functions
- -- Timers
- function pingRequestTimer() -- Time with no signal form server before the client declares itself 'loosing connection'
- timerA = os.startTimer(15)
- end
- function severTimeoutTimer() -- Time with no signal form server before the client declares itself 'disconnected'
- timerB = os.startTimer(30)
- end
- -- Get Current In-Game Time
- function osTime()
- local time = os.time()
- formattedTime = textutils.formatTime(time, true)
- end
- -- Rounder Function
- function round(number,decimal)
- local multiplier = 10^(decimal or 0)
- return math.floor(number * multiplier + 0.5) / multiplier
- end
- -- Peripheral/Devices Functions
- -- Sort devices into their types.
- function sortDevs()
- for k,v in pairs(devList) do
- if peripheral.getType(v) == "batbox" then
- MFSUs[k] = devList[k]
- mfsuCount = mfsuCount + 1
- elseif peripheral.getType(v) == "redstone_energy_cell" then
- rsCells[k] = devList[k]
- cellCount = cellCount + 1
- elseif peripheral.getType(v) == "steel_tank_valve" or peripheral.getType(v) == "iron_tank_valve" then
- tanks[k] = devList[k]
- tankCount = tankCount + 1
- end
- end
- for k,v in pairs(MFSUs) do
- --print(tostring(k).." "..tostring(v))
- end
- for k,v in pairs(rsCells) do
- --print(tostring(k).." "..tostring(v))
- end
- end
- -- Calculate the Eu and Mj capacity.
- function getCap()
- capEu = 0
- capMj = 0
- for k,v in pairs(MFSUs) do
- capEu = capEu + modem.callRemote(tostring(v),"getCapacity")
- --print(tostring(k).." | "..tostring(v))
- end
- for k,v in pairs(rsCells) do
- capMj = capMj + modem.callRemote(tostring(v),"getMaxEnergyStored")
- end
- osTime()
- print(formattedTime.." - EUCap "..capEu)
- print(formattedTime.." - MJCap "..capMj)
- end
- -- Get Energy Currently Stored in connected devices.
- function getStored()
- storedEu = 0
- storedMj = 0
- for k,v in pairs(MFSUs) do
- storedEu = storedEu + modem.callRemote(v,"getStored")
- end
- for k,v in pairs(rsCells) do
- storedMj = storedMj + modem.callRemote(v,"getEnergyStored")
- end
- end
- -- Get connected tank information and store it.
- function getTanks()
- i = 0
- for k,v in pairs(tanks) do
- i = i + 1
- tankData[i] = modem.callRemote(v,"getTanks","unknown")
- end
- i = 0
- for k,v in pairs(tankData) do
- i = i + 1
- tankInfo[i] = v[3],v[4],v[5]
- -- for k2, v2 in pairs(tankData[k][1]) do
- -- print(k2.." = "..v2)
- -- end
- end
- end
- --[[ Low power alert to bridge disabled
- note: when low power alert is activated send a message to network controller containing
- the id of the computer to activate a redstone signal, side, colour ect.
- function lowPowerAlert()
- timer = timer + updateSpeed
- if timer >= 1 then
- if toggle then
- toggle = false
- timer = 0
- else
- toggle = true
- timer = 0
- end
- end
- if toggle then
- bridge.addText(xpos,ypos + 75,"WARNING ENERGY LOW",0xFF0000)
- end
- end
- --]]
- -- Monitor Display Functions
- -- Gather all info and send it to the monitor
- function display()
- monitor.setBackgroundColor(backGround)
- monitor.clear()
- monitor.setBackgroundColor(textBack)
- heading()
- displayOutput()
- displayTanks()
- end
- -- Display the Current Energy Storage Levels
- function displayOutput()
- if mfsuCount > 0 then
- monitor.setCursorPos(1,2)
- monitor.write("MFSUs: "..tostring(round(storedEu / capEu * 100, 2)).."%")
- monitor.setCursorPos(22,2)
- monitor.write("Storage Units:"..tostring(mfsuCount))
- monitor.setCursorPos(1,4)
- monitor.write("EU Storage:"..tostring(round(storedEu/1000000,3)).."M")
- monitor.setCursorPos(22,4)
- monitor.write("Max:"..tostring(round(capEu/1000000,3)).."M")
- end
- if cellCount > 0 then
- monitor.setCursorPos(1,3)
- monitor.write("RSCells: "..tostring(round(storedMj / capMj * 100, 2)).."%")
- monitor.setCursorPos(22,3)
- monitor.write("Storage Units:"..tostring(cellCount))
- monitor.setCursorPos(1,5)
- monitor.write("MJ Storage:"..tostring(round(storedMj/1000000,3)).."M")
- monitor.setCursorPos(22,5)
- monitor.write("Max:"..tostring(round(capMj/1000000,3)).."M")
- end
- end
- -- Monitor Heading
- function heading()
- ln2 = "------Tank Stats------"
- monitor.setCursorPos((w-string.len(" Universal Storage Monitor "))/2+1,1)
- monitor.write(" Universal Storage Monitor ")
- monitor.setCursorPos(1,h)
- --monitor.write(version.." by Brandon3055")
- monitor.setCursorPos((w-string.len(ln2))/2+1,6)
- monitor.write(ln2)
- end
- -- Function to Display Tanks on Monitor
- function displayTanks()
- width = 0
- lines = h - 7
- linesUsed = 0
- lnx = 1
- for i = 1, tankCount do
- name = tankData[i][1]["name"] if name == nil then name = "Empty" end
- capacity = tonumber(tankData[i][1]["capacity"])
- amount = tonumber(tankData[i][1]["amount"]) if amount == nil then amount = 0 end
- percent = round(amount / capacity * 100, 2)
- --print(name.." | "..percent.." | "..amount.." | "..capacity)
- if linesUsed < lines - 4 then
- monitor.setCursorPos(lnx,7 + linesUsed)
- monitor.write(name)
- monitor.setCursorPos(lnx,7 + linesUsed + 1)
- monitor.write(percent.."%")
- monitor.setCursorPos(lnx,7 + linesUsed + 2)
- monitor.write(tostring(amount / 1000).."B")
- monitor.setCursorPos(lnx,7 + linesUsed + 3)
- monitor.write("/"..tostring(capacity / 1000).."B")
- linesUsed = linesUsed + 5
- else
- lnx = lnx + width + 2
- width = 0
- linesUsed = 0
- monitor.setCursorPos(lnx,7 + linesUsed)
- monitor.write(name)
- monitor.setCursorPos(lnx,7 + linesUsed + 1)
- monitor.write(percent.."%")
- monitor.setCursorPos(lnx,7 + linesUsed + 2)
- monitor.write(tostring(amount / 1000).."B")
- monitor.setCursorPos(lnx,7 + linesUsed + 3)
- monitor.write("/"..tostring(capacity / 1000).."B")
- linesUsed = linesUsed + 5
- end
- if string.len(name) > width then
- width = string.len(name)
- end
- if string.len("/"..tostring(capacity / 1000).."B") > width then
- width = string.len("/"..tostring(capacity / 1000).."B")
- end
- if string.len(percent.."%") > width then
- width = string.len(percent)
- end
- if string.len(tostring(amount / 1000).."B") > width then
- width = string.len(tostring(amount / 1000).."B")
- end
- end
- end
- -- Connection Functions
- function loosingConnection()
- print(formattedTime.." - No message received from server in 15s")
- print(formattedTime.." - Loosing connection to server...")
- messageReceived = false
- end
- function lostConnection()
- if messageReceived == false then
- print(formattedTime.." - Lost connection to server.")
- print(formattedTime.." - Waiting for server message.")
- connected = false
- end
- end
- -- Message Protocol Functions
- -- connectionMessages
- function broadcastMessage()
- if connected == false then
- serverID = senderID
- print(formattedTime.." - Found Server!")
- print(formattedTime.." - Server ID: "..serverID)
- print(formattedTime.." - Connecting...")
- rednet.send(senderID,"1~2")
- else
- --print(formattedTime.." - Broadcast from server received, already connected, Ignoring...")
- end
- end
- function clientNameRequest()
- local sendMessage = "1~4~"..myName
- rednet.send(serverID,sendMessage)
- print(formattedTime.." - Sent my name to server.")
- print(formattedTime.." - Connected!")
- messageReceived = true
- connected = true
- pingRequestTimer()
- severTimeoutTimer()
- end
- function ping()
- if connected == true then
- if messageReceived == false then
- print(formattedTime.." - Server reconnected.")
- end
- rednet.send(serverID,"1~6")
- messageReceived = true
- pingRequestTimer()
- severTimeoutTimer()
- end
- end
- -- rednetOutputMessages
- function rednetOutputOn()
- local side = sides[packetLayer2]
- local colour = rednetColours[packetLayer3]
- print(formattedTime.." - rednetOutput Message received.")
- print("side = "..side..", colour = "..colour..", state = on")
- if colour == "colors.white" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.white)
- elseif colour == "colors.orange" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.orange)
- elseif colour == "colors.magenta" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.magenta)
- elseif colour == "colors.lightBlue" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.lightBlue)
- elseif colour == "colors.yellow" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.yellow)
- elseif colour == "colors.lime" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.lime)
- elseif colour == "colors.pink" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.pink)
- elseif colour == "colors.gray" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.gray)
- elseif colour == "colors.lightGray" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.lightGray)
- elseif colour == "colors.cyan" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.cyan)
- elseif colour == "colors.purple" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.purple)
- elseif colour == "colors.blue" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.blue)
- elseif colour == "colors.brown" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.brown)
- elseif colour == "colors.green" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.green)
- elseif colour == "colors.red" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.red)
- elseif colour == "colors.black" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)+colors.black)
- end
- end
- function rednetOutputOff()
- local side = sides[packetLayer2]
- local colour = rednetColours[packetLayer3]
- print(formattedTime.." - rednetOutput Message received.")
- print("side = "..side..", colour = "..colour..", state = off")
- if colour == "colors.white" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.white)
- elseif colour == "colors.orange" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.orange)
- elseif colour == "colors.magenta" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.magenta)
- elseif colour == "colors.lightBlue" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.lightBlue)
- elseif colour == "colors.yellow" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.yellow)
- elseif colour == "colors.lime" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.lime)
- elseif colour == "colors.pink" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.pink)
- elseif colour == "colors.gray" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.gray)
- elseif colour == "colors.lightGray" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.lightGray)
- elseif colour == "colors.cyan" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.cyan)
- elseif colour == "colors.purple" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.purple)
- elseif colour == "colors.blue" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.blue)
- elseif colour == "colors.brown" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.brown)
- elseif colour == "colors.green" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.green)
- elseif colour == "colors.red" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.red)
- elseif colour == "colors.black" then
- rs.setBundledOutput(side,rs.getBundledOutput(side)-colors.black)
- end
- end
- -- Message Protocol Table
- local rednetOutputState = {
- [1] = rednetOutputOn,
- [2] = rednetOutputOff
- }
- local rednetOutputColour = {
- [1] = rednetOutputState,
- [2] = rednetOutputState,
- [3] = rednetOutputState,
- [4] = rednetOutputState,
- [5] = rednetOutputState,
- [6] = rednetOutputState,
- [7] = rednetOutputState,
- [8] = rednetOutputState,
- [9] = rednetOutputState,
- [10] = rednetOutputState,
- [11] = rednetOutputState,
- [12] = rednetOutputState,
- [13] = rednetOutputState,
- [14] = rednetOutputState,
- [15] = rednetOutputState,
- [16] = rednetOutputState
- }
- local rednetOutputSide = {
- [1] = rednetOutputColour,
- [2] = rednetOutputColour,
- [3] = rednetOutputColour,
- [4] = rednetOutputColour,
- [5] = rednetOutputColour,
- [6] = rednetOutputColour
- }
- local connectionMessages = {
- [1] = broadcastMessage,
- [2] = broadcastResponse,
- [3] = clientNameRequest,
- [4] = clientNameResponse,
- [5] = ping,
- [6] = pingResponse
- }
- local protocol = {
- [1] = connectionMessages,
- [2] = rednetOutputSide
- }
- -- Watch-out Functions (always running/waiting)
- -- Event Handler, detects system events such as timers going off and key presses.
- function sysEvents()
- while true do
- local event, parameter = os.pullEvent()
- if event == "key" then
- if parameter == 50 then -- Keycode 50 = 'M' Key
- menu()
- else
- print(formattedTime.." - You did not enter one of the required keystrokes.")
- print(formattedTime.." - Press 'M' to return to the menu.")
- end
- end
- if event == "timer" then
- if parameter == timerA then -- If pingRequestTimer timer goes off, run loosingConnection().
- loosingConnection()
- end
- if parameter == timerB then -- If severTimeoutTimer timer goes off, run lostConnection().
- lostConnection()
- end
- end
- end
- end
- -- Incoming Message handler, decodes and decides what to do with incoming messages from network controller.
- function messageRecieved()
- while true do
- senderID, message = rednet.receive()
- osTime()
- local _, count = string.gsub(message, "%~", "")
- --print(formattedTime.." - Number of times '~' occurs in message = "..count)
- if count == 1 then
- --print(formattedTime.." - 2 Layers of Protocol exist in message")
- packetLayer1, packetLayer2 = string.match(message, "(.+)~(.+)")
- --print(formattedTime.." - packetLayer1 = "..packetLayer1)
- --print(formattedTime.." - packetLayer2 = "..packetLayer2)
- packetLayer1 = tonumber(packetLayer1)
- packetLayer2 = tonumber(packetLayer2)
- protocol[packetLayer1][packetLayer2]()
- end
- if count == 2 then
- --print(formattedTime.." - 3 Layers of Protocol exist in message")
- packetLayer1, packetLayer2, packetLayer3 = string.match(message, "(.+)~(.+)~(.+)")
- --print(formattedTime.." - packetLayer1 = "..packetLayer1)
- --print(formattedTime.." - packetLayer2 = "..packetLayer2)
- --print(formattedTime.." - packetLayer3 = "..packetLayer3)
- packetLayer1 = tonumber(packetLayer1)
- packetLayer2 = tonumber(packetLayer2)
- if packetLayer2 == 4 then -- If message is 'clientNameResponse' then packetLayer3 is a string/name not number.
- --print(formattedTime.." - Message is a name response from a client")
- name = packetLayer3
- clientNames[senderID] = name
- --print("Client Name = "..name)
- protocol[packetLayer1][packetLayer2]()
- else
- packetLayer2 = tonumber(packetLayer3)
- print(formattedTime.." - packetLayer1 = "..packetLayer1)
- print(formattedTime.." - packetLayer2 = "..packetLayer2)
- print(formattedTime.." - packetLayer3 = "..packetLayer3)
- protocol[packetLayer1][packetLayer2][packetLayer3]()
- end
- end
- if count == 3 then
- --print(formattedTime.." - 4 Layers of Protocol exist in message")
- packetLayer1, packetLayer2, packetLayer3, packetLayer4 = string.match(message, "(.+)~(.+)~(.+)~(.+)")
- --print(formattedTime.." - packetLayer1 = "..packetLayer1)
- --print(formattedTime.." - packetLayer2 = "..packetLayer2)
- --print(formattedTime.." - packetLayer3 = "..packetLayer3)
- --print(formattedTime.." - packetLayer4 = "..packetLayer4)
- packetLayer1 = tonumber(packetLayer1)
- packetLayer2 = tonumber(packetLayer2)
- packetLayer3 = tonumber(packetLayer3)
- packetLayer4 = tonumber(packetLayer4)
- protocol[packetLayer1][packetLayer2][packetLayer3][packetLayer4]()
- end
- if count == 4 then
- --print(formattedTime.." - 5 Layers of Protocol exist in message")
- packetLayer1, packetLayer2, packetLayer3, packetLayer5 = string.match(message, "(.+)~(.+)~(.+)~(.+)~(.+)")
- --print(formattedTime.." - packetLayer1 = "..packetLayer1)
- --print(formattedTime.." - packetLayer2 = "..packetLayer2)
- --print(formattedTime.." - packetLayer3 = "..packetLayer3)
- --print(formattedTime.." - packetLayer4 = "..packetLayer4)
- --print(formattedTime.." - packetLayer5 = "..packetLayer5)
- packetLayer1 = tonumber(packetLayer1)
- packetLayer2 = tonumber(packetLayer2)
- packetLayer3 = tonumber(packetLayer3)
- packetLayer4 = tonumber(packetLayer4)
- packetLayer5 = tonumber(packetLayer5)
- protocol[packetLayer1][packetLayer2][packetLayer3][packetLayer4][packetLayer5]()
- end
- end
- end
- -- Functions that monitor all connected devices.
- function systemMonitor()
- while true do
- getStored()
- getTanks()
- display()
- sleep(updateSpeed)
- end
- end
- -- Menu Function
- function menu()
- term.clear()
- term.setCursorPos(1,1)
- print("--====== Brum's Client Computer ======--")
- print("Version: " ..version)
- print("Computer ID: " ..myID)
- print("Computer Name: " ..myName)
- print("Modem Side: " ..modemSide)
- print("--------------------MENU---------------------------")
- print("Press 'M' to show this menu at any time. ")
- print("---------------------------------------------------")
- end
- -- StartUp Function
- function startUp()
- sleep(1)
- menu()
- sortDevs()
- getCap()
- osTime()
- print(formattedTime.." - Waiting for server message...")
- parallel.waitForAny(sysEvents, messageRecieved, systemMonitor)
- end
- startUp()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement