Advertisement
Corbinhol

Fusion Reactor API

Mar 10th, 2023
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 KB | None | 0 0
  1. --Allows managing and controlling the reactor via network cards.
  2. --General Purpose API
  3. local component = require("component");
  4. local thread = require("thread");
  5. local event = require("event");
  6. local serialization = require("serialization");
  7. local modem;
  8.  
  9. function start()
  10.     return "does not exist"
  11. end
  12.  
  13. function stop()
  14.     return "does not exist"
  15. end
  16.  
  17. if component.isAvailable("tunnel") then
  18.     if api == nil then
  19.         modem = component.tunnel;
  20.         modem.open(60);
  21.         modem.setWakeMessage("wakeup");
  22.         apiData = {};
  23.         apiChannel = "";
  24.         apiAlias = "";
  25.  
  26.         local function respond(address, message1, message2, message3, message4, message5, message6, message7)
  27.             local out = modem.send(address, 60, "apiResponse", message1, message2, message3, message4, message5, message6, message7);
  28.             return out, message1;
  29.         end
  30.  
  31.         local function apiLoop()
  32.             while true do
  33.                 local _, _, from, _, _, _, arg1, arg2, arg3, arg4, arg5, arg6, arg7 = event.pull("modem_message", nil, nil, nil, nil, "api");
  34.                 if arg1 == nil then arg1 = "nothing"; else
  35.                 --print("Recieved " .. tostring(arg1));
  36.                 end
  37.                 --Commands
  38.                 if arg1 == "getdata" then
  39.                     if apiData[arg2] == nil then modem.send(from, 60, "nothing") else
  40.                         respond(from, apiData[arg2]);
  41.                     end
  42.                 elseif arg1 == "setdata" then
  43.                     local tempData = apiData[arg2];
  44.                     if tempData == nil then tempData = "nothing"; end
  45.                     apiData[arg2] = arg3;
  46.                     respond(from, true, tempData);
  47.                 elseif arg1 == "isOnline" then
  48.                     respond(from, true);
  49.                 elseif arg1 == "whois" then
  50.                     if arg2 == apiAlias then
  51.                         respond(from, "me");
  52.                     end
  53.                 elseif arg1 == "stop" then
  54.                     respond(from, stop())
  55.                 elseif arg1 == "start" then
  56.                     respond(from, start())
  57.                 else
  58.                     respond(from, "command unrecognized");
  59.                 end
  60.                 os.sleep(0);
  61.             end
  62.         end
  63.         --apiLoop();
  64.         api = thread.create(apiLoop);
  65.         api:detach();
  66.         end
  67.     end
  68. else
  69.     print("Error: No Tunnel...Not Starting API");
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement