tinderfridge 0.13.0 → 0.15.0

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: 2805da1fbb45d1938cde253637d7a66119718191692caf44ceb22f5d684ed58a
4
- data.tar.gz: 5e3dbb8a81b3a5f59975a0b4ef4f1c5cbaa76e97d1fc6d9034cd14edc0f0371b
3
+ metadata.gz: d3c51a0740fb7714bbb9012543195df2539e310653510dfd6f6b4972aaf4b7dd
4
+ data.tar.gz: d02f24ea04041305ac5cc7b40c73e651451bca9caff08be1476bc592ac9fe56b
5
5
  SHA512:
6
- metadata.gz: e5d86b78c3c4351b63d9bcbad1032a661d6fdd9b00457a59deaed677d358577781fdc2809ebe7f8a7c762c77638685d601349bb2065930e9f5dad8232ae61a2f
7
- data.tar.gz: bc3e28b83f1704169827b0a4650e1f5a360714d70e5f153e62e4428f33360821e2514e31a10ff089cdfa4955225656106b26e314b03d6b24cdf91fe7190014a9
6
+ metadata.gz: 8d8e2423d20924eb5bcb96083268c337d3dbbb571f395d1d6861ce39de4f2391784de84674b59875fea61a0dc9aca7085aa41c7a7274362394582a6453d5cf5a
7
+ data.tar.gz: 838806023856f1f566b4f49ce73b04ff6d1bae47b395e7b7d089ee288e1ed0ad85396fda76d7401866dea848b0288fd6be4a6cc1a8e306c0a11a192f09582445
@@ -1,4 +1,5 @@
1
1
  require 'tinkerforge/ip_connection'
2
+ require 'tinderfridge/shared/logger'
2
3
 
3
4
  module Tinkerforge
4
5
 
@@ -56,6 +57,8 @@ module Tinkerforge
56
57
  # Instance Methods #
57
58
  #----------------------------------------------------------------------#
58
59
 
60
+ include Tinkerforge::Shared::Logger
61
+
59
62
  # Returns the device's UID. Not to be confused with #uid, which returns the numeric UID.
60
63
  attr_reader :uid_string
61
64
 
@@ -68,6 +71,15 @@ module Tinkerforge
68
71
  # Returns the device's IPConnection object.
69
72
  attr_reader :ipcon
70
73
 
74
+ alias original_initialize initialize
75
+
76
+ def initialize(uid, ipcon, device_identifier, device_display_name)
77
+ original_initialize(uid, ipcon, device_identifier, device_display_name)
78
+ if respond_to? 'get_identity'
79
+ logger_debug "Created %s '%s'" % [self.class, uid_string]
80
+ end
81
+ end
82
+
71
83
  # Returns device information.
72
84
  #
73
85
  # Device information is an array:
@@ -80,15 +92,22 @@ module Tinkerforge
80
92
 
81
93
  # Returns a programmer-friendly representation of the device.
82
94
  def inspect
83
- "%s (%s@%s:%s)" % [self.class, uid_string, ipcon.host, ipcon.port]
95
+ "#{self.class} (#{uid_string}" + ( ipcon.host ? "@#{ipcon.host}:#{ipcon.port})" : ')' )
84
96
  end
85
97
 
86
98
  # Returns the device's properties.
87
99
  def properties
88
- @properties ||= {
89
- 'device_identifier' => device_identifier,
90
- 'device_display_name' => device_display_name,
91
- }.merge load_properties
100
+
101
+ # BrickDaemon inherits from Device, but has no #get_identity.
102
+ return {} unless respond_to? 'get_identity'
103
+
104
+ identity = get_identity
105
+
106
+ @properties ||= [
107
+ [ 'device_identifier' , device_identifier ],
108
+ [ 'device_display_name', device_display_name ],
109
+ [ 'hardware_version' , identity[3].join('.') ],
110
+ ].compact.to_h.merge load_properties
92
111
  end
93
112
 
94
113
  alias props properties
@@ -127,6 +146,12 @@ module Tinkerforge
127
146
  @config ||= {}
128
147
  end
129
148
 
149
+ # Sets configuration data (a Hash) for the device.
150
+ def config=(configuration)
151
+ raise(ArgumentError, 'Invalid configuration') unless configuration.class == Hash
152
+ @config = configuration
153
+ end
154
+
130
155
  # Opens the online documentation for the device (Mac OS only).
131
156
  #
132
157
  # When the URL for the documentation is not known, does nothing.
@@ -2,6 +2,8 @@ module Tinkerforge
2
2
 
3
3
  class DeviceCollection < Hash
4
4
 
5
+ alias devices values
6
+
5
7
  # Returns the temperatures as measured inside the microcontrollers of devices in the collection.
6
8
  #
7
9
  # Nil for devices that do not support the get_chip_temperature method.
@@ -82,6 +84,16 @@ module Tinkerforge
82
84
  smap 'config'
83
85
  end
84
86
 
87
+ # Sets configuration data of devices in the collection.
88
+ def config=(configuration)
89
+ raise ArgumentError, 'invalid configuration' unless (configuration.class == Hash)
90
+ each do |k,v|
91
+ if configuration[k]
92
+ v.config = configuration[k]
93
+ end
94
+ end
95
+ end
96
+
85
97
  # Opens the online documentation for the devices in the collection (Mac OS only).
86
98
  #
87
99
  # When the URL for a device's documentation is not known, does nothing.
@@ -115,6 +127,13 @@ module Tinkerforge
115
127
  smap('ipcon').values.compact.uniq
116
128
  end
117
129
 
130
+ # Disconnects IP Connections used by devices in the collection, and removes all devices from the collection.
131
+ def disconnect
132
+ result = ipcons.map { |i| [i, (i.get_connection_state == 0 ? nil : i.disconnect) ] }.to_h
133
+ clear
134
+ result
135
+ end
136
+
118
137
  # Returns an array of devices in the collection matching the selector.
119
138
  #
120
139
  # Selector argument can be:
@@ -6,6 +6,55 @@ module Tinkerforge
6
6
  CHIP_TEMPERATURE_UNIT = 0.1
7
7
  # REVIEW: This should ideally be part of base Tinkerforge.
8
8
 
9
+ # Returns the device's state.
10
+ def state
11
+ super.merge [
12
+ safe_send_state('connection_type' , 'get_connection_type' ), # FW 2.4.0
13
+ safe_send_state('status_led_enabled', 'is_status_led_enabled'), # FW 2.3.2
14
+ safe_send_state('chibi_present' , 'is_chibi_present' ),
15
+ safe_send_state('rs485_present' , 'is_rs485_present' ),
16
+ safe_send_state('ethernet_present' , 'is_ethernet_present' ), # FW 2.1.0
17
+ safe_send_state('wifi_present' , 'is_wifi_present' ),
18
+ safe_send_state('wifi2_present' , 'is_wifi2_present' ), # FW 2.4.0
19
+ ].compact.to_h
20
+ end
21
+
22
+ # Returns the state of the WIFI Extension 2.0.
23
+ def wifi2_state
24
+ if is_wifi2_present
25
+ [
26
+ safe_send_state('mesh_configuration' , 'get_wifi2_mesh_configuration' ), # FW 2.4.2 / 2.1.0
27
+ safe_send_state('mesh_router_ssid' , 'get_wifi2_mesh_router_ssid' ), # FW 2.4.2 / 2.1.0
28
+ safe_send_state('mesh_common_status' , 'get_wifi2_mesh_common_status' ), # FW 2.4.2 / 2.1.0
29
+ safe_send_state('mesh_client_status' , 'get_wifi2_mesh_client_status' ), # FW 2.4.2 / 2.1.0
30
+ safe_send_state('mesh_ap_status' , 'get_wifi2_mesh_ap_status' ), # FW 2.4.2 / 2.1.0
31
+ safe_send_state('configuration' , 'get_wifi2_configuration' ), # FW 2.4.0
32
+ safe_send_state('status' , 'get_wifi2_status' ), # FW 2.4.0
33
+ safe_send_state('client_configuration', 'get_wifi2_client_configuration'), # FW 2.4.0
34
+ safe_send_state('client_hostname' , 'get_wifi2_client_hostname' ), # FW 2.4.0
35
+ safe_send_state('ap_configuration' , 'get_wifi2_ap_configuration' ), # FW 2.4.0
36
+
37
+ if r = safe_send_state('firmware_version', 'get_wifi2_firmware_version' ) # FW 2.4.0
38
+ [ r[0], r[1].join('.') ]
39
+ end
40
+ ].compact.to_h
41
+ end
42
+ rescue
43
+ nil
44
+ end
45
+
46
+ private
47
+
48
+ # Safely call a method for inclusion in state hash.
49
+ # When method blows up, state can ignore it.
50
+ # REVIEW: possible candidate for Device class
51
+ def safe_send_state(key, methot)
52
+ [key, send(methot)]
53
+ rescue => e
54
+ logger_warn "Device '#{uid_string}': '#{methot}' failed. #{e}"
55
+ nil
56
+ end
57
+
9
58
  end
