monitor.rb now also uses websockets instead of raw UDP stuff.

This commit is contained in:
Fabian Schlenz 2019-10-23 05:38:38 +02:00
parent f76354a4d5
commit aa72196a07

View File

@ -1,5 +1,5 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'socket' require 'websocket-eventmachine-client'
require 'pp' require 'pp'
def rgb2ansi(r, g, b) def rgb2ansi(r, g, b)
@ -13,47 +13,42 @@ def rgb2ansi(r, g, b)
end end
IP = ARGV[0] IP = ARGV[0]
PORT = 2122 uri = "ws://#{IP}:81"
EFFECT = ARGV[1] puts "Connecting to #{uri}..."
puts "Connecting to #{IP}:#{PORT}..." EM.run do
ws = WebSocket::EventMachine::Client.connect(uri: uri)
s = TCPSocket.new(IP, PORT)
ws.onopen do
puts "Connected." puts "\033[2J\033[H\n Connected."
init = s.recv(3).unpack("C*") ws.send "E:blur2d"
end
raise "Initial data packet wasn't usable!" if init[2] != 0xFF
puts "Got initial data packet." ws.onmessage do |msg, type|
raise "Unexpected message type #{type.inspect}" if type!=:binary
dim_x, dim_y = *init data = msg.unpack("C*")
len = dim_x * dim_y * 3 + 3 width = data.shift
height = data.shift
puts "Size: #{dim_x}x#{dim_y}. Expecting packet length #{len}." splitter = data.shift
raise "Unexpected value #{splitter} at index 2" unless splitter==0xFF
puts "Opening local UDP socket..." expected_length = width * height * 3 + 4
udp = UDPSocket.new raise "Unexpected message length. Expected: #{expected_length}, was: #{data.count + 3}" unless data.count + 3==expected_length
udp.bind("10.10.2.1", 13330)
puts "Waiting for UDP packets on port 13330..." str = "\033[H+#{"-"*width}+\n"
s.sendmsg("P\x12\x34\x00") (0...height).each do |y|
s.sendmsg("E#{EFFECT}\x00") if EFFECT str += "|"
(0...width).each do |x|
r, g, b = *data.shift(3)
while 1 color_code = rgb2ansi(r, g, b)
data = udp.recvfrom(1024)[0].unpack("C*") str += "\033[48;5;#{color_code}m "
#puts "Packet. ID: #{data[0]}, length: #{data.length}" end
raise "Unexpected packet length" unless data.count == len str += "\033[0m|\n"
raise "Invalid data packet" unless data[len - 1]==0xFF
id = data.shift << 8 | data.shift
#next
#print "."
puts "\033[2J"
(0...dim_y).each do |y|
(0...dim_x).each do |x|
r, g, b = *data.shift(3)
color_code = rgb2ansi(r, g, b)
print "\033[48;5;#{color_code}m "
end end
puts "\033[0m" str += "+#{"-"*width}+\n"
puts str
end
ws.onclose do |msg, reason|
puts "Disconnected. Message: #{msg}. Reason: #{reason}."
end end
end end