tinderfridge 0.13.0 → 0.14.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: a5370c2b6210bbb986208ce2afc7f6ce9573592b7707bf14a8bc371ffb47050c
4
+ data.tar.gz: 79338d5081d87b1aff9402eaf656be89ed5a50c52f59f597c68a84e9462ccdd4
5
5
  SHA512:
6
- metadata.gz: e5d86b78c3c4351b63d9bcbad1032a661d6fdd9b00457a59deaed677d358577781fdc2809ebe7f8a7c762c77638685d601349bb2065930e9f5dad8232ae61a2f
7
- data.tar.gz: bc3e28b83f1704169827b0a4650e1f5a360714d70e5f153e62e4428f33360821e2514e31a10ff089cdfa4955225656106b26e314b03d6b24cdf91fe7190014a9
6
+ metadata.gz: c0077e242170d67d5c8ffdc63efd702183865d2e475c4be0c0f93bc4bf33aff1c313838a96ade44255245c320057e5369133d860fd5755b585f0fa92f1ae823c
7
+ data.tar.gz: 3a98205cf201dd8df8ca95378ad1bb82fa41af517f2f8b87a910f4f43b7753344ea0538d90aab6015fd3b8d16d2627b3ef24167380457601cf20f7aae19addfe
@@ -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,7 +92,7 @@ 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.
@@ -127,6 +139,12 @@ module Tinkerforge
127
139
  @config ||= {}
128
140
  end
129
141
 
142
+ # Sets configuration data (a Hash) for the device.
143
+ def config=(configuration)
144
+ raise(ArgumentError, 'Invalid configuration') unless configuration.class == Hash
145
+ @config = configuration
146
+ end
147
+
130
148
  # Opens the online documentation for the device (Mac OS only).
131
149
  #
132
150
  # When the URL for the documentation is not known, does nothing.
@@ -82,6 +82,16 @@ module Tinkerforge
82
82
  smap 'config'
83
83
  end
84
84
 
85
+ # Sets configuration data of devices in the collection.
86
+ def config=(configuration)
87
+ raise ArgumentError, 'invalid configuration' unless (configuration.class == Hash)
88
+ each do |k,v|
89
+ if configuration[k]
90
+ v.config = configuration[k]
91
+ end
92
+ end
93
+ end
94
+
85
95
  # Opens the online documentation for the devices in the collection (Mac OS only).
86
96
  #
87
97
  # When the URL for a device's documentation is not known, does nothing.
@@ -115,6 +125,11 @@ module Tinkerforge
115
125
  smap('ipcon').values.compact.uniq
116
126
  end
117
127
 
128
+ # Disconnects IP Connections used by devices in the collection.
129
+ def disconnect
130
+ ipcons.map { |i| [i, (i.get_connection_state == 0 ? nil : i.disconnect) ] }.to_h
131
+ end
132
+
118
133
  # Returns an array of devices in the collection matching the selector.
119
134
  #
120
135
  # Selector argument can be:
@@ -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
 
@@ -15,7 +18,7 @@ module Tinkerforge
15
18
 
16
19
  # Returns a programmer-friendly representation of the object.
17
20
  def inspect
18
- "%s (%s:%s)" % [self.class, host, port]
21
+ "#{self.class} (%s:%s)" % (host ? [host, port] : ['-', '-'] )
19
22
  end
20
23
 
21
24
  # Returns the state of the IP Connection.
@@ -86,6 +89,7 @@ module Tinkerforge
86
89
  list = Tinkerforge::DeviceCollection.new
87
90
 
88
91
  self.register_callback(CALLBACK_ENUMERATE) do |*args|
92
+ logger_log_enum(args)
89
93
  case args[6]
90
94
  when 0, 1
91
95
  unless list.key?(args[0])
@@ -95,8 +99,6 @@ module Tinkerforge
95
99
  end
96
100
  when 2
97
101
  list.delete args[0]
98
- else
99
- raise "Unknown Enumeration Type: #{args[6]}"
100
102
  end
101
103
  end
102
104
 
@@ -112,7 +114,7 @@ module Tinkerforge
112
114
  #
113
115
  # Requires Brick Viewer version 2.4.23 or later.
114
116
  def open_brick_viewer
115
- if RUBY_PLATFORM =~ /darwin/
117
+ if host and (RUBY_PLATFORM =~ /darwin/)
116
118
  `open -n -a Brickv --args #{host} --port #{port}`
117
119
  "#{host}:#{port}"
118
120
  end
@@ -128,11 +130,19 @@ module Tinkerforge
128
130
  require "tinkerforge/#{dev_info[2][1]}"
129
131
  Tinkerforge.const_get(dev_info[2][0]).new enum_data[0], self
130
132
  else
131
- warn "Unknown Device Identifier: #{enum_data[5]} (UID: #{enum_data[0]})"
133
+ logger_warn "Unknown Device Identifier: #{enum_data[5]} (UID: #{enum_data[0]})"
132
134
  nil
133
135
  end
134
136
  end
135
137
 
138
+ def logger_log_enum(enum_data)
139
+ logger_debug(
140
+ "Device '#{enum_data[0]}' " +
141
+ ['available', 'connected', 'disconnected'][enum_data[6]] +
142
+ ( enum_data[6] == 2 ? '' : " (Device Identifier: #{enum_data[5]})" )
143
+ )
144
+ end
145
+
136
146
  end
137
147
 
138
148
 
@@ -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.14.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.14.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: 2023-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tinkerforge
@@ -156,6 +156,7 @@ files:
156
156
  - lib/tinderfridge/devices/bricklet_xmc1400_breakout/bricklet_xmc1400_breakout.json
157
157
  - lib/tinderfridge/ip_connection.rb
158
158
  - lib/tinderfridge/shared/display_ibm437_encoding.rb
159
+ - lib/tinderfridge/shared/logger.rb
159
160
  - lib/tinderfridge/tinkerforge.rb
160
161
  - lib/tinderfridge/version.rb
161
162
  homepage: https://github.com/lllisteu/tinderfridge