Advertisement
derkoch

SteamBoilerEnergy

Oct 6th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. os.loadAPI("helper")
  2.  
  3. local tArgs = {...}
  4. local version = "0.1"
  5. local func
  6. local redstoneside = "left"
  7. local rednetside = "back"
  8. local boilerside = "right"
  9. local monitorside = "bottom"
  10. local defColor = colors.white
  11.  
  12. -- init peripheral and rednet
  13. local highPressureBoiler = peripheral.wrap(boilerside)
  14. local monitor = peripheral.wrap(monitorside)
  15. rednet.open(rednetside)
  16.  
  17. monitor.setTextScale(0.5)
  18. term.redirect(monitor)
  19.  
  20. -- equivalent to "condition ? true : false"
  21. function fif(condition, if_true, if_false)
  22.   if condition then return if_true else return if_false end
  23. end
  24.  
  25. function powerStatus()
  26.     return redstone.testBundledInput(redstoneside, colors.white)
  27. end
  28.  
  29.  
  30.  
  31.  
  32. function getStatus()
  33.     local boilerTemperature = highPressureBoiler.getTemperature()
  34.     local boilerTanks = highPressureBoiler.getTanks("")
  35.     -- Remove information from Tank-Table that is not needed.
  36.     tanks = { }
  37.     for i, tank in pairs(boilerTanks) do
  38.         if tank["name"] ~= nil then
  39.             tankname = tank["name"]
  40.             tanks[tankname] = { }
  41.             tanks[tankname]["amount"] = tank["amount"]
  42.             tanks[tankname]["capacity"] = tank["capacity"] 
  43.         end
  44.     end
  45.     local boilerInfo = { }
  46.     boilerInfo["boilerTemp"] = boilerTemperature
  47.     boilerInfo["boilerTanks"] = tanks  
  48.    
  49.     local status = { }
  50.     status["computerID"] = os.computerID()
  51.     status["computerName"] = os.getComputerLabel()
  52.     status["powerStatus"] = powerStatus()
  53.     status["boilerInfo"] = boilerInfo
  54.        
  55.     return status  
  56. end
  57.  
  58. function updateComputerStatusText(status)
  59.     if status == nil then
  60.         return
  61.     end
  62.     term.clear()
  63.     term.setCursorPos(1,1) 
  64.     print(status["computerName"] .. " (id:" .. status["computerID"] .. ")")
  65.     print("PowerStatus:\t" .. fif(status["powerStatus"],"ON","OFF"))   
  66.     print("Boiler Temperature:\t" ..    status["boilerInfo"]["boilerTemp"])
  67.     print("Boiler Tanks:\t")
  68.  
  69.         local tmp
  70.         header = { "" , "Amount", "Capacity", "%"}
  71.         tmp = status["boilerInfo"]["boilerTanks"]["Steam"]
  72.         steam = { "Steam:", "" .. tmp["amount"], "" .. tmp["capacity"], "" .. math.floor(tmp["amount"]/tmp["capacity"]*100)}
  73.         tmp = status["boilerInfo"]["boilerTanks"]["Fuel"]
  74.         fuel = { "Fuel:", "" .. tmp["amount"], "" .. tmp["capacity"], "" .. math.floor(tmp["amount"]/tmp["capacity"]*100)}
  75.         tmp = status["boilerInfo"]["boilerTanks"]["Water"]
  76.         water = { "Water:", "" .. tmp["amount"], "" .. tmp["capacity"], "" .. math.floor(tmp["amount"]/tmp["capacity"]*100)}
  77.        
  78.        
  79.         textutils.tabulate(header, steam, fuel, water)
  80.  
  81.    
  82. end
  83.  
  84. function togglePower(id)
  85.     if powerStatus() then
  86.         turnOff(id)
  87.     else
  88.         turnOn(id)
  89.     end
  90.     updateComputerStatusText(getStatus())  
  91.     return powerStatus()
  92. end
  93.  
  94. function turnOn(id)
  95.     redstone.setBundledOutput(redstoneside, colors.white)
  96.     return true
  97. end
  98.  
  99. function turnOff(id)
  100.     redstone.setBundledOutput(redstoneside, 0)
  101.     return true
  102. end
  103.  
  104. function coreStatus(id)
  105.     local status = getStatus()
  106.     updateComputerStatusText(status)
  107.     rednet.send(id, status)
  108.     return true
  109. end
  110.  
  111. updateComputerStatusText(getStatus())
  112.  
  113.  
  114. while true do
  115.     local id,msg,dist = rednet.receive()
  116.     func = loadstring("return " .. msg.. "(...)")
  117.     setfenv(func, getfenv())
  118.     isSuccess = func(id)
  119.     if isSuccess == nil then
  120.         rednet.send(id,"nil")
  121.     elseif isSuccess then
  122.         rednet.send(id,"Done")
  123.     else
  124.         rednet.send(id,"Failed")
  125.     end
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement