Advertisement
TheProgrammer

Energy Checker with UHSLC (beta)

Aug 23rd, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local uk = "unknown"
  2. local p = peripheral
  3. local cname = "cofh_thermalexpansion_energycell_"
  4. local mname = "monitor_"
  5. local sm = "                                                                     "
  6. local k = 0 --X
  7. local l = 0 --X
  8. local m = 0 --X
  9. local n = 0 --X
  10. local o = 0 --X
  11. local q = 0 --X
  12. local r = 0 --X
  13. local s = 0 --X
  14. local t = 0
  15. local UHSLCmode = false
  16. local num = 0
  17. local energy = 0
  18. local maxenergy = 0
  19. local monitors = {}
  20. local cells = {}
  21. local timeINNum = 0
  22. local midnum = 0
  23. local cme = 0
  24. local ce = 0
  25. local mx = 0
  26. local inStartNum
  27. local inSumNum
  28. local inIDNum
  29. local onUHSLCmsg = "activateUHSLC"
  30. local offUHSLCmsg = "deactivateUHSLC"
  31. local onUHSLCmsgBACKDOOR = "openUHSLCbackdoor_activateUHSLC"
  32. local offUHSLCmsgBACKDOOR = "openUHSLCbackdoor_deactivateUHSLC"
  33.  
  34. function configMonitor()
  35.  while s==0 do
  36.   term.clear()
  37.   term.setCursorPos(1,1)
  38.   local mid = 0
  39.   print("Bitte gebe die aktive(!) Rednet-ID des 3x2 (bxl) Monitors ein!")
  40.   print("Bitte beachte, dass das Programm abstuerzt, wenn es keine Zahl erhaelt!")
  41.   mid = read()
  42.   if tonumber(mid)>=0 then
  43.    mx = mid+midnum
  44.    mon = p.wrap(mname..tonumber(mx))
  45.    mon.clear()
  46.    print("Monitor erfolgreich konfiguriert!")
  47.    os.sleep(1)
  48.    s=1
  49.   else
  50.    print("Behindert?")
  51.    os.sleep(1)
  52.    s=0
  53.   end
  54.  end
  55. end
  56.  
  57. function configCells()
  58.  while l==0 do
  59.   term.clear()
  60.   term.setCursorPos(1,1)
  61.   local cin = ""
  62.   local inStart = ""
  63.   local inSum = ""
  64.   local inID = ""
  65.   print("Willst du den automatischen oder den manuellen Modus für die Konfiguration deiner")
  66.   print("Energiezellen benutzen?")
  67.   print("Fuer den automatischen Modus, gebe 'auto' ein")
  68.   print("Fuer den manuellen Modus, gebe 'manu' ein")
  69.   print("Bitte beachte beim automatischen Modus, dass die IDs nacheinander folgen muessen! (Wie z.B.: 1,2,3,4,5,...)")
  70.   print("(Der autom. Modus ist einfacher, der manuelle Modus ist auch nicht schlecht!)")
  71.   print("HINWEIS: Dieses Programm ist fuer die Energiezellen von Thermal Expansion 4 geschrieben worden!")
  72.   print("(Fuer PROs: Das bedeutet, dass fuer dieses Programm die Standardbezeichnung")
  73.   print("'cofh_thermalexpansion_energycell_' benutzt wird!)")
  74.   print("Bei Problemen entweder den Code aendern oder warten, bis einer was macht!")
  75.   cin = read()
  76.   if cin=="auto" then
  77.    l=1
  78.    term.clear()
  79.    term.setCursorPos(1,1)
  80.    print("Du hast den automatischen Modus gewaehlt")
  81.    os.sleep(1)
  82.    print("Gebe nun die niedrigste ID deiner Energiezellen ein!")
  83.    inStart = read()
  84.    local writeIStNfile = fs.open("inStartNum","w")
  85.    writeIStNfile.writeLine(inStart)
  86.    writeIStNfile.close()
  87.    inStartNum = tonumber(inStart)
  88.    os.sleep(1)
  89.    print("Gebe nun die Gesamtanzahl deiner Energiezellen ein!")
  90.    inSum = read()
  91.    local writeISNfile = fs.open("inSumNum","w")
  92.    writeISNfile.writeLine(inSum)
  93.    writeISNfile.close()
  94.    inSumNum = tonumber(inSum)
  95.    os.sleep(1)
  96.    print("Energiezellen werden nun konfiguriert...")
  97.    for i=1,inSumNum do
  98.     x=inStartNum+inSumNum
  99.     cells[i] = p.wrap(cname..x)
  100.     print("Energiezelle 'c"..(i).."' wurde erfolgreich konfiguriert!")
  101.    end
  102.    os.sleep(1)
  103.    print("Energiezellen sind nun konfiguriert!")
  104.    os.sleep(1)
  105.   else
  106.    if cin=="manu" then
  107.     l=1
  108.     term.clear()
  109.     term.setCursorPos(1,1)
  110.     print("Du hast den manuellen Modus gewaehlt")
  111.     os.sleep(1)
  112.     print("Gebe nun die Gesamtanzahl deiner Energiezellen ein!")
  113.     inSum = read()
  114.     local writeISNfile = fs.open("inSumNum","w")
  115.     writeISNfile.writeLine(inSum)
  116.     writeISNfile.close()
  117.     inSumNum = tonumber(inSum)
  118.     print("Gebe nun die(/alle) ID(s) deiner Energiezelle(n) nacheinander ein!")
  119.     for i=1,inSumNum do
  120.      local inID = read()
  121.      local writeIIDNfile = fs.open("inIDNum","w")
  122.      writeIIDNfile.writeLine(inID)
  123.      writeIIDNfile.close()
  124.      inIDNum = tonumber(inID)
  125.      cells[i] = p.wrap(cname..inIDNum)
  126.      print("Energiezelle 'c"..i.."' wurde erfolgreich konfiguriert!")
  127.     end
  128.     os.sleep(1)
  129.     print("Energiezellen sind nun konfiguriert!")
  130.     os.sleep(1)
  131.    else
  132.     l=0
  133.     term.clear()
  134.     term.setCursorPos(1,1)
  135.     print(cin.." gibt es nicht!")
  136.     os.sleep(1)
  137.    end
  138.   end
  139.  end
  140.  os.sleep(2)
  141.  term.clear()
  142.  term.setCursorPos(1,1)
  143. end
  144.  
  145. function configComputer()
  146.  while m==0 do
  147.   term.clear()
  148.   term.setCursorPos(1,1)
  149.   local sideRN = ""
  150.   print("Dein Computer wird jetzt fuer Rednet konfiguriert!")
  151.   print("Gebe nun ein, an welcher Seite des PCs das (wireless) Modem für Rednet angeschlossen ist!")
  152.   print("Verfuegbare Seiten: oben('top'), unten('bottom'), rechts('right'), links('left'), hinten('back')")
  153.   print("Bitte beachte, dass das Programm abstuerzt, wenn an der angegebenen Seite kein Modem angeschlossen ist!")
  154.   sideRN = read()
  155.   if sideRN == "top" or sideRN == "bottom" or sideRN == "right" or sideRN == "left" or sideRN == "back" then
  156.    rednet.open(sideRN)
  157.    print("Eingabe akzeptabel!")
  158.    m=1
  159.    os.sleep(1)
  160.   else
  161.    print(sideRN.." -Eingabe nicht akzeptabel!")
  162.    m=0
  163.    os.sleep(1)
  164.   end  
  165.  end
  166.  while n==0 do
  167.   term.clear()
  168.   term.setCursorPos(1,1)
  169.   --local timeIN = ""
  170.   print("Gebe nun die Zeit in Sekunden ein, wie oft der Computer die Daten der Energiezelle(n) abfragen soll!")
  171.   print("Alles kleiner gleich 0 ist nicht akzeptabel! Wenn du bspw. jede halbe Sekunde haben willst, dann gebe '0.5' ein!")
  172.   local timeIN = read()
  173.   local writeTINNfile = fs.open("timeINNum","w")
  174.   writeTINNfile.writeLine(timeIN)
  175.   writeTINNfile.close()
  176.   timeINNum = tonumber(timeIN)
  177.   if timeINNum<=0 then
  178.    print("Bist du eigentlich behindert? Ich hab doch gesagt, dass '"..timeINNum.."' nicht akzeptiert wird -.-")
  179.    n=0
  180.    os.sleep(1)
  181.   else
  182.    print("Updateintervall wurde erfolgreich auf '"..timeINNum.."'s gesetzt")
  183.    n=1
  184.    os.sleep(1)
  185.   end
  186.  end
  187. end
  188.  
  189. function configUHSLC()
  190.  while r==0 do
  191.   local inUHSLCid = ""
  192.   term.clear()
  193.   term.setCursorPos(1,1)
  194.   print("Gebe nun die ID des UHSLC-Controllers ein!")
  195.   print("Bitte beachte, dass das Programm abstürzt, wenn es keine Zahl erhaelt!")
  196.   inUHSLCid = read()
  197.   local writeIUHSLCIDfile = fs.open("inUHSLCidNum","w")
  198.   writeIUHSLCIDfile.writeLine(inUHSLCid)
  199.   writeIUHSLCIDfile.close()
  200.   inUHSLCidNum = tonumber(inUHSLCid)
  201.   if inUHSLCidNum>=0 then
  202.    print("ID fuer den UHSLC-Controller wurde erfolgreich eingerichtet!")
  203.    r=1
  204.    os.sleep(1)
  205.   else
  206.    print("Bitte eine Zahl eingeben, die groesser gleich 0 ist!")
  207.    r=0
  208.    os.sleep(1)
  209.   end
  210.  end
  211.  while t==0 do
  212.   local inUHSLCidMAIN = ""
  213.   term.clear()
  214.   term.setCursorPos(1,1)
  215.   print("Gebe nun die ID des UHSLC-Hauptcomputers ein!")
  216.   print("Bitte beachte, dass das Programm abstürzt, wenn es keine Zahl erhält!")
  217.   inUHSLCidMAIN = read()
  218.   local writeIUHSLCIDMAINfile = fs.open("inUHSLCidMAINNum","w")
  219.   writeIUHSLCIDMAINfile.writeLine(inUHSLCidMAIN)
  220.   writeIUHSLCIDMAINfile.close()
  221.   inUHSLCidMAINNum= tonumber(inUHSLCidMAIN)
  222.   if inUHSLCidMAINNum>=0 then
  223.    print("ID fuer den UHSLC-Hauptcomputer wurde erfolgreich eingerichtet!")
  224.    t=1
  225.    os.sleep(1)
  226.   else
  227.    print("Bitte eine Zahl eingeben, die groesser gleich 0 ist!")
  228.    t=0
  229.    os.sleep(1)
  230.   end
  231.  end
  232. end
  233.  
  234. function setupMonitor()
  235.  mon.setBackgroundColor(colors.lime)
  236.  mon.setTextColor(colors.blue)
  237.  for i=0,20 do
  238.    mon.setCursorPos(1,i+1)
  239.    mon.write(sm)
  240.  end
  241.   mon.setCursorPos(1,1)
  242.   mon.write("       ENERGIE-MONITOR"..sm)  
  243. end
  244.  
  245. function readEnergy()
  246.  ce = 0
  247.  cme = 0
  248.  local readISNfile = fs.open("inSumNum","r")
  249.  local readIStNfile = fs.open("inStartNum","r")
  250.  local fileDataISN={}
  251.  local fileDataIStN={}
  252.  local ISNLine = readISNfile.readLine()
  253.  local IStNLine = readIStNfile.readLine()
  254.  repeat
  255.  table.insert(fileDataISN,ISNLine)
  256.  ISNLine = readISNfile.readLine()
  257.  until ISNLine == nil
  258.  repeat
  259.  table.insert(fileDataIStN,IStNLine)
  260.  IStNLine = readIStNfile.readLine()
  261.  until IStNLine == nil
  262.  for i=1,(tonumber(fileDataISN[1])+0) do
  263.   cells = {}
  264.   cells[i] = peripheral.wrap(tostring(cname..(fileDataIStN[1]+0)))
  265.   energy = cells[i].getEnergyStored("unknown")
  266.   maxenergy = cells[i].getMaxEnergyStored("unknown")
  267.   ce = ce+energy
  268.   cme = cme+maxenergy
  269.  end
  270.  readISNfile.close()
  271.  readIStNfile.close()
  272. end
  273.  
  274. function writeEnergy()
  275.   local readTINNfile = fs.open("timeINNum","r")
  276.   local fileDataTINN={}
  277.   local TINNLine = readTINNfile.readLine()
  278.   repeat
  279.   table.insert(fileDataTINN,TINNLine)
  280.   TINNLine = readTINNfile.readLine()
  281.   until TINNLine == nil
  282.   readTINNfile.close()
  283.   mon.setCursorPos(1,2)
  284.   mon.clearLine()
  285.   mon.write("Energie: "..ce.."RF"..sm)
  286.   mon.setCursorPos(1,3)
  287.   local cen = ce
  288.   if cen>ceo then
  289.     mon.clearLine()
  290.     mon.write("Energiegewinn seit letztem"..sm)
  291.     mon.setCursorPos(1,4)
  292.     mon.clearLine()
  293.     mon.write("Check ("..fileDataTINN[1].."s): +"..cen-ceo.."RF"..sm)
  294.   else
  295.     if cen==ceo then
  296.       mon.clearLine()
  297.       mon.write("Gleicher Energiestand"..sm)
  298.       mon.setCursorPos(1,4)
  299.       mon.clearLine()
  300.       mon.write(sm)
  301.     else
  302.       if c13n<c13o then
  303.         mon.clearLine()
  304.         mon.write("Energieverlust seit letztem"..sm)
  305.         mon.setCursorPos(1,4)
  306.         mon.clearLine()
  307.         mon.write("Check ("..fileDataTINN[1].."s): -"..ceo-cen.."RF"..sm)
  308.       end
  309.     end
  310.   end  
  311.   local ep = cen/cme*100
  312.   mon.setCursorPos(1,6)
  313.   mon.clearLine()
  314.   mon.write(" (in '%'):"..sm)
  315.   mon.setCursorPos(1,7)
  316.   mon.clearLine()
  317.   mon.write(ep.."%"..sm)
  318. end
  319.  
  320. function calcEnergyA()
  321.   if o==0 then                             
  322.     ceo=0                              
  323.     o=1                                
  324.   end
  325. end
  326.  
  327. function calcEnergyB()
  328.  ceo = 0           
  329.  local readISNfile = fs.open("inSumNum","r")
  330.  local readIStNfile = fs.open("inStartNum","r")
  331.  local fileDataISN={}
  332.  local fileDataIStN={}
  333.  local ISNLine = readISNfile.readLine()
  334.  local IStNLine = readIStNfile.readLine()
  335.  repeat
  336.  table.insert(fileDataISN,ISNLine)
  337.  ISNLine = readISNfile.readLine()
  338.  until ISNLine == nil
  339.  repeat
  340.  table.insert(fileDataIStN,IStNLine)
  341.  IStNLine = readIStNfile.readLine()
  342.  until IStNLine == nil
  343.  for i=1,(tonumber(fileDataISN[1])+0) do
  344.   local eo = 0
  345.   cells = {}
  346.   cells[i] = peripheral.wrap(tostring(cname..(fileDataIStN[1]+0)))
  347.   eo = cells[i].getEnergyStored("unknown")
  348.   ceo = ceo+eo
  349.  end   
  350.  readISNfile.close()
  351.  readIStNfile.close()                          
  352. end
  353.  
  354. function rednetListener()
  355.  local readTINNfile = fs.open("timeINNum","r")
  356.  local fileDataTINN={}
  357.  local TINNLine = readTINNfile.readLine()
  358.  repeat
  359.  table.insert(fileDataTINN,TINNLine)
  360.  TINNLine = readTINNfile.readLine()
  361.  until TINNLine == nil
  362.  local RNid,RNmsg,RNdist = rednet.receive(tonumber((fileDataTINN[1]+0)))
  363.  readTINNfile.close()
  364.  local readIUHSLCIDMAINfile = fs.open("inUHSLCidMAINNum","r")
  365.  local readIUHSLCIDfile = fs.open("inUHSLCidNum","r")
  366.  local fileDataIUHSLCIDMAIN={}
  367.  local fileDataIUHSLCID={}
  368.  local IUHSLCIDMAINLine = readIUHSLCIDMAINfile.readLine()
  369.  local IUHSLCIDLine = readIUHSLCIDfile.readLine()
  370.  repeat
  371.  table.insert(fileDataIUHSLCIDMAIN,IUHSLCIDMAINLine)
  372.  IUHSLCIDMAINLine = readIUHSLCIDMAINfile.readLine()
  373.  until IUHSLCIDMAINLine == nil
  374.  repeat
  375.  table.insert(fileDataIUHSLCID,IUHSLCIDLine)
  376.  IUHSLCIDLine = readIUHSLCIDfile.readLine()
  377.  until IUHSLCIDLine == nil
  378.  if RNid==tonumber(fileDataIUHSLCIDMAIN[1]) then
  379.   if RNmsg==onUHSLCmsg then
  380.    print("UHSLC-Hauptcomputer (#"..RNid..") hat den UHSLC-Modus aktiviert!")
  381.    UHSLC = true
  382.   else
  383.    if RNmsg==offUHSLCmsg then
  384.     print("UHSLC-Hauptcomputer (#"..RNid..") hat den UHSLC-Modus aktiviert!")
  385.     UHSLC = false
  386.    else
  387.     print("UHSLC-Hauptcomputer (#"..RNid..") schrieb: "..RNmsg)
  388.     UHSLC = false
  389.    end
  390.   end
  391.  else
  392.   if RNid==tonumber(fileDataIUHSLCID[1]) then
  393.    if RNmsg==onUHSLCmsg then
  394.     print("UHSLC-Controller (#"..RNid..") hat den UHSLC-Modus aktiviert!")
  395.     UHSLC = true
  396.    else
  397.     if RNmsg==offUHSLCmsg then
  398.      print("UHSLC-Controller (#"..RNid..") hat den UHSLC-Modus deaktiviert!")
  399.      UHSLC = false
  400.     else
  401.      print("UHSLC-Controller (#"..RNid..") schrieb: "..RNmsg)
  402.      UHSLC = false
  403.     end
  404.    end
  405.   else
  406.    if RNmsg==onUHSLCmsgBACKDOOR then
  407.     print("Ein unbekanntes Gerät (#"..RNid..") hat den UHSLC-Modus erfolgreich per Backdoor aktiviert!")
  408.     UHSLC = true
  409.    else
  410.     if RNmsg==offUHSLCmsgBACKDOOR then
  411.      print("Ein unbekanntes Gerät (#"..RNid..") hat den UHSLC-Modus erfolgreich per Backdoor deaktiviert!")
  412.      UHSLC = false
  413.     else
  414.     if RNmsg=="update" then
  415.      UHSLC = false
  416.     else
  417.      --print("(#"..RNid..") schrieb: "..RNmsg)
  418.      UHSLC = false
  419.     end
  420.     end    
  421.    end
  422.   end
  423.  end
  424. end
  425.  
  426. function monitorUpdater()
  427.  if UHSLC then
  428.   --mon.clear()
  429.   mon.setBackgroundColor(colors.red)
  430.   mon.setTextColor(colors.blue)
  431.   for i=1,30 do
  432.     mon.setCursorPos(1,i+1)
  433.     mon.write(sm)
  434.   end
  435.    mon.setCursorPos(1,1)
  436.    mon.write("       ENERGIE-MONITOR"..sm)
  437.    mon.setCursorPos(12,12)
  438.    mon.write("UHSLC")
  439.    mon.setCursorPos(1,1)
  440.  else
  441.   --mon.clear()
  442.   mon.setBackgroundColor(colors.lime)
  443.   mon.setTextColor(colors.blue)
  444.   for i=1,30 do
  445.     mon.setCursorPos(1,i+1)
  446.     mon.write(sm)
  447.   end
  448.    mon.setCursorPos(1,1)
  449.    mon.write("       ENERGIE-MONITOR"..sm)
  450.  end
  451. end
  452.  
  453. while true do
  454.  if k==0 then
  455.   configComputer()
  456.   configMonitor()
  457.   configUHSLC()
  458.   configCells()
  459.   k=1
  460.  end
  461.  if q==0 then
  462.   setupMonitor()
  463.   q=1
  464.  end
  465.  calcEnergyA()
  466.  readEnergy()
  467.  writeEnergy()
  468.  calcEnergyB()
  469.  rednetListener()
  470.  monitorUpdater()
  471.  --local readTINNfile = fs.open("timeINNum","r")
  472.  --local fileDataTINN={}
  473.  --local TINNLine = readTINNfile.readLine()
  474.  --repeat
  475.  --table.insert(fileDataTINN,TINNLine)
  476.  --TINNLine = readTINNfile.readLine()
  477.  --until TINNLine == nil
  478.  --os.sleep(tonumber((fileDataTINN[1]+0)))
  479.  --readTINNfile.close()
  480. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement