Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- matrix_dashboard.lua
- -- Zentraler Computer/Monitor-Skript für Mekanism Induction Matrix.
- -- Kommuniziert per rednet mit dem Induction-Client, zeigt ascii-grafische Anzeige.
- -----------------------------------------
- -- 1) KONFIGURATION
- -----------------------------------------
- local modemSide = "bottom" -- Seite deines (Wireless/Wired) Modems
- local monSide = "top" -- Seite deines Monitors
- local clientID = 5 -- Computer-ID des matrix_client
- local refreshDelay = 3 -- Sekunden zwischen Updates
- -----------------------------------------
- -- 2) INIT
- -----------------------------------------
- local modem = peripheral.wrap(modemSide)
- if not modem then
- error("Modem an Seite '"..modemSide.."' nicht gefunden!")
- end
- rednet.open(modemSide)
- local mon = peripheral.wrap(monSide)
- if not mon then
- error("Monitor an Seite '"..monSide.."' nicht gefunden!")
- end
- mon.setTextScale(0.5)
- local w, h = mon.getSize()
- print("Matrix-Dashboard gestartet (ID: "..os.getComputerID()..")")
- print("Empfange Daten von Client:", clientID)
- -----------------------------------------
- -- 3) GLOBALE DATEN
- -----------------------------------------
- -- Hier speichern wir die Werte, die wir vom Client bekommen
- local matrixData = {
- stored = 0,
- capacity = 1, -- capacity=1 um Division by zero zu vermeiden
- inputRate = 0,
- outputRate = 0
- }
- -----------------------------------------
- -- 4) HILFSFUNKTIONEN
- -----------------------------------------
- -- 4.1) Bildschirm löschen
- local function clear()
- mon.setBackgroundColor(colors.black)
- mon.clear()
- mon.setCursorPos(1,1)
- end
- -- 4.2) Text zentriert ausgeben
- local function centerText(y, text, color)
- color = color or colors.white
- mon.setTextColor(color)
- local x = math.floor((w - #text) / 2)
- mon.setCursorPos(x+1, y)
- mon.write(text)
- end
- -- 4.3) Zahl mit K/M/G/T abkürzen
- local function formatNumberShort(num)
- local absNum = math.abs(num)
- local sign = (num < 0) and "-" or ""
- if absNum >= 10^12 then
- return sign .. string.format("%.2fT", absNum / 10^12)
- elseif absNum >= 10^9 then
- return sign .. string.format("%.2fG", absNum / 10^9)
- elseif absNum >= 10^6 then
- return sign .. string.format("%.2fM", absNum / 10^6)
- elseif absNum >= 10^3 then
- return sign .. string.format("%.2fk", absNum / 10^3)
- else
- return tostring(num)
- end
- end
- -- 4.4) ASCII-Farbiger Balken
- -- x, y = Startkoordinate
- -- width = Breite
- -- fillPercent = 0..1
- -- fillColor = z. B. colors.lime
- local function drawBar(x, y, width, fillPercent, fillColor)
- local fillCount = math.floor(width * fillPercent)
- mon.setCursorPos(x, y)
- mon.setBackgroundColor(fillColor)
- mon.write(string.rep(" ", fillCount))
- mon.setBackgroundColor(colors.gray)
- mon.write(string.rep(" ", width - fillCount))
- -- Hintergrund zurücksetzen
- mon.setBackgroundColor(colors.black)
- end
- -----------------------------------------
- -- 5) DATEN VOM CLIENT HOLEN
- -----------------------------------------
- local function fetchMatrixData()
- rednet.send(clientID, {cmd="GET_DATA"}, "mekanism_matrix")
- local sender, resp, proto = rednet.receive("mekanism_matrix", 3)
- if sender == clientID and type(resp)=="table" then
- matrixData.stored = resp.stored or 0
- matrixData.capacity = resp.capacity or 1
- matrixData.inputRate = resp.inputRate or 0
- matrixData.outputRate = resp.outputRate or 0
- end
- end
- -----------------------------------------
- -- 6) DASHBOARD-ZEICHNUNG
- -----------------------------------------
- local function drawDashboard()
- clear()
- centerText(1, "Mekanism Induction Matrix", colors.cyan)
- -- Daten holen aus matrixData
- local stored = matrixData.stored
- local capacity = matrixData.capacity
- local inputRate = matrixData.inputRate
- local outputRate = matrixData.outputRate
- -- Kurzformat
- local storedStr = formatNumberShort(stored)
- local capacityStr = formatNumberShort(capacity)
- local inStr = formatNumberShort(inputRate)
- local outStr = formatNumberShort(outputRate)
- -- Füllstand in %
- local fillPercent = (capacity > 0) and (stored / capacity) or 0
- mon.setCursorPos(2, 3)
- mon.setTextColor(colors.white)
- mon.write(("Energie: %s / %s FE"):format(storedStr, capacityStr))
- mon.setCursorPos(2, 4)
- mon.write(("Input: %s FE/t, Output: %s FE/t"):format(inStr, outStr))
- mon.setCursorPos(2, 6)
- mon.write("Füllstand:")
- -- Farbauswahl basierend auf fillPercent
- local fillColor = colors.lime
- if fillPercent >= 0.75 then
- fillColor = colors.lime
- elseif fillPercent >= 0.5 then
- fillColor = colors.yellow
- elseif fillPercent >= 0.25 then
- fillColor = colors.orange
- else
- fillColor = colors.red
- end
- -- Balken-Darstellung
- local barWidth = math.min(w - 3, 50)
- drawBar(2, 7, barWidth, fillPercent, fillColor)
- local p100 = math.floor(fillPercent * 100)
- mon.setCursorPos(2, 9)
- mon.setTextColor(colors.white)
- mon.write(p100.."%")
- end
- -----------------------------------------
- -- 7) HAUPT-LOOP
- -----------------------------------------
- local function mainLoop()
- while true do
- fetchMatrixData()
- drawDashboard()
- sleep(refreshDelay)
- end
- end
- mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment