Advertisement
ComputerMan123

HouseControl

Nov 21st, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. --[[
  2. HouseControl by houseofkraft
  3. ]]--
  4.  
  5. local args = { ... }
  6.  
  7. if #args < 1 then
  8. printError("Usages:")
  9. printError("housecontrol host")
  10. printError("housecontrol join <ID>")
  11. error()
  12. end
  13.  
  14. function pushEvent()
  15. while true do
  16. local e = {os.pullEvent()}
  17. info = {e}
  18. rednet.send(sid, "event-stc", info)
  19. end
  20. end
  21.  
  22. function respondRednet()
  23. while true do
  24. id, message, info = rednet.receive()
  25. if message == "ping" then
  26. rednet.send(id, "pong")
  27. elseif message == "event-cts" then
  28. local event = info[1]
  29. local p2 = info[2]
  30. local p3 = info[3]
  31. local p4 = info[4]
  32. local p5 = info[5]
  33. -- Usually events don't exceed 4 args, If the events do, feel free to change this
  34. os.queueEvent(event, p2, p3, p4, p5)
  35. elseif message == "disconnect" then
  36. print("The client has disconnected from you.")
  37. break
  38. end
  39. end
  40. end
  41.  
  42.  
  43. if args[1] == "host" then
  44. -- Define custom functions
  45. local oldPrint = print
  46. local oldWrite = write
  47. print = function(msg)
  48. oldPrint(msg)
  49. info = {msg}
  50. rednet.send(sid, "print", info)
  51. end
  52. write = function(msg)
  53. oldWrite(msg)
  54. info = {msg}
  55. rednet.send(sid, "write", info)
  56. end
  57. term.write = function(msg)
  58. oldWrite(msg)
  59. info = {msg}
  60. rednet.send(sid, "write", info)
  61. end
  62. term.clear = function()
  63. oldClear()
  64. info = {msg}
  65. rednet.send(sid, "clear")
  66. end
  67. while true do
  68. id, message, info = rednet.receive()
  69. if message == "connect" then
  70. local id = info[1]
  71. if id == os.getComputerID() then
  72. rednet.send(id, "sucess")
  73. sid = id
  74. parallel.waitForAny(pushEvent, respondRednet)
  75. end
  76. end
  77. end
  78. elseif args[1] == "join" then
  79. local function listen()
  80. while true do
  81. id, message, info = rednet.receive(1)
  82. if message == "clear" then
  83. term.clear()
  84. elseif message == "print" then
  85. local text = info[1]
  86. print(tostirng(text))
  87. elseif message == "write" then
  88. local text = info[1]
  89. print(tostirng(text))
  90. elseif message == "event-stc" then
  91. local event = info[1]
  92. local p2 = info[2]
  93. local p3 = info[3]
  94. local p4 = info[4]
  95. local p5 = info[5]
  96. os.queueEvent(event, p2, p3, p4, p5)
  97. end
  98. end
  99. end
  100. local function pullEvent()
  101. while true do
  102. local e = {os.pullEvent()}
  103. info = {e}
  104. rednet.send(sid, "event-cts", info)
  105. end
  106. end
  107. if #args < 2 then
  108. printError("Usage: housecontrol join <ID>")
  109. error()
  110. end
  111. local sid = args[2]
  112. info = {sid}
  113. rednet.send(sid, "connect", info)
  114. id, message = rednet.receive()
  115. if message == "sucess" then
  116. parallel.waitForAll(listen, pullEvent)
  117. else
  118. printError("No response!")
  119. error()
  120. end
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement