Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Remote Energy Farm Monitor Script
- -- Receives wireless data and displays on monitor
- local WIRELESS_CHANNEL = 100
- local MONITOR_SIDE = "top" -- Change this to match your monitor position
- local REFRESH_TIMEOUT = 30 -- seconds before showing "no data" warning
- -- Initialize peripherals
- local wirelessModem = peripheral.find("modem", function(name, modem)
- return modem.isWireless()
- end)
- if not wirelessModem then
- error("No wireless modem found")
- end
- print("Found wireless modem: " .. peripheral.getName(wirelessModem))
- local monitor = peripheral.wrap(MONITOR_SIDE)
- if not monitor then
- error("No monitor found on " .. MONITOR_SIDE)
- end
- -- Open wireless channel
- wirelessModem.open(WIRELESS_CHANNEL)
- print("Opened wireless channel " .. WIRELESS_CHANNEL)
- -- Monitor setup
- monitor.setTextScale(0.5)
- local monitorWidth, monitorHeight = monitor.getSize()
- -- Color definitions
- local colorScheme = {
- background = colors.black,
- header = colors.yellow,
- good = colors.green,
- warning = colors.orange,
- critical = colors.red,
- info = colors.white,
- accent = colors.cyan
- }
- -- Data storage
- local lastData = nil
- local lastUpdateTime = 0
- -- Function to format energy values
- local function formatEnergy(energy)
- if energy >= 1000000000 then
- return string.format("%.2fGFE", energy / 1000000000)
- elseif energy >= 1000000 then
- return string.format("%.2fMFE", energy / 1000000)
- elseif energy >= 1000 then
- return string.format("%.2fkFE", energy / 1000)
- else
- return string.format("%.0fFE", energy)
- end
- end
- -- Function to get color based on percentage
- local function getPercentageColor(percentage)
- if percentage >= 80 then
- return colorScheme.good
- elseif percentage >= 50 then
- return colorScheme.warning
- else
- return colorScheme.critical
- end
- end
- -- Function to draw a progress bar
- local function drawProgressBar(x, y, width, percentage, label)
- local fillWidth = math.floor((percentage / 100) * width)
- monitor.setCursorPos(x, y)
- monitor.setTextColor(colorScheme.info)
- monitor.write(label .. ":")
- monitor.setCursorPos(x, y + 1)
- monitor.setBackgroundColor(colors.gray)
- monitor.write(string.rep(" ", width))
- monitor.setCursorPos(x, y + 1)
- monitor.setBackgroundColor(getPercentageColor(percentage))
- monitor.write(string.rep(" ", fillWidth))
- monitor.setCursorPos(x + width + 1, y + 1)
- monitor.setBackgroundColor(colorScheme.background)
- monitor.setTextColor(colorScheme.info)
- monitor.write(string.format(" %.1f%%", percentage))
- end
- -- Function to draw header
- local function drawHeader()
- monitor.setBackgroundColor(colorScheme.background)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setTextColor(colorScheme.header)
- monitor.setBackgroundColor(colorScheme.background)
- local title = "ENERGY FARM MONITORING SYSTEM"
- local centerX = math.floor((monitorWidth - string.len(title)) / 2) + 1
- monitor.setCursorPos(centerX, 1)
- monitor.write(title)
- monitor.setCursorPos(1, 2)
- monitor.setTextColor(colorScheme.accent)
- monitor.write(string.rep("=", monitorWidth))
- end
- -- Function to draw connection status
- local function drawConnectionStatus()
- local currentTime = os.epoch("utc") / 1000
- local timeSinceUpdate = currentTime - lastUpdateTime
- monitor.setCursorPos(1, 3)
- monitor.setTextColor(colorScheme.info)
- monitor.write("Status: ")
- if timeSinceUpdate > REFRESH_TIMEOUT then
- monitor.setTextColor(colorScheme.critical)
- monitor.write("NO DATA - Connection Lost")
- elseif timeSinceUpdate > 10 then
- monitor.setTextColor(colorScheme.warning)
- monitor.write("Delayed - " .. math.floor(timeSinceUpdate) .. "s ago")
- else
- monitor.setTextColor(colorScheme.good)
- monitor.write("Connected - Live Data")
- end
- monitor.setCursorPos(monitorWidth - 19, 3)
- monitor.setTextColor(colorScheme.info)
- monitor.write("Time: " .. textutils.formatTime(os.time(), true))
- end
- -- Function to draw solar panel section
- local function drawSolarSection(stats, startY)
- monitor.setCursorPos(1, startY)
- monitor.setTextColor(colorScheme.header)
- monitor.write("SOLAR PANELS")
- local y = startY + 1
- monitor.setCursorPos(1, y)
- monitor.setTextColor(colorScheme.info)
- monitor.write("Energy: " .. formatEnergy(stats.solar.totalEnergy) .. " / " .. formatEnergy(stats.solar.totalMaxEnergy))
- y = y + 1
- monitor.setCursorPos(1, y)
- monitor.write("Production: " .. formatEnergy(stats.solar.totalProductionRate) .. "/t")
- y = y + 1
- monitor.setCursorPos(1, y)
- monitor.setTextColor(stats.solar.panelsSeeSun == stats.solar.totalPanels and colorScheme.good or colorScheme.warning)
- monitor.write("Sun Visibility: " .. stats.solar.panelsSeeSun .. "/" .. stats.solar.totalPanels .. " panels")
- y = y + 2
- drawProgressBar(1, y, 30, stats.solar.averagePercentage, "Solar Fill Level")
- return y + 3
- end
- -- Function to draw energy cube section
- local function drawCubeSection(stats, startY)
- monitor.setCursorPos(1, startY)
- monitor.setTextColor(colorScheme.header)
- monitor.write("ENERGY STORAGE")
- local y = startY + 1
- monitor.setCursorPos(1, y)
- monitor.setTextColor(colorScheme.info)
- monitor.write("Energy: " .. formatEnergy(stats.cubes.totalEnergy) .. " / " .. formatEnergy(stats.cubes.totalMaxEnergy))
- y = y + 1
- monitor.setCursorPos(1, y)
- monitor.setTextColor(stats.cubes.ejectingCubes > 0 and colorScheme.good or colorScheme.warning)
- monitor.write("Ejecting: " .. stats.cubes.ejectingCubes .. "/" .. stats.cubes.totalCubes .. " cubes")
- y = y + 2
- drawProgressBar(1, y, 30, stats.cubes.averagePercentage, "Storage Fill Level")
- return y + 3
- end
- -- Function to draw system overview
- local function drawSystemOverview(stats, startY)
- monitor.setCursorPos(1, startY)
- monitor.setTextColor(colorScheme.header)
- monitor.write("SYSTEM OVERVIEW")
- local y = startY + 1
- monitor.setCursorPos(1, y)
- monitor.setTextColor(colorScheme.info)
- monitor.write("Total Energy: " .. formatEnergy(stats.system.totalSystemEnergy))
- y = y + 1
- monitor.setCursorPos(1, y)
- monitor.write("Total Capacity: " .. formatEnergy(stats.system.totalSystemCapacity))
- y = y + 2
- drawProgressBar(1, y, 40, stats.system.systemPercentage, "System Fill Level")
- return y + 3
- end
- -- Function to draw individual device details
- local function drawDeviceDetails(data, startY)
- if startY >= monitorHeight - 2 then
- return startY
- end
- monitor.setCursorPos(1, startY)
- monitor.setTextColor(colorScheme.header)
- monitor.write("DEVICE STATUS")
- local y = startY + 1
- local col1 = 1
- local col2 = math.floor(monitorWidth / 2) + 1
- -- Solar panels column
- monitor.setCursorPos(col1, y)
- monitor.setTextColor(colorScheme.accent)
- monitor.write("Solar Panels:")
- y = y + 1
- local solarCount = 0
- for name, panelData in pairs(data.solar) do
- if y >= monitorHeight then break end
- solarCount = solarCount + 1
- if solarCount <= 5 then -- Show first 5 panels
- monitor.setCursorPos(col1, y)
- monitor.setTextColor(panelData.canSeeSun and colorScheme.good or colorScheme.warning)
- local shortName = string.gsub(name, "advancedSolarGenerator_", "S")
- monitor.write(shortName .. ": " .. string.format("%.0f%%", panelData.energyPercentage))
- y = y + 1
- end
- end
- -- Energy cubes column
- y = startY + 1
- monitor.setCursorPos(col2, y)
- monitor.setTextColor(colorScheme.accent)
- monitor.write("Energy Cubes:")
- y = y + 1
- local cubeCount = 0
- for name, cubeData in pairs(data.cubes) do
- if y >= monitorHeight then break end
- cubeCount = cubeCount + 1
- if cubeCount <= 5 then -- Show first 5 cubes
- monitor.setCursorPos(col2, y)
- monitor.setTextColor(cubeData.isEjecting and colorScheme.good or colorScheme.info)
- local shortName = string.gsub(name, "advancedEnergyCube_", "C")
- monitor.write(shortName .. ": " .. string.format("%.0f%%", cubeData.energyPercentage))
- y = y + 1
- end
- end
- return math.max(startY + 7, y)
- end
- -- Function to update display
- local function updateDisplay()
- if not lastData then
- drawHeader()
- drawConnectionStatus()
- monitor.setCursorPos(1, 5)
- monitor.setTextColor(colorScheme.warning)
- monitor.write("Waiting for data from energy farm...")
- return
- end
- drawHeader()
- drawConnectionStatus()
- local y = 5
- y = drawSolarSection(lastData.stats, y)
- y = drawCubeSection(lastData.stats, y)
- y = drawSystemOverview(lastData.stats, y)
- if y < monitorHeight - 5 then
- drawDeviceDetails(lastData, y)
- end
- end
- -- Function to handle incoming messages
- local function handleMessage(message)
- print("Received message: " .. textutils.serialize(message))
- if type(message) == "table" and message.data and message.source == "computer_1" then
- lastData = message.data
- lastUpdateTime = os.epoch("utc") / 1000
- updateDisplay()
- print("Data updated successfully")
- else
- print("Message format invalid or wrong source")
- end
- end
- -- Main program loop
- local function main()
- print("Starting Remote Energy Monitor...")
- print("Listening on channel: " .. WIRELESS_CHANNEL)
- print("Monitor size: " .. monitorWidth .. "x" .. monitorHeight)
- -- Initial display
- updateDisplay()
- while true do
- local event, side, channel, replyChannel, message, distance = os.pullEvent()
- if event == "modem_message" then
- print("Modem message received on channel " .. channel .. " (listening on " .. WIRELESS_CHANNEL .. ")")
- if channel == WIRELESS_CHANNEL then
- handleMessage(message)
- end
- elseif event == "timer" then
- -- Periodic update to refresh connection status
- updateDisplay()
- end
- -- Set a timer for periodic updates
- os.startTimer(5)
- end
- end
- -- Error handling wrapper
- local function runWithErrorHandling()
- local success, error = pcall(main)
- if not success then
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setTextColor(colors.red)
- monitor.write("ERROR: " .. tostring(error))
- print("Error occurred: " .. tostring(error))
- print("Restarting in 5 seconds...")
- sleep(5)
- runWithErrorHandling()
- end
- end
- -- Start the program
- runWithErrorHandling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement