Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Energy Farm Data Collection Script
- -- Collects data from solar panels and energy cubes, transmits to remote monitor
- local WIRELESS_CHANNEL = 100
- local UPDATE_INTERVAL = 5 -- seconds
- local COMPUTER_ID = "computer_1"
- -- Initialize wireless modem
- local wirelessModem = peripheral.wrap("left")
- if not wirelessModem then
- error("No wireless modem found on left of computer")
- end
- wirelessModem.open(WIRELESS_CHANNEL)
- -- Function to safely get peripheral data
- local function safeGetData(peripheral, method, default)
- local success, result = pcall(function()
- return peripheral[method]()
- end)
- return success and result or default
- end
- -- Function to collect solar panel data
- local function collectSolarData()
- local solarData = {}
- for i = 0, 9 do
- local panelName = "advancedSolarGenerator_" .. i
- local panel = peripheral.wrap(panelName)
- if panel then
- solarData[panelName] = {
- energy = safeGetData(panel, "getEnergy", 0),
- maxEnergy = safeGetData(panel, "getMaxEnergy", 0),
- energyPercentage = safeGetData(panel, "getEnergyFilledPercentage", 0),
- productionRate = safeGetData(panel, "getProductionRate", 0),
- maxOutput = safeGetData(panel, "getMaxOutput", 0),
- canSeeSun = safeGetData(panel, "canSeeSun", false),
- energyNeeded = safeGetData(panel, "getEnergyNeeded", 0),
- comparatorLevel = safeGetData(panel, "getComparatorLevel", 0)
- }
- else
- print("Warning: Solar panel " .. panelName .. " not found")
- end
- end
- return solarData
- end
- -- Function to collect energy cube data
- local function collectEnergyCubeData()
- local cubeData = {}
- for i = 0, 5 do
- local cubeName = "advancedEnergyCube_" .. i
- local cube = peripheral.wrap(cubeName)
- if cube then
- cubeData[cubeName] = {
- energy = safeGetData(cube, "getEnergy", 0),
- maxEnergy = safeGetData(cube, "getMaxEnergy", 0),
- energyPercentage = safeGetData(cube, "getEnergyFilledPercentage", 0),
- isEjecting = safeGetData(cube, "isEjecting", false),
- mode = safeGetData(cube, "getMode", "Unknown"),
- inputColor = safeGetData(cube, "getInputColor", "None"),
- outputColor = safeGetData(cube, "getOutputColor", "None"),
- comparatorLevel = safeGetData(cube, "getComparatorLevel", 0),
- canEject = safeGetData(cube, "canEject", false),
- hasStrictInput = safeGetData(cube, "hasStrictInput", false)
- }
- else
- print("Warning: Energy cube " .. cubeName .. " not found")
- end
- end
- return cubeData
- end
- -- Function to calculate totals and statistics
- local function calculateStats(solarData, cubeData)
- local stats = {
- solar = {
- totalEnergy = 0,
- totalMaxEnergy = 0,
- totalProductionRate = 0,
- averagePercentage = 0,
- panelsSeeSun = 0,
- totalPanels = 0
- },
- cubes = {
- totalEnergy = 0,
- totalMaxEnergy = 0,
- averagePercentage = 0,
- ejectingCubes = 0,
- totalCubes = 0
- },
- system = {
- totalSystemEnergy = 0,
- totalSystemCapacity = 0,
- systemPercentage = 0
- }
- }
- -- Calculate solar stats
- for name, data in pairs(solarData) do
- stats.solar.totalEnergy = stats.solar.totalEnergy + data.energy
- stats.solar.totalMaxEnergy = stats.solar.totalMaxEnergy + data.maxEnergy
- stats.solar.totalProductionRate = stats.solar.totalProductionRate + data.productionRate
- stats.solar.averagePercentage = stats.solar.averagePercentage + data.energyPercentage
- if data.canSeeSun then
- stats.solar.panelsSeeSun = stats.solar.panelsSeeSun + 1
- end
- stats.solar.totalPanels = stats.solar.totalPanels + 1
- end
- if stats.solar.totalPanels > 0 then
- stats.solar.averagePercentage = stats.solar.averagePercentage / stats.solar.totalPanels
- end
- -- Calculate cube stats
- for name, data in pairs(cubeData) do
- stats.cubes.totalEnergy = stats.cubes.totalEnergy + data.energy
- stats.cubes.totalMaxEnergy = stats.cubes.totalMaxEnergy + data.maxEnergy
- stats.cubes.averagePercentage = stats.cubes.averagePercentage + data.energyPercentage
- if data.isEjecting then
- stats.cubes.ejectingCubes = stats.cubes.ejectingCubes + 1
- end
- stats.cubes.totalCubes = stats.cubes.totalCubes + 1
- end
- if stats.cubes.totalCubes > 0 then
- stats.cubes.averagePercentage = stats.cubes.averagePercentage / stats.cubes.totalCubes
- end
- -- Calculate system totals
- stats.system.totalSystemEnergy = stats.solar.totalEnergy + stats.cubes.totalEnergy
- stats.system.totalSystemCapacity = stats.solar.totalMaxEnergy + stats.cubes.totalMaxEnergy
- if stats.system.totalSystemCapacity > 0 then
- stats.system.systemPercentage = (stats.system.totalSystemEnergy / stats.system.totalSystemCapacity) * 100
- end
- return stats
- end
- -- Function to format energy values for display
- 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 transmit data
- local function transmitData(data)
- local message = {
- timestamp = os.epoch("utc"),
- source = COMPUTER_ID,
- data = data
- }
- wirelessModem.transmit(WIRELESS_CHANNEL, WIRELESS_CHANNEL, message)
- print("Data transmitted on channel " .. WIRELESS_CHANNEL)
- end
- -- Function to display local status
- local function displayLocalStatus(stats)
- term.clear()
- term.setCursorPos(1, 1)
- print("=== ENERGY FARM MONITOR ===")
- print("Time: " .. textutils.formatTime(os.time(), true))
- print("")
- print("SOLAR PANELS:")
- print(" Total Energy: " .. formatEnergy(stats.solar.totalEnergy))
- print(" Capacity: " .. formatEnergy(stats.solar.totalMaxEnergy))
- print(" Production: " .. formatEnergy(stats.solar.totalProductionRate) .. "/t")
- print(" Average Fill: " .. string.format("%.1f%%", stats.solar.averagePercentage))
- print(" Sun Visible: " .. stats.solar.panelsSeeSun .. "/" .. stats.solar.totalPanels)
- print("")
- print("ENERGY CUBES:")
- print(" Total Energy: " .. formatEnergy(stats.cubes.totalEnergy))
- print(" Capacity: " .. formatEnergy(stats.cubes.totalMaxEnergy))
- print(" Average Fill: " .. string.format("%.1f%%", stats.cubes.averagePercentage))
- print(" Ejecting: " .. stats.cubes.ejectingCubes .. "/" .. stats.cubes.totalCubes)
- print("")
- print("SYSTEM TOTAL:")
- print(" Energy: " .. formatEnergy(stats.system.totalSystemEnergy))
- print(" Capacity: " .. formatEnergy(stats.system.totalSystemCapacity))
- print(" Fill Level: " .. string.format("%.1f%%", stats.system.systemPercentage))
- print("")
- print("Next update in " .. UPDATE_INTERVAL .. " seconds...")
- end
- -- Main monitoring loop
- local function main()
- print("Starting Energy Farm Monitor...")
- print("Wireless channel: " .. WIRELESS_CHANNEL)
- print("Update interval: " .. UPDATE_INTERVAL .. " seconds")
- print("")
- while true do
- local solarData = collectSolarData()
- local cubeData = collectEnergyCubeData()
- local stats = calculateStats(solarData, cubeData)
- local fullData = {
- solar = solarData,
- cubes = cubeData,
- stats = stats
- }
- transmitData(fullData)
- displayLocalStatus(stats)
- sleep(UPDATE_INTERVAL)
- end
- end
- -- Error handling wrapper
- local function runWithErrorHandling()
- local success, error = pcall(main)
- if not success then
- 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