From 87ba71cd762390fa48f930055255f9d118cbfcd9 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Thu, 29 Dec 2022 10:46:41 +0100 Subject: [PATCH] Added quantum-beacon. --- quantum-beacon.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 quantum-beacon.lua diff --git a/quantum-beacon.lua b/quantum-beacon.lua new file mode 100644 index 0000000..4607446 --- /dev/null +++ b/quantum-beacon.lua @@ -0,0 +1,42 @@ +--[[ + Quantum Beacon + +A script that runs on computers connected to an AE quantum ring to ping their +status back to a central computer. +]] + +local SEND_CHANNEL = 100 +local RECEIVE_CHANNEL = 101 +local NODE_NAME = "TMP" +local TRANSMIT_INTERVAL = 15 + +local function getPeripheralOrWait(name) + local p = nil + repeat + p = peripheral.find(name) + if p == nil then + print("Error: Couldn't find an attached peripheral with name \"" .. name .. "\". Attach one please.") + os.pullEvent("peripheral") + end + until p ~= nil + return p +end + +-- We consider a foreign quantum link to be connected if we detect crafting +-- CPUs on the network, since subnetworks shouldn't ever have these. +local function meSystemConnected(meBridge) + local craftingCpus = meBridge.getCraftingCPUs() + return craftingCpus ~= nil and #craftingCpus > 0 +end + +while true do + local modem = getPeripheralOrWait("modem") + local meBridge = getPeripheralOrWait("meBridge") + local packet = { + node = NODE_NAME, + date = os.date("*t"), + online = meSystemConnected(meBridge) + } + modem.transmit(SEND_CHANNEL, RECEIVE_CHANNEL, packet) + os.sleep(TRANSMIT_INTERVAL) +end