Guest User

newClient.lua

a guest
Jul 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  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.identify then
  53.                 if data.identify == key then
  54.                     print("Received signal")
  55.                     local handle = fs.open("/rrp/" .. name,"w")
  56.                     handle.writeLine(data.channel)
  57.                     handle.writeLine(data.header)
  58.                     handle.close()
  59.                     close(65000)
  60.                     break
  61.                 end
  62.             end
  63.         end
  64.     end
  65.     parallel.waitForAny(sync,wait)
  66.     sleep(2)
  67. end
  68.  
  69. local function on()
  70.     local prof = selectProfile()
  71.     send(prof.ch,prof.head .. "on")
  72.     print("Done")
  73.     sleep(1)
  74.     close(prof.ch)
  75. end
  76.  
  77. local function off()
  78.     local prof = selectProfile()
  79.     send(prof.ch,prof.head .. "off")
  80.     print("Done")
  81.     sleep(1)
  82.     close(prof.ch)
  83. end
  84.  
  85. local function click()
  86.     local prof = selectProfile()
  87.     send(prof.ch,prof.head .. "on")
  88.     sleep(1)
  89.     send(prof.ch,prof.head .. "off")
  90.     print("Done")
  91.     sleep(1)
  92.     close(prof.ch)
  93. end
  94.  
  95. local function main()
  96.     while true do
  97.     term.clear()
  98.     term.setCursorPos(1,1)
  99.     print("RemoteRedstone  Client by Rahph")
  100.     print("")
  101.     print("1) Add profile")
  102.     print("2) Enable signal on profile")
  103.     print("3) Disable signal on profile")
  104.     print("4) Simulate button press on profile\n")
  105.     print("9) Exit")
  106.     local sel = read()
  107.     if sel == "1" then
  108.         add()
  109.     elseif sel == "2" then
  110.         on()
  111.     elseif sel == "3" then
  112.         off()
  113.     elseif sel == "4" then
  114.         click()
  115.     elseif sel == "9" then
  116.         break
  117.     else
  118.         print("Uhhhh... no!")
  119.         sleep(2)
  120.     end
  121.     end
  122. end
  123. main()
Advertisement
Add Comment
Please, Sign In to add comment