ruby_home 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bca7f2c1b6c63b9083605bb967d75f4c56219825c03068f972f5d57dfbf227bd
4
- data.tar.gz: f5a79a5a8c4e6b77302f736d22898be8bef2972c6fd7da06d3f4d7a51e66a186
3
+ metadata.gz: cf4e167745548086679350014c0375cf64eeb8733b9e6948ef0846ea36fd4f11
4
+ data.tar.gz: d8583372ac979eceafcd3c6a1cf36f8f40c32d5ab543ff28b7cf03e11e294d9b
5
5
  SHA512:
6
- metadata.gz: 0bbff1f14b617fbbf00c8c1f3ed529e1d1fb7545f316f582a0b6ba196213f6e0560679fa7cb7a8de4499c74af74af00ec1c1b8ea4b4158c4816b2265a8c44988
7
- data.tar.gz: 8a8ce2a35a678aaf7b4ecea34f8aedcfc40741d1afc4de8e06bd6eb0bf982685ae99c7ce6f85b778249c28f0c0da94a5aba5c37a3c8a5fb7820b22e3e6e02d13
6
+ metadata.gz: 84a6186ceb1d259b60bfe877ee3e12c78c4d9a25cb3fc9433605c45264a3bc174796650bb87e95795f8c39ea6dd365bc22425c6360636e07923f731cacaf185d
7
+ data.tar.gz: 19899a408055f4dd74993b03679cee97ea4d440b09beccbf6aaf4d034db727a96d09a73fc85ddb85f3fc4326704782ec861f9e006149a05c9e6c00c57123513d
data/.travis.yml CHANGED
@@ -7,6 +7,7 @@ install:
7
7
  - 'bundle --version'
8
8
  - bundle
9
9
  rvm:
10
+ - 2.5.1
10
11
  - 2.5.0
11
12
  - ruby-head
12
13
  os:
@@ -50,7 +50,7 @@ module RubyHome
50
50
  end
51
51
 
52
52
  def set_password
53
- self.password ||= '031-45-154'
53
+ self.password ||= Password.generate
54
54
  end
55
55
 
56
56
  def set_signature_key
@@ -1,5 +1,5 @@
1
1
  module RubyHome
2
- class DeviceID
2
+ module DeviceID
3
3
  class << self
4
4
  DELIMITER = ':'
5
5
 
@@ -1,12 +1,7 @@
1
1
  module RubyHome
2
2
  class CharacteristicFactory
3
3
  DEFAULT_VALUES = {
4
- firmware_revision: '1.0',
5
4
  identify: nil,
6
- manufacturer: 'Default-Manufacturer',
7
- model: 'Default-Model',
8
- name: 'RubyHome',
9
- serial_number: 'Default-SerialNumber',
10
5
  }.freeze
11
6
 
12
7
  def self.create(characteristic_name, options={}, &block)
@@ -44,9 +39,13 @@ module RubyHome
44
39
 
45
40
  def default_value
46
41
  DEFAULT_VALUES.fetch(characteristic_name.to_sym) do
47
- case template.format
48
- when 'bool'
49
- false
42
+ klass = "RubyHome::#{template.format.classify}DefaultValue".safe_constantize || NullDefaultValue
43
+ default_value = klass.new(template).default
44
+
45
+ if default_value.nil?
46
+ raise "No default value available for characteristic: #{characteristic_name} of type: #{template.format}"
47
+ else
48
+ default_value
50
49
  end
51
50
  end
52
51
  end
