Rahph

rrp-client

Jul 25th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function send(ch,msg)
  2.     local modems = {peripheral.find("modem")}
  3.     local ch = tonumber(ch)
  4.     for k,v in pairs(modems) do
  5.         v.open(ch)
  6.     end
  7.     for k,v in pairs(modems) do
  8.         v.transmit(ch,ch,msg)
  9.     end
  10. end
  11.  
  12. local function close(ch)
  13.     ch = tonumber(ch)
  14.     local modems = {peripheral.find("modem")}
  15.     for k,v in pairs(modems) do
  16.         v.close(ch)
  17.     end
  18. end
  19.  
  20. local function selectProfile()
  21.     local profiles = fs.list("/rrp")
  22.     for i=1,#profiles do
  23.         print(i .. ") " .. profiles[i])
  24.     end
  25.     print("Select")
  26.     local sel = tonumber(read())
  27.     local prof = {}
  28.     local handle = fs.open("/rrp/" .. profiles[sel],"r")
  29.     prof.ch = handle.readLine()
  30.     prof.head = handle.readLine()
  31.     handle.close()
  32.     return prof
  33. end
  34.  
  35. local function add()
  36.     print("Sync key?")
  37.     local key = read()
  38.     print("Profile name?")
  39.     local name = read()
  40.     --send(65000,key)
  41.     local function wait()
  42.         sleep(5)
  43.         error("Synchronisation failed!")
  44.     end
  45.     local function sync()
  46.         local modems = {peripheral.find("modem")}
  47.         for k,v in pairs(modems) do v.open(65000) end
  48.         send(65000,key)
  49.         while true do
  50.             local r = {os.pullEvent("modem_message")}
  51.             local data = textutils.unserialise(r[5])
  52.             if data then
  53.                 if data.identify then
  54.                     if data.identify == key then
  55.                         print("Received signal")
  56.                         local handle = fs.open("/rrp/" .. name,"w")
  57.                         handle.writeLine(data.channel)
  58.                         handle.writeLine(data.header)
  59.                         handle.close()
  60.                         close(65000)
  61.                         break
  62.                     end
  63.                 end
  64.             end
  65.         end
  66.     end
  67.     parallel.waitForAny(sync,wait)
  68.     sleep(2)
  69. end
  70.  
  71. local function on()
  72.     local prof = selectProfile()
  73.     send(prof.ch,prof.head .. "on")
  74.     print("Done")
  75.     sleep(1)
  76.     close(prof.ch)
  77. end
  78.  
  79. local function off()
  80.     local prof = selectProfile()
  81.     send(prof.ch,prof.head .. "off")
  82.     print("Done")
  83.     sleep(1)
  84.     close(prof.ch)
  85. end
  86.  
  87. local function click()
  88.     local prof = selectProfile()
  89.     send(prof.ch,prof.head .. "on")
  90.     sleep(1)
  91.     send(prof.ch,prof.head .. "off")
  92.     print("Done")
  93.     sleep(1)
  94.     close(prof.ch)
  95. end
  96.  
  97. local function main()
  98.     while true do
  99.     term.clear()
  100.     term.setCursorPos(1,1)
  101.     print("RemoteRedstone  Client by Rahph")
  102.     print("")
  103.     print("1) Add profile")
  104.     print("2) Enable signal on profile")
  105.     print("3) Disable signal on profile")
  106.     print("4) Simulate button press on profile\n")
  107.     print("9) Exit")
  108.     local sel = read()
  109.     if sel == "1" then
  110.         add()
  111.     elseif sel == "2" then
  112.         on()
  113.     elseif sel == "3" then
  114.         off()
  115.     elseif sel == "4" then
  116.         click()
  117.     elseif sel == "9" then
  118.         break
  119.     else
  120.         print("Uhhhh... no!")
  121.         sleep(2)
  122.     end
  123.     end
  124. end
  125. main()
Add Comment
Please, Sign In to add comment