local configPath="config"
-- ========= TROVA PERIFERICHE =========
local function findPeripheral(t)
for _,side in ipairs(rs.getSides())do
if peripheral.isPresent(side)and peripheral.getType(side)==t then
return peripheral.wrap(side),side
end
end
return nil
end
-- ========= CONFIGURAZIONE =========
local function loadConfig()
if not fs.exists(configPath)then return nil end
local f=fs.open(configPath,"r")
local data=f.readAll() f.close()
local func=loadstring(data)
if func then return func() else error("Errore nel config") end
end
term.setTextColor(colors.yellow)
print("Avvio modulo reattore...")
sleep(0.3)
local config=loadConfig()
if not config then
local validColors = {}
for k, v in pairs(colors) do
validColors[k] = v
end
term.setTextColor(colors.white)
print("Configurazione non trovata. Creazione guidata...\n")
local myID
local serverID
local color
repeat
write("ID di questo reattore: ") myID=tonumber(read())
until myID
repeat
write("ID del server: ") serverID=tonumber(read())
until serverID
repeat
write("Colore asseganto al reattore(white,blue ...): \n") color = read()
until validColors[color]
config={id=myID, server_id=serverID, color = color, update_interval=2}
local f=fs.open(configPath,"w")
f.write("return "..textutils.serialize(config))
f.close()
end
if type(config.update_interval)~="number"then config.update_interval=2 end
term.setTextColor(colors.cyan)
print("\n[ Inizializzazione periferiche ]")
sleep(0.3)
local modem,modemSide=findPeripheral("modem")
if not modem then error("Nessun modem trovato!")end
rednet.open(modemSide)
term.setTextColor(colors.lime)
print("✔ Modem su "..modemSide)
sleep(0.2)
local reader,readerSide=findPeripheral("nuclearReader")
if not reader then error("Advanced Nuclear Information Reader non trovato!")end
term.setTextColor(colors.lime)
print("✔ Sensore su "..readerSide)
sleep(0.4)
term.setTextColor(colors.yellow)
print("\nIn attesa di comandi dal server " .. config.server_id .. "...")
sleep(1)
term.clear()
term.setCursorPos(1,1)
-- ========= FUNZIONE DATI =========
local function leggiDati()
local a,b,c,info=reader.get(1)
local a,b,c,infoShunt=reader.get(2)
local data={
id=config.id,
temp=tonumber(info["heat"] or 0),
maxHeat=tonumber(info["maxHeat"] or 1),
maxOutput=tonumber(info["output"] or 0),
effectiveOutput = tonumber(infoShunt["average"] or 0),
active=info["reactorPoweredB"] or false
}
return data
end
-- ========= LOOP =========
print("Aspetto master...")
while true do
term.setTextColor(colors.white)
local sender, msgTmp=rednet.receive()
msg = textutils.unserialize(msgTmp)
if sender==config.server_id then
if msg.command =="reactor_info" and type(msg)=="table" then
term.setTextColor(colors.lightBlue)
print("Richiesta dati ricevuta dal server.")
local data=leggiDati()
term.setTextColor(colors.cyan)
print(string.format("Temp:%d Max:%d EU:%d MAXEU: %d Stato:%s",
data.temp,data.maxHeat,data.effectiveOutput,data.maxOutput,tostring(data.active)))
term.setTextColor(colors.yellow)
print("Invio dati al server...")
local result = textutils.serialize({type="info",data=data})
rednet.send(config.server_id,result)
term.setTextColor(colors.lime)
print("Dati inviati con successo!\n")
elseif msg.command =="reactor_control" and type(msg)=="table"then
term.setTextColor(colors.orange)
print("Comando di controllo ricevuto.")
local state=msg.newState
if type(state)=="boolean"then
if state then
rs.setBundledOutput("back",colors[config.color])
else
rs.setBundledOutput("back",0)
end
local result = textutils.serialize({type="control_result",success=true})
rednet.send(config.server_id, result)
print("Stato cambiato a "..tostring(state))
end
end
end
sleep(0.1)
end