Advertisement
Noobyhead99

energyMonitor

Sep 26th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.28 KB | None | 0 0
  1. --0x0B4387
  2. --0x4E8741
  3.  
  4. local terminalGlassesBridge
  5. local wrappedPeripherals = {}
  6. local storageFunctions
  7. local textObjects = {}
  8. local boxObjects = {}
  9.  
  10. local function printInfoText(text)
  11.   textObjects["infoText"] = terminalGlassesBridge.addText(2,2,text,0xFFFFFF)
  12. end
  13.  
  14. local function addPeripheral(peripheralName,barColor,label)
  15.   if not peripheral.getType(peripheralName) then
  16.     textObjects["infoText"].setText("Invalid peripheral")
  17.     return
  18.   elseif not storageFunctions[peripheral.getType(peripheralName)] then
  19.     textObjects["infoText"].setText("Function rules not found for peripheral type '"..peripheral.getType(peripheralName).."'. To add them, use the command '$$rule <type> <getStoredRule> <getCapacityRule>', e.g. '$$rule batbox getStored getCapacity' for a BatBox, MFE or MFSU.")
  20.     return
  21.   end
  22.   if type(barColor) ~= "number" then
  23.     barColor = math.random(0,16777215)
  24.   end
  25.   if type(label) ~= "string" then
  26.     label = ""
  27.   end
  28.   if not wrappedPeripherals[peripheralName] then
  29.     wrappedPeripherals[peripheralName] = peripheral.wrap(peripheralName)
  30.     textObjects[peripheralName] = {}
  31.     textObjects[peripheralName][1] = terminalGlassesBridge.addText(0,0,label,0xFFFFFF)
  32.     --textObjects[peripheralName][1] = terminalGlassesBridge.addText(1,0,"",barColor)
  33.     textObjects[peripheralName][2] = terminalGlassesBridge.addText(0,70,"",0xFFFFFF)
  34.     boxObjects[peripheralName] = {}
  35.     boxObjects[peripheralName][1] = terminalGlassesBridge.addBox(0,66,20,0,barColor,0.8)
  36.     boxObjects[peripheralName][2] = terminalGlassesBridge.addBox(0,66,20,0,0x000000,0.5)
  37.   end
  38.   textObjects[peripheralName][1].setText(label)
  39.   textObjects[peripheralName][1].setScale(1.75-#textObjects[peripheralName][1].getText()*0.25)
  40.   textObjects[peripheralName][1].setY(55)
  41.   textObjects[peripheralName][2].setScale(0.5)
  42.   textObjects[peripheralName][2].setZIndex(4)
  43.   boxObjects[peripheralName][1].setZIndex(3)
  44.   boxObjects[peripheralName][1].setColor(barColor)
  45.   boxObjects[peripheralName][2].setZIndex(2)
  46. end
  47.  
  48. local function updateReadings()
  49.   while true do
  50.     local peripheralCounter = 0
  51.     for k,v in pairs(wrappedPeripherals) do
  52.       --textObjects[k][1].setY(peripheralCounter*9+1)
  53.       textObjects[k][1].setX(22*peripheralCounter+8)
  54.       --textObjects[k][1].setText(k..": "..tostring(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]()))
  55.       textObjects[k][2].setText(tostring(math.floor(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]()/wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][2]]()*100)).."%")
  56.       textObjects[k][2].setX(22*peripheralCounter+11+math.ceil((24-textObjects[k][2].getWidth())/4))
  57.       local pixelCount = math.floor(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]()/wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][2]]()*142)
  58.       boxObjects[k][1].setHeight(pixelCount)
  59.       boxObjects[k][1].setY(66+(142-pixelCount))
  60.       boxObjects[k][1].setX(22*peripheralCounter+7)
  61.       boxObjects[k][2].setHeight(142-pixelCount)
  62.       boxObjects[k][2].setX(22*peripheralCounter+7)
  63.       peripheralCounter = peripheralCounter + 1
  64.     end
  65.     boxObjects["backgroundShader"].setWidth(22*peripheralCounter+2)
  66.     sleep(0)
  67.   end
  68. end
  69.  
  70. local function commandListener()
  71.  
  72.   local function add(words)
  73.     local barColor
  74.     local label = ""
  75.     for k,v in pairs(words) do
  76.       if string.sub(v,1,6) == "color:" then
  77.         barColor = tonumber(string.match(v,"%d.+"))
  78.       elseif string.sub(v,1,6) == "label:" then
  79.         label = string.match(v,"[^:]+$")
  80.       end
  81.     end
  82.     addPeripheral(words[2],barColor,label)
  83.     local fileHandle = io.open(".energyMonitor_peripherals","w")
  84.     for k,v in pairs(wrappedPeripherals) do
  85.       fileHandle:write(textutils.serialize({k,boxObjects[k][1].getColor(),textObjects[k][1].getText()}).."\n")
  86.     end
  87.     fileHandle:close()
  88.   end
  89.  
  90.   local function rule(words)
  91.     storageFunctions[words[2]] = {words[3],words[4]}
  92.     local fileHandle = io.open(".energyMonitor_functions","w")
  93.     fileHandle:write(textutils.serialize(storageFunctions))
  94.     fileHandle:close()
  95.   end
  96.  
  97.   local function remove(words)
  98.     wrappedPeripherals[words[2]] = nil
  99.     textObjects[words[2]][1].delete()
  100.     textObjects[words[2]][2].delete()
  101.     textObjects[words[2]] = nil
  102.     boxObjects[words[2]][1].delete()
  103.     boxObjects[words[2]][2].delete()
  104.     boxObjects[words[2]] = nil
  105.     local fileHandle = io.open(".energyMonitor_peripherals","w")
  106.     for k,v in pairs(wrappedPeripherals) do
  107.       fileHandle:write(textutils.serialize({k,boxObjects[k][1].getColor()}).."\n")
  108.     end
  109.     fileHandle:close()
  110.   end
  111.  
  112.   local function exit()
  113.     terminalGlassesBridge.clear()
  114.     error()
  115.   end
  116.  
  117.   local function reload()
  118.     shell.run("hud") -- autodetect the path
  119.     error()
  120.   end
  121.  
  122.   local commands = {
  123.     ["add"]=add,
  124.     ["rule"]=rule,
  125.     ["remove"]=remove,
  126.     ["exit"]=exit,
  127.     ["reload"]=reload
  128.   }
  129.  
  130.   while true do
  131.     local words = {}
  132.     local eventData = {os.pullEvent("chat_command")}
  133.     textObjects["infoText"].setText("")
  134.     for word in string.gmatch(eventData[2],"%S+") do
  135.       table.insert(words,word)
  136.     end
  137.     if commands[words[1]] then
  138.       commands[words[1]](words)
  139.     end
  140.   end
  141.  
  142. end
  143.  
  144. for k,v in pairs(peripheral.getNames()) do
  145.   if peripheral.getType(v) == "terminal_glasses_bridge" then
  146.     terminalGlassesBridge = peripheral.wrap(v)
  147.   end
  148. end
  149.  
  150. if not terminalGlassesBridge then
  151.   print("Please attach a Terminal Glasses Bridge to any side.")
  152.   return
  153. end
  154.  
  155. if not fs.exists(".energyMonitor_functions") then io.open(".energyMonitor_functions","w"):close() end
  156. if not fs.exists(".energyMonitor_peripherals") then io.open(".energyMonitor_peripherals","w"):close() end
  157.  
  158. local fileHandle = io.open(".energyMonitor_functions","r")
  159. storageFunctions = textutils.unserialize(fileHandle:read())
  160. fileHandle:close()
  161.  
  162. terminalGlassesBridge.clear()
  163.  
  164. local fileHandle = io.open(".energyMonitor_peripherals","r")
  165. for line in fileHandle:lines() do
  166.   line = textutils.unserialize(line)
  167.   addPeripheral(line[1],line[2],line[3])
  168. end
  169. fileHandle:close()
  170.  
  171. boxObjects["backgroundShader"] = terminalGlassesBridge.addBox(5,45,0,165,0x000000,0.45)
  172. boxObjects["backgroundShader"].setZIndex(1)
  173. parallel.waitForAny(updateReadings,commandListener)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement