Advertisement
logit667

PowerProbe

Jul 24th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.24 KB | None | 0 0
  1. --x,y,z= gps.locate(5)
  2. function RANDBI()
  3.     NUM1 = math.random(2)
  4.     return NUM1
  5. end
  6.            
  7. function netcheck()
  8.     while (DR==false) do
  9.         --term.clear()
  10.         rednet.open("top")
  11.         rednet.broadcast("0"..",".."0"..",".."0")
  12.         term.setCursorPos(1,4)
  13.         print("Sent Announcement")
  14.         term.setCursorPos(1,5)
  15.         print("Waiting")
  16.         term.setCursorPos(1,6)
  17.         print("                                                 ")
  18.         term.setCursorPos(1,7)
  19.         print("                                                 ")
  20.         term.setCursorPos(1,8)
  21.         print("                                                 ")
  22.         term.setCursorPos(1,9)
  23.         print("                                                 ")
  24.         senderID, message = rednet.receive(1)
  25.         if(senderID==nil)then
  26.             DR = false
  27.             term.setCursorPos(1,6)
  28.             print("No Reply Trying Again")
  29.         else
  30.             if(message==ID..",OK") then
  31.                 DR = true
  32.                 term.setCursorPos(1,6)
  33.                 print("Reply Received From: "..senderID.." Message: "..message)
  34.                 controllerid = senderID
  35.             else
  36.                 DR = false
  37.                 term.setCursorPos(1,6)
  38.                 print("Reply But Not Me. Trying Again")
  39.             end
  40.         end
  41.         rednet.close("top")
  42.         os.sleep(RANDBI())
  43.     end
  44. end
  45. function cellcheck()
  46.     CellMax = 0
  47.     CellStored = 0
  48.     CellPercent = 0
  49.    
  50.     for i = 1, #PowerBlocks do
  51.         cell = peripheral.wrap(PowerBlocks[i])
  52.         CellMax = CellMax + cell.getMaxEnergyStored()
  53.         CellStored = CellStored + cell.getEnergyStored()
  54.         print (PowerBlocks[i] .." = "..cell.getEnergyStored().."/"..cell.getMaxEnergyStored().."/"..math.floor(100*cell.getEnergyStored()/cell.getMaxEnergyStored()).."%                   ")
  55.         --print (cell.getEnergyStored())
  56.        
  57.         os.sleep(.01)
  58.     end
  59.     CellPercent = math.floor(100*CellStored/CellMax)
  60.  
  61.     if (CellPercent <= 25) then
  62.         mon.setBackgroundColor(colors.red)
  63.         mon.clear()
  64.    
  65.     elseif (CellPercent <= 50) then
  66.         mon.setBackgroundColor(colors.yellow)
  67.         mon.clear()
  68.    
  69.     elseif (CellPercent <= 75) then
  70.         mon.setBackgroundColor(colors.blue)
  71.         mon.clear()
  72.    
  73.     elseif (CellPercent <= 100) then
  74.         mon.setBackgroundColor(colors.green)
  75.         mon.clear()
  76.     end
  77.    
  78. end
  79. term.clear()
  80. PowerBlocks = {}
  81.  
  82. term.setCursorPos(1,1)
  83. print ("Building Local Power network =")
  84. term.setCursorPos(31,1)
  85. os.sleep(1)
  86. --Build Power array off of wired net
  87. --===============================================
  88. for _, name in pairs(peripheral.getNames()) do
  89.     if peripheral.getType(name) == "powered_tile" then --# change "power_tille" to your peripheral type.
  90.         table.insert(PowerBlocks, name)
  91.     end
  92. end
  93. --===============================================
  94. print (" Done #"..#PowerBlocks)
  95. os.sleep(2)
  96. mon = peripheral.wrap("right")
  97. CellStored = 0
  98. CellMax = 0
  99. CellPercent = 0
  100. controllerid = 0
  101. CounterCT=0
  102. ID = os.getComputerID()
  103. DR = false
  104. term.clear()
  105. while (true) do
  106. --term.clear()
  107.     if (DR~=true)then
  108.         term.clear()
  109.         term.setCursorPos(1,2)
  110.         print("this might take a few mins")
  111.         print("Connecting to Network =")
  112.         netcheck()
  113.         term.setCursorPos(24,3)
  114.         print (" Done")
  115.         os.sleep(1)
  116.         term.clear()
  117.     end
  118.     rednet.open("top")
  119.     --term.clear()
  120.     term.setCursorPos(1,1)
  121.     --print("Waiting-Tries:"..CounterCT)
  122.     senderID, message = rednet.receive(2)
  123.     if (senderID ~= nil)then
  124.         if (senderID==controllerid)then
  125.             print("My Traffic")
  126.             if(message==ID..",Update"==true)then
  127.                 print("Send Update")
  128.                 cellcheck()
  129.                 print("Sent:"..CellPercent.." "..CellStored.."/"..CellMax)
  130.                 rednet.broadcast(CellPercent..","..CellStored..","..CellMax)
  131.                 --print(CellFull)
  132.                 Rid, Mess = rednet.receive(2)
  133.                 rednet.close("top")
  134.                 CounterCT=0
  135.                 os.sleep(0.5)
  136.             elseif(message==ID..",Reboot"==true)then   
  137.                 print("Rebooting")
  138.                 rednet.broadcast("RemoveNow")
  139.                 Rid, Mess = rednet.receive(2)
  140.                 --rednet.send(controllerid,"RemoveNow")
  141.                 os.sleep(1)
  142.                 rednet.close("top")
  143.                 os.sleep(2)
  144.                 os.reboot()
  145.             else
  146.                
  147.             end
  148.         else
  149.             term.setCursorPos(1,1)
  150.             print("Not My Traffic")
  151.         end
  152.     else
  153.         term.setCursorPos(1,1)
  154.         print("No Com Traffic: "..CounterCT.."                                           ")
  155.        
  156.         cellcheck()
  157.         print(CellStored.."/"..CellMax.."/"..CellPercent.."%                                    ")
  158.         if (CounterCT == 25)then
  159.             DR=false
  160.             CounterCT=0
  161.             term.clear()
  162.             rednet.close("top")
  163.         end
  164.         CounterCT=CounterCT+1
  165.     end
  166.    
  167.     --os.sleep(0.1)
  168. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement