Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.wrap("back")
- local monitor = peripheral.wrap("top")
- DISPLAYMODE = 1 -- 1 = EnergyUsage Persentage Vertical Bar
- local MFSU = {StartCHANNEL = 1200, StopCHANNEL = 1200}
- local EnergyUsage = {StartCHANNEL = 2200, StopCHANNEL = 2200}
- local EnergyProduction = {StartCHANNEL = 3200, StopCHANNEL = 3200}
- local StoredValues= {} --Stores the recent Values
- local MFSUData = {}
- local EnergyUsageData = {}
- local EnergyProductionData = {}
- function openChannels(startChannel, stopChannel)
- if modem == null then
- print("Place a Modem at the Bottom")
- else
- for i=startChannel, startChannel + stopChannel - startChannel, 1 do
- modem.open(i)
- end
- end
- end
- function resetMonitor()
- width, height = monitor.getSize()
- monitor.clear()
- monitor.setBackgroundColor(colors.black)
- monitor.setTextScale(0.5)
- for i = 1, height,1 do
- monitor.setCursorPos(1, i)
- monitor.clearLine()
- end
- end
- local function splitData(message)
- sD = {}
- for token in string.gmatch(message, "([^,]+),%s*") do
- if token ~= null then
- table.insert(sD,1, token)
- end
- end
- return sD
- end
- function addValuesMFSU(senderChannel, message)
- if senderChannel >= MFSU.StartCHANNEL then
- if senderChannel <= MFSU.StopCHANNEL then
- sD = splitData(message)
- exist = false
- for k,v in pairs(MFSUData) do
- if v ~= 0 then
- if v.channel == senderChannel then
- v.count = sD[1]
- v.capacity = sD[2]
- v.stored = sD[3]
- exist = true
- end
- end
- end
- if exist == false then
- item = { channel = senderChannel, count = sD[1], capacity = sD[2], stored = sD[3] }
- table.insert(MFSUData,1, item)
- end
- end
- end
- end
- function addValuesEnergyUsage(senderChannel, message)
- if senderChannel >= EnergyUsage.StartCHANNEL then
- if senderChannel <= EnergyUsage.StopCHANNEL then
- sD = splitData(message)
- exist = false
- for k,v in pairs(EnergyUsageData) do
- if v ~= 0 then
- if v.channel == senderChannel then
- v.count = sD[1]
- v.maxEnergy = sD[2]
- v.energy = sD[3]
- exist = true
- end
- end
- end
- if exist == false then
- item = { channel = senderChannel, count = sD[1], maxEnergy = sD[2], energy = sD[3] }
- table.insert(EnergyUsageData,1, item)
- end
- end
- end
- end
- function addValuesEnergyProduction(senderChannel, message)
- if senderChannel >= EnergyProduction.StartCHANNEL then
- if senderChannel <= EnergyProduction.StopCHANNEL then
- sD = splitData(message)
- exist = false
- for k,v in pairs(EnergyProductionData) do
- if v ~= 0 then
- if v.channel == senderChannel then
- v.countLV = sD[1]
- v.countMV = sD[2]
- v.countHV = sD[3]
- v.energyEmitted = sD[4]
- exist = true
- end
- end
- end
- if exist == false then
- item = { channel = senderChannel, countLV = sD[1], countMV = sD[2], countHV = sD[3], energyEmitted = sD[4] }
- table.insert(EnergyProductionData,1, item)
- end
- end
- end
- end
- local function calculateMFSU()
- MFSUCalc = { count = 0, capacity = 0, stored = 0 }
- for k,v in pairs(MFSUData) do
- if v ~= 0 then
- MFSUCalc.count = MFSUCalc.count + v.count
- MFSUCalc.capacity = MFSUCalc.capacity + v.capacity
- MFSUCalc.stored = MFSUCalc.stored + v.stored
- end
- end
- return MFSUCalc
- end
- local function calculateEnergyUsage()
- EnergyUsageCalc = { count = 0, maxEnergy = 0, energy = 0 }
- for k,v in pairs(EnergyUsageData) do
- if v ~= 0 then
- EnergyUsageCalc.count = EnergyUsageCalc.count + v.count
- EnergyUsageCalc.maxEnergy = EnergyUsageCalc.maxEnergy + v.maxEnergy
- EnergyUsageCalc.energy = EnergyUsageCalc.energy + v.energy
- end
- end
- return EnergyUsageCalc
- end
- local function calculatEnergyProduction()
- EnergyProductionCalc = { countLV = 0, countMV = 0, countHV = 0, energyEmitted = 0 }
- for k,v in pairs(EnergyProductionData) do
- if v ~= 0 then
- EnergyProductionCalc.countLV = EnergyProductionCalc.countLV + v.countLV
- EnergyProductionCalc.countMV = EnergyProductionCalc.countMV + v.countMV
- EnergyProductionCalc.countHV = EnergyProductionCalc.countHV + v.countHV
- EnergyProductionCalc.energyEmitted = EnergyProductionCalc.energyEmitted + v.energyEmitted
- end
- end
- return EnergyProductionCalc
- end
- function displayMode1(StoredMFSU, StoredEnergyUsage, StoredEnergyProduction)
- local width, height = monitor.getSize()
- bXStart = width / 2
- bXStop = bXStart
- bYStart = 0
- bYStop = height
- usagePerc = StoredEnergyUsage.energy / ( StoredEnergyProduction.energyEmitted /100 )
- if usagePerc == null then
- usagePerc = 100
- end
- term.redirect(monitor)
- if usagePerc < 100 then
- bXStop = bXStart + (width / 2 ) * usagePerc
- if bXStop > width then
- bXStop = width
- -- Notice more than 100%
- end
- print("xStart " ..bXStart)
- print("yStart " ..bXStop)
- print("xStop " ..bYStart)
- print("yStop " ..bYStop)
- paintutils.drawFilledBox(bXStart, bYStart, bXStop, bYStop, colors.green)
- end
- if usagePerc > 100 then
- bXStop = bXStart - (width / 2 ) * usagePerc
- if bXStop > width then
- bXStop = 0
- -- Notice more than 100%
- end
- paintutils.drawFilledBox(bXStart, bYStart, bXStop, bYStop, colors.red)
- end
- paintutils.drawLine(width /2 , 0, width /2 , height, colors.white)
- term.restore()
- end
- local function countTableItems(table)
- maxCount = 0
- for k,v in pairs(table) do
- if v ~= 0 then
- maxCount = maxCount +1
- end
- end
- return maxCount
- end
- -- START
- openChannels(EnergyUsage.StartCHANNEL, EnergyUsage.StopCHANNEL)
- openChannels(EnergyProduction.StartCHANNEL, EnergyProduction.StopCHANNEL)
- openChannels(MFSU.StartCHANNEL, MFSU.StopCHANNEL)
- resetMonitor()
- while (true) do
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- if message ~= null then
- if message ~= "" then
- addValuesMFSU(senderChannel, message)
- addValuesEnergyUsage(senderChannel, message)
- addValuesEnergyProduction(senderChannel, message)
- end
- end
- MFSUCalc = calculateMFSU()
- EnergyUsageCalc = calculateEnergyUsage()
- EnergyProductionCalc = calculatEnergyProduction()
- resetMonitor()
- -- storedValuesCount = countTableItems(tableStoredValues)
- if DISPLAYMODE == 1 then
- displayMode1(MFSUCalc, EnergyUsageCalc, EnergyProductionCalc)
- end
- sleep(0.15)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement