Necrotico

OC Logistics Robot MK1 Updater

Sep 13th, 2021 (edited)
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. -- Logistics Robot Update
  2. -- Program to be run first on a wireless-capable computer with an updated "/home/LRobot.lua" file, then on a wireless-capable robot within range to receive that file.
  3.  
  4. local component = require("component")
  5. local event = require("event")
  6. local m = component.modem -- get primary modem component
  7. local fs = require("filesystem")
  8. local term = require("term")
  9.  
  10. local PORT = 10
  11.  
  12. local argv = {...}
  13.  
  14. local mode = "receive" -- "send" or "receive"
  15. if argv[1] == "send" then
  16.     mode = "send"
  17. end
  18.  
  19. local signal_strength = tonumber(argv[2])
  20.  
  21. local function writeFile(path, data_string)
  22.     local f_h = fs.open(path, "w")
  23.     f_h:write(data_string)
  24.     f_h:close()
  25. end
  26.  
  27. local function getMessage()
  28.     local _, _, from, port, _, message = event.pull("modem_message")
  29.     return message
  30. end
  31.  
  32. m.open(PORT)
  33. m.setStrength(signal_strength)
  34.  
  35. if mode == "send" then
  36.     print("Preparing to send file...")
  37.     local max_send_length = 250
  38.     if not fs.exists("/home/LRobot.lua") then
  39.         print("ERROR: No such file '/home/LRobot.lua!'")
  40.         os.exit()
  41.     end
  42.     local send_h = fs.open("/home/LRobot.lua", "r")
  43.  
  44.     local send_total_length = fs.size("/home/LRobot.lua")
  45.     local total_sent = 0
  46.  
  47.     local next_send_length = 0
  48.     local next_send_string = ""
  49.     local function getNextSendString()
  50.         next_send_length = math.min(max_send_length, send_total_length - total_sent)
  51.         next_send_string = send_h:read(next_send_length)
  52.     end
  53.  
  54.     getNextSendString()
  55.     while total_sent < send_total_length do
  56.         term.clear()
  57.         local broadcast_status = m.broadcast(PORT, next_send_string)
  58.         if not broadcast_status then
  59.             print("ERROR: Couldn't broadcast!")
  60.             os.exit()
  61.         end
  62.         local msg = getMessage()
  63.         if msg == "next" then
  64.             total_sent = total_sent + next_send_length
  65.             getNextSendString()
  66.         end
  67.         if total_sent == 0 then
  68.             print("Waiting for receiver to accept data...")
  69.         else
  70.             print("Sending File. (" .. tostring(math.floor(((total_sent / send_total_length) * 100) * 100) / 100) .. "%)")
  71.         end
  72.         os.sleep(1)
  73.     end
  74.     term.clear()
  75.  
  76.     m.broadcast(PORT, "DOWNLOAD_END")
  77.  
  78.     print("File sent successfully!")
  79.  
  80.     send_h:close()
  81. elseif mode == "receive" then
  82.     local received_data = ""
  83.  
  84.     local data_end = false
  85.     while not data_end do
  86.         term.clear()
  87.         print("Listening...")
  88.         local msg = getMessage()
  89.         if msg then
  90.             if msg == "DOWNLOAD_END" then
  91.                 data_end = true
  92.             else
  93.                 received_data = received_data .. msg
  94.                 print("Packet Received!")
  95.                 m.broadcast(PORT, "next")
  96.             end
  97.         else
  98.             print("Waiting for next packet...")
  99.         end
  100.         os.sleep(1)
  101.     end
  102.  
  103.     writeFile("/home/LRobot.lua", received_data)
  104.     print("Download Complete!")
  105. end
  106.  
  107. m.close(PORT)
Add Comment
Please, Sign In to add comment