Advertisement
Guest User

rsslave

a guest
May 19th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. ------------------------------------------------------------------
  2. ------------------------------------------------------------------
  3.  
  4. local codename = 'rsslave'
  5. local codeversion = '0.1.1.0'
  6. local whoami = tonumber(os.getComputerID ())
  7. local verbose = false
  8.  
  9. term.clear ()
  10. print (">>>"..codename.." Version "..codeversion.."@ID:"..whoami)
  11.  
  12. ------------------------------------------------------------------
  13. ------------------------------------------------------------------
  14.  
  15. local msg_expected_paramcount = 4 -- message format: <SenderID>|<ReceiverID>|<Secret>|<Command>
  16. local shared_secret = "OpenUpMofo"
  17. local listenChannel = 59999
  18. local config_filename = 'rsslave.conf'
  19. local current_status = false
  20.  
  21. ------------------------------------------------------------------
  22.  
  23. local function parseMessage (msg)
  24.     local msgparam = {}
  25.     for param in string.gmatch (msg, "[^|]+") do
  26.         table.insert (msgparam, param)
  27.     end
  28.    
  29.     return msgparam
  30. end
  31.  
  32. ------------------------------------------------------------------
  33.  
  34. local function readConfig ()
  35.     local cfgfile = fs.open (config_filename, 'r')
  36.    
  37.     if (cfgfile == nil) then
  38.         print ("*error reading config: "..config_filename)
  39.     else
  40.         local rsout = cfgfile.readLine ()
  41.         cfgfile.close ()
  42.         print ("*read from config: "..rsout)
  43.         return (rsout)
  44.     end
  45. end
  46.  
  47. ------------------------------------------------------------------
  48.  
  49.  
  50.  
  51. -- MAIN ----------------------------------------------------------
  52.  
  53. os.loadAPI ('calicommAPI')
  54. --calicommAPI.enableDebug (true)
  55. calicommAPI.initModems ()
  56.  
  57. local rs_signal_output = readConfig ()
  58.  
  59. calicommAPI.openModemChannel (calicommAPI.modemtable[1][1], listenChannel)
  60.  
  61. while true do
  62.     print "*listening..."
  63.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  64.    
  65.     local msg_elements = {}
  66.     msg_elements = parseMessage (message)
  67.    
  68.     if (msg_expected_paramcount ~= #msg_elements) then
  69.         print ("*invalid number of message elements")
  70.     elseif (whoami ~= tonumber(msg_elements[2])) then
  71.         print ("*message is not for me")
  72.     elseif (shared_secret ~= msg_elements[3]) then
  73.         print ("*sender failed to authenticate")
  74.     else
  75.         print ("*found valid message")
  76.         if (msg_elements[4] == 'on') then
  77.             print ("*redstone signal @"..rs_signal_output..":ON")
  78.             redstone.setOutput (rs_signal_output, true)
  79.             current_status = true
  80.         elseif (msg_elements[4] == 'off') then
  81.             print ("*redstone signal @"..rs_signal_output..":OFF")
  82.             redstone.setOutput (rs_signal_output, false)
  83.             current_status = false
  84.         end
  85.     end
  86. end
  87. calicommAPI.closeModemChannel (calicommAPI.modemtable[1][1], listenChannel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement