Advertisement
Guest User

wirelessAPI

a guest
Mar 3rd, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.01 KB | None | 0 0
  1. local isMaster = true -- true, ha szerver, false, ha kliens
  2. local status = "offline"
  3. local protocol = "workshop"
  4. local timerID = 0
  5.  
  6. if os.getComputerLabel() == nil then
  7.     os.setComputerLabel("networking"..os.getComputerID())
  8. end
  9.  
  10. function send(ID, msg)
  11.     rednet.send(ID, msg, protocol)
  12. end
  13.  
  14. function downloadStatus(ID)
  15.     send(ID, "sendstatus")
  16.     local sID, msg, dist, prot = rednet.receive(protocol)
  17.     return msg
  18. end
  19.  
  20. function getName(ID)
  21.     send(ID, "sendname", protocol)
  22.     local sID, msg, dist, prot = rednet.receive(protocol)
  23.     return msg
  24. end
  25.  
  26. function sendName(ID)
  27.     send(ID, os.getComputerLabel())
  28. end
  29.  
  30.  
  31. function getStatus(ID)
  32.     if ID == nil or ID == os.getComputerID() then
  33.         return status
  34.     end
  35.    
  36.     return downloadStatus(ID)
  37. end
  38.  
  39.  
  40. function setStatus(ID, stat)
  41.     if ID == nil or ID == os.getComputerID() then
  42.         status = stat
  43.     end
  44.    
  45.     if isMaster then
  46.         send(ID, stat)
  47.     end
  48. end
  49.  
  50. function sendStatus(ID)
  51.     send(ID, status)
  52. end
  53.  
  54. function turnOn(ID)
  55.     setStatus(ID, "0")
  56. end
  57.  
  58. function turnOff(ID)
  59.     setStatus(ID, "offline")
  60. end
  61.  
  62. function open(side)
  63.     if not rednet.isOpen(side) then
  64.         rednet.open(side)
  65.     end
  66. end
  67.  
  68. function close(side)
  69.     if rednet.isOpen(side) then
  70.         rednet.close(side)
  71.     end
  72. end
  73.  
  74. function getClients()
  75.     return rednet.lookup(protocol)
  76. end
  77.  
  78. function joinProtocol(prot)
  79.     protocol = prot
  80.     rednet.host(protocol, os.getComputerLabel())
  81. end
  82.  
  83. function leaveProtocol()
  84.     rednet.unhost(protocol, os.getComputerLabel())
  85. end
  86.  
  87. function init(master, side, prot, stat)
  88.     isMaster = master
  89.     status = stat
  90.     open(side)
  91.     joinProtocol(prot)
  92. end
  93.  
  94. function listen(timeout)
  95.     if timeout ~= nil then
  96.         timerID = os.startTimer(timeout)
  97.     end
  98.     local event, p2, p3, p4, p5 = os.pullEvent()
  99.     if ((event == "timer") and (p2 == timerID)) then
  100.         timerID = 0
  101.         return nil
  102.     end
  103.     if p3 == "sendstatus" then
  104.         sendStatus(ID)
  105.         print("Status sent") -- debug
  106.     end
  107.     if p3 == "sendname" then
  108.         sendName(ID)
  109.         print("Name sent") -- debug
  110.         return nil
  111.     end
  112.     return event, p2, p3, p4, p5
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement