Module: HAP::Client

Includes:
Log, Pairing, Parser, Request
Defined in:
lib/hap_client.rb

Constant Summary

Constants included from Pairing

Pairing::ERROR_NAMES, Pairing::ERROR_TYPES

Constants included from EncryptionRequest

EncryptionRequest::AAD_LENGTH_BYTES, EncryptionRequest::AUTHENTICATE_TAG_LENGTH_BYTES

Constants included from Log

Log::LOG_LVL

Instance Attribute Summary

Attributes included from EncryptionRequest

#decryption_count, #encryption_count

Instance Method Summary collapse

Methods included from Pairing

#pair_setup, #pair_verify

Methods included from Request

#get, #init_request, #post, #put

Methods included from EncryptionRequest

#encryption_ready?

Methods included from Parser

#init_parser, #on_body, #on_headers_complete, #on_message_begin, #on_message_complete, #receive_data

Methods included from Log

#debug, #error, #fatal, #info, #init_log, #log_debug?, #warn

Instance Method Details

#get_accessories(&block) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/hap_client.rb', line 65

def get_accessories(&block)
  info("Get Accessories")
  get("/")

  if block_given?
    @callback = block
  end
end

#get_id(service_id, characteristic_id) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hap_client.rb', line 82

def get_id(service_id, characteristic_id)
  @services.each do |service|
    if service[:type] == service_id
      service[:characteristics].each do |char|
        if char[:type] == characteristic_id
          return char
        end
      end
    end
  end
  return nil
end

#get_type(aid, iid) ⇒ Object



78
79
80
# File 'lib/hap_client.rb', line 78

def get_type(aid, iid)
  @ids[aid][iid]
end

#get_value(aid, iid) ⇒ Object



74
75
76
# File 'lib/hap_client.rb', line 74

def get_value(aid, iid)
  @values[aid][iid][:value]
end

#initializeObject



15
16
17
18
19
20
21
22
23
# File 'lib/hap_client.rb', line 15

def initialize
  @name = "Unknown Client"
  @mode = :init
  @values = {}
  @ids = {}

  init_request()
  init_log()
end

#set_value(aid, iid, value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hap_client.rb', line 25

def set_value(aid, iid, value)
  info("Set Value #{aid}:#{iid} to #{value}")
  data = {
    "characteristics" => [{
                            "aid" => aid.to_i,
                            "iid" => iid.to_i,
                            "value" => value.to_i
                          }]
  }

  put("/characteristics", "application/hap+json", JSON.generate(data))
end

#subscribe(&block) ⇒ Object



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
# File 'lib/hap_client.rb', line 38

def subscribe(&block)
  events = []
  @values.each do |service|
    service[1].each do |val|
      value = val[1]
      if value[:perms].include?("ev")
        info("Subscribe to #{value[:aid]}:#{value[:iid]}")
        events.push({
                      :aid => value[:aid],
                      :iid => value[:iid],
                      :ev => true
                    })
      end
    end
  end

  data = {
    :characteristics => events
  }

  put("/characteristics", "application/hap+json", JSON.generate(data))

  if block_given?
    @callback = block
  end
end

#to_sObject



95
96
97
# File 'lib/hap_client.rb', line 95

def to_s
  @name
end