Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------------------------------
- ------------------------------------------------------------------
- local codename = 'rsslave'
- local codeversion = '0.1.1.0'
- local whoami = tonumber(os.getComputerID ())
- local verbose = false
- term.clear ()
- print (">>>"..codename.." Version "..codeversion.."@ID:"..whoami)
- ------------------------------------------------------------------
- ------------------------------------------------------------------
- local msg_expected_paramcount = 4 -- message format: <SenderID>|<ReceiverID>|<Secret>|<Command>
- local shared_secret = "OpenUpMofo"
- local listenChannel = 59999
- local config_filename = 'rsslave.conf'
- local current_status = false
- ------------------------------------------------------------------
- local function parseMessage (msg)
- local msgparam = {}
- for param in string.gmatch (msg, "[^|]+") do
- table.insert (msgparam, param)
- end
- return msgparam
- end
- ------------------------------------------------------------------
- local function readConfig ()
- local cfgfile = fs.open (config_filename, 'r')
- if (cfgfile == nil) then
- print ("*error reading config: "..config_filename)
- else
- local rsout = cfgfile.readLine ()
- cfgfile.close ()
- print ("*read from config: "..rsout)
- return (rsout)
- end
- end
- ------------------------------------------------------------------
- -- MAIN ----------------------------------------------------------
- os.loadAPI ('calicommAPI')
- --calicommAPI.enableDebug (true)
- calicommAPI.initModems ()
- local rs_signal_output = readConfig ()
- calicommAPI.openModemChannel (calicommAPI.modemtable[1][1], listenChannel)
- while true do
- print "*listening..."
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- local msg_elements = {}
- msg_elements = parseMessage (message)
- if (msg_expected_paramcount ~= #msg_elements) then
- print ("*invalid number of message elements")
- elseif (whoami ~= tonumber(msg_elements[2])) then
- print ("*message is not for me")
- elseif (shared_secret ~= msg_elements[3]) then
- print ("*sender failed to authenticate")
- else
- print ("*found valid message")
- if (msg_elements[4] == 'on') then
- print ("*redstone signal @"..rs_signal_output..":ON")
- redstone.setOutput (rs_signal_output, true)
- current_status = true
- elseif (msg_elements[4] == 'off') then
- print ("*redstone signal @"..rs_signal_output..":OFF")
- redstone.setOutput (rs_signal_output, false)
- current_status = false
- end
- end
- end
- calicommAPI.closeModemChannel (calicommAPI.modemtable[1][1], listenChannel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement