Class: BWA::Server
- Inherits:
-
Object
- Object
- BWA::Server
- Defined in:
- lib/bwa/server.rb
Instance Method Summary collapse
-
#initialize(port = 4257) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
- #run_client(socket) ⇒ Object
- #send_configuration(socket) ⇒ Object
- #send_control_configuration(socket) ⇒ Object
- #send_control_configuration2(socket) ⇒ Object
- #send_message(socket, message) ⇒ Object
- #send_status(socket) ⇒ Object
Constructor Details
Instance Method Details
#run ⇒ Object
14 15 16 17 18 19 |
# File 'lib/bwa/server.rb', line 14 def run loop do socket = @listen_socket.accept run_client(socket) end end |
#run_client(socket) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/bwa/server.rb', line 28 def run_client(socket) BWA.logger.info "Received connection from #{socket.remote_address.inspect}" send_status(socket) loop do if socket.wait_readable(1) data = socket.recv(128) break if data.empty? begin = Message.parse(data) BWA.logger.info BWA.raw2str(.raw_data) BWA.logger.info .inspect case when Messages::ConfigurationRequest send_configuration(socket) when Messages::ControlConfigurationRequest (.type == 1) ? send_control_configuration(socket) : send_control_configuration2(socket) when Messages::SetTargetTemperature temperature = .temperature temperature /= 2.0 if @status.temperature_scale == :celsius @status.target_temperature = temperature when Messages::SetTemperatureScale @status.temperature_scale = .scale when Messages::ToggleItem case .item when :heating_mode @status.heating_mode = ((@status.heating_mode == :rest) ? :ready : :rest) when :temperature_range @status.temperature_range = ((@status.temperature_range == :low) ? :high : :low) when :pump1 @status.pump1 = (@status.pump1 + 1) % 3 when :pump2 @status.pump2 = (@status.pump2 + 1) % 3 when :light1 @status.light1 = !@status.light1 end end rescue BWA::InvalidMessage => e BWA.logger.warn e. BWA.logger.warn BWA.raw2str(e.raw_data) end else send_status(socket) end end end |
#send_configuration(socket) ⇒ Object
82 83 84 85 |
# File 'lib/bwa/server.rb', line 82 def send_configuration(socket) (socket, "\x0a\xbf\x94\x02\x02\x80\x00\x15\x27\x10\xab\xd2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x15\x27\xff\xff\x10\xab\xd2") # rubocop:disable Layout/LineLength end |
#send_control_configuration(socket) ⇒ Object
87 88 89 90 |
# File 'lib/bwa/server.rb', line 87 def send_control_configuration(socket) (socket, "\x0a\xbf\x24\x64\xdc\x11\x00\x42\x46\x42\x50\x32\x30\x20\x20\x01\x3d\x12\x38\x2e\x01\x0a\x04\x00") end |
#send_control_configuration2(socket) ⇒ Object
92 93 94 |
# File 'lib/bwa/server.rb', line 92 def send_control_configuration2(socket) (socket, "\x0a\xbf\x2e\x0a\x00\x01\xd0\x00\x44") end |
#send_message(socket, message) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/bwa/server.rb', line 21 def (socket, ) length = .length + 2 = "#{length.chr}#{}" checksum = CRC.checksum() socket.send("\x7e#{}#{checksum.chr}\x7e".b, 0) end |