I'm having a problem with ruby code and wondered if anyone can help. This is what I have so far as a test (forgive my first attempts at programming a ruby script!):
require 'socket'
include Socket::Constants
device_id=162
def connect_lmce(device_id)
in_socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
in_sockaddr = Socket.pack_sockaddr_in( 3450, 'localhost' )
in_socket.connect( in_sockaddr )
out_socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
out_sockaddr = Socket.pack_sockaddr_in( 3450, 'localhost' )
out_socket.connect( out_sockaddr )
puts "Setting receiving connection"
in_socket.send("COMMAND #{device_id}\n",0)
puts in_socket.recv( 100 )
puts "Setting up sending connection"
out_socket.send("EVENT #{device_id}\n",0)
puts out_socket.recv( 100 )
sleep 1
puts "Registering for plain text messages"
out_socket.send("PLAIN_TEXT\n",0)
puts out_socket.recv( 100 )
puts "Finished initialising"
return in_socket, out_socket
end
def register_messages(device_id, in_socket, out_socket)
puts "Registering for message intercepts (lighting devices)"
out_socket.send("MESSAGET 24 \n #{device_id} -1000 8 0 5 2 4 84\n",0)
puts out_socket.recv(100)
puts "Registering for message intercepts (security devices)"
out_socket.send("MESSAGET 24 \n #{device_id} -1000 8 0 5 1 4 73 \n",0)
puts out_socket.recv(100)
puts "Finished registration"
end
in_socket, out_socket = connect_lmce(device_id)
register_messages(device_id, in_socket, out_socket)
messageCount = 0
while data = in_socket.recv( 100 )
data = data.gsub('"', '').gsub('&', '')
if data.size > 0
messageCount += 1
puts "Received message #{messageCount}: #{data}"
else
puts "empty"
end
end
The last part is of most interest:
messageCount = 0
while data = in_socket.recv( 100 )
data = data.gsub('"', '').gsub('&', '')
if data.size > 0
messageCount += 1
puts "Received message #{messageCount}: #{data}"
else
puts "empty"
end
end
Basically I put the empty check in because I am seeing some weird stuff going on. When I run the script everything works fine while receiving certain messages, but then when one particular kind of message is received the program freezes for 15 secs, then goes into a crazy loop of empty messages. Like this:
Finished registration
Received message 1: MESSAGET 18
1234 162 999 9999
Received message 2: MESSAGET 18
1234 162 999 9999
Received message 3: MESSAGET 18
1234 162 999 9999
Received message 4: MESSAGET 35
0 162 9 0 82 -1000 2 9 25 0
empty
empty
empty
empty
...
Can anyone help me out here?
Thanks,
Chris