Class: IntesisBox::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/intesis_box/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, port = 3310) ⇒ Client



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/intesis_box/client.rb', line 10

def initialize(ip, port = 3310)
  @limits = {}
  @ip = ip
  @io = TCPSocket.new(ip, port)

  @io.puts("LIMITS:*")
  poll(1)
  @io.puts("CFG:DEVICENAME")
  poll(1)
  @io.puts("GET,1:*")
  poll(1)
  # this is purposely last, since mac is what we check for it being ready
  @io.puts("ID")
  poll(1)
end

Instance Attribute Details

#ambtempObject (readonly)

Returns the value of attribute ambtemp.



8
9
10
# File 'lib/intesis_box/client.rb', line 8

def ambtemp
  @ambtemp
end

#devicenameObject

Returns the value of attribute devicename.



7
8
9
# File 'lib/intesis_box/client.rb', line 7

def devicename
  @devicename
end

#errcodeObject (readonly)

Returns the value of attribute errcode.



8
9
10
# File 'lib/intesis_box/client.rb', line 8

def errcode
  @errcode
end

#errstatusObject (readonly)

Returns the value of attribute errstatus.



8
9
10
# File 'lib/intesis_box/client.rb', line 8

def errstatus
  @errstatus
end

#fanspObject (readonly)

Returns the value of attribute fansp.



8
9
10
# File 'lib/intesis_box/client.rb', line 8

def fansp
  @fansp
end

#ipObject (readonly)

Returns the value of attribute ip.



5
6
7
# File 'lib/intesis_box/client.rb', line 5

def ip
  @ip
end

#limitsObject (readonly)

Returns the value of attribute limits.



6
7
8
# File 'lib/intesis_box/client.rb', line 6

def limits
  @limits
end

#macObject (readonly)

Returns the value of attribute mac.



5
6
7
# File 'lib/intesis_box/client.rb', line 5

def mac
  @mac
end

#modeObject (readonly)

Returns the value of attribute mode.



8
9
10
# File 'lib/intesis_box/client.rb', line 8

def mode
  @mode
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/intesis_box/client.rb', line 5

def model
  @model
end

#onoffObject

Returns the value of attribute onoff.



8
9
10
# File 'lib/intesis_box/client.rb', line 8

def onoff
  @onoff
end

#rssiObject (readonly)

Returns the value of attribute rssi.



5
6
7
# File 'lib/intesis_box/client.rb', line 5

def rssi
  @rssi
end

#setptempObject

Returns the value of attribute setptemp.



8
9
10
# File 'lib/intesis_box/client.rb', line 8

def setptemp
  @setptemp
end

#vanelrObject (readonly)

Returns the value of attribute vanelr.



8
9
10
# File 'lib/intesis_box/client.rb', line 8

def vanelr
  @vanelr
end

#vaneudObject (readonly)

Returns the value of attribute vaneud.



8
9
10
# File 'lib/intesis_box/client.rb', line 8

def vaneud
  @vaneud
end

#versionObject (readonly)

Returns the value of attribute version.



5
6
7
# File 'lib/intesis_box/client.rb', line 5

def version
  @version
end

Instance Method Details

#pingObject



57
58
59
# File 'lib/intesis_box/client.rb', line 57

def ping
  @io.puts("PING")
end

#poll(timeout = 30) ⇒ Object



26
27
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
# File 'lib/intesis_box/client.rb', line 26

def poll(timeout = 30)
  return false if @io.wait_readable(timeout).nil?

  loop do
    line = @io.readline.strip
    cmd, args = line.split(':', 2)
    case cmd
    when "ID"
      @model, @mac, _ip, _protocol, @version, @rssi = args.split(',')
    when "LIMITS"
      function, limits = args.split(",",2)
      limits = limits[1...-1].split(",")
      next if function == 'ONOFF'
      limits.map! { |l| l.to_f / 10 } if %w{SETPTEMP AMBTEMP}.include?(function)
      @limits[function.downcase.to_sym] = limits
    when "CHN,1"
      function, value = args.split(",")
      value = value == 'ON' if function == 'ONOFF'
      value = value.to_f / 10 if %w{SETPTEMP AMBTEMP}.include?(function)
      value = value.to_i if function == 'ERRCODE'
      value = nil if value == -3276.8
      instance_variable_set(:"@#{function.downcase}", value)
    when "CFG"
      function, value = args.split(",", 2)
      @devicename = value if function == 'DEVICENAME'
    end
    break unless @io.ready?
  end
  true
end