Advertisement
logit667

PowerProbe2

Aug 3rd, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.90 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.     term.redirect(mon)
  61.     local w, h = term.getSize()
  62.    
  63.     bar1 = ""
  64.     bar1 = string.rep("#",w)
  65.     bar2 = ""
  66.     bar2 = string.rep(" ",w)
  67.     for i =1, w do
  68.        
  69.     end
  70.    
  71.     --onechunk = math.floor(30 / 100)
  72.    
  73.     seg = math.floor(h/100)
  74.     barleng = h
  75.     filledleng = math.floor(barleng * CellStored / CellMax)
  76.     pos = h
  77.     curpos = 0
  78.     for i = 1, h do
  79.        
  80.         if i <= filledleng then
  81.             term.setCursorPos(1,pos+1)
  82.             term.setTextColor( colors.green )
  83.             print (bar1)
  84.             pos = pos -1
  85.         else
  86.             term.setCursorPos(1,pos+1)
  87.             term.setTextColor( colors.black )
  88.             print (bar2)
  89.             pos = pos -1
  90.            
  91.         end
  92.     end
  93.    
  94.     term.redirect(term.native())
  95.    
  96.     -- if (CellPercent <= 25) then
  97.         -- mon.setBackgroundColor(colors.red)
  98.         -- mon.clear()
  99.    
  100.     -- elseif (CellPercent <= 50) then
  101.         -- mon.setBackgroundColor(colors.yellow)
  102.         -- mon.clear()
  103.    
  104.     -- elseif (CellPercent <= 75) then
  105.         -- mon.setBackgroundColor(colors.blue)
  106.         -- mon.clear()
  107.    
  108.     -- elseif (CellPercent <= 100) then
  109.         -- mon.setBackgroundColor(colors.green)
  110.         -- mon.clear()
  111.     -- end
  112.    
  113. end
  114. term.clear()
  115. PowerBlocks = {}
  116.  
  117. term.setCursorPos(1,1)
  118. print ("Building Local Power network =")
  119. term.setCursorPos(31,1)
  120. os.sleep(1)
  121. --Build Power array off of wired net
  122. --===============================================
  123. for _, name in pairs(peripheral.getNames()) do
  124.     if peripheral.getType(name) == "powered_tile" then --# change "power_tille" to your peripheral type.
  125.         table.insert(PowerBlocks, name)
  126.     end
  127. end
  128. --===============================================
  129. print (" Done #"..#PowerBlocks)
  130. os.sleep(2)
  131. mon = peripheral.wrap("right")
  132. ID = os.getComputerID()
  133. CellStored = 0
  134. CellMax = 0
  135. CellPercent = 0
  136. controllerid = 0
  137. CounterCT=0
  138. DR = true
  139. term.clear()
  140. while (true) do
  141. --term.clear()
  142.     if (DR~=true)then
  143.         term.clear()
  144.         term.setCursorPos(1,2)
  145.         print("this might take a few mins")
  146.         print("Connecting to Network =")
  147.         netcheck()
  148.         term.setCursorPos(24,3)
  149.         print (" Done")
  150.         os.sleep(1)
  151.         term.clear()
  152.     end
  153.     rednet.open("top")
  154.     --term.clear()
  155.     term.setCursorPos(1,1)
  156.     --print("Waiting-Tries:"..CounterCT)
  157.     senderID, message = rednet.receive(2)
  158.     if (senderID ~= nil)then
  159.         if (senderID==controllerid)then
  160.             print("My Traffic")
  161.             if(message==ID..",Update"==true)then
  162.                 print("Send Update")
  163.                 cellcheck()
  164.                 print("Sent:"..CellPercent.." "..CellStored.."/"..CellMax)
  165.                 rednet.broadcast(CellPercent..","..CellStored..","..CellMax)
  166.                 --print(CellFull)
  167.                 Rid, Mess = rednet.receive(2)
  168.                 rednet.close("top")
  169.                 CounterCT=0
  170.                 os.sleep(0.5)
  171.             elseif(message==ID..",Reboot"==true)then   
  172.                 print("Rebooting")
  173.                 rednet.broadcast("RemoveNow")
  174.                 Rid, Mess = rednet.receive(2)
  175.                 --rednet.send(controllerid,"RemoveNow")
  176.                 os.sleep(1)
  177.                 rednet.close("top")
  178.                 os.sleep(2)
  179.                 os.reboot()
  180.             else
  181.                
  182.             end
  183.         else
  184.             term.setCursorPos(1,1)
  185.             print("Not My Traffic")
  186.         end
  187.     else
  188.         term.setCursorPos(1,1)
  189.         print("No Com Traffic: "..CounterCT.."                                           ")
  190.        
  191.         cellcheck()
  192.         print(CellStored.."/"..CellMax.."/"..CellPercent.."%                                    ")
  193.         if (CounterCT == 25)then
  194.             DR=true
  195.             CounterCT=0
  196.             term.clear()
  197.             rednet.close("top")
  198.         end
  199.         CounterCT=CounterCT+1
  200.     end
  201.    
  202.     --os.sleep(0.1)
  203. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement