April_The_Sergal

METoggleController

Jan 9th, 2025 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by cdbab.
  4. --- DateTime: 1/9/2025 7:22 AM
  5. ---
  6. if rednet.isOpen() then
  7.     rednet.close()
  8. end
  9. local modem = peripheral.find("modem", rednet.open)
  10. local subnetStatus = false
  11. local delay = 120
  12. local authCode = 1111
  13. local cmdType = {AUTH = "authCode", CMD = "command", ARG = "var"}
  14.  
  15. local function getSubnetStatus()
  16.     return subnetStatus
  17. end
  18.  
  19. local function getDelay()
  20.     return delay
  21. end
  22.  
  23. local function sendMsg(cmd, ...)
  24.     local arg={...}
  25.     print("sending message")
  26.     local response
  27.     local msg = {}
  28.     msg["authCode"] = authCode
  29.     msg["command"] = cmd
  30.     print(tostring(#arg))
  31.     if #arg > 0 then
  32.         print("Args: "..tostring(arg[1]))
  33.         msg["args"] = arg
  34.     end
  35.     rednet.broadcast(msg)
  36. end
  37.  
  38.  
  39. local function toggleSubnet()
  40.     local previousStatus = subnetStatus
  41.     subnetStatus = not subnetStatus
  42.     redstone.setOutput("back",subnetStatus)
  43.     return previousStatus ~= subnetStatus
  44. end
  45.  
  46. local function handleMessage(msg)
  47.     local response = ""
  48.     local cmd = msg[cmdType.CMD]
  49.     if cmd == "on" then
  50.         delay = tonumber(string.sub(msg,4))
  51.         toggleSubnet()
  52.         os.sleep(delay)
  53.         toggleSubnet()
  54.     elseif cmd == "getSubnetStatus" then
  55.         response = getSubnetStatus()
  56.     elseif cmd == "getDelay" then
  57.         response = getDelay()
  58.     elseif cmd == "setDelay" then
  59.         local arg = msg[cmdType.ARG]
  60.         delay = arg[1]
  61.         response = true
  62.     end
  63.     sendMsg(cmdType.ARG,response)
  64. end
  65.  
  66. local function findXmit()
  67.     while true do
  68.         local id, message = rednet.receive()
  69.         if message ~= nil then
  70.             print("Message Received!")
  71.         end
  72.         print("is auth code valid: "..message[cmdType.AUTH]..", "..tostring(authCode)..": "..tostring(message[cmdType.AUTH] == authCode))
  73.         if message[cmdType.AUTH] == authCode then
  74.             handleMessage(message)
  75.         end
  76.     end
  77. end
  78.  
  79. local function main()
  80.     while true do
  81.         local rsSignal = redstone.getAnalogInput("front")
  82.         if rsSignal > 0 and not getSubnetStatus() then
  83.             toggleSubnet()
  84.             os.sleep(120)
  85.             toggleSubnet()
  86.         end
  87.         os.sleep(.1)
  88.     end
  89. end
  90.  
  91. parallel.waitForAny(main, findXmit)
Advertisement
Add Comment
Please, Sign In to add comment