Advertisement
Masserio1

MonitorOS

Sep 27th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.66 KB | None | 0 0
  1. --[[
  2.  MonitorOS v1.0 by Masserio
  3.   __  __             _ _              ____   _____
  4.  |  \/  |           (_) |            / __ \ / ____|
  5.  | \  / | ___  _ __  _| |_ ___  _ __| |  | | (___  
  6.  | |\/| |/ _ \| '_ \| | __/ _ \| '__| |  | |\___ \
  7.  | |  | | (_) | | | | | || (_) | |  | |__| |____) |
  8.  |_|  |_|\___/|_| |_|_|\__\___/|_|   \____/|_____/
  9.  
  10.  MonitorOS takes in all information gathered by BatteryHost and
  11.  EngineOS (see list below), and prints it to the screen. My setup
  12.  consists of a max size screen of advanced monitors (8 wide and 6 tall),
  13.  and the programme is designed thereafter.
  14.  
  15.  This is one part in a series of programs, and this will not work as
  16.  intended on it's own. If you're planning on using this program please
  17.  check out the following programs as well, as they all communicate:
  18.  -BatteryOS v1.1 Pastebin: 3xja4QFK (Stable)
  19.    Monitors Redstone Energy Cells and sends status messages
  20.  -BatteryHost v1.1 Pastebin: m2HkbMiJ (Stable)
  21.    The program collecting statuses from BatteryOS and processes it.
  22.  -EngineOS v1.0 Pastebin: PYRX7G2t (Stable)
  23.    Simple engine control which also sends operational status
  24.  
  25.  Feel free to copy parts or this whole code, and change it as you like!
  26. --]]
  27.  
  28. -- Set up some variables to be used throughout the programme --
  29. batteryHostID = nil -- ID of computer running BatteryHost
  30. batteryStatus = {} -- Stores statuses for all cells
  31. statusUpdated = false -- For color coding if updated or not
  32. batteryOutput = 0 -- Maximum power output
  33. outputUpdated = false -- For color coding if updated or not
  34. batteryStored = 0 -- Estimate of how much power is stored
  35. storedUpdated = false -- For color coding if updated or not
  36. engineID = nil -- ID of computer running EngineOS
  37. enginesRunning = 0 -- How many engines are currently running
  38. enginesUpdated = false -- For color coding if updated or not
  39. monitor = nil -- Monitor where the magic happens, wrapped in a peripheral
  40. monitorWidth = 0 -- Width of the monitor
  41. monitorHeight = 0 -- Height of the monitor
  42. modem = nil -- Side of the modem
  43. updateTimer = 0 -- To synch with the BatteryHost update time (every ~3 minutes)
  44.  
  45. -- Function to produce the time to next update bar --
  46. function updateBar()
  47.   bar = "["
  48.   timer = math.floor(updateTimer / 2)
  49.   for i=1, timer do
  50.     bar = bar.."I"
  51.   end
  52.   for i=1, 5 - timer do
  53.     bar = bar.." "
  54.   end
  55.   bar = bar.."]"
  56.   return bar
  57. end
  58.  
  59. -- Update everything to the screen --
  60. function updateScreen()
  61.   monitor.clear()
  62.   monitor.setCursorPos((monitorWidth / 3) - 20, 13)
  63.   monitor.write("Next update "..updateBar())
  64.   monitor.setCursorPos(2, 2)
  65.   monitor.write("Max Output: "..batteryOutput.."MJ/t")
  66.   monitor.setCursorPos(2, 3)
  67.   monitor.write("Stored Energy: "..batteryStored.."M MJ")
  68.   monitor.setCursorPos(2, 4)
  69.   monitor.write("Active engines: "..enginesRunning.." at "..(enginesRunning * 6).."MJ/t")
  70.   for i=1, #batteryStatus do
  71.     oldx, oldy = monitor.getCursorPos()
  72.     if i == 1 or i == 3 or i == 5 or i == 7 or
  73.       i == 9 or i == 11 or i == 13 or i == 15 then
  74.       monitor.setCursorPos(2, oldy + 1)
  75.     else
  76.       monitor.setCursorPos((monitorWidth / 3) - 12, oldy)
  77.     end
  78.     monitor.write("Cell"..string.format("%02d", i)..":"..string.format("%3d", batteryStatus[i]).."%")
  79.   end
  80. end
  81.  
  82. -- Get one of the statuses --
  83. function getOneCellStatus(ID, msg)
  84.   status = nil
  85.   done = false
  86.   if ID == batteryHostID then
  87.     status = msg
  88.     rednet.send(batteryHostID, "Thanks")
  89.     ID, msg = rednet.receive(1)
  90.     if ID == batteryHostID and msg == "Next" then
  91.       done = true
  92.       print("Done, go to next")
  93.     end
  94.   else
  95.     print("Error in getting a cell status, abort")
  96.   end
  97.   return status, done
  98. end
  99.  
  100. -- Get the cell statuses --
  101. function getStatuses()
  102.   print("Getting cell statuses")
  103.   done = false
  104.   rednet.send(batteryHostID, "Status")
  105.   gettingStatuses = true
  106.   newBatteryStatus = {}
  107.   while gettingStatuses do
  108.     ID, msg = rednet.receive(1)
  109.     if ID == batteryHostID then
  110.       if msg ~= "Done" and msg ~= "Calling" then
  111.         status, isDone = getOneCellStatus(ID, msg)
  112.         if isDone then
  113.           table.insert(newBatteryStatus, status)
  114.           print("Status added")
  115.         else
  116.           gettingStatuses = false
  117.         end
  118.       elseif msg == "Done" then
  119.         done = true
  120.         gettingStatuses = false
  121.         batteryStatus = newBatteryStatus
  122.         print("Got all statuses")
  123.       else
  124.         -- Timed it with BatteryHost trying to locate new BatteryOS, try again
  125.         print("Timed it with BatteryHost trying to locate new BatteryOS, try again")
  126.         sleep(3)
  127.         getStatuses()
  128.       end
  129.     else
  130.       print("Wrong or no answer, abort")
  131.       gettingStatuses = false
  132.     end
  133.   end
  134. end
  135.  
  136. -- Get the maximum power output --
  137. function getOutput(times)
  138.   print("Getting maximum power output")
  139.   done = false
  140.   rednet.send(batteryHostID, "Output")
  141.   ID, msg = rednet.receive(1)
  142.   if ID == batteryHostID and msg ~= "Calling" then
  143.     batteryOutput = tonumber(msg) * 0.75
  144.     done = true
  145.     rednet.send(batteryHostID, "Thanks")
  146.     print("Got the output: "..msg)
  147.   elseif times == 4 then
  148.     print("Answer timeout, abort")
  149.   else
  150.     print("Wrong or no answer, try again")
  151.     sleep(1)
  152.     getOutput(times + 1)
  153.   end
  154.   return done
  155. end
  156.  
  157. -- Get the estimate of how much power is stored --
  158. function getStored(times)
  159.   print("Getting stored energy estimate")
  160.   done = false
  161.   rednet.send(batteryHostID, "Power")
  162.   ID, msg = rednet.receive(1)
  163.   if ID == batteryHostID and msg ~= "Calling" then
  164.     batteryStored = msg
  165.     done = true
  166.     rednet.send(batteryHostID, "Thanks")
  167.     print("Got the estimate: "..msg)
  168.   elseif times == 4 then
  169.     print("Answer timeout, abort")
  170.   else
  171.     print("Wrong or no answer, try again")
  172.     sleep(1)
  173.     getStored(times + 1)
  174.   end
  175.   return done
  176. end
  177.  
  178. -- Get the amount of engines running --
  179. function getEnginesRunning(times)
  180.   print("Getting amount of engines running")
  181.   done = false
  182.   rednet.send(engineID, "Running")
  183.   ID, msg = rednet.receive(1)
  184.   if ID == engineID then
  185.     enginesRunning = msg
  186.     done = true
  187.     rednet.send(engineID, "Thanks")
  188.     print("Got amount: "..msg)
  189.   elseif times == 4 then
  190.     print("Answer timeout, abort")
  191.   else
  192.     print("Wrong or no answer, try again")
  193.     getEnginesRunning(times + 1)
  194.   end
  195.  return done
  196. end
  197.  
  198. -- Find the computer running BatteryHost --
  199. function findBatteryHost(times)
  200.   print("Find the computer running BatteryHost")
  201.   done = false
  202.   rednet.broadcast("Host")
  203.   ID, msg = rednet.receive(1)
  204.   if ID ~= nil and msg == "Here" then
  205.     rednet.send(ID, "Monitor")
  206.     ID, msg = rednet.receive(1)
  207.     if ID ~= nil and msg == "Thanks" then
  208.       batteryHostID = ID
  209.       done = true
  210.       print("Host computer found and connected with ID: "..ID)
  211.     else
  212.       print("No confirmation, try again")
  213.       findBatteryHost(times + 1)
  214.     end
  215.   elseif times == 10 then
  216.     print("Tried too many times with no luck, is a host set up?")
  217.   else
  218.     print("Host busy, try again in a sec")
  219.     sleep(2)
  220.     findBatteryHost(times + 1)
  221.   end
  222.   return done
  223. end
  224.  
  225. -- Find the computer running EngineOS --
  226. function findEngineOS(times)
  227.   print("Find the computer running EngineOS")
  228.   done = false
  229.   rednet.broadcast("Engine")
  230.   ID, msg = rednet.receive(1)
  231.   if ID ~= nil and msg == "Here" then
  232.     rednet.send(ID, "Monitor")
  233.     ID, msg = rednet.receive(1)
  234.     if ID ~= nil and msg == "Thanks" then
  235.       engineID = ID
  236.       done = true
  237.       print("EngineOS computer found and connected with ID: "..ID)
  238.     else
  239.       print("No confirmation, try again")
  240.       findEngineOS(times + 1)
  241.     end
  242.   elseif times == 10 then
  243.     print("Tried too many times with no luck, is EngineOS set up?")
  244.   else
  245.     print("EngineOS busy, try again in a sec")
  246.     sleep(2)
  247.     findEngineOS(times + 1)
  248.   end
  249.   return done
  250. end
  251.  
  252. -- Function to detect peripherals --
  253. function detectSides()
  254.   print("Detecting Modem and Monitor")
  255.   for i, side in pairs(rs.getSides()) do
  256.     if peripheral.isPresent(side) then
  257.       if peripheral.getType(side) == "modem" then
  258.         print("Modem set to "..side.." side")
  259.         modem = side
  260.       elseif peripheral.getType(side) == "monitor" then
  261.         print("Monitor set to "..side.." side")
  262.         monitor = peripheral.wrap(side)
  263.       end
  264.     end
  265.   end
  266. end
  267.  
  268. -- If it couldn't find BatteryHost or EngineOS pc, give option to input ID manually --
  269. function getInput(missing)
  270.   print("Could not find "..missing..". Input manually or write 'terminate' to exit program")
  271.   input = io.read()
  272.   if input == "terminate" then
  273.     print("Please configure "..missing.." correctly and try again")
  274.     error()
  275.   elseif tonumber(input) ~= nil then
  276.     if missing == "BatteryHost" then
  277.       batteryHostID = math.floor(input)
  278.       print("BatteryHost computer set to ID: "..batteryHostID)
  279.     elseif missing == "EngineOS" then
  280.       engineID = math.floor(input)
  281.       print("EngineOS computer set to ID: "..engineID)
  282.     else
  283.       print("Something went wrong! Number input, but not correct missing statement")
  284.       error()
  285.     end
  286.   else
  287.     print("Input not a number, try again")
  288.     getInput(missing)
  289.   end
  290. end
  291.  
  292. -- Set up the monitor pc --
  293. function setup()
  294.   term.clear()
  295.   term.setCursorPos(1, 1)
  296.   print("Running MonitorOS v1.0")
  297.   detectSides()
  298.   if modem == nil then
  299.     print("No modem detected, please attach a modem and try again")
  300.     error()
  301.   end
  302.   if monitor == nil then
  303.     print("No monitor detected, please attach a monitor and try again")
  304.     error()
  305.   end
  306.   width, height = monitor.getSize()
  307.   monitorWidth = width
  308.   monitorHeight = height
  309.   rednet.open(modem)
  310.   print("Modem open and ready")
  311.  
  312.   -- Wait 45 seconds for BatteryHost and EngineOS to update
  313.   monitor.clear()
  314.   monitor.setTextScale(3)
  315.   monitor.setCursorPos(3, 7)
  316.   monitor.write("Setting up, please wait")
  317.   monitor.setCursorPos(4, 8)
  318.   monitor.write("Takes up to 1 minute")
  319.   sleep(45)
  320.   if not findBatteryHost(1) then
  321.     getInput("BatteryHost")
  322.   end
  323.   sleep(1)
  324.   if not findEngineOS(1) then
  325.     getInput("EngineOS")
  326.   end
  327.   sleep(1)
  328.   updateTimer = 10 -- Run an update via the main loop
  329.   print("Setup complete")
  330. end
  331.  
  332. -- Run the setup, and then start main loop --
  333. setup()
  334. while true do
  335.   if updateTimer == 10 then
  336.     -- Get all the info, then update screen
  337.     statusUpdated = getStatuses()
  338.     sleep(1)
  339.     storedUpdated = getStored(1)
  340.     sleep(1)
  341.     outputUpdated = getOutput(1)
  342.     enginesUpdated = getEnginesRunning(1)
  343.     updateScreen()
  344.     updateTimer = 0
  345.   else
  346.     updateTimer = updateTimer + 1
  347.     updateScreen()
  348.     ID, msg = rednet.receive(10)
  349.     if msg == "Reboot" then
  350.       rednet.close(modem)
  351.       os.reboot()
  352.     end
  353.   end
  354. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement