Adding monitor-system

This commit is contained in:
Gabriel Moreira Minossi 2020-12-19 16:25:23 -03:00
parent a3dd881119
commit 0876ba1245
24 changed files with 686 additions and 0 deletions

3
monitor-system/.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"lua.targetVersion": "5.3"
}

View File

@ -0,0 +1,16 @@
-- Import section
local playTune = require("api.sound.play-tune")
--
local tune = {
{pitch = "A4", duration = 0.1, wait = 0.0},
{pitch = "A3", duration = 0.3, wait = 0.4},
{pitch = "A4", duration = 0.1, wait = 0.0},
{pitch = "A3", duration = 0.3, wait = 0.4},
{pitch = "A4", duration = 0.1, wait = 0.0},
{pitch = "A3", duration = 0.3, wait = 0.4},
{pitch = "A4", duration = 0.1, wait = 0.0},
{pitch = "A3", duration = 0.3, wait = 0.4}
}
return playTune(tune)

View File

@ -0,0 +1,14 @@
-- Import section
local playTune = require("api.sound.play-tune")
--
local tune = {
"E5",
"G5",
"E6",
"C6",
"D6",
"G6"
}
return playTune(tune)

View File

@ -0,0 +1,58 @@
-- Import section
local computer = require("computer")
--
local notes = {
["G#3"] = 207.6523,
["A3"] = 220.0000,
["A#3"] = 233.0819,
["B3"] = 246.9417,
["C4"] = 261.6256,
["C#4"] = 277.1826,
["D4"] = 293.6648,
["D#4"] = 311.1270,
["E4"] = 329.6276,
["F4"] = 349.2282,
["F#4"] = 369.9944,
["G4"] = 391.9954,
["G#4"] = 415.3047,
["A4"] = 440.0000,
["A#4"] = 466.1638,
["B4"] = 493.8833,
["C5"] = 523.2511,
["C#5"] = 554.3653,
["D5"] = 587.3295,
["D#5"] = 622.2540,
["E5"] = 659.2551,
["F5"] = 698.4565,
["F#5"] = 739.9888,
["G5"] = 783.9909,
["G#5"] = 830.6094,
["A5"] = 880.000,
["A#5"] = 932.3275,
["B5"] = 987.7666,
["C6"] = 1046.502,
["C#6"] = 1108.731,
["D6"] = 1174.659,
["D#6"] = 1244.508,
["E6"] = 1318.510,
["F6"] = 1396.913,
["F#6"] = 1479.978,
["G6"] = 1567.982,
["E5"] = 659.2551,
["G#6"] = 1661.219,
["A6"] = 1760.000,
["A#6"] = 1864.655,
["B6"] = 1975.533
}
local function playTune(tune)
return function()
for i, note in ipairs(tune) do
computer.beep(notes[note.pitch or note], note.duration or 0.1)
os.sleep(note.wait or 0.01)
end
end
end
return playTune

View File

@ -0,0 +1,16 @@
-- Import section
local playTune = require('api.sound.play-tune')
--
local tune = {
'F#6',
'F6',
'D6',
'G#5',
'G5',
'D#6',
'G6',
'B6'
}
return playTune(tune)

View File

@ -0,0 +1,13 @@
local adresses = {
laserEngraver1 = "d9a13648-977b-464e-a228-46bf12e38525",
laserEngraver2 = "a4fd6874-f4a9-40d9-a47b-f88c3ef7d0fa",
laserEngraver3 = "843022a0-5687-49d9-b94c-6637ae8a8ec9",
circuitAssembler = "89249f2e-b145-4691-8b34-a612ca804d03",
laserEngraver4 = "d6ceb940-e127-46bb-97bc-6bd4b15f06a2",
laserEngraver5 = "30f0e31f-a60c-4d9e-9421-b2e43b59fa14",
laserEngraver6 = "d41c45bd-b25b-41ea-8905-0ae56827580f",
laserEngraver7 = "81dc6006-30ff-432a-ab4b-6ed168f2ccfe",
cleanroom = "753f5619-7076-45c0-8f0a-f3899bae00e1"
}
return adresses

View File

@ -0,0 +1,3 @@
local address = "3691f797-8297-4239-9963-66be089d5390"
return address

View File

@ -0,0 +1,6 @@
local adresses = {
EBF11 = "1b05ef68-0fa7-4f94-818d-3e8c079e299d",
cleanroom = "753f5619-7076-45c0-8f0a-f3899bae00e1"
}
return adresses

View File

@ -0,0 +1,72 @@
-- Import section
local parser = require("util.parser")
local inherits = require("util.class.inherits")
local SingleBlock = require("data.datasource.single-block")
local mock = require("data.mock.mock-energy-provider")
--
local EnergyProvider =
inherits(
SingleBlock,
{
mock = mock,
name = "EnergyProvider"
}
)
function EnergyProvider:getBatteryCharge(slot)
return self.block.getBatteryCharge(slot)
end
function EnergyProvider:getAllBatteryCharges()
local batteryCharges = {}
local i = 1
while true do
local successfull =
pcall(
function()
table.insert(batteryCharges, self:getBatteryCharge(i))
end
)
if (not successfull) then
return batteryCharges
end
i = i + 1
end
end
function EnergyProvider:getBatteryChargesSum()
local batterySum = 0
local i = 1
while true do
local successfull =
pcall(
function()
batterySum = batterySum + self:getBatteryCharge(i)
end
)
if (not successfull) then
return batterySum
end
i = i + 1
end
end
function EnergyProvider:getTotalEnergy()
local sensorInformation = self:getSensorInformation()
return parser.parseStoredEnergy(sensorInformation[3])
end
function EnergyProvider:getAverageInput()
local sensorInformation = self:getSensorInformation()
return parser.parseAverageInput(sensorInformation[5])
end
function EnergyProvider:getAverageOutput()
local sensorInformation = self:getSensorInformation()
return parser.parseAverageOutput(sensorInformation[7])
end
return EnergyProvider

View File

View File

@ -0,0 +1,27 @@
-- Import section
local parser = require("util.parser")
local inherits = require("util.class.inherits")
local SingleBlock = require("data.datasource.single-block")
local mock = require("data.mock.mock-multi-block")
--
local MultiBlock =
inherits(
SingleBlock,
{
mock = mock,
name = "MultiBlock"
}
)
function MultiBlock:getNumberOfProblems()
local sensorInformation = self:getSensorInformation()
return parser.parseProblems(sensorInformation[5])
end
function MultiBlock:getEfficiencyPercentage()
local sensorInformation = self:getSensorInformation()
return parser.parseEfficiency(sensorInformation[5])
end
return MultiBlock

View File

@ -0,0 +1,104 @@
-- Import section
local component = require("component")
local mock = require("data.mock.mock-single-block")
--
local SingleBlock = {
mock = mock,
name = "SingleBlock"
}
function SingleBlock:setWorkAllowed(allow)
self.block.setWorkAllowed(allow)
end
function SingleBlock:isWorkAllowed()
return self.block.isWorkAllowed()
end
function SingleBlock:getAverageElectricInput()
return self.block.getAverageElectricInput()
end
function SingleBlock:getOwnerName()
return self.block.getOwnerName()
end
function SingleBlock:getEUStored()
return self.block.getEUStored()
end
function SingleBlock:getWorkMaxProgress()
return self.block.getWorkMaxProgress()
end
function SingleBlock:getSensorInformation()
return self.block.getSensorInformation()
end
function SingleBlock:getEUOutputAverage()
return self.block.getEUOutputAverage()
end
function SingleBlock:getEUInputAverage()
return self.block.getEUInputAverage()
end
function SingleBlock:getStoredEU()
return self.block.getStoredEU()
end
function SingleBlock:isMachineActive()
return self.block.isMachineActive()
end
function SingleBlock:getOutputVoltage()
return self.block.getOutputVoltage()
end
function SingleBlock:getAverageElectricOutput()
return self.block.getAverageElectricOutput()
end
function SingleBlock:hasWork()
return self.block.hasWork()
end
function SingleBlock:getOutputAmperage()
return self.block.getOutputAmperage()
end
function SingleBlock:getEUCapacity()
return self.block.getEUCapacity()
end
function SingleBlock:getWorkProgress()
return self.block.getWorkProgress()
end
function SingleBlock:getEUMaxStored()
return self.block.getEUMaxStored()
end
function SingleBlock:new(partialAdress)
local machine = {}
setmetatable(machine, self)
if (partialAdress == "") then
partialAdress = nil
end
local successfull =
pcall(
function()
machine.block = component.proxy(component.get(partialAdress))
end
)
if (not successfull) then
machine.block = self.mock:new()
end
return machine
end
return SingleBlock

View File

@ -0,0 +1,37 @@
-- Import section
local parser = require("util.parser")
local inherits = require("util.class.inherits")
local MockSingleBlock = require("data.mock.mock-single-block")
local new = require("util.class.new")
--
local MockEnergyProvider =
inherits(
MockSingleBlock,
{
name = "MockEnergyProvider"
}
)
function MockEnergyProvider.getSensorInformation()
return {
"§9Insane Voltage Battery Buffer§r",
"Stored Items:",
"§a1,608,383,129§r EU / §e1,608,388,608§r EU",
"Average input:",
"11,396 EU/t",
"Average output:",
"11,158 EU/t",
n = 7
}
end
function MockEnergyProvider.getBatteryCharge(slot)
return 1000
end
function MockEnergyProvider.getTotalEnergy()
return 10000
end
return MockEnergyProvider

View File

@ -0,0 +1,27 @@
-- Import section
local MockSingleBlock = require("data.mock.mock-single-block")
local inherits = require("util.class.inherits")
local new = require("util.class.new")
--
local MockMultiBlock =
inherits(
MockSingleBlock,
{
name = "MockMultiBlock"
}
)
function MockMultiBlock.getSensorInformation()
return {
"Progress: §a2§r s / §e5§r s",
"Stored Energy: §a1000§r EU / §e1000§r EU",
"Probably uses: §c4§r EU/t",
"Max Energy Income: §e128§r EU/t(x2A) Tier: §eMV§r",
"Problems: §c0§r Efficiency: §e100.0§r %",
"Pollution reduced to: §a0§r %",
n = 6
}
end
return MockMultiBlock

View File

@ -0,0 +1,99 @@
-- Import section
local new = require("util.class.new")
--
local MockSingleBlock = {
name = "MockSingleBlock"
}
function MockSingleBlock.setWorkAllowed(allow)
MockSingleBlock.workAllowed = allow
end
function MockSingleBlock.isWorkAllowed()
return MockSingleBlock.workAllowed
end
function MockSingleBlock.getAverageElectricInput()
return 0.0
end
function MockSingleBlock.getOwnerName()
return "gordominossi"
end
function MockSingleBlock.getEUStored()
return MockSingleBlock.storedEU
end
function MockSingleBlock.getWorkMaxProgress()
return MockSingleBlock.workMaxProgress
end
function MockSingleBlock.getSensorInformation()
return {
"§9gt.recipe.laserengraver§r",
"Progress:",
"§a2§r s / §e5§r s",
"Stored Energy:",
"§a1000§r EU / §e1000§r EU",
"Probably uses: §c0§r EU/t at §c0§r A",
n = 6
}
end
function MockSingleBlock.getEUOutputAverage()
return 128
end
function MockSingleBlock.getEUInputAverage()
return 128
end
function MockSingleBlock.getStoredEU()
return MockSingleBlock.storedEU
end
function MockSingleBlock.isMachineActive()
return MockSingleBlock.active
end
function MockSingleBlock.getOutputVoltage()
return MockSingleBlock.outputVoltage
end
function MockSingleBlock.getAverageElectricOutput()
return 0.0
end
function MockSingleBlock.hasWork()
return MockSingleBlock.workProgress < MockSingleBlock.workMaxProgress
end
function MockSingleBlock.getOutputAmperage()
return 2
end
function MockSingleBlock.getEUCapacity()
return MockSingleBlock.EUCapacity
end
function MockSingleBlock.getWorkProgress()
return MockSingleBlock.workProgress
end
function MockSingleBlock.getEUMaxStored()
return MockSingleBlock.EUCapacity
end
MockSingleBlock.__index = MockSingleBlock
function MockSingleBlock:new()
return new(self)
end
function MockSingleBlock:getEfficiencyPercentage()
return 100
end
return MockSingleBlock

View File

@ -0,0 +1,32 @@
-- Import section
local alarm = require('api.sound.alarm')
--
local function halt(machines)
alarm()
for i = 1, #machines do
machines[i]:setWorkAllowed(false)
end
end
local function resume(machines)
for i = 1, #machines do
machines[i]:setWorkAllowed(true)
end
end
local function exec(cleanroom, machines)
if (tonumber(cleanroom:getEfficiencyPercentage()) < 100) then
if (not cleanroom.isHalted) then
halt(machines)
cleanroom.isHalted = true
end
else
if (cleanroom.isHalted) then
resume(machines)
cleanroom.isHalted = false
end
end
end
return exec

View File

@ -0,0 +1,2 @@
return function (arg1, arg2, arg3)
end

View File

