Advertisement
Masserio1

BatteryHost

Sep 27th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.07 KB | None | 0 0
  1. --[[
  2.  BatteryHost v1.1 by Masserio
  3.   ____        _   _                  _    _           _  
  4.  |  _ \      | | | |                | |  | |         | |  
  5.  | |_) | __ _| |_| |_ ___ _ __ _   _| |__| | ___  ___| |_
  6.  |  _ < / _` | __| __/ _ \ '__| | | |  __  |/ _ \/ __| __|
  7.  | |_) | (_| | |_| ||  __/ |  | |_| | |  | | (_) \__ \ |_
  8.  |____/ \__,_|\__|\__\___|_|   \__, |_|  |_|\___/|___/\__|
  9.                                 __/ |                    
  10.                                |___/                      
  11.  
  12.  This is a program that collects information from several computers
  13.  running BatteryOS (see list below), analyses the data, and sends
  14.  status updates and information to a computer running either
  15.  EngineOS or MonitorOS (again, list below).
  16.  
  17.  This is one part in a series of programs, and this will not work as
  18.  intended on it's own. If you're planning on using this program please
  19.  check out the following programs as well, as they all communicate:
  20.  -BatteryOS v1.1 Pastebin: 3xja4QFK (Stable)
  21.    Monitors Redstone Energy Cells and sends status messages
  22.  -MonitorOS v1.0 Pastebin: HpFunt8W (Stable)
  23.    Updating statuses on a big screen
  24.  -EngineOS v1.0 Pastebin: PYRX7G2t (Stable)
  25.    Simple engine control which also sends operational status
  26.  
  27.  Feel free to copy parts or this whole code, and change it as you like!
  28. --]]
  29.  
  30. -- Set up some variables to be used throughout the programme --
  31. computers = {} -- Table with all known computers
  32. modem = nil -- Modem side
  33. cellStatus = {} -- Cell statuses
  34. powerOutput = 0 -- Maximum power output from Cells
  35. powerEstimate = 0 -- Estimate of how much power is stored in million MJ
  36. monitorID = nil -- ID of the computer that runs MonitorOS
  37. engineID = nil -- ID of the computer that runs EngineOS
  38. statusUpdateTimer = 0 -- To keep track of time between status updates
  39. computerAmount = 0 -- Amount of computers connected
  40.  
  41. -- Function to detect peripherals --
  42. function detectModem()
  43.   for i, side in pairs(rs.getSides()) do
  44.     if peripheral.isPresent(side) then
  45.       if peripheral.getType(side) == "modem" then
  46.         modem = side
  47.       end
  48.     end
  49.   end
  50.   if modem == nil then
  51.     print("No modem connected, attach a modem and try again")
  52.     error()
  53.   end
  54. end
  55.  
  56. -- Add a computer to the table --
  57. function addComputer(ID, status, cell)
  58.   comp = {ID, status, cell}
  59.   -- Computers are stores in the table based on cell nr
  60.   table.insert(computers, cell, comp)
  61.   computerAmount = computerAmount + 1
  62. end
  63.  
  64. -- Get new computer info
  65. function connectComputer(compID)
  66.   cell = nil
  67.   status = nil
  68.   rednet.send(compID, "Cell")
  69.   ID, msg = rednet.receive(1)
  70.   if ID == compID then
  71.     cell = msg
  72.     rednet.send(compID, "Status")
  73.     ID, msg = rednet.receive(1)
  74.     if ID == compID then
  75.       status = msg
  76.       rednet.send(compID, "Thanks")
  77.     else
  78.       print("Wrong ID or timeout while getting status")
  79.     end
  80.   else
  81.     print("Wrong ID or timeout while getting Cell")  
  82.   end
  83.   if cell ~= nil and status ~= nil then
  84.     addComputer(compID, status, cell)
  85.     print("Computer added")
  86.   else
  87.     print("Did not receive all the information, aborting process")
  88.   end
  89. end
  90.  
  91. -- Check for more computers that are not connected
  92. function checkForNew(times)
  93.   if computerAmount < 16 then
  94.     rednet.broadcast("Calling")
  95.     ID, msg = rednet.receive(1)
  96.     if msg == "Here" then
  97.       print("New computer found with ID: "..ID)
  98.       sleep(0.5) -- Give it time to avoid interference
  99.       rednet.send(ID, "Standby")
  100.       connectComputer(ID)
  101.       checkForNew(1)
  102.     elseif times == 10 then
  103.       print("Giving up, found "..computerAmount.." computers total")
  104.     else
  105.       checkForNew(times + 1)
  106.     end
  107.   else
  108.     print("Computers reached "..computerAmount)
  109.   end
  110. end
  111.  
  112. -- Request status from a ccomputer --
  113. function requestStatus(compID)
  114.   status = nil
  115.   rednet.send(compID, "Status")
  116.   ID, msg = rednet.receive(1)
  117.   if ID ~= compID and ID ~= nil then
  118.     print("Wrong computer answered, trying again")
  119.     requestStatus(compID)
  120.   elseif ID == nil then
  121.     -- No answer, do nothing
  122.   else
  123.     status = msg
  124.     rednet.send(compID, "Thanks")
  125.   end
  126.   return status
  127. end
  128.  
  129. -- Update Cell statuses --
  130. function updateCellStatus()
  131.   output = 0
  132.   estimate = 0
  133.   for i=1, 16 do
  134.     comp = computers[i]
  135.     if comp ~= nil then
  136.       status = comp[2]
  137.       cellStatus[comp[3]] = status
  138.       if status == 100 then
  139.         output = output + 100
  140.         estimate = estimate + 1.8
  141.       elseif status == 80 then
  142.         output = output + 100
  143.         estimate = estimate + 1.5
  144.       elseif status == 60 then
  145.         output = output + 100
  146.         estimate = estimate + 1.0
  147.       elseif status == 40 then
  148.         output = output + 100
  149.         estimate = estimate + 0.8
  150.       elseif status == 20 then
  151.         output = output + 100
  152.         estimate = estimate + 0.4
  153.       elseif status == 10 then
  154.         estimate = estimate + 0.1
  155.       else
  156.         -- Both output and estimate 0, add nothing
  157.       end
  158.     end
  159.   end
  160.   powerOutput = output
  161.   powerEstimate = estimate
  162.   print("Updated cell statuses")
  163. end
  164.  
  165. -- Get statuses from all computers --
  166. function getStatuses()
  167.   -- Go trough all computers
  168.   for i=1, 16 do
  169.     -- Get the computers status
  170.     comp = computers[i]
  171.     if comp ~= nil then
  172.       status = requestStatus(comp[1])
  173.       -- Update the status, and the table of computers
  174.       comp[2] = status
  175.       computers[i] = comp
  176.     end
  177.   end
  178.   updateCellStatus()
  179.   print("Status gathering complete")
  180. end
  181.  
  182. -- Setup modem etc --
  183. function setup()
  184.   detectModem()
  185.   rednet.open(modem)
  186.   checkForNew(1)
  187.   getStatuses()
  188.   print("Max Power Output:  "..powerOutput)
  189.   print("Estimated power storage: "..powerEstimate.."million MJ")
  190.   print("Setup complete")
  191. end
  192.  
  193. -- Send individual cell status (can't send table via modem) --
  194. function sendOneCellStatus(status)
  195.   done = false
  196.   rednet.send(monitorID, status)
  197.   ID, msg = rednet.receive(1)
  198.   if ID == monitorID and msg == "Thanks" then
  199.     done = true
  200.     rednet.send(monitorID, "Next") -- Prepare the pc for the next status
  201.   end
  202.   return done
  203. end
  204.  
  205. -- Send status of the cells --
  206. function sendCellStatuses()
  207.   print("Request for cell statuses received")
  208.   for i=1, 16 do
  209.     comp = computers[i]
  210.     status = nil
  211.     if comp ~= nil then
  212.       status = comp[2]
  213.     else
  214.       -- No cell registered
  215.       status = "Err"
  216.     end
  217.     if not sendOneCellStatus(status) then
  218.       print("Error in sending cell status, abort")
  219.       break
  220.     end
  221.   end
  222.   rednet.send(monitorID, "Done")
  223.   print("All cell statuses sent")
  224. end
  225.  
  226. -- Send current max power output --
  227. function sendPowerOutput(compID, times)
  228.   print("Request for max power output received by "..compID)
  229.   rednet.send(compID, powerOutput)
  230.   ID, msg = rednet.receive(1)
  231.   if ID == nil then
  232.     print("No answer, abort")
  233.   elseif ID ~= compID then
  234.     print("Wrong pc answered, try again")
  235.     sendPowerOutput(times + 1)
  236.   end
  237.   return times
  238. end
  239.  
  240. -- Send the estimated amount of power stored --
  241. function sendPowerEstimate(compID, times)
  242.   print("Request for power estimate received by "..compID)
  243.   rednet.send(compID, powerEstimate)
  244.   ID, msg = rednet.receive(1)
  245.   if ID == nil then
  246.     print("No answer, abort")
  247.   elseif ID ~= compID then
  248.     print("Wrong pc answered, try again")
  249.     sendPowerEstimate(compID, times + 1)
  250.   end
  251.   return times
  252. end
  253.  
  254. -- Connect to monitor pc or engine pc --
  255. function connect(compID)
  256.   rednet.send(ID, "Here")
  257.   ID, msg = rednet.receive(1)
  258.   if ID == compID and msg == "Monitor" then
  259.     monitorID = ID
  260.     rednet.send(ID, "Thanks")
  261.     -- reset the update timer to synch with MonitorOS
  262.     statusUpdateTimer = 0
  263.     print("Monitor computer found with ID: "..ID)
  264.   elseif ID == compID and msg == "Engine" then
  265.     engineID = ID
  266.     rednet.send(ID, "Thanks")
  267.     print("Engine computer found with ID: "..ID)
  268.   else
  269.     print("Wrong or no answer during connection, abort")
  270.   end
  271. end
  272.  
  273. -- Do some stuff before the main loop starts --
  274. term.clear()
  275. term.setCursorPos(1,1)
  276. print("Running BatteryHost v1.0")
  277. setup()
  278.  
  279. -- Main loop --
  280. while true do
  281.   ID, msg = rednet.receive(10)
  282.   if msg == "Host" then
  283.     connect(ID)
  284.   elseif ID == monitorID and msg == "Status" then
  285.     sendCellStatuses()
  286.     statusUpdateTimer = 0
  287.   elseif (ID == monitorID or ID == engineID) and msg == "Output" then
  288.     sendPowerOutput(ID, 1)
  289.     if ID == monitorID then statusUpdateTimer = 0 end
  290.   elseif (ID == monitorID or ID == engineID) and msg == "Power" then
  291.     sendPowerEstimate(ID, 1)
  292.     if ID == monitorID then statusUpdateTimer = 0 end
  293.   elseif ID ~= nil then
  294.     if msg == "Reboot" then
  295.       rednet.close(modem)
  296.       os.reboot()
  297.     else
  298.       print("Unknown message from ID"..ID..":")
  299.       print(msg)
  300.     end
  301.   end
  302.   if statusUpdateTimer == 8 then -- This makes it about 3 minutes between status updates
  303.     getStatuses()
  304.     statusUpdateTimer = 0
  305.     print("Max Power Output:  "..powerOutput)
  306.     print("Estimated power storage: "..powerEstimate.."million MJ")
  307.   else
  308.     statusUpdateTimer = statusUpdateTimer + 1
  309.     print("Idle "..statusUpdateTimer)
  310.   end
  311. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement