Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- --- Generated by EmmyLua(https://github.com/EmmyLua)
- --- Created by cdbab.
- --- DateTime: 1/9/2025 7:22 AM
- ---
- if rednet.isOpen() then
- rednet.close()
- end
- local modem = peripheral.find("modem", rednet.open)
- local subnetStatus = false
- local delay = 120
- local authCode = 1111
- local cmdType = {AUTH = "authCode", CMD = "command", ARG = "var"}
- local function getSubnetStatus()
- return subnetStatus
- end
- local function getDelay()
- return delay
- end
- local function sendMsg(cmd, ...)
- local arg={...}
- print("sending message")
- local response
- local msg = {}
- msg["authCode"] = authCode
- msg["command"] = cmd
- print(tostring(#arg))
- if #arg > 0 then
- print("Args: "..tostring(arg[1]))
- msg["args"] = arg
- end
- rednet.broadcast(msg)
- end
- local function toggleSubnet()
- local previousStatus = subnetStatus
- subnetStatus = not subnetStatus
- redstone.setOutput("back",subnetStatus)
- return previousStatus ~= subnetStatus
- end
- local function handleMessage(msg)
- local response = ""
- local cmd = msg[cmdType.CMD]
- if cmd == "on" then
- delay = tonumber(string.sub(msg,4))
- toggleSubnet()
- os.sleep(delay)
- toggleSubnet()
- elseif cmd == "getSubnetStatus" then
- response = getSubnetStatus()
- elseif cmd == "getDelay" then
- response = getDelay()
- elseif cmd == "setDelay" then
- local arg = msg[cmdType.ARG]
- delay = arg[1]
- response = true
- end
- sendMsg(cmdType.ARG,response)
- end
- local function findXmit()
- while true do
- local id, message = rednet.receive()
- if message ~= nil then
- print("Message Received!")
- end
- print("is auth code valid: "..message[cmdType.AUTH]..", "..tostring(authCode)..": "..tostring(message[cmdType.AUTH] == authCode))
- if message[cmdType.AUTH] == authCode then
- handleMessage(message)
- end
- end
- end
- local function main()
- while true do
- local rsSignal = redstone.getAnalogInput("front")
- if rsSignal > 0 and not getSubnetStatus() then
- toggleSubnet()
- os.sleep(120)
- toggleSubnet()
- end
- os.sleep(.1)
- end
- end
- parallel.waitForAny(main, findXmit)
Advertisement
Add Comment
Please, Sign In to add comment