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.



6
7
8
9
# File 'lib/bwa/server.rb', line 6

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

Instance Method Details

#runObject



11
12
13
14
15
16
17
18
19
# File 'lib/bwa/server.rb', line 11

def run
  loop do
    socket = @listen_socket.accept
    #Thread.new do
      run_client(socket)
    #end
    break
  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
# File 'lib/bwa/server.rb', line 28

def run_client(socket)
  puts "Received connection from #{socket.remote_address.inspect}"

  send_status(socket)
  loop do
    if IO.select([socket], nil, nil, 1)
      data = socket.recv(128)
      break if data.empty?
      begin
        message = Message.parse(data)
        puts message.raw_data.unpack("H*").first.scan(/[0-9a-f]{2}/).join(' ')
        puts 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::SetTemperature
          temperature = message.temperature
          temperature /= 2.0 if @status.temperature_scale == :celsius
          @status.set_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
        puts e.message
        puts e.raw_data.unpack("H*").first.scan(/[0-9a-f]{2}/).join(' ')
      end
    else
      send_status(socket)
    end
  end
end

#send_configuration(socket) ⇒ Object



81
82
83
# File 'lib/bwa/server.rb', line 81

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")
end

#send_control_configuration(socket) ⇒ Object



85
86
87
# File 'lib/bwa/server.rb', line 85

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



89
90
91
# File 'lib/bwa/server.rb', line 89

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}".force_encoding(Encoding::ASCII_8BIT)
  checksum = CRC.checksum(full_message)
  socket.send("\x7e#{full_message}#{checksum.chr}\x7e".force_encoding(Encoding::ASCII_8BIT), 0)
end

#send_status(socket) ⇒ Object



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

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