Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Simple Solar Monitor - Shows actual available data
- local modem = peripheral.find("modem")
- local monitor = peripheral.find("monitor")
- if not modem then
- print("No modem found!")
- return
- end
- if monitor then
- monitor.setTextScale(0.5)
- monitor.clear()
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- end
- modem.open(101)
- local solarData = {}
- local lastUpdate = {}
- function formatNumber(num)
- if not num then return "0" end
- if num >= 1000000 then
- return string.format("%.1fM", num / 1000000)
- elseif num >= 1000 then
- return string.format("%.1fK", num / 1000)
- else
- return tostring(math.floor(num))
- end
- end
- function requestData()
- modem.transmit(100, 101, "REQUEST_DATA")
- end
- function updateDisplay()
- if not monitor then return end
- monitor.clear()
- local w, h = monitor.getSize()
- -- Header
- monitor.setCursorPos(1, 1)
- monitor.setTextColor(colors.yellow)
- monitor.write("SOLAR PANEL MONITORING")
- monitor.setCursorPos(1, 2)
- monitor.setTextColor(colors.white)
- monitor.write("Time: " .. textutils.formatTime(os.time(), false))
- monitor.write(" | Active Panels: " .. #solarData)
- monitor.setCursorPos(1, 3)
- monitor.write(string.rep("-", w))
- -- Calculate totals
- local totalEnergy = 0
- local totalCapacity = 0
- local totalNeeded = 0
- for _, data in pairs(solarData) do
- totalEnergy = totalEnergy + (data.energy or 0)
- totalCapacity = totalCapacity + (data.maxEnergy or 0)
- totalNeeded = totalNeeded + (data.energyNeeded or 0)
- end
- -- System totals
- monitor.setCursorPos(1, 5)
- monitor.setTextColor(colors.lightBlue)
- monitor.write("SYSTEM TOTALS:")
- monitor.setCursorPos(1, 6)
- monitor.setTextColor(colors.white)
- monitor.write("Total Energy Stored: " .. formatNumber(totalEnergy) .. " FE")
- monitor.setCursorPos(1, 7)
- monitor.write("Total Capacity: " .. formatNumber(totalCapacity) .. " FE")
- monitor.setCursorPos(1, 8)
- monitor.write("Total Space Available: " .. formatNumber(totalNeeded) .. " FE")
- if totalCapacity > 0 then
- local percent = (totalEnergy / totalCapacity) * 100
- monitor.setCursorPos(1, 9)
- monitor.write("System Full: " .. string.format("%.1f%%", percent))
- end
- -- Individual panels
- monitor.setCursorPos(1, 11)
- monitor.setTextColor(colors.lightBlue)
- monitor.write("INDIVIDUAL PANELS:")
- monitor.setCursorPos(1, 12)
- monitor.setTextColor(colors.white)
- monitor.write("ID Energy Stored Capacity Available Full%")
- monitor.setCursorPos(1, 13)
- monitor.write(string.rep("-", w))
- -- Sort panels by ID
- local sortedPanels = {}
- for id, data in pairs(solarData) do
- table.insert(sortedPanels, {id = id, data = data})
- end
- table.sort(sortedPanels, function(a, b) return a.id < b.id end)
- local line = 14
- for _, panel in pairs(sortedPanels) do
- local id = panel.id
- local data = panel.data
- monitor.setCursorPos(1, line)
- monitor.setTextColor(colors.white)
- local energy = data.energy or 0
- local maxEnergy = data.maxEnergy or 0
- local needed = data.energyNeeded or 0
- local percent = maxEnergy > 0 and (energy / maxEnergy) * 100 or 0
- -- Color code based on fullness
- if percent >= 95 then
- monitor.setTextColor(colors.red)
- elseif percent >= 80 then
- monitor.setTextColor(colors.orange)
- else
- monitor.setTextColor(colors.lime)
- end
- local line_text = string.format("%-4s %-15s %-12s %-12s %.1f%%",
- id,
- formatNumber(energy) .. " FE",
- formatNumber(maxEnergy) .. " FE",
- formatNumber(needed) .. " FE",
- percent)
- monitor.write(line_text)
- line = line + 1
- end
- monitor.setTextColor(colors.white)
- end
- -- Main loop
- print("Starting Solar Monitor...")
- while true do
- requestData()
- -- Listen for responses
- local timeout = os.startTimer(2)
- while true do
- local event, p1, p2, p3, p4 = os.pullEvent()
- if event == "timer" and p1 == timeout then
- break
- elseif event == "modem_message" then
- local channel, data = p2, p4
- if channel == 101 and type(data) == "table" and data.id then
- solarData[data.id] = data
- lastUpdate[data.id] = os.time()
- end
- end
- end
- updateDisplay()
- sleep(3)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement