CCCPaste

worker_energy_core_reader

-- peripheral setup
local core = peripheral.find("draconic_rf_storage")
local modem = peripheral.find("modem")
modem.open(1)

-- variables setup

local function getCoreTier(max)
    local tier = 0
    if max < 50000000 then
        tier = 1
    elseif max < 300000000 then
        tier = 2
    elseif max < 2000000000 then
        tier = 3
    elseif max < 10000000000 then
        tier = 4
    elseif max < 50000000000 then
        tier = 5
    elseif max < 400000000000 then
        tier = 6
    elseif max < 3000000000000 then
        tier = 7
    else
        tier = 8
    end
    return tier
end

function getRequest()
    local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
    if message == "provideCoreData" then
        local now = core.getEnergyStored()
        local max = core.getMaxEnergyStored()
        local energyRate = core.getTransferPerTick()
        local core_data = {
            now = now,
            max = max,
            rate = energyRate,
            tier = getCoreTier(max)
        }
        modem.transmit(replyChannel, channel, core_data)
    end
end

local function waitForRequest()
    parallel.waitForAny(wait, getRequest)
end

function wait()
    sleep(0.5)
end

while true do
    waitForRequest()
end