@@ -0,0 +1,15 @@
1
+ module RubyHome
2
+ class BaseValue
3
+ def initialize(template)
4
+ @template = template
5
+ end
6
+
7
+ def default
8
+ raise NotImplementedError
9
+ end
10
+
11
+ private
12
+
13
+ attr_reader :template
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'base_value'
2
+
3
+ module RubyHome
4
+ class BoolDefaultValue < BaseValue
5
+ def default
6
+ false
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'base_value'
2
+
3
+ module RubyHome
4
+ class FloatDefaultValue < BaseValue
5
+ def default
6
+ minimum_value.to_f
7
+ end
8
+
9
+ private
10
+
11
+ def minimum_value
12
+ template.constraints.fetch('MinimumValue')
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'base_value'
2
+
3
+ module RubyHome
4
+ class Int32DefaultValue < BaseValue
5
+ def default
6
+ minimum_value.to_i
7
+ end
8
+
9
+ private
10
+
11
+ def minimum_value
12
+ template.constraints.fetch('MinimumValue')
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'base_value'
2
+
3
+ module RubyHome
4
+ class NullDefaultValue < BaseValue
5
+ def default
6
+ nil
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'base_value'
2
+
3
+ module RubyHome
4
+ class StringDefaultValue < BaseValue
5
+ DEFAULT_VALUES = {
6
+ firmware_revision: '1.0',
7
+ hardware_revision: '1.0',
8
+ manufacturer: 'Default-Manufacturer',
9
+ model: 'Default-Model',
10
+ name: 'RubyHome',
11
+ serial_number: 'Default-SerialNumber',
12
+ version: '1.0',
13
+ }.freeze
14
+
15
+ def default
16
+ DEFAULT_VALUES[name]
17
+ end
18
+
19
+ private
20
+
21
+ def name
22
+ template.name
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'base_value'
2
+
3
+ module RubyHome
4
+ class Uint32DefaultValue < BaseValue
5
+ DEFAULT_VALUES = {
6
+ color_temperature: 50,
7
+ lock_management_auto_security_timeout: 0,
8
+ }.freeze
9
+
10
+ def default
11
+ DEFAULT_VALUES[name]
12
+ end
13
+
14
+ private
15
+
16
+ def name
17
+ template.name
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'base_value'
2
+
3
+ module RubyHome
4
+ class Uint8DefaultValue < BaseValue
5
+ def default
6
+ first_default_value.to_i
7
+ end
8
+
9
+ private
10
+
11
+ def first_default_value
12
+ valid_values.keys.first
13
+ end
14
+
15
+ def valid_values
16
+ template.constraints.fetch('ValidValues')
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ module RubyHome
2
+ module Greeter
3
+ class << self
4
+ def run
5
+ unless paired?
6
+ puts "Please enter this code with your HomeKit app on your iOS device to pair with RubyHome"
7
+ puts " "
8
+ puts " ┌────────────┐ "
9
+ puts " │ " + pin + " │ "
10
+ puts " └────────────┘ "
11
+ puts " "
12
+ end
13
+ end
14
+
15
+ def pin
16
+ accessory_info.password
17
+ end
18
+
19
+ def paired?
20
+ accessory_info.paired?
21
+ end
22
+
23
+ def accessory_info
24
+ AccessoryInfo.instance
25
+ end
26
+ end
27
+ end
28
+ end
@@ -3,46 +3,48 @@ require_relative 'application_controller'
3
3
  module RubyHome
4
4
  module HTTP
5
5
  class CharacteristicsController < ApplicationController
6
- get '/' do
6
+ before do
7
7
  content_type 'application/hap+json'
8
+ require_session
9
+ end
8
10
 
9
- if cache[:controller_to_accessory_key] && cache[:accessory_to_controller_key]
10
- accessory_id, instance_id = params[:id].split('.')
11
- characteristics = IdentifierCache.find_characteristics(
12
- accessory_id: accessory_id.to_i,
13
- instance_id: instance_id.to_i
14
- )
15
-
16
- CharacteristicValueSerializer.new(characteristics).serialized_json
17
- else
18
- status 401
19
- JSON.generate({"status" => -70401})
20
- end
11
+ get '/' do
12
+ CharacteristicValueSerializer.new(find_characteristics).serialized_json
21
13
  end
22
14
 
23
15
  put '/' do
24
- content_type 'application/hap+json'
16
+ json_body.fetch('characteristics', []).each do |characteristic_params|
17
+ update_characteristics(characteristic_params)
18
+ end
19
+
20
+ status 204
21
+ end
22
+
23
+ private
25
24
 
26
- if cache[:controller_to_accessory_key] && cache[:accessory_to_controller_key]
27
- json_body.fetch('characteristics', []).each do |characteristic_params|
28
- accessory_id = characteristic_params['aid']
29
- instance_id = characteristic_params['iid']
30
- characteristic = IdentifierCache.find_characteristics(
31
- accessory_id: accessory_id.to_i,
32
- instance_id: instance_id.to_i
33
- ).first
34
-
35
- if characteristic_params['value']
36
- characteristic.value = characteristic_params['value']
37
- end
25
+ def find_characteristics
26
+ params.fetch(:id, "").split(',').flat_map do |characteristic_params|
27
+ aid, iid = characteristic_params.split('.')
28
+ find_characteristic(aid: aid, iid: iid)
38
29
  end
30
+ end
39
31
 
40
- status 204
41
- else
42
- status 401
43
- JSON.generate({"status" => -70401})
32
+ def update_characteristics(characteristic_params)
33
+ characteristic = find_characteristic(**characteristic_params.symbolize_keys.slice(:aid, :iid))
34
+ if characteristic && characteristic_params['value']
35
+ characteristic.value = characteristic_params['value']
36
+ end
37
+ end
38
+
39
+ def find_characteristic(aid:, iid:)
40
+ IdentifierCache.find_characteristic(accessory_id: aid.to_i, instance_id: iid.to_i)
41
+ end
42
+
43
+ def require_session
44
+ unless cache[:controller_to_accessory_key] && cache[:accessory_to_controller_key]
45
+ halt 401, JSON.generate({"status" => -70401})
46
+ end
44
47
  end
45
- end
46
48
  end
47
49
  end
48
50
  end
@@ -19,8 +19,8 @@ module RubyHome
19
19
  services.flat_map(&:characteristics)
20
20
  end
21
21
 
22
- def find_characteristics(attributes)
23
- characteristics.select do |characteristic|
22
+ def find_characteristic(attributes)
23
+ characteristics.find do |characteristic|
24
24
  attributes.all? do |key, value|
25
25
  characteristic.send(key) == value
26
26
  end
@@ -0,0 +1,46 @@
1
+ module RubyHome
2
+ module Password
3
+ class << self
4
+ DELIMITER = '-'
5
+ LENGTH = 8
6
+ INVALIDS = [
7
+ '00000000',
8
+ '11111111',
9
+ '22222222',
10
+ '33333333',
11
+ '44444444',
12
+ '55555555',
13
+ '66666666',
14
+ '77777777',
15
+ '88888888',
16
+ '99999999',
17
+ '12345678',
18
+ '87654321',
19
+ ]
20
+
21
+ # The Setup Code must conform to the format XXX-XX-XXX where each X is a
22
+ # 0-9 digit and dashes are required. For example, "101-48-005" (without
23
+ # quotes). The accessory must generate its SRP verifier with the full
24
+ # Setup Code, including dashes.
25
+
26
+ def generate
27
+ loop do
28
+ digits = random_digits(LENGTH)
29
+ break format(digits.to_s) unless INVALIDS.include?(digits)
30
+ end
31
+ end
32
+
33
+ def format(digits)
34
+ [
35
+ digits[0...3],
36
+ digits[3...5],
37
+ digits[5...8],
38
+ ].join(DELIMITER)
39
+ end
40
+
41
+ def random_digits(length)
42
+ Array.new(length) { rand(0..9) }.join
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module RubyHome
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
data/lib/ruby_home.rb CHANGED
@@ -18,6 +18,7 @@ module RubyHome
18
18
  trap 'INT' do shutdown end
19
19
  trap 'TERM' do shutdown end
20
20
 
21
+ greet
21
22
  start
22
23
  end
23
24
 
@@ -42,5 +43,9 @@ module RubyHome
42
43
  def http_server
43
44
  @@_http_server ||= RubyHome::HTTP::Application.new
44
45
  end
46
+
47
+ def greet
48
+ Greeter.run
49
+ end
45
50
  end
46
51
  end
data/rubyhome.gemspec CHANGED
@@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
16
16
  provided by manufacturers of smart home devices.
17
17
  DESCRIPTION
18
18
  spec.homepage = 'https://github.com/karlentwistle/ruby_home'
19
+ spec.license = 'MIT'
19
20
 
20
21
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
22
  f.match(%r{^(test|spec|features)/})
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_home
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karl Entwistle
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-02 00:00:00.000000000 Z
11
+ date: 2018-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -304,8 +304,17 @@ files:
304
304
  - lib/ruby_home/dns/text_record.rb
305
305
  - lib/ruby_home/factories/accessory_factory.rb
306
306
  - lib/ruby_home/factories/characteristic_factory.rb
307
+ - lib/ruby_home/factories/default_values/base_value.rb
308
+ - lib/ruby_home/factories/default_values/bool_value.rb
309
+ - lib/ruby_home/factories/default_values/float_value.rb
310
+ - lib/ruby_home/factories/default_values/int32_value.rb
311
+ - lib/ruby_home/factories/default_values/null_value.rb
312
+ - lib/ruby_home/factories/default_values/string_value.rb
313
+ - lib/ruby_home/factories/default_values/uint32_value.rb
314
+ - lib/ruby_home/factories/default_values/uint8_value.rb
307
315
  - lib/ruby_home/factories/templates/characteristic_template.rb
308
316
  - lib/ruby_home/factories/templates/service_template.rb
317
+ - lib/ruby_home/greeter.rb
309
318
  - lib/ruby_home/hap/accessory.rb
310
319
  - lib/ruby_home/hap/characteristic.rb
311
320
  - lib/ruby_home/hap/crypto/chacha20poly1305.rb
@@ -333,6 +342,7 @@ files:
333
342
  - lib/ruby_home/http/services/start_srp_service.rb
334
343
  - lib/ruby_home/http/services/verify_srp_service.rb
335
344
  - lib/ruby_home/identifier_cache.rb
345
+ - lib/ruby_home/password.rb
336
346
  - lib/ruby_home/rack/handler/hap_server.rb
337
347
  - lib/ruby_home/version.rb
338
348
  - lib/ruby_home/yaml_record.rb
@@ -340,7 +350,8 @@ files:
340
350
  - sbin/characteristic_generator.rb
341
351
  - sbin/service_generator.rb
342
352
  homepage: https://github.com/karlentwistle/ruby_home
343
- licenses: []
353
+ licenses:
354
+ - MIT
344
355
  metadata: {}
345
356
  post_install_message:
346
357
  rdoc_options: []