10
59
 
11
60
  end
@@ -16,6 +16,14 @@ module Tinkerforge
16
16
 
17
17
  private
18
18
 
19
+ def _print_4
20
+ if (co2 = get_co2_concentration) > 9999
21
+ ' ol '
22
+ else
23
+ '%4d' % co2
24
+ end
25
+ end
26
+
19
27
  def _view_21x8
20
28
  "CO2V2 #{uid_string.rjust 8}\n\n\n" +
21
29
  ('%d PPM' % get_co2_concentration).center(21)
@@ -0,0 +1,38 @@
1
+ module Tinkerforge
2
+
3
+ class BrickletParticulateMatter
4
+
5
+ # Enables the fan and the laser diode.
6
+ def enable
7
+ set_enable true
8
+ end
9
+
10
+ # Disables the fan and the laser diode.
11
+ def disable
12
+ set_enable false
13
+ end
14
+
15
+ # Returns the device's state.
16
+ def state
17
+ super.merge(
18
+ 'enabled' => get_enable,
19
+ 'pm_concentration' => get_pm_concentration,
20
+ 'pm_count' => get_pm_count,
21
+ 'sensor_info' => get_sensor_info,
22
+ )
23
+ end
24
+
25
+ private
26
+
27
+ def _view_21x8
28
+ "PM #{uid_string.rjust 8}\n\n" +
29
+ if get_enable
30
+ " PM1 %4d\n PM2.5 %4d\n PM10 %4d" % get_pm_concentration
31
+ else
32
+ "\n disabled"
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -35,30 +35,39 @@ module Tinkerforge
35
35
  self.segments = segments.gsub(/[ _]/, '').ljust(35, '0').chars.map { |s| s == '1' }
36
36
  end
37
37
 
38
+ # Turns all 35 segments on.
39
+ def all_on
40
+ self.segments = [true]*35
41
+ end
42
+
38
43
  # Returns the device's state.
39
44
  def state
40
45
  super.merge( 'brightness' => get_brightness )
41
46
  end
42
47
 
43
48
  # Displays a string.
44
- def print(text='')
45
- out = ''
46
- colon = false
47
-
48
- text.to_s.chars.each do |c|
49
- if glyphs.key? c
50
- out << glyphs[c] << '0'
51
- elsif c == '.' and out[-1] == '0'
52
- out[-1] = '1'
53
- elsif c == ':' and out.size == 16
54
- colon = true
55
- else
56
- raise "Can not display '#{text}'"
49
+ def print(text_or_object='')
50
+ if text_or_object.respond_to?( :_print_4, true)
51
+ print ( text_or_object.send(:_print_4) rescue '' )
52
+ else
53
+ out = ''
54
+ colon = false
55
+
56
+ text_or_object.to_s.chars.each do |c|
57
+ if glyphs.key? c
58
+ out << glyphs[c] << '0'
59
+ elsif c == '.' and out[-1] == '0'
60
+ out[-1] = '1'
61
+ elsif c == ':' and out.size == 16
62
+ colon = true
63
+ else
64
+ raise "Can not display '#{text}'"
65
+ end
57
66
  end
58
- end
59
67
 
60
- self.segments_string = out[0,32].ljust(32,'0') + ( colon ? '110' : '000' )
61
- nil
68
+ self.segments_string = out[0,32].ljust(32,'0') + ( colon ? '110' : '000' )
69
+ nil
70
+ end
62
71
  end
63
72
 
64
73
  # Returns the definition of glyphs for Unicode chracters.
@@ -78,6 +87,7 @@ module Tinkerforge
78
87
 
79
88
  'A' => '1110111',
80
89
  'b' => '0011111',
90
+ 'B' => '1111111',
81
91
  'c' => '0001101',
82
92
  'C' => '1001110',
83
93
  'd' => '0111101',
@@ -91,6 +101,7 @@ module Tinkerforge
91
101
  'J' => '0111000',
92
102
  'L' => '0001110',
93
103
  'l' => '0001100',
104
+ 'N' => '1110110',
94
105
  'n' => '0010101',
95
106
  'O' => '1111110',
96
107
  'o' => '0011101',
@@ -103,7 +114,8 @@ module Tinkerforge
103
114
  'u' => '0011100',
104
115
  'V' => '0111110',
105
116
  'v' => '0011100',
106
- 'y' => '0110011',
117
+ 'Y' => '0111011',
118
+ 'y' => '0111011',
107
119
 
108
120
  'ö' => '1011101',
109
121
  'ü' => '1011100',
@@ -1,9 +1,12 @@
1
1
  require 'tinkerforge/ip_connection'
2
+ require 'tinderfridge/shared/logger'
2
3
 
3
4
  module Tinkerforge
4
5
 
5
6
  class IPConnection
6
7
 
8
+ include Tinkerforge::Shared::Logger
9
+
7
10
  # Returns the host for the IP Connection.
8
11
  attr_reader :host
9
12
 
@@ -13,9 +16,27 @@ module Tinkerforge
13
16
  # Returns the network socket used by the IP Connection.
14
17
  attr_reader :socket
15
18
 
19
+ alias original_connect connect
20
+
21
+ # Creates a TCP/IP connection to the given host and port. Logs events if event logging is enabled.
22
+ def connect(host, port)
23
+ logger_debug "Connecting to #{host}:#{port}"
24
+ original_connect(host, port)
25
+ logger_debug "Connected to #{host}:#{port}"
26
+ end
27
+
28
+ alias original_disconnect disconnect
29
+
30
+ # Disconnects the TCP/IP connection. Logs events if event logging is enabled.
31
+ def disconnect
32
+ logger_debug "Disconnecting from #{host}:#{port}"
33
+ original_disconnect
34
+ logger_debug "Disconnected from #{host}:#{port}"
35
+ end
36
+
16
37
  # Returns a programmer-friendly representation of the object.
17
38
  def inspect
18
- "%s (%s:%s)" % [self.class, host, port]
39
+ "#{self.class} (%s:%s)" % (host ? [host, port] : ['-', '-'] )
19
40
  end
20
41
 
21
42
  # Returns the state of the IP Connection.
@@ -86,6 +107,7 @@ module Tinkerforge
86
107
  list = Tinkerforge::DeviceCollection.new
87
108
 
88
109
  self.register_callback(CALLBACK_ENUMERATE) do |*args|
110
+ logger_log_enum(args)
89
111
  case args[6]
90
112
  when 0, 1
91
113
  unless list.key?(args[0])
@@ -95,8 +117,6 @@ module Tinkerforge
95
117
  end
96
118
  when 2
97
119
  list.delete args[0]
98
- else
99
- raise "Unknown Enumeration Type: #{args[6]}"
100
120
  end
101
121
  end
102
122
 
@@ -112,7 +132,7 @@ module Tinkerforge
112
132
  #
113
133
  # Requires Brick Viewer version 2.4.23 or later.
114
134
  def open_brick_viewer
115
- if RUBY_PLATFORM =~ /darwin/
135
+ if host and (RUBY_PLATFORM =~ /darwin/)
116
136
  `open -n -a Brickv --args #{host} --port #{port}`
117
137
  "#{host}:#{port}"
118
138
  end
@@ -128,11 +148,19 @@ module Tinkerforge
128
148
  require "tinkerforge/#{dev_info[2][1]}"
129
149
  Tinkerforge.const_get(dev_info[2][0]).new enum_data[0], self
130
150
  else
131
- warn "Unknown Device Identifier: #{enum_data[5]} (UID: #{enum_data[0]})"
151
+ logger_warn "Unknown Device Identifier: #{enum_data[5]} (UID: #{enum_data[0]})"
132
152
  nil
133
153
  end
134
154
  end
135
155
 
156
+ def logger_log_enum(enum_data)
157
+ logger_debug(
158
+ "Device '#{enum_data[0]}' " +
159
+ ['available', 'connected', 'disconnected'][enum_data[6]] +
160
+ ( enum_data[6] == 2 ? '' : " (Device Identifier: #{enum_data[5]})" )
161
+ )
162
+ end
163
+
136
164
  end
137
165
 
138
166
 
@@ -0,0 +1,51 @@
1
+ module Tinkerforge
2
+
3
+ module Shared
4
+
5
+ # Mixin for event logging to a Logger instance.
6
+ #
7
+ # Logger is part of the Ruby Standard Library:
8
+ # - https://docs.ruby-lang.org/en/master/Logger.html
9
+ module Logger
10
+
11
+ private
12
+
13
+ def logger
14
+ Tinkerforge.logger
15
+ end
16
+
17
+ def logger_debug(msg)
18
+ if logger
19
+ logger.debug(msg)
20
+ end
21
+ end
22
+
23
+ def logger_info(msg)
24
+ if logger
25
+ logger.info(msg)
26
+ end
27
+ end
28
+
29
+ def logger_warn(msg)
30
+ if logger
31
+ logger.warn(msg)
32
+ end
33
+ end
34
+
35
+ def logger_error(msg)
36
+ if logger
37
+ logger.error(msg)
38
+ end
39
+ end
40
+
41
+ def logger_fatal(msg)
42
+ if logger
43
+ logger.fatal(msg)
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -7,6 +7,8 @@ module Tinkerforge
7
7
 
8
8
  class << self
9
9
 
10
+ @@logger = nil
11
+
10
12
  # Returns the directory where Tinkerforge bindings appear to be installed.
11
13
  def lib_dir
12
14
  File.dirname File.dirname Device.instance_method('uid').source_location.first
@@ -30,6 +32,25 @@ module Tinkerforge
30
32
  connect('localhost', port).discover(0.25)
31
33
  end
32
34
 
35
+ # Assign a Logger object to enable logging of Tinkerforge events.
36
+ def logger=(logger)
37
+ if logger
38
+ if logger.respond_to? :debug
39
+ @@logger = logger
40
+ logger.debug(about)
41
+ else
42
+ raise ArgumentError, 'Invalid Logger'
43
+ end
44
+ else
45
+ @@logger = nil
46
+ end
47
+ end
48
+
49
+ # Returns the Logger, or nil
50
+ def logger
51
+ @@logger
52
+ end
53
+
33
54
  private
34
55
 
35
56
  def _view_21x8
@@ -3,7 +3,7 @@ require 'tinkerforge/version'
3
3
  module Tinkerforge
4
4
 
5
5
  # Tinderfridge version.
6
- TINDERFRIDGE_VERSION = '0.13.0'
6
+ TINDERFRIDGE_VERSION = '0.15.0'
7
7
 
8
8
  # About Tinkerforge & Tinderfridge.
9
9
  def self.about
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tinderfridge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - lllist.eu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-21 00:00:00.000000000 Z
11
+ date: 2024-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tinkerforge
@@ -120,6 +120,7 @@ files:
120
120
  - lib/tinderfridge/devices/bricklet_outdoor_weather/bricklet_outdoor_weather.json
121
121
  - lib/tinderfridge/devices/bricklet_outdoor_weather/bricklet_outdoor_weather.rb
122
122
  - lib/tinderfridge/devices/bricklet_particulate_matter/bricklet_particulate_matter.json
123
+ - lib/tinderfridge/devices/bricklet_particulate_matter/bricklet_particulate_matter.rb
123
124
  - lib/tinderfridge/devices/bricklet_performance_dc/bricklet_performance_dc.json
124
125
  - lib/tinderfridge/devices/bricklet_piezo_speaker_v2/bricklet_piezo_speaker_v2.json
125
126
  - lib/tinderfridge/devices/bricklet_piezo_speaker_v2/bricklet_piezo_speaker_v2.rb
@@ -156,6 +157,7 @@ files:
156
157
  - lib/tinderfridge/devices/bricklet_xmc1400_breakout/bricklet_xmc1400_breakout.json
157
158
  - lib/tinderfridge/ip_connection.rb
158
159
  - lib/tinderfridge/shared/display_ibm437_encoding.rb
160
+ - lib/tinderfridge/shared/logger.rb
159
161
  - lib/tinderfridge/tinkerforge.rb
160
162
  - lib/tinderfridge/version.rb
161
163
  homepage: https://github.com/lllisteu/tinderfridge