@ -0,0 +1,22 @@
-- Import section
local getConsumption = require("domain.energy.get-consumption-usecase")
local getProduction = require("domain.energy.get-production-usecase")
--
local function exec(energyProducers, energyBuffer)
-- local comsumption = getConsumption(energyBuffer)
-- local production = getProduction(energyBuffer)
local consumption = energyBuffer:getAverageInput()
local production = energyBuffer:getAverageOutput()
local energyCapacity = energyBuffer:getTotalEnergy().maximum
local timeToFull = energyCapacity / (production - consumption)
local timetoEmpty = -timeToFull
return {
consumption = consumption,
production = production,
timeToFull = timeToFull,
timetoEmpty = timetoEmpty
}
end
return exec

View File

@ -0,0 +1,3 @@
return function (arg1, arg2, arg3)
end

View File

@ -0,0 +1,3 @@
return function (arg1, arg2, arg3)
end

41
monitor-system/src/init.lua Executable file
View File

@ -0,0 +1,41 @@
-- Import section
Computer = require("computer")
Component = require("component")
MultiBlock = require("data.datasource.multi-block")
SingleBlock = require("data.datasource.single-block")
EnergyProvider = require("data.datasource.energy-provider")
local cleanroomAddresses = require("data.database.cleanroom")
local multiBlockAddresses = require("data.database.multi-blocks")
local energyBufferAddress = require("data.database.energy-buffer")
local protectCleanroomRecipes = require("domain.cleanroom.protect-recipes-usecase")
local getMultiblockStatuses = require("domain.multiblock.get-status-usecase")
local getEnergyStatus = require("domain.energy.get-energy-status-usecase")
--
local cleanroom = MultiBlock:new(multiBlockAddresses.cleanroom)
local cleanroomMachines = {}
for address in pairs(cleanroomAddresses.machines) do
table.insert(cleanroomMachines, SingleBlock:new(address))
end
local EBF11 = MultiBlock:new(multiBlockAddresses.EBF11)
local multiblocks = {cleanroom, EBF11}
local energyBuffer = EnergyProvider:new(energyBufferAddress)
local energyProducers = {}
local i = 1
while true do
if (i > 100) then
break
end
print(i)
protectCleanroomRecipes(cleanroom, cleanroomMachines)
local multiblockStatuses = getMultiblockStatuses(multiblocks)
local energyStatus = getEnergyStatus(energyProducers, energyBuffer)
os.sleep(0)
i = i + 1
end

View File

@ -0,0 +1,39 @@
local function inherits(...)
local newClass = {}
function newClass:new(object)
object = object or {}
setmetatable(object, newClass)
return object
end
local function search(key, parentList)
for i = 1, #parentList do
local value = parentList[i][key]
if value then
return value
end
end
end
local parents = {...}
for i = 1, #parents do
for key, value in pairs(parents[i]) do
newClass[key] = value
end
end
setmetatable(
newClass,
{
__index = function(table, key)
return search(key, parents)
end
}
)
newClass.__index = newClass
return newClass
end
return inherits

View File

@ -0,0 +1,13 @@
local function new(class, props)
local newObject = props or {}
for key, value in pairs(class) do
newObject[key] = value
end
setmetatable(newObject, {__index = class})
return newObject
end
return new

View File

@ -0,0 +1,36 @@
local Parser = {
parseProgress = function(progressString)
local a = "Progress: §a2§r s / §e5§r s"
return {current = 0, max = 1}
end,
parseStoredEnergy = function(storedEnergyString)
local noCommaString = string.gsub(storedEnergyString, ",", "")
local current = string.sub(noCommaString, string.find(noCommaString, "%ba§"))
current = string.gsub(current, "a", "")
current = tonumber((string.gsub(current, "§", "")))
local maximum = string.sub(noCommaString, string.find(noCommaString, "%be§"))
maximum = string.gsub(maximum, "e", "")
maximum = tonumber((string.gsub(maximum, "§", "")))
return {current = current, maximum = maximum}
end,
parseAverageInput = function(averageInputString)
local noCommaString = string.gsub(averageInputString, ",", "")
return tonumber((string.sub(noCommaString, string.find(noCommaString, "%d+"))))
end,
parseAverageOutput = function(averageOutputString)
local noCommaString = string.gsub(averageOutputString, ",", "")
return tonumber((string.sub(noCommaString, string.find(noCommaString, "%d+"))))
end,
parseProblems = function(problemsString)
return tonumber((string.gsub(string.sub(problemsString, string.find(problemsString, "c%d")), "c", "")))
end,
parseEfficiency = function(efficiencyString)
local noParagraphMarkString = string.gsub(efficiencyString, "§r", "")
local efficiency = string.sub(noParagraphMarkString, string.find(noParagraphMarkString, "%d+%.*%d*%s%%"))
return tonumber((string.gsub(efficiency, "%s%%", "")))
end
}
return Parser