pitrix/src/tools/monitor.rb

55 lines
1.3 KiB
Ruby

#!/usr/bin/env ruby
require 'websocket-eventmachine-client'
require 'pp'
def rgb2ansi(r, g, b)
if r==g && g==b
return 16 if r<8
return 231 if r>248
return (((r - 8) / 247.0) * 24).round + 232
end
return 16 + 36*(r/51.0).round + 6*(g/51.0).round + (b/51.0).round
end
IP = ARGV[0]
uri = "ws://#{IP}:80/ws"
puts "Connecting to #{uri}..."
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
str += "+#{"-"*width}+\n"
puts str
end
ws.onclose do |msg, reason|
puts "Disconnected. Message: #{msg}. Reason: #{reason}."
end
end