Encreedem

Bluetooth AddOn

Jul 17th, 2013
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.59 KB | None | 0 0
  1. objects = [[
  2. <objects>
  3.   <object>
  4.     objectID=sendFile
  5.     objectType=Button
  6.     defaultWidth=14
  7.     defaultHeight=3
  8.     text=Send file
  9.   </object>
  10.   <object>
  11.     objectID=recFile
  12.     objectType=Button
  13.     defaultWidth=14
  14.     defaultHeight=3
  15.     text=Receive file
  16.   </object>
  17.   <object>
  18.     objectID=fileInput
  19.     objectType=Input
  20.     message=Please enter the name of the file that you want to send.
  21.   </object>
  22.   <object>
  23.     objectID=idInput
  24.     objectType=Input
  25.     message=Please enter the ID of the computer.
  26.   </object>
  27. </objects>
  28. ]]
  29.  
  30. local args = { ... }
  31. local callType
  32. local objectID
  33. local systemInfo
  34.  
  35. local modemSide = "left"
  36.  
  37. function sendFile()
  38.   computerID = systemInfo.userInputs.idInput
  39.   filename = systemInfo.userInputs.fileInput
  40.  
  41.   if (computerID == nil or filename == nil or not fs.exists(filename)) then
  42.     return
  43.   end
  44.  
  45.   finished = false
  46.   while not finished do
  47.     rednet.send(computerID, "bluetooth.connect")
  48.     os.startTimer(1)
  49.     local e,p1,p2,p3 = os.pullEvent()
  50.     if e == "rednet_message" then
  51.       local id,msg = p1,p2
  52.       if msg == "bluetooth.accept" then
  53.         finished = true
  54.       end
  55.     elseif e == "monitor_touch" then
  56.       return
  57.     end
  58.   end
  59.  
  60.   sleep(0.8)
  61.   rednet.send(computerID, "bluetooth.filename:" .. filename)
  62.  
  63.   sleep(0.5)
  64.     local sr = fs.open(filename, "r")
  65.    
  66.     local data
  67.     while true do
  68.       data = sr.readLine()
  69.       if data == nil then
  70.         break
  71.       end
  72.       rednet.send(computerID, "bluetooth.file:" .. data)
  73.       sleep(0.1)
  74.     end
  75.     rednet.send(computerID, "bluetooth.done")
  76. end
  77.  
  78. function receiveFile()
  79.   while true do
  80.     local e,p1,p2,p3 = os.pullEvent()
  81.     if e == "rednet_message" then
  82.       local id,msg = p1,p2
  83.       if msg == "bluetooth.scan" then
  84.         sleep(0.1)
  85.         rednet.send(id, "bluetooth.scan.reply:" .. os.getComputerLabel())
  86.       elseif msg == "bluetooth.connect" then
  87.         local remoteID = id
  88.         rednet.send(id, "bluetooth.accept")
  89.        
  90.         local filename
  91.        
  92.         local id,msg = "",""
  93.         while true do
  94.           id,msg = rednet.receive()
  95.          
  96.           if id == remoteID and string.sub(msg, 1, #"bluetooth.filename:") == "bluetooth.filename:" then
  97.             filename = string.sub(msg, #"bluetooth.filename:" + 1)
  98.             break
  99.           end
  100.         end
  101.        
  102.         if fs.exists(filename) then
  103.           fs.delete(filename)
  104.         end
  105.        
  106.         local file = fs.open(filename, "w")
  107.        
  108.         local id,msg
  109.         while true do
  110.           id,msg = rednet.receive()
  111.           if id == remoteID then
  112.             if string.sub(msg, 1, #"bluetooth.file:") == "bluetooth.file:" then
  113.               local subbedData = string.sub(msg, #"bluetooth.file:" + 1)
  114.              
  115.               file.writeLine(subbedData)
  116.             elseif msg == "bluetooth.done" then
  117.               file.flush()
  118.               file.close()
  119.              
  120.               break
  121.             end
  122.           end
  123.         end
  124.       end
  125.     elseif e == "monitor_touch" then
  126.       break
  127.     end
  128.   end
  129. end
  130.  
  131. function showObject()
  132.  
  133. end
  134.  
  135. function callFunction()
  136.   rednet.open(modemSide)
  137.  
  138.   if (objectID == "recFile") then
  139.     receiveFile()
  140.   elseif (objectID == "sendFile") then
  141.     sendFile()
  142.   end
  143. end
  144.  
  145. function main()
  146.   if (#args == 3) then
  147.     callType = args[1]
  148.     objectID = args[2]
  149.     systemInfo = textutils.unserialize(args[3])
  150.    
  151.     if (callType == "Show") then
  152.       showObject()
  153.     elseif (callType == "Click") then
  154.       callFunction()
  155.     end
  156.   end
  157. end
  158.  
  159. main()
Advertisement
Add Comment
Please, Sign In to add comment