Class: RubyHome::AccessoryInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_home/accessory_info.rb

Constant Summary collapse

PERSISTABLE =
[ :device_id, :paired_clients, :password, :signature_key, :username ].freeze
@@accessory_info =
AccessoryInfo.new(YAML::Store.new 'accessory_info.yml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ AccessoryInfo

Returns a new instance of AccessoryInfo.



9
10
11
12
13
14
15
16
17
# File 'lib/ruby_home/accessory_info.rb', line 9

def initialize(store)
  @store = store

  read

  @device_id ||= DeviceID.generate
  @paired_clients ||= []
  @signature_key ||= Ed25519::SigningKey.generate.to_bytes.unpack1('H*')
end

Instance Attribute Details

#device_idObject

Returns the value of attribute device_id.



20
21
22
# File 'lib/ruby_home/accessory_info.rb', line 20

def device_id
  @device_id
end

#paired_clientsObject

Returns the value of attribute paired_clients.



20
21
22
# File 'lib/ruby_home/accessory_info.rb', line 20

def paired_clients
  @paired_clients
end

#passwordObject

Returns the value of attribute password.



20
21
22
# File 'lib/ruby_home/accessory_info.rb', line 20

def password
  @password
end

#signature_keyObject

Returns the value of attribute signature_key.



20
21
22
# File 'lib/ruby_home/accessory_info.rb', line 20

def signature_key
  @signature_key
end

#storeObject (readonly)

Returns the value of attribute store.



19
20
21
# File 'lib/ruby_home/accessory_info.rb', line 19

def store
  @store
end

#usernameObject

Returns the value of attribute username.



20
21
22
# File 'lib/ruby_home/accessory_info.rb', line 20

def username
  @username
end

Class Method Details

.add_paired_client(admin: false, identifier:, public_key:) ⇒ Object



88
89
90
91
# File 'lib/ruby_home/accessory_info.rb', line 88

def add_paired_client(admin: false, identifier: , public_key: )
  @@accessory_info.paired_clients << { admin: admin, identifier: identifier, public_key: public_key }
  save
end

.paired?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/ruby_home/accessory_info.rb', line 84

def paired?
  @@accessory_info.paired_clients.any?
end

.pstore=(new_storage) ⇒ Object



58
59
60
# File 'lib/ruby_home/accessory_info.rb', line 58

def self.pstore=(new_storage)
  @@accessory_info = AccessoryInfo.new(new_storage)
end

.remove_paired_client(identifier) ⇒ Object



93
94
95
96
# File 'lib/ruby_home/accessory_info.rb', line 93

def remove_paired_client(identifier)
  @@accessory_info.paired_clients.delete_if { |h| h[:identifier] == identifier }
  save
end

.saveObject



76
77
78
# File 'lib/ruby_home/accessory_info.rb', line 76

def save
  @@accessory_info.save
end

.signing_keyObject



80
81
82
# File 'lib/ruby_home/accessory_info.rb', line 80

def signing_key
  @@accessory_info.signing_key
end

Instance Method Details

#readObject



44
45
46
47
48
49
50
# File 'lib/ruby_home/accessory_info.rb', line 44

def read
  store.transaction(true) do
    store.fetch(:accessory_info, {}).each do |key, value|
      send("#{key}=", value)
    end
  end
end

#saveObject



52
53
54
55
56
# File 'lib/ruby_home/accessory_info.rb', line 52

def save
  store.transaction do
    store[:accessory_info] = to_hash
  end
end

#signing_keyObject



28
29
30
# File 'lib/ruby_home/accessory_info.rb', line 28

def signing_key
  @signing_key ||= Ed25519::SigningKey.new([signature_key].pack('H*'))
end

#to_hashObject



40
41
42
# File 'lib/ruby_home/accessory_info.rb', line 40

def to_hash
  Hash[*PERSISTABLE.flat_map { |attribute| [attribute, send(attribute)] }]
end