Advertisement
Masserio1

BatteryOS

Sep 21st, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.32 KB | None | 0 0
  1. --[[
  2.  BatteryOS v1.1 by Masserio
  3.   ____        _   _                   ____   _____
  4.  |  _ \      | | | |                 / __ \ / ____|
  5.  | |_) | __ _| |_| |_ ___ _ __ _   _| |  | | (___  
  6.  |  _ < / _` | __| __/ _ \ '__| | | | |  | |\___ \
  7.  | |_) | (_| | |_| ||  __/ |  | |_| | |__| |____) |
  8.  |____/ \__,_|\__|\__\___|_|   \__, |\____/|_____/
  9.                                 __/ |              
  10.                                |___/              
  11.  
  12.  This program can be started without requireing input by using
  13.  'shell.run("BatteryOS", <Cell>, "<Input side>", "<Output side>")'
  14.  from any program (most commonly via 'startup').
  15.  Input side is where power goes in to the cells, and output where
  16.  it goes out from the cells.
  17.  
  18.  This is just a simple program I made for a setup in Minecraft.
  19.  It's made to monitor 'Cells' of Redstone Energy Cells that powers
  20.  my machinery. My setup consists of three Redstone Cells per 'Cell'.
  21.  Two Gate Readers is hooked up to the input cell and output cell, with
  22.  a computer in the middle to read info from both readers.
  23.  If I can be bothered I might update this with a link to screenshots
  24.  of my setup for further explanations.
  25.  
  26.  This is one part in a series of programs, and this will not work as
  27.  intended on it's own. If you're planning on using this program please
  28.  check out the following programs as well, as they all communicate:
  29.  -BatteryHost v1.1 Pastebin: m2HkbMiJ (Stable)
  30.    The program collecting statuses from BatteryOS and processes it.
  31.  -MonitorOS v1.0 Pastebin: HpFunt8W (Stable)
  32.    Updating statuses on a big screen
  33.  -EngineOS v1.0 Pastebin: PYRX7G2t (Stable)
  34.    Simple engine control which also sends operational status
  35.  
  36.  Feel free to copy parts or this whole code, and change it as you like!
  37. --]]
  38.  
  39. -- Set up some variables to be used throughout the programme --
  40. runArgs = {...} -- If started by another program with cell and gate info
  41. cell = nil -- Info about what Cell the computer is monitoring
  42. inputGate = nil -- Gate Reader on the input side of the cell
  43. outputGate = nil -- Gate Reader on the output side of the cell
  44. modem = nil -- Side of the Modem
  45. hostID = 0 -- ID of the host computer where statuses are sent
  46. connected = false -- True if connected to the host
  47. status = "Empty" -- Status, default is Empty
  48. change = true -- Just something to keep monitor clear during main loop
  49.  
  50. -- Get user input: Cell --
  51. local function getCell()
  52.   print("Input Cell: number between 1 and 16")
  53.   local input = io.read()
  54.   -- Floor to get rid of decimals and check if it is within range and that it is a number
  55.   if tonumber(input) ~= nil and math.floor(input) <= 16 and math.floor(input) >= 1 then
  56.     cell = math.floor(input)
  57.     print("Cell set to: "..cell)
  58.   else
  59.     print(input.." is an invalid Cell number, try again")
  60.     getCell()
  61.   end
  62. end
  63.  
  64. -- Get user input: Side of the Input Gate Reader --
  65. local function getInputGate()
  66.   print("Input side for Gate Reader in input cell")
  67.   local input = io.read()
  68.   if (input ~= nil or input ~= outputGate) and (input == "front" or
  69.     input == "back" or input == "left" or input == "right" or
  70.     input == "top" or input == "bottom") then
  71.     inputGate = peripheral.wrap(input)
  72.     print("Input Gate Reader set to: "..input)
  73.   else
  74.     print(input.." is an invalid position for the Input Gate")
  75.     getInputGate()
  76.   end
  77. end
  78.  
  79. -- Get user input: Side of the Output Gate Reader --
  80. local function getOutputGate()
  81.   print("Input side for Gate Reader in output cell")
  82.   local input = io.read()
  83.   if (input ~= nil or input ~= inputGate) and (input == "front" or
  84.     input == "back" or input == "left" or input == "right" or
  85.     input == "top" or input == "bottom") then
  86.     outputGate = peripheral.wrap(input)
  87.     print("Output Gate Reader set to: "..input)
  88.   else
  89.     print(input.." is an invalid position for the Output Gate")
  90.     getOutputGate()
  91.   end
  92. end
  93.  
  94. -- Main input function to handle all inputs --
  95. local function getInput()
  96.   -- If info is not sent as arguments when starting the program, get input
  97.   if runArgs == nil then
  98.     -- One function for each to easily handle wrong inputs
  99.     print("Getting inputs")
  100.     getCell()
  101.     getInputGate()
  102.     getOutputGate()
  103.     print("Inputs completed")
  104.   else
  105.     -- Inputs were sent via arguments, no input required
  106.     print("Info gotten trough arguments, no input required")
  107.     if tonumber(runArgs[1]) ~= nil and math.floor(runArgs[1]) <= 16 and math.floor(runArgs[1]) >= 1 then
  108.       cell = math.floor(runArgs[1])
  109.       print("Cell set to: "..cell)
  110.     else
  111.       -- Invalid Cell number, has to be input manually
  112.       print(runArgs[1].." is an invalid Cell number, input manually")
  113.       getCell()
  114.     end
  115.     if runArgs[2] == "front" or runArgs[2] == "back" or runArgs[2] == "left" or
  116.       runArgs[2] == "right" or runArgs[2] == "top" or runArgs[2] == "bottom" then
  117.       inputGate = peripheral.wrap(runArgs[2])
  118.       print("InputGate set to "..runArgs[2])
  119.     else
  120.       -- Invalid position, has to be input manually
  121.       print("Invalid position, input manually")
  122.       getInputGate()
  123.     end
  124.     if runArgs[3] == "front" or runArgs[3] == "back" or runArgs[3] == "left" or
  125.       runArgs[3] == "right" or runArgs[3] == "top" or runArgs[3] == "bottom" then
  126.       outputGate = peripheral.wrap(runArgs[3])
  127.       print("OutputGate set to "..runArgs[3])
  128.     else
  129.       -- Invalid position, has to be input manually
  130.       print("Invalid position, input manually")
  131.       getOutputGate()
  132.     end
  133.   end
  134. end
  135.  
  136. -- Function to detect peripherals --
  137. function detectModem()
  138.   print("Detecting Modem")
  139.   for i, side in pairs(rs.getSides()) do
  140.     if peripheral.isPresent(side) then
  141.       if peripheral.getType(side) == "modem" then
  142.         print("Modem set to "..side.." side")
  143.         modem = side
  144.       end
  145.     end
  146.   end
  147. end
  148.  
  149. -- Send Cell info and wait for confirmation --
  150. function sendCell(times)
  151.   done = false
  152.   ID, msg = rednet.receive(1)
  153.   if ID == hostID and msg == "Cell" then
  154.     done = true
  155.     print("Request for Cell received")
  156.     rednet.send(hostID, cell)
  157.     print("Cell info sent")
  158.   elseif times == 4 then
  159.     print("Timeout, abort")
  160.   else
  161.     print("No answer or wrong sender, try again")
  162.     sendCell(times + 1)
  163.   end
  164.   return done
  165. end
  166.  
  167. -- Confirm the host is trying to connect with this computer --
  168. function confirmConnect()
  169.   confirmation = false
  170.   ID, msg = rednet.receive(3)
  171.   if ID == hostID then
  172.     if msg == "Standby" then
  173.       print("Connection is a go, begin connection")
  174.       confirmation = true
  175.     end
  176.   elseif ID == nil then
  177.     -- No answer, another computer got the confirmation
  178.     print("No answer, aborting")   
  179.   else
  180.     -- Someone else sent the message, try again
  181.     confirmConnect()
  182.   end
  183.   return confirmation
  184. end
  185.  
  186. -- Function to send info to the host, and confirm it has been set up properly --
  187. function connect(ID)
  188.   hostID = ID
  189.   rednet.send(hostID, "Here")
  190.   print("Replied to call, waiting for instructions")
  191.   if confirmConnect() then
  192.     if sendCell(1) then
  193.       connected = true
  194.     end
  195.   end
  196.  
  197.   if connected then
  198.     print("Connection completed")
  199.   else
  200.     print("Connection interrupted, waiting for new call")
  201.   end
  202. end
  203.  
  204. -- Set up the monitoring computer --
  205. function setup()
  206.   -- Get input from the user
  207.   getInput()
  208.   -- Detect peripherals
  209.   detectModem()
  210.   if modem == nil then
  211.     print("No modem detected, please attach a modem and try again")
  212.     error()
  213.   end
  214.   -- Open the modem --
  215.   rednet.open(modem)
  216.   print("Modem open and ready")
  217.   print("Setup complete")
  218. end
  219.  
  220. -- Get status from first Gate Reader --
  221. function getInputGateStatus()
  222.   returnValue = ""
  223.   data = inputGate.get()
  224.   if data["Full Energy"] then
  225.     returnValue = "Full"
  226.   elseif data["Energy Stored"] then
  227.     returnValue = "Stored"
  228.   else
  229.     returnValue = "Empty"
  230.   end
  231.   print("InputGate status: "..returnValue)
  232.   return returnValue
  233. end
  234.  
  235. -- Get status from second Gate Reader --
  236. function getOutputGateStatus()
  237.   returnValue = ""
  238.   data = outputGate.get()
  239.   if data["Full Energy"] then
  240.     returnValue = "Full"
  241.   elseif data["Energy Stored"] then
  242.     returnValue = "Stored"
  243.   else
  244.     returnValue = "Empty"
  245.   end
  246.   print("OutputGate status: "..returnValue)
  247.   return returnValue
  248. end
  249.  
  250. -- Function to update status --
  251. function updateStatus()
  252.   print("Calculating status")
  253.   -- Get status from both Gate Readers and update status
  254.   outStatus = getOutputGateStatus()
  255.   inStatus = getInputGateStatus()
  256.   if outStatus == "Full" and inStatus == "Full" then
  257.     status = 100
  258.   elseif outStatus == "Full" and inStatus == "Stored" then
  259.     status = 80
  260.   elseif outStatus == "Full" and inStatus == "Empty" then
  261.     status = 60
  262.   elseif outStatus == "Stored" and inStatus == "Full" then
  263.     status = 60
  264.   elseif outStatus == "Stored" and inStatus == "Stored" then
  265.     status = 40
  266.   elseif outStatus == "Stored" and inStatus == "Empty" then
  267.     status = 20
  268.   elseif outStatus == "Empty" and inStatus == "Full" then
  269.     status = nil
  270.   elseif outStatus == "Empty" and inStatus == "Stored" then
  271.     status = 10
  272.   else
  273.     status = 0
  274.   end
  275.   print("Status: "..status)
  276. end
  277.  
  278. --Function to send status and receive confirmation --
  279. function sendStatus(times)
  280.   print("Sending status")
  281.   updateStatus()
  282.   rednet.send(hostID, status)
  283.   ID, msg = rednet.receive(1)
  284.   if ID == hostID and msg == "Thanks" then
  285.     print("Status sent and received")
  286.   elseif times == 4 then
  287.     print("Timeout, abort")
  288.   else
  289.     print("No answer or wrong sender, try again")
  290.     sendStatus(times + 1)
  291.   end
  292. end
  293.  
  294. -- Clear terminal and run setup before entering the main loop --
  295. term.clear()
  296. term.setCursorPos(1,1)
  297. print("Running BatteryOS v1.1")
  298. setup()
  299.  
  300. -- Main loop to handle incoming unscheduled messages --
  301. while true do
  302.   if change then
  303.     print("Main loop entered, waiting for message")
  304.     change = false
  305.   end
  306.   ID, msg = rednet.receive()
  307.   if msg == "Calling" then
  308.     if not connected then
  309.       print("Call received, initiate connection")
  310.       connect(ID)
  311.       change = true
  312.     end
  313.   elseif ID == hostID and msg == "Status" then
  314.     print("Request for status received")
  315.     sendStatus(1)
  316.     change = true
  317.   elseif msg == "Reboot" then
  318.     print("Reboot order received, rebooting")
  319.     rednet.close(modem)
  320.     os.reboot()
  321.   else
  322.     print("Other message received: "..msg)
  323.     change = true
  324.   end
  325. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement