Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- client.lua
- -- AE2-Client, empfängt Daten per rednet und zeigt AE2 UI an
- -------------------------
- -- 1) KONFIG
- -------------------------
- local monSide = "right" -- Monitor-Seite
- local modemSide = "top" -- Wireless Modem-Seite
- local BOX_W = 14 -- Breite Kästchen
- local BOX_H = 4 -- Höhe Kästchen
- local ITEMS_PER_PAGE = 10 -- Items in Detailansicht
- local REDNET_PROTOCOL = "AE2_DATA" -- Muss zum Server passen
- local DATA_TIMEOUT = 15 -- Sekunden, bis wir "keine Daten" anzeigen
- -------------------------
- -- 2) Peripherie-Abfrage
- -------------------------
- local mon = peripheral.wrap(monSide)
- if not mon then
- error("Monitor an Seite '"..monSide.."' nicht gefunden!")
- end
- local modem = peripheral.wrap(modemSide)
- if not modem then
- error("Modem an Seite '"..modemSide.."' nicht gefunden!")
- end
- rednet.open(modemSide)
- mon.setTextScale(0.5)
- -------------------------
- -- 3) Globale Variablen
- -------------------------
- local width, height = mon.getSize()
- local currentScreen = "main"
- local currentPage = 1
- -- Daten vom Server
- local serverData = {
- cells = {}, -- { {usedBytes=..., totalBytes=...}, ... }
- itemCount = 0,
- freePercent = 0,
- items = {}, -- Komplette Liste (sortiert)
- }
- -------------------------
- -- 4) Hilfsfunktionen
- -------------------------
- local function clear()
- mon.setBackgroundColor(colors.black)
- mon.clear()
- mon.setCursorPos(1,1)
- end
- local function centerText(y, text, color)
- color = color or colors.white
- mon.setTextColor(color)
- local x = math.floor((width - #text) / 2)
- mon.setCursorPos(x+1, y)
- mon.write(text)
- end
- local function getColorForUsage(used, total)
- if total == 0 then
- return colors.lime
- end
- local percent = used / total
- if percent >= 0.9 then
- return colors.red
- elseif percent >= 0.5 then
- return colors.yellow
- else
- return colors.lime
- end
- end
- -------------------------
- -- 4.1) Box-Zeichnung (Cell UI)
- -------------------------
- local function drawCellBox(x, y, w, h, cellIndex, usedB, totalB)
- mon.setCursorPos(x, y)
- mon.setTextColor(colors.white)
- mon.write("+")
- for i = 1, w-2 do
- mon.write("-")
- end
- mon.write("+")
- for row = 1, h-2 do
- mon.setCursorPos(x, y+row)
- mon.write("|")
- mon.setCursorPos(x+w-1, y+row)
- mon.write("|")
- end
- mon.setCursorPos(x, y+h-1)
- mon.write("+")
- for i = 1, w-2 do
- mon.write("-")
- end
- mon.write("+")
- -- Info
- mon.setCursorPos(x+1, y+1)
- mon.setTextColor(colors.white)
- mon.write("Cell "..cellIndex)
- local percent = 0
- if totalB > 0 then
- percent = math.floor((usedB / totalB)*100)
- end
- local usageColor = getColorForUsage(usedB, totalB)
- mon.setTextColor(usageColor)
- mon.setCursorPos(x+1, y+2)
- mon.write(usedB.."/"..totalB.." ("..percent.."%)")
- end
- -------------------------
- -- 5) Hauptmenü zeichnen
- -------------------------
- local function drawMainScreen()
- clear()
- centerText(1, "AE2 Cells Overview (Page "..currentPage..")", colors.cyan)
- local cells = serverData.cells or {}
- local columns = math.floor(width / BOX_W)
- local rows = math.floor((height - 3) / BOX_H)
- if columns < 1 or rows < 1 then
- centerText(math.floor(height/2), "Monitor zu klein!", colors.red)
- return
- end
- local boxesPerPage = columns * rows
- local totalCells = #cells
- local totalPages = math.ceil(totalCells / boxesPerPage)
- -- "globale" totalPages Updaten, damit wir blättern können
- _G.totalPages = totalPages
- local startCell = (currentPage - 1)*boxesPerPage + 1
- local endCell = math.min(startCell + boxesPerPage - 1, totalCells)
- local idx = startCell
- for row=0, rows-1 do
- for col=0, columns-1 do
- if idx > endCell then break end
- local c = cells[idx]
- local usedB = c.usedBytes or 0
- local totalB = c.totalBytes or 0
- local x = col*BOX_W + 1
- local y = row*BOX_H + 2
- drawCellBox(x, y, BOX_W, BOX_H, idx, usedB, totalB)
- idx = idx + 1
- end
- if idx > endCell then break end
- end
- -- Infozeile (Cells, Items, Frei)
- mon.setCursorPos(2, height-1)
- mon.setTextColor(colors.white)
- local msg = string.format("Cells: %d Items: %d Frei: %d%%",
- #cells, serverData.itemCount, serverData.freePercent)
- mon.write(msg)
- -- Seitenbuttons
- mon.setCursorPos(2, height)
- if currentPage > 1 then
- mon.write("[ < ]")
- end
- mon.setCursorPos(width-5, height)
- if currentPage < totalPages then
- mon.write("[ > ]")
- end
- end
- -------------------------
- -- 5.1) Detail-Items
- -------------------------
- local function drawCellDetails(cellIndex)
- clear()
- centerText(1, "Cell "..cellIndex.." - Top Items", colors.cyan)
- local items = serverData.items or {}
- local y = 3
- local shown = 0
- for _, it in ipairs(items) do
- if y >= height then break end
- mon.setCursorPos(2, y)
- mon.setTextColor(colors.white)
- mon.write((it.displayName or "?")..": "..(it.amount or 0))
- y = y + 1
- shown = shown + 1
- if shown >= ITEMS_PER_PAGE then
- break
- end
- end
- mon.setCursorPos(2, height)
- mon.setTextColor(colors.gray)
- mon.write("[ Zurueck ]")
- end
- -------------------------
- -- 6) Klick -> Welche Cell?
- -------------------------
- local function getCellIndexFromClick(x, y)
- local cells = serverData.cells or {}
- local columns = math.floor(width / BOX_W)
- local rows = math.floor((height - 3) / BOX_H)
- local boxesPerPage = columns * rows
- local totalCells = #cells
- local totalPages = math.ceil(totalCells / boxesPerPage)
- _G.totalPages = totalPages -- for global reference
- local startCell = (currentPage - 1)*boxesPerPage + 1
- local endCell = math.min(startCell + boxesPerPage - 1, totalCells)
- local col = math.floor((x-1)/BOX_W)
- local row = math.floor((y-2)/BOX_H)
- if col<0 or col>=columns or row<0 or row>=rows then
- return nil
- end
- local cellOffset = row*columns + col
- local actualIndex = startCell + cellOffset
- if actualIndex <= endCell then
- return actualIndex
- end
- return nil
- end
- -------------------------
- -- 7) Haupt-Loop
- -------------------------
- local refreshTimer = os.startTimer(1) -- alle 1s checken, ob neue Daten reingekommen sind
- -- Wir speichern Zeitstempel, wann wir zuletzt Daten erhielten
- local lastDataTime = os.epoch("utc")
- -- Wir zeigen init
- drawMainScreen()
- while true do
- local event, p1, p2, p3, p4 = os.pullEvent()
- if event == "rednet_message" then
- local senderID, message, protocol = p1, p2, p3
- if protocol == REDNET_PROTOCOL and type(message)=="table" then
- -- Neue Daten vom Server
- serverData = message
- lastDataTime = os.epoch("utc")
- if currentScreen == "main" then
- drawMainScreen()
- end
- end
- elseif event == "monitor_touch" then
- local x, y = p2, p3
- if currentScreen == "main" then
- -- Blättern?
- if y == height then
- if x>=2 and x<=5 and currentPage>1 then
- currentPage = currentPage - 1
- drawMainScreen()
- elseif x>=width-4 and x<=width and currentPage<_G.totalPages then
- currentPage = currentPage + 1
- drawMainScreen()
- end
- else
- -- Klick auf Cell => Detail
- local cellIndex = getCellIndexFromClick(x,y)
- if cellIndex then
- currentScreen = "cell"
- drawCellDetails(cellIndex)
- end
- end
- elseif currentScreen == "cell" then
- -- Klick auf [Zurueck]
- if y == height then
- currentScreen = "main"
- drawMainScreen()
- end
- end
- elseif event == "timer" then
- if p1 == refreshTimer then
- -- Prüfe, ob wir zu lange keine Daten hatten
- local now = os.epoch("utc")
- if now - lastDataTime > (DATA_TIMEOUT*1000) then
- -- Zu lange keine Daten => Anzeige leeren
- clear()
- centerText(math.floor(height/2), "Keine Daten vom Server!", colors.red)
- end
- refreshTimer = os.startTimer(1) -- Timer neu
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement