Class: Tinkerforge::Device

Inherits:
Object
  • Object
show all
Includes:
Shared::Logger
Defined in:
lib/tinderfridge/device.rb,
lib/tinderfridge/device/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uid, ipcon, device_identifier, device_display_name) ⇒ Device

Returns a new instance of Device.



77
78
79
80
81
82
# File 'lib/tinderfridge/device.rb', line 77

def initialize(uid, ipcon, device_identifier, device_display_name)
  original_initialize(uid, ipcon, device_identifier, device_display_name)
  if respond_to? 'get_identity'
    logger_debug "Created #{self.class}"
  end
end

Instance Attribute Details

#device_display_nameObject (readonly)

Returns the device’s Display Name.



70
71
72
# File 'lib/tinderfridge/device.rb', line 70

def device_display_name
  @device_display_name
end

#device_identifierObject (readonly)

Returns the device’s numeric Device Identifier.



67
68
69
# File 'lib/tinderfridge/device.rb', line 67

def device_identifier
  @device_identifier
end

#ipconObject (readonly)

Returns the device’s IPConnection object.



73
74
75
# File 'lib/tinderfridge/device.rb', line 73

def ipcon
  @ipcon
end

#uid_stringObject (readonly)

Returns the device’s UID. Not to be confused with #uid, which returns the numeric UID.



64
65
66
# File 'lib/tinderfridge/device.rb', line 64

def uid_string
  @uid_string
end

Class Method Details

.class_mapObject

Returns a map of currently defined device classes.



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

def class_map
  descendants.map do |klass|
    if klass.const_defined? 'DEVICE_IDENTIFIER'
      [
        klass.const_get('DEVICE_IDENTIFIER'),
        klass.const_get('DEVICE_DISPLAY_NAME'),
        klass,
        File.basename(klass.instance_method('initialize').source_location.first, '.rb')
      ]
    else
      nil
    end
  end.compact.sort_by { |i| i[0] }
end

.descendantsObject

Returns all currently defined classes that inherited from this class.

With help from:



19
20
21
22
# File 'lib/tinderfridge/device.rb', line 19

def descendants
  # NOTE: Ruby 3.1 has Class#subclasses
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

Instance Method Details

#configObject

Returns configuration data for the device (a mutable Hash).



5
6
7
# File 'lib/tinderfridge/device/configuration.rb', line 5

def config
  @config ||= {}
end

#config=(configuration) ⇒ Object

Sets configuration data (a Hash) for the device.

Raises:

  • (ArgumentError)


10
11
12
13
# File 'lib/tinderfridge/device/configuration.rb', line 10

def config=(configuration)
  raise(ArgumentError, 'Invalid configuration') unless configuration.class == Hash
  @config = configuration
end

#device_infoObject

Returns device information.

Device information is an array:

  • 0 : Device Identifier

  • 1 : Device Display Name

  • 2 : Associated class name and source file



90
91
92
# File 'lib/tinderfridge/device.rb', line 90

def device_info
  Tinkerforge.device_info device_identifier
end

#identify(seconds = 10) ⇒ Object

Identifies a Tinkerforge device by blinking its status led.

Supports recent devices. When invoked on some older devices, does nothing.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/tinderfridge/device.rb', line 162

def identify(seconds=10)
  case status_led_api_variety
  when 1
    seconds = seconds.to_i
    state   = is_status_led_enabled

    (seconds*2).times do |n|
      if n.even?
        disable_status_led
      else
        enable_status_led
      end
      sleep 0.5
    end

    if state
      enable_status_led
    else
      disable_status_led
    end

    seconds
  when 2
    seconds = seconds.to_i
    state = get_status_led_config

    (seconds*2).times do |n|
      set_status_led_config n.remainder(2)
      sleep 0.5
    end

    set_status_led_config state
    seconds
  else
    false
  end
end

#inspectObject

Returns a programmer-friendly representation of the device.



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

def inspect
  "#{self.class} (#{uid_string}" + ( ipcon.host ? "@#{ipcon.host}:#{ipcon.port})" : ')' )
end

#open_documentationObject Also known as: doc

Opens the online documentation for the device (Mac OS only).

When the URL for the documentation is not known, does nothing.



148
149
150
151
152
153
154
155
# File 'lib/tinderfridge/device.rb', line 148

def open_documentation
  if properties['documentation_en_url'] and ( RUBY_PLATFORM =~ /darwin/ )
    `open #{properties['documentation_en_url']}`
    properties['documentation_en_url']
  else
    nil
  end
end

#original_initializeObject



75
# File 'lib/tinderfridge/device.rb', line 75

alias original_initialize initialize

#propertiesObject Also known as: props

Returns the device’s properties.



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/tinderfridge/device.rb', line 100

def properties

  # BrickDaemon inherits from Device, but has no #get_identity.
  return {} unless respond_to? 'get_identity'

  identity = get_identity

  @properties ||= [
    [ 'device_identifier'  , device_identifier     ],
    [ 'device_display_name', device_display_name   ],
    [ 'hardware_version'   , identity[3].join('.') ],
  ].compact.to_h.merge load_properties
end

#stateObject

Returns the device’s state.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/tinderfridge/device.rb', line 117

def state

  # BrickDaemon inherits from Device, but has no #get_identity.
  return {} unless respond_to? 'get_identity'

  identity = get_identity

  [
    [ 'uid'                , uid_string            ],
    [ 'update_time'        , Time.now.gmtime       ],
    [ 'firmware_version'   , identity[4].join('.') ],

    [ 'connected', { 'uid'  => identity[1], 'position' => identity[2] } ],
    [ 'ipcon'    , { 'host' => ipcon.host , 'port'     => ipcon.port  } ],

    respond_to?('get_chip_temperature'  ) ? [ 'chip_temperature'  , get_chip_temperature   ] : nil,

    if respond_to?('get_spitfp_error_count') and (method(:get_spitfp_error_count).arity == 0)
      [ 'spitfp_error_count', get_spitfp_error_count ]
    else
      nil
    end,

    respond_to?('get_status_led_config' ) ? [ 'status_led_config' , get_status_led_config  ] : nil,

  ].compact.to_h
end