Advertisement
Noobyhead99

energyMonitorLegacy

Sep 26th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local terminalGlassesBridge
  2. local wrappedPeripherals = {}
  3. local storageFunctions
  4. local textObjects = {}
  5. local boxObjects = {}
  6.  
  7. local function addPeripheral(peripheralName,barColor)
  8.   wrappedPeripherals[peripheralName] = peripheral.wrap(peripheralName)
  9.   textObjects[peripheralName] = {}
  10.   textObjects[peripheralName][1] = terminalGlassesBridge.addText(1,0,"",barColor)
  11.   textObjects[peripheralName][2] = terminalGlassesBridge.addText(0,70,"",0xFFFFFF)
  12.   textObjects[peripheralName][2].setScale(0.5)
  13.   textObjects[peripheralName][2].setZIndex(4)
  14.   boxObjects[peripheralName] = {}
  15.   boxObjects[peripheralName][1] = terminalGlassesBridge.addBox(0,66,20,0,barColor,0.8)
  16.   boxObjects[peripheralName][1].setZIndex(3)
  17.   boxObjects[peripheralName][2] = terminalGlassesBridge.addBox(0,66,20,0,0x000000,0.5)
  18.   boxObjects[peripheralName][2].setZIndex(2)
  19. end
  20.  
  21. local function updateReadings()
  22.   while true do
  23.     local peripheralCounter = 0
  24.     for k,v in pairs(wrappedPeripherals) do
  25.       textObjects[k][1].setY(peripheralCounter*9+1)
  26.       textObjects[k][1].setText(k..": "..tostring(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]()))
  27.       textObjects[k][2].setText(tostring(math.floor(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]()/wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][2]]()*100)).."%")
  28.       textObjects[k][2].setX(22*peripheralCounter+11+math.ceil((24-textObjects[k][2].getWidth())/4))
  29.       local pixelCount = math.floor(wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][1]]()/wrappedPeripherals[k][storageFunctions[peripheral.getType(k)][2]]()*142)
  30.       boxObjects[k][1].setHeight(pixelCount)
  31.       boxObjects[k][1].setY(66+(142-pixelCount))
  32.       boxObjects[k][1].setX(22*peripheralCounter+7)
  33.       boxObjects[k][2].setHeight(142-pixelCount)
  34.       boxObjects[k][2].setX(22*peripheralCounter+7)
  35.       peripheralCounter = peripheralCounter + 1
  36.     end
  37.     boxObjects["backgroundShader"].setWidth(22*peripheralCounter+2)
  38.     sleep(0)
  39.   end
  40. end
  41.  
  42. local function commandListener()
  43.   while true do
  44.     local words = {}
  45.     local eventData = {os.pullEvent("chat_command")}
  46.     for word in string.gmatch(eventData[2],"%S+") do
  47.       table.insert(words,word)
  48.     end
  49.     if words[1] == "add" and not wrappedPeripherals[words[2]] then
  50.       addPeripheral(words[2],math.random(0,16777215))  
  51.       for k,v in pairs(words) do
  52.         if string.sub(v,1,6) == "color:" then
  53.           textObjects[words[2]][1].setColor(tonumber(string.match(v,"%d.+")))
  54.           boxObjects[words[2]][1].setColor(tonumber(string.match(v,"%d.+")))
  55.         elseif string.sub(v,1,6) == "label:" then
  56.           tonumber(string.match(v,"[^:]+$"))
  57.         end
  58.       end
  59.       local fileHandle = io.open(".energyMonitor_peripherals","w")
  60.       for k,v in pairs(wrappedPeripherals) do
  61.         fileHandle:write(textutils.serialize({k,boxObjects[k][1].getColor()}).."\n")
  62.       end
  63.       fileHandle:close()
  64.     elseif words[1] == "remove" then
  65.       wrappedPeripherals[words[2]] = nil
  66.       textObjects[words[2]][1].delete()
  67.       textObjects[words[2]][2].delete()
  68.       textObjects[words[2]] = nil
  69.       boxObjects[words[2]][1].delete()
  70.       boxObjects[words[2]][2].delete()
  71.       boxObjects[words[2]] = nil
  72.       local fileHandle = io.open(".energyMonitor_peripherals","w")
  73.       for k,v in pairs(wrappedPeripherals) do
  74.         fileHandle:write(textutils.serialize({k,boxObjects[k][1].getColor()}).."\n")
  75.       end
  76.       fileHandle:close()
  77.     elseif words[1] == "exit" then
  78.       return
  79.     end
  80.   end
  81. end
  82.  
  83. for k,v in pairs(peripheral.getNames()) do
  84.   if peripheral.getType(v) == "terminal_glasses_bridge" then
  85.     terminalGlassesBridge = peripheral.wrap(v)
  86.   end
  87. end
  88.  
  89. if not terminalGlassesBridge then
  90.   print("Please attach a Terminal Glasses Bridge to any side.")
  91.   return
  92. end
  93.  
  94. if not fs.exists(".energyMonitor_functions") then io.open(".energyMonitor_functions","w"):close() end
  95. if not fs.exists(".energyMonitor_peripherals") then io.open(".energyMonitor_peripherals","w"):close() end
  96.  
  97. local fileHandle = io.open(".energyMonitor_functions","r")
  98. storageFunctions = textutils.unserialize(fileHandle:read())
  99. fileHandle:close()
  100.  
  101. local fileHandle = io.open(".energyMonitor_peripherals","r")
  102. for line in fileHandle:lines() do
  103.   line = textutils.unserialize(line)
  104.   addPeripheral(line[1],line[2])
  105. end
  106. fileHandle:close()
  107.  
  108. terminalGlassesBridge.clear()
  109. boxObjects["backgroundShader"] = terminalGlassesBridge.addBox(5,45,0,165,0x000000,0.45)
  110. boxObjects["backgroundShader"].setZIndex(1)
  111. parallel.waitForAny(updateReadings,commandListener)
  112. terminalGlassesBridge.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement