Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- settings.load()
- -- #region Setup
- local info = {
- generators = {},
- batteries = {},
- tanks = {},
- input = 0,
- output = 0,
- }
- local generators = {}
- for i, unit in pairs(settings.get("notstrom.generators")) do
- generators[i] = peripheral.wrap(unit)
- generators[i].setEnabled(false)
- info.generators[i] = {
- fuel = 0,
- state = "unknown"
- }
- end
- local batteries = {}
- for i, unit in pairs(settings.get("notstrom.batteries")) do
- batteries[i] = peripheral.wrap(unit)
- info.batteries[i] = {
- energy = 0,
- max_energy = batteries[i].getEnergyCapacity(),
- state = "unknown"
- }
- end
- local tanks = {}
- for i, unit in pairs(settings.get("notstrom.tanks")) do
- tanks[i] = peripheral.wrap(unit)
- info.tanks[i] = {
- amount = 0,
- max_amount = tanks[i].getInfo().capacity
- }
- end
- local monitor = peripheral.wrap(settings.get("notstrom.monitor"))
- local input_meter = peripheral.wrap(settings.get("notstrom.input_meter"))
- local output_meter = peripheral.wrap(settings.get("notstrom.output_meter"))
- local max_threshold = settings.get("notstrom.max_threshold")
- local min_threshold = settings.get("notstrom.min_threshold")
- -- #endregion
- local function update_data()
- for i, generator in pairs(generators) do
- info.generators[i].fuel = generator.getFluid().amount
- if generator.getEnabled() then
- if generator.isRunning() then
- info.generators[i].state = "running"
- else
- info.generators[i].state = "enabled"
- end
- else
- info.generators[i].state = "disabled"
- end
- end
- for i, battery in pairs(batteries) do
- local previous_energy = info.batteries[i].energy
- local current_energy = battery.getEnergy()
- info.batteries[i].energy = current_energy
- if math.abs(current_energy - previous_energy) <= 16000 then
- info.batteries[i].state = "ok"
- elseif current_energy > previous_energy then
- info.batteries[i].state = "charging"
- else
- info.batteries[i].state = "discharging"
- end
- end
- for i, tank in pairs(tanks) do
- info.tanks[i].amount = tank.getInfo().amount
- end
- info.input = input_meter.getAveragePower()
- info.output = output_meter.getAveragePower()
- end
- local function control_generators()
- print("Input:", info.input, "Output:", info.output)
- local generator_output = 4096
- local generators_running = 0
- for i, generator in pairs(generators) do
- if generator.isRunning() then
- generators_running = generators_running + 1
- end
- end
- local below_max_threshold = 0
- local below_min_threshold = 0
- for i, battery in pairs(info.batteries) do
- if battery.energy < max_threshold then
- below_max_threshold = below_max_threshold + 1
- print("Battery", i, "below threshold")
- end
- if battery.energy < min_threshold then
- below_min_threshold = below_min_threshold + 1
- print("Battery", i, "below minimal threshold")
- end
- end
- local generators_needed = math.ceil(((info.output - 200 - generator_output * generators_running) - info.input) / generator_output)
- generators_needed = math.max(generators_needed, below_min_threshold)
- generators_needed = math.min(generators_needed, #generators)
- if below_max_threshold > 0 then
- print("Generators needed: ", generators_needed)
- for i, generator in pairs(generators) do
- if i <= generators_needed then
- generator.setEnabled(true)
- print("Enabling generator", i)
- else
- generator.setEnabled(false)
- print("Disabling generator", i)
- end
- end
- else
- print("No generators needed")
- for i, generator in pairs(generators) do
- generator.setEnabled(false)
- end
- end
- end
- local function display_clock(x, y)
- monitor.setCursorPos(x, y)
- local time = textutils.formatTime(os.time(), true)
- if #time == 4 then
- time = "0" .. time
- end
- monitor.write(time)
- end
- local function display_battery(id, x, y)
- monitor.setCursorPos(x, y)
- monitor.write("Batterie "..id)
- -- energy
- local energy_value = info.batteries[id].energy
- if energy_value > max_threshold then
- monitor.setTextColor(colors.green)
- elseif energy_value > min_threshold then
- monitor.setTextColor(colors.yellow)
- else
- monitor.setTextColor(colors.red)
- end
- local energy = tostring(energy_value)
- if #energy < 4 then
- energy = "< 1"
- else
- energy = energy:sub(1, #energy - 3)
- end
- for i = 1, 5 - #energy do
- energy = " " .. energy
- end
- energy = energy .. " kRF"
- monitor.setCursorPos(x+1, y+2)
- monitor.write(energy)
- monitor.setTextColor(colors.white)
- -- energy percentage
- local percentage = math.floor(info.batteries[id].energy / info.batteries[id].max_energy * 100)
- if percentage == 100 then
- monitor.setCursorPos(x+3, y+3)
- elseif percentage < 10 then
- monitor.setCursorPos(x+5, y+3)
- else
- monitor.setCursorPos(x+4, y+3)
- end
- monitor.write(tostring(percentage) .. " %")
- -- state
- local state = info.batteries[id].state
- monitor.setCursorPos(x+1, y+4)
- if state == "unknown" then
- monitor.setTextColor(colors.lightGray)
- monitor.write("UNBEKANNT")
- elseif state == "ok" then
- monitor.setTextColor(colors.lime)
- monitor.write("STATISCH")
- elseif state == "charging" then
- monitor.setTextColor(colors.green)
- monitor.write("LAEDT")
- elseif state == "discharging" then
- monitor.setTextColor(colors.orange)
- monitor.write("ENTLAEDT")
- end
- monitor.setTextColor(colors.white)
- end
- local function display_fuel(x, y)
- monitor.setCursorPos(x, y)
- monitor.write("Dieseltank ")
- local fuel = 0
- local max_fuel = 0
- for i, tank in pairs(tanks) do
- fuel = fuel + info.tanks[i].amount
- max_fuel = max_fuel + info.tanks[i].max_amount
- end
- local percentage = math.floor(fuel / max_fuel * 100)
- fuel = fuel / 1000
- local fuel_text = tostring(fuel)
- for i = 1, 5 - #fuel_text do
- fuel_text = " " .. fuel_text
- end
- monitor.write(fuel_text .. "l ")
- if percentage == 100 then
- monitor.setCursorPos(x+18, y)
- elseif percentage < 10 then
- monitor.setTextColor(colors.red)
- monitor.setCursorPos(x+20, y)
- else
- monitor.setCursorPos(x+19, y)
- end
- monitor.setTextColor(colors.white)
- monitor.write(tostring(percentage) .. "%")
- end
- local function display_throuput(x, y)
- monitor.setCursorPos(x, y)
- monitor.write("Durchsatz")
- local input = tostring(info.input)
- for i = 1, 5 - #input do
- input = " " .. input
- end
- input = input .. " RF"
- local output = tostring(info.output)
- for i = 1, 5 - #output do
- output = " " .. output
- end
- output = output .. " RF"
- monitor.setCursorPos(x+12, y)
- monitor.write("Input: " .. input)
- monitor.setCursorPos(x+30, y)
- monitor.write("Output: " .. output)
- end
- local function display_generator(id, x, y)
- monitor.setCursorPos(x, y)
- monitor.write("Generator"..id)
- local state = info.generators[id].state
- monitor.setCursorPos(x+1, y+2)
- if state == "unknown" then
- monitor.setTextColor(colors.lightGray)
- monitor.write("UNBEKANNT")
- elseif state == "running" then
- monitor.setTextColor(colors.orange)
- monitor.write("AKTIV")
- elseif state == "disabled" then
- monitor.setTextColor(colors.lime)
- monitor.write("BEREIT")
- end
- monitor.setTextColor(colors.white)
- end
- local function display_info()
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.clear()
- monitor.setCursorPos(3, 2)
- monitor.write("Notstrom Ueberwachung")
- display_clock(44, 2)
- local battery_offset = 12
- for i, _ in ipairs(info.batteries) do
- display_battery(i, 3 + battery_offset * (i-1), 4)
- end
- display_fuel(3, 11)
- display_throuput(3, 14)
- local generator_offset = 12
- for i, _ in ipairs(info.generators) do
- display_generator(i, 3 + generator_offset * (i-1), 17)
- end
- end
- while true do
- update_data()
- control_generators()
- display_info()
- sleep(5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement