From aa72196a07587d44cc43587da5cef5af1c3ed7e3 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 23 Oct 2019 05:38:38 +0200 Subject: [PATCH] monitor.rb now also uses websockets instead of raw UDP stuff. --- src/tools/monitor.rb | 77 +++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/src/tools/monitor.rb b/src/tools/monitor.rb index dff7ad6..a5b88b7 100644 --- a/src/tools/monitor.rb +++ b/src/tools/monitor.rb @@ -1,5 +1,5 @@ #!/usr/bin/env ruby -require 'socket' +require 'websocket-eventmachine-client' require 'pp' def rgb2ansi(r, g, b) @@ -13,47 +13,42 @@ def rgb2ansi(r, g, b) end IP = ARGV[0] -PORT = 2122 -EFFECT = ARGV[1] +uri = "ws://#{IP}:81" +puts "Connecting to #{uri}..." -puts "Connecting to #{IP}:#{PORT}..." - -s = TCPSocket.new(IP, PORT) - -puts "Connected." -init = s.recv(3).unpack("C*") - -raise "Initial data packet wasn't usable!" if init[2] != 0xFF -puts "Got initial data packet." - -dim_x, dim_y = *init -len = dim_x * dim_y * 3 + 3 - -puts "Size: #{dim_x}x#{dim_y}. Expecting packet length #{len}." - -puts "Opening local UDP socket..." -udp = UDPSocket.new -udp.bind("10.10.2.1", 13330) -puts "Waiting for UDP packets on port 13330..." -s.sendmsg("P\x12\x34\x00") -s.sendmsg("E#{EFFECT}\x00") if EFFECT - - -while 1 - data = udp.recvfrom(1024)[0].unpack("C*") - #puts "Packet. ID: #{data[0]}, length: #{data.length}" - raise "Unexpected packet length" unless data.count == len - 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 " +EM.run do + ws = WebSocket::EventMachine::Client.connect(uri: uri) + + ws.onopen do + puts "\033[2J\033[H\n Connected." + ws.send "E:blur2d" + end + + ws.onmessage do |msg, type| + raise "Unexpected message type #{type.inspect}" if type!=:binary + data = msg.unpack("C*") + width = data.shift + height = data.shift + splitter = data.shift + raise "Unexpected value #{splitter} at index 2" unless splitter==0xFF + expected_length = width * height * 3 + 4 + raise "Unexpected message length. Expected: #{expected_length}, was: #{data.count + 3}" unless data.count + 3==expected_length + + str = "\033[H+#{"-"*width}+\n" + (0...height).each do |y| + str += "|" + (0...width).each do |x| + r, g, b = *data.shift(3) + color_code = rgb2ansi(r, g, b) + str += "\033[48;5;#{color_code}m " + end + str += "\033[0m|\n" end - puts "\033[0m" + str += "+#{"-"*width}+\n" + puts str + end + + ws.onclose do |msg, reason| + puts "Disconnected. Message: #{msg}. Reason: #{reason}." end end