maxheyer

Untitled

Jan 14th, 2018
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.99 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local term = require("term")
  4. local thread = require("thread")
  5.  
  6. local modem = component.modem
  7.  
  8. -- Constants
  9. local PORT = 1
  10. local PING = "ping"
  11. local PONG = "pong"
  12.  
  13. local devices = {}
  14.  
  15. -- Open port if needed
  16. if modem.isOpen(PORT) == false then
  17.   modem.open(PORT)
  18.   print("PORT '" .. PORT .. "' opened successfully!")
  19. end
  20.  
  21. -- Read whole file
  22. local function readAll(file)
  23.     local f = assert(io.open(file, "r"))
  24.  
  25.     local content = f:read("*all")
  26.     f:close()
  27.     return content
  28. end
  29.  
  30. -- Quit our program
  31. local function quit()
  32.   os.exit(1)
  33. end
  34.  
  35. -- Remove the first element of a table
  36. local function reorderParameters(parameters)
  37.   local reordered = {}
  38.   for k, param in pairs(parameters) do
  39.     if k > 1 then
  40.       reordered[k-1] = param
  41.     end
  42.   end
  43.  
  44.   return reordered
  45. end
  46.  
  47. -- Send network message to all registered devices
  48. local function sendToDevices(...)
  49.   for _, device in pairs(devices) do
  50.     modem.broadcast(PORT, ...)
  51.   end
  52. end
  53.  
  54. -- Commands
  55. local commands = {
  56.   -- Find all IoT devices
  57.   discover = function()
  58.     devices = {}
  59.  
  60.     local collectThread = thread.create(function()
  61.       print("Discover Internet of Things...")
  62.       modem.broadcast(PORT, PING)
  63.       while true do
  64.         local _, _, remoteAddress, _, _, payload = event.pull("modem_message")
  65.         if payload == PONG then
  66.           table.insert(devices, remoteAddress)
  67.         end
  68.       end
  69.     end)
  70.  
  71.     -- Wait some seconds and kill the thread
  72.     collectThread:join(2)
  73.     collectThread:kill()
  74.     print(#devices .. " devices found!")
  75.   end,
  76.   -- Load program from filesystem and execute on all devices
  77.   executefile = function(filename)
  78.     if not filename then
  79.       print("Usage:")
  80.       print("executeFile <filename>")
  81.       return false
  82.     end
  83.     local program = readAll(filename)
  84.  
  85.     if program == nil then
  86.       print("File not found!")
  87.       return false
  88.     end
  89.  
  90.     print("Execute '" .. filename .. "' on " .. #devices .. " devices...")
  91.     sendToDevices(tostring(program))
  92.   end,
  93.   clear = function()
  94.     term.clear()
  95.   end
  96. }
  97. -- Find devices on startup
  98. commands.discover()
  99.  
  100. -- Terminal history for up and down navigation
  101. local history = {}
  102.  
  103. --  Main loop
  104. while term.isAvailable() do
  105.   -- Prefix input field
  106.   io.write("\27[33mcommand> \27[37m")
  107.  
  108.   -- Read input
  109.   local input = term.read(history)
  110.   if not input then quit() end
  111.  
  112.   -- Push input to history
  113.   table.insert(history, input)
  114.  
  115.   -- Get command parameters
  116.   local parameters = {}
  117.   for param in input:gmatch("%S+") do
  118.     table.insert(parameters, param)
  119.   end
  120.  
  121.   -- Define our command
  122.   local command = parameters[1]
  123.  
  124.   -- Remove command from parameters
  125.   parameters = reorderParameters(parameters)
  126.  
  127.   -- Check whether the command does not exist or should be called
  128.   if commands[command] == nil then
  129.     print("Command not found!")
  130.   else
  131.     pcall(commands[command], table.unpack(parameters))
  132.   end
  133. end
Add Comment
Please, Sign In to add comment