Grauly

CC_IEC-OS energybank v1

Apr 15th, 2021 (edited)
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.14 KB | None | 0 0
  1. local screenSide = "front"
  2. local modemSide = "back"
  3. local capacitorSide = "left"
  4. local capLabel = "Capacitor"
  5. local JoulesToRF = 0.4 --this value is the multiplicator for Energy readouts
  6. local IECNET = true
  7. -- end of config
  8. local args = {...}
  9.  
  10. --IECNET setup
  11. local gpsEnabled = false
  12. local net = "Energynet"
  13. local host = 0
  14. function iecnetSetup()
  15.     if (IECNET) then
  16.         if (peripheral.isPresent(modemSide)) then
  17.             if (peripheral.getType(modemSide) == "modem") then
  18.                 --peripheral is present, starting IECNET
  19.                 rednet.open(modemSide)
  20.                 host = rednet.lookup(net,"IEC-Host")
  21.                 if (host == nil) then
  22.                     print(net.." does not have a Host or Host is Offline, broadcasts will go unheard.")
  23.                     IECNET = false
  24.                 end
  25.                 x,y,z = gps.locate()
  26.                 if (x == nil) then
  27.                     print("GPS not available, will not send Location Data")
  28.                 else
  29.                     gpsEnabled = true;
  30.                 end
  31.                 rednet.broadcast("Online",net)
  32.             else
  33.                 print("IECNET was enabled, but no modem was found.")
  34.                 IECNET = false
  35.             end
  36.         else
  37.             print("IECNET was enabled, but no modem was found.")
  38.             IECNET = false
  39.         end
  40.     end
  41. end
  42.  
  43. function getLocation()
  44.     x,y,z = gps.locate()
  45.     if (x == nil) then
  46.         gpsEnabled = false
  47.         return nil
  48.     else
  49.         if (not gpsEnabled) then
  50.             gpsEnabled = true
  51.         end
  52.         x = math.floor(x+0.5)
  53.         y = math.floor(y+0.5)
  54.         z = math.floor(z+0.5)
  55.         return tostring(x).." "..tostring(y).." "..tostring(z)
  56.     end
  57. end
  58.  
  59. --end of IECNET primary functions
  60. hasScreen = false
  61. mon = nil
  62. bank = nil
  63. function peripheralSetup()
  64.     if (peripheral.isPresent(screenSide)) then
  65.         if (peripheral.getType(screenSide) == "monitor") then
  66.             print("Screen found on side: \""..screenSide.."\" running with screen.")
  67.             mon = peripheral.wrap(screenSide)
  68.             x,y = mon.getSize()
  69.             if (x < 16 or y < 12) then
  70.                 print("Screen does not meet the minimum size: [16 : 12], current size: ["..x.." : "..y.."] ,running screenless")
  71.                 hasScreen = false
  72.             else
  73.                 hasScreen = true
  74.             end
  75.         else
  76.             print("Peripheral is present on side: \""..screenSide.."\" but this is not a monitor.")
  77.         end
  78.     else
  79.         print("No screen was found, running screenless.")
  80.     end
  81.     if (peripheral.isPresent(capacitorSide)) then
  82.         if (peripheral.getType(capacitorSide) == "Induction Matrix") then
  83.             print("Capacitor found on side: \""..capacitorSide.."\"!")
  84.             bank = peripheral.wrap(capacitorSide)
  85.         else
  86.             error("Peripheral is present on side: \""..capacitorSide.."\" but this is not a valid capacitor.")
  87.         end
  88.     else
  89.         error("No capacitor was found on the specified side, exiting.")
  90.     end
  91. end
  92.  
  93. function nextLine()
  94.     x,y = mon.getCursorPos()
  95.     mon.setCursorPos(1,y+1)
  96. end
  97.  
  98. function divider()
  99.     x,y = mon.getSize()
  100.     mon.write("+")
  101.     for i=1,(x-2),1 do
  102.         mon.write("-")
  103.     end
  104.     mon.write("+")
  105. end
  106.  
  107. function screenHead(label)
  108.     x,y = mon.getSize()
  109.     mon.write("+-["..label.."]")
  110.     length = 4+ string.len(label)+1
  111.     for i=1,(x-length),1 do
  112.         mon.write("-")
  113.     end
  114.     mon.write("+")
  115.     nextLine()
  116. end
  117.  
  118. function head()
  119.     mon.setCursorPos(1,1)
  120.     x,y = mon.getSize()
  121.     mon.write("+")
  122.     for i=1,(x-13)/2,1 do
  123.         mon.write("-")
  124.     end
  125.     mon.write("[")
  126.     mon.setTextColor(colors.yellow)
  127.     mon.write("IEC-OS.aic")
  128.     mon.setTextColor(colors.white)
  129.     mon.write("]")
  130.     for i=1,(x-13)/2,1 do
  131.         mon.write("-")
  132.     end
  133.     mon.write("+")
  134. end
  135.  
  136. function foot()
  137.     x,y = mon.getSize()
  138.     mon.setCursorPos(1,y)
  139.     mon.write("+")
  140.     for i=1,x-(12),1 do
  141.         mon.write("-")
  142.     end
  143.     mon.write("[")
  144.     if (IECNET) then   
  145.         mon.setTextColor(colors.green)
  146.         mon.write("Online")
  147.         mon.setTextColor(colors.white)
  148.         mon.write("]--+")
  149.     else
  150.         mon.setTextColor(colors.red)
  151.         mon.write("Offline")
  152.         mon.setTextColor(colors.white)
  153.         mon.write("]-+")
  154.     end
  155. end
  156.  
  157. function functionals()
  158.     mon.clear()
  159.     head()
  160.     foot()
  161.     mon.setCursorPos(1,2)
  162. end
  163.  
  164. function round(num, numDecimalPlaces)
  165.   local mult = 10^(numDecimalPlaces or 0)
  166.   return math.floor(num * mult + 0.5) / mult
  167. end
  168.  
  169. function getPercentFilled()
  170.     return math.floor((((bank.getEnergy()*JoulesToRF)/(bank.getMaxEnergy()*JoulesToRF))*100)*10)/10
  171. end
  172.  
  173. function printPercentFilled(startLine)
  174.     percent = getPercentFilled()
  175.     mon.setCursorPos(4,startLine)
  176.     mon.write("Filled to: ")
  177.     percent = getPercentFilled()
  178.     if (percent > 30) then
  179.         mon.setTextColor(colors.green)
  180.     elseif (percent > 15) then
  181.         mon.setTextColor(colors.yellow)
  182.     elseif (percent > 7) then
  183.         mon.setTextColor(colors.orange)
  184.     else
  185.         mon.setTextColor(colors.red)
  186.     end
  187.     mon.setCursorPos(4,startLine+1)
  188.     mon.write(percent)
  189.     mon.setTextColor(colors.white)
  190.     mon.write("%")
  191.     mon.setCursorPos(4,startLine+2)
  192. end
  193.  
  194. function getBankIO()
  195.     return math.floor((bank.getInput()*JoulesToRF - bank.getOutput()*JoulesToRF)*100)/100
  196. end
  197.  
  198. function getBankIOWithUnit(bankIO)
  199.     unit = "RF/t"
  200.     inverted = false
  201.     if (bankIO < 0) then
  202.         bankIO = bankIO*-1
  203.         inverted = true
  204.     end
  205.     if ((bankIO/1000) > 1) then
  206.         unit = "kRF/t"
  207.         bankIO = round(bankIO/1000,3)
  208.         if ((bankIO/1000) > 1) then
  209.             unit = "MRF/t"
  210.             bankIO = round(bankIO/1000,3)
  211.             if ((bankIO/1000) > 1) then
  212.                 unit = "GRF/t"
  213.                 bankIO = round(bankIO/1000,2)
  214.                 if ((bankIO/1000) > 1) then
  215.                     unit = "TRF/t"
  216.                     bankIO = round(bankIO/1000,2)
  217.                     if ((bankIO/1000) > 1) then
  218.                         unit = "PRF/t"
  219.                         bankIO = round(bankIO/1000,2)
  220.                     end
  221.                 end
  222.             end
  223.         end
  224.     end
  225.     if (inverted) then
  226.         bankIO = bankIO*-1
  227.     end
  228.     return bankIO,unit
  229. end
  230.  
  231. function printIO(startLine)
  232.     bankIO = getBankIO()
  233.     mon.setCursorPos(4,startLine)
  234.     mon.write("I/O:")
  235.     if (bankIO > 0) then
  236.         mon.setTextColor(colors.green)
  237.     elseif (bankIO < 0) then
  238.         mon.setTextColor(colors.red)
  239.     else
  240.         mon.setTextColor(colors.blue)
  241.     end
  242.     mon.setCursorPos(4,startLine+1)
  243.     bankIO,unit = getBankIOWithUnit(bankIO)
  244.     mon.write(bankIO)
  245.     mon.setTextColor(colors.white)
  246.     mon.write(" "..unit)
  247.     mon.setCursorPos(4,startLine+3)
  248. end
  249.  
  250. function getETA()
  251.     bankIO = getBankIO()
  252.     sign = ""
  253.     eta = 0
  254.     if (bankIO > 0) then
  255.         sign = "+"
  256.         eta = math.floor((round(((bank.getMaxEnergy()*JoulesToRF) - (bank.getEnergy()*JoulesToRF)) / bankIO)/20)*10)/10 --time in seconds untill bank is filled up
  257.     elseif (bankIO < 0) then
  258.         sign = "-"
  259.         bankIO = bankIO*-1
  260.         eta = math.floor(((round(bank.getEnergy()*JoulesToRF / bankIO)/20)*10)/10) --time in seconds untill bank is empty
  261.     else
  262.         sign = "="
  263.     end
  264.     days = 0
  265.     hours = 0
  266.     minutes = 0
  267.     seconds = eta
  268.     if (seconds > 60) then
  269.         minutes = math.floor(seconds/60)
  270.         seconds = math.floor(seconds % 60)
  271.     end
  272.     if (minutes > 60) then
  273.         hours = math.floor(minutes/60)
  274.         minutes = minutes % 60
  275.     end
  276.     if (hours > 24) then
  277.         days = math.floor(hours/24)
  278.         hours = hours % 24
  279.     end
  280.     s = days.."d"..hours.."h"..minutes.."m"..seconds.."s"
  281.     if (days <= 0) then
  282.         s = hours.."h"..minutes.."m"..seconds.."s"
  283.         if (hours <= 0) then
  284.             s = minutes.."m"..seconds.."s"
  285.             if (minutes <= 0) then
  286.                 s = seconds.."s"
  287.             end
  288.         end
  289.     end
  290.     return sign,s
  291. end
  292.  
  293. function printETA(startLine)
  294.     sign,eta = getETA()
  295.     mon.setCursorPos(4,startLine)
  296.     if (sign == "+") then
  297.         mon.write("Full in:")
  298.         mon.setTextColor(colors.white)
  299.     elseif (sign == "-") then
  300.         mon.write("Empty in:")
  301.         mon.setTextColor(colors.red)
  302.     else
  303.         mon.write("Full in:")
  304.         mon.setCursorPos(4,startLine+1)
  305.         if( getPercentFilled() == 100) then
  306.             eta = "Overfilling!"
  307.         else
  308.             eta = "No I/O"
  309.         return
  310.         end
  311.     end
  312.     mon.setCursorPos(4,startLine+1)
  313.     mon.write(eta)
  314.     mon.setTextColor(colors.white)
  315.     mon.setCursorPos(4,startLine+2)
  316. end
  317.  
  318. function printMainBar(startLine)
  319.     x = 2
  320.     mon.setCursorPos(x,startLine)
  321.     filledPercent = getPercentFilled()
  322.     filledPercent = math.ceil(filledPercent/10)
  323.     for i=1,10-filledPercent,1 do
  324.         x,y = mon.getCursorPos()
  325.         mon.write("|")
  326.         mon.setCursorPos(x,y+1)
  327.     end
  328.     for i=1,filledPercent,1 do
  329.         x,y = mon.getCursorPos()
  330.         mon.write("#")
  331.         mon.setCursorPos(x,y+1)
  332.     end
  333. end
  334.  
  335. function readouts(startLine)
  336.     mon.setCursorPos(4,startLine)
  337.     printPercentFilled(startLine)
  338.     nextLine()
  339.     x,y = mon.getCursorPos()
  340.     printIO(y)
  341.     nextLine()
  342.     x,y = mon.getCursorPos()
  343.     printETA(y)
  344.     printMainBar(startLine)
  345. end
  346.  
  347. --rednet handeling
  348. local ticksSinceLastPing = 0
  349. function rednetHandeling(sender,message,prot)
  350.     if (message == "pong" and prot == net) then --online checker
  351.         ticksSinceLastPing = 0
  352.         return
  353.     end
  354. end
  355.  
  356. function routine()
  357.     if (hasScreen) then
  358.         functionals()
  359.         readouts(2)
  360.     end
  361.     if (ticksSinceLastPing > 5) then
  362.         IECNET = false
  363.     else
  364.         IECNET = true
  365.     end
  366.     if (IECNET) then
  367.         message = {}
  368.         message["online"] = true
  369.         message["label"] = capLabel
  370.         sign, eta = getETA()
  371.         message["eta"] = eta
  372.         message["sign"] = sign
  373.         bankIO,unit = getBankIOWithUnit(getBankIO())
  374.         message["bankIO"] = bankIO
  375.         message["unit"] = unit
  376.         message["fillstat"] = getPercentFilled()
  377.         rednet.broadcast(message,net)
  378.     end
  379.     rednet.broadcast("ping",net) --ping the network to see if it is online
  380. end
  381.  
  382.  
  383. iecnetSetup()
  384. peripheralSetup()
  385.  
  386. local timer = os.startTimer(1)
  387.  
  388. while true do
  389.     event,a1,a2,a3 = os.pullEvent()
  390.     if (event == "timer" and a1 == timer) then
  391.         ticksSinceLastPing = ticksSinceLastPing +1
  392.         routine()
  393.         timer = os.startTimer(1)
  394.     elseif (event == "rednet_message") then
  395.         rednetHandeling(a1,a2,a3)
  396.     end
  397. end
Add Comment
Please, Sign In to add comment