Class: BWA::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/bwa/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(port = 4257) ⇒ Server

Returns a new instance of Server.



9
10
11
12
# File 'lib/bwa/server.rb', line 9

def initialize(port = 4257)
  @listen_socket = TCPServer.open(port)
  @status = Messages::Status.new
end

Instance Method Details

#runObject



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 = Message.parse(data)
        BWA.logger.info BWA.raw2str(message.raw_data)
        BWA.logger.info message.inspect

        case message
        when Messages::ConfigurationRequest
          send_configuration(socket)
        when Messages::ControlConfigurationRequest
          (message.type == 1) ? send_control_configuration(socket) : send_control_configuration2(socket)
        when Messages::SetTargetTemperature
          temperature = message.temperature
          temperature /= 2.0 if @status.temperature_scale == :celsius
          @status.target_temperature = temperature
        when Messages::SetTemperatureScale
          @status.temperature_scale = message.scale
        when Messages::ToggleItem
          case message.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.message
        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)
  send_message(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)
  send_message(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)
  send_message(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 send_message(socket, message)
  length = message.length + 2
  full_message = "#{length.chr}#{message}"
  checksum = CRC.checksum(full_message)
  socket.send("\x7e#{full_message}#{checksum.chr}\x7e".b, 0)
end

#send_status(socket) ⇒ Object



77
78
79
80
# File 'lib/bwa/server.rb', line 77

def send_status(socket)
  BWA.logger.info "sending #{@status.inspect}"
  socket.send(@status.serialize, 0)
end