openhab-scripting 2.16.2 → 2.19.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/openhab.rb +12 -16
- data/lib/openhab/core/entity_lookup.rb +173 -0
- data/lib/openhab/core/openhab_setup.rb +31 -0
- data/lib/openhab/core/osgi.rb +61 -0
- data/lib/openhab/dsl/actions.rb +105 -0
- data/lib/openhab/dsl/dsl.rb +49 -0
- data/lib/openhab/{core/dsl → dsl}/gems.rb +0 -1
- data/lib/openhab/dsl/group.rb +100 -0
- data/lib/openhab/dsl/items/datetime_item.rb +97 -0
- data/lib/openhab/dsl/items/items.rb +46 -0
- data/lib/openhab/dsl/items/number_item.rb +352 -0
- data/lib/openhab/dsl/items/rollershutter_item.rb +179 -0
- data/lib/openhab/dsl/items/string_item.rb +120 -0
- data/lib/openhab/dsl/monkey_patch/actions/actions.rb +4 -0
- data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +32 -0
- data/lib/openhab/dsl/monkey_patch/events/events.rb +5 -0
- data/lib/openhab/dsl/monkey_patch/events/item_command.rb +23 -0
- data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +35 -0
- data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +33 -0
- data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +61 -0
- data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +193 -0
- data/lib/openhab/dsl/monkey_patch/items/group_item.rb +37 -0
- data/lib/openhab/dsl/monkey_patch/items/items.rb +133 -0
- data/lib/openhab/dsl/monkey_patch/items/metadata.rb +281 -0
- data/lib/openhab/dsl/monkey_patch/items/persistence.rb +70 -0
- data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +95 -0
- data/lib/openhab/dsl/monkey_patch/ruby/number.rb +39 -0
- data/lib/openhab/dsl/monkey_patch/ruby/range.rb +47 -0
- data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +8 -0
- data/lib/openhab/dsl/monkey_patch/ruby/string.rb +41 -0
- data/lib/openhab/dsl/monkey_patch/ruby/time.rb +32 -0
- data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +70 -0
- data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +51 -0
- data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +36 -0
- data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +32 -0
- data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +69 -0
- data/lib/openhab/dsl/monkey_patch/types/types.rb +9 -0
- data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +33 -0
- data/lib/openhab/dsl/persistence.rb +25 -0
- data/lib/openhab/dsl/rules/automation_rule.rb +342 -0
- data/lib/openhab/dsl/rules/guard.rb +134 -0
- data/lib/openhab/dsl/rules/property.rb +102 -0
- data/lib/openhab/dsl/rules/rule.rb +116 -0
- data/lib/openhab/dsl/rules/rule_config.rb +151 -0
- data/lib/openhab/dsl/rules/triggers/changed.rb +143 -0
- data/lib/openhab/dsl/rules/triggers/channel.rb +53 -0
- data/lib/openhab/dsl/rules/triggers/command.rb +104 -0
- data/lib/openhab/dsl/rules/triggers/cron.rb +177 -0
- data/lib/openhab/dsl/rules/triggers/trigger.rb +124 -0
- data/lib/openhab/dsl/rules/triggers/updated.rb +98 -0
- data/lib/openhab/dsl/states.rb +61 -0
- data/lib/openhab/dsl/things.rb +91 -0
- data/lib/openhab/dsl/time_of_day.rb +232 -0
- data/lib/openhab/dsl/timers.rb +77 -0
- data/lib/openhab/dsl/types/datetime.rb +326 -0
- data/lib/openhab/dsl/types/quantity.rb +290 -0
- data/lib/openhab/dsl/units.rb +39 -0
- data/lib/openhab/log/configuration.rb +21 -0
- data/lib/openhab/log/logger.rb +172 -0
- data/lib/openhab/version.rb +1 -1
- metadata +60 -58
- data/lib/openhab/configuration.rb +0 -16
- data/lib/openhab/core/cron.rb +0 -27
- data/lib/openhab/core/debug.rb +0 -34
- data/lib/openhab/core/dsl.rb +0 -51
- data/lib/openhab/core/dsl/actions.rb +0 -107
- data/lib/openhab/core/dsl/entities.rb +0 -147
- data/lib/openhab/core/dsl/group.rb +0 -102
- data/lib/openhab/core/dsl/items/items.rb +0 -51
- data/lib/openhab/core/dsl/items/number_item.rb +0 -323
- data/lib/openhab/core/dsl/items/string_item.rb +0 -122
- data/lib/openhab/core/dsl/monkey_patch/actions/actions.rb +0 -4
- data/lib/openhab/core/dsl/monkey_patch/actions/script_thing_actions.rb +0 -22
- data/lib/openhab/core/dsl/monkey_patch/events.rb +0 -5
- data/lib/openhab/core/dsl/monkey_patch/events/item_command.rb +0 -13
- data/lib/openhab/core/dsl/monkey_patch/events/item_state_changed.rb +0 -25
- data/lib/openhab/core/dsl/monkey_patch/events/thing_status_info.rb +0 -26
- data/lib/openhab/core/dsl/monkey_patch/items/contact_item.rb +0 -54
- data/lib/openhab/core/dsl/monkey_patch/items/dimmer_item.rb +0 -182
- data/lib/openhab/core/dsl/monkey_patch/items/group_item.rb +0 -27
- data/lib/openhab/core/dsl/monkey_patch/items/items.rb +0 -132
- data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +0 -283
- data/lib/openhab/core/dsl/monkey_patch/items/persistence.rb +0 -72
- data/lib/openhab/core/dsl/monkey_patch/items/switch_item.rb +0 -87
- data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +0 -41
- data/lib/openhab/core/dsl/monkey_patch/ruby/range.rb +0 -47
- data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +0 -7
- data/lib/openhab/core/dsl/monkey_patch/ruby/string.rb +0 -43
- data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -60
- data/lib/openhab/core/dsl/monkey_patch/types/on_off_type.rb +0 -41
- data/lib/openhab/core/dsl/monkey_patch/types/open_closed_type.rb +0 -25
- data/lib/openhab/core/dsl/monkey_patch/types/percent_type.rb +0 -23
- data/lib/openhab/core/dsl/monkey_patch/types/quantity_type.rb +0 -58
- data/lib/openhab/core/dsl/monkey_patch/types/types.rb +0 -8
- data/lib/openhab/core/dsl/persistence.rb +0 -27
- data/lib/openhab/core/dsl/property.rb +0 -96
- data/lib/openhab/core/dsl/rule/automation_rule.rb +0 -345
- data/lib/openhab/core/dsl/rule/guard.rb +0 -136
- data/lib/openhab/core/dsl/rule/rule.rb +0 -117
- data/lib/openhab/core/dsl/rule/rule_config.rb +0 -153
- data/lib/openhab/core/dsl/rule/triggers/changed.rb +0 -145
- data/lib/openhab/core/dsl/rule/triggers/channel.rb +0 -55
- data/lib/openhab/core/dsl/rule/triggers/command.rb +0 -106
- data/lib/openhab/core/dsl/rule/triggers/cron.rb +0 -160
- data/lib/openhab/core/dsl/rule/triggers/trigger.rb +0 -126
- data/lib/openhab/core/dsl/rule/triggers/updated.rb +0 -100
- data/lib/openhab/core/dsl/states.rb +0 -63
- data/lib/openhab/core/dsl/things.rb +0 -93
- data/lib/openhab/core/dsl/time_of_day.rb +0 -231
- data/lib/openhab/core/dsl/timers.rb +0 -79
- data/lib/openhab/core/dsl/types/quantity.rb +0 -292
- data/lib/openhab/core/dsl/units.rb +0 -41
- data/lib/openhab/core/log.rb +0 -170
- data/lib/openhab/core/patch_load_path.rb +0 -7
- data/lib/openhab/core/startup_delay.rb +0 -23
- data/lib/openhab/osgi.rb +0 -59
@@ -1,292 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
require 'forwardable'
|
5
|
-
|
6
|
-
module OpenHAB
|
7
|
-
module Core
|
8
|
-
module DSL
|
9
|
-
#
|
10
|
-
# Ruby implementation of OpenHAB Types
|
11
|
-
#
|
12
|
-
module Types
|
13
|
-
#
|
14
|
-
# Ruby implementation for OpenHAB quantities
|
15
|
-
#
|
16
|
-
# rubocop: disable Metrics/ClassLength
|
17
|
-
# Disabled because this class has a single responsibility, there does not appear a logical
|
18
|
-
# way of breaking it up into multiple classes
|
19
|
-
class Quantity < Numeric
|
20
|
-
extend Forwardable
|
21
|
-
include Logging
|
22
|
-
|
23
|
-
def_delegator :@quantity, :to_s
|
24
|
-
|
25
|
-
java_import org.openhab.core.library.types.QuantityType
|
26
|
-
java_import 'tec.uom.se.format.SimpleUnitFormat'
|
27
|
-
java_import 'tec.uom.se.AbstractUnit'
|
28
|
-
|
29
|
-
# @return [Hash] Mapping of operation symbols to BigDecimal methods
|
30
|
-
OPERATIONS = {
|
31
|
-
'+' => 'add',
|
32
|
-
'-' => 'subtract',
|
33
|
-
'*' => 'multiply',
|
34
|
-
'/' => 'divide'
|
35
|
-
}.freeze
|
36
|
-
|
37
|
-
private_constant :OPERATIONS
|
38
|
-
|
39
|
-
attr_reader :quantity
|
40
|
-
|
41
|
-
#
|
42
|
-
# Create a new Quantity
|
43
|
-
#
|
44
|
-
# @param [object] quantity String,QuantityType or Numeric to be this quantity
|
45
|
-
#
|
46
|
-
# Cop disabled, case statement is compact and idiomatic
|
47
|
-
def initialize(quantity)
|
48
|
-
@quantity = case quantity
|
49
|
-
when String then QuantityType.new(quantity)
|
50
|
-
when QuantityType then quantity
|
51
|
-
when NumberItem, Numeric then QuantityType.new(quantity.to_d.to_java, AbstractUnit::ONE)
|
52
|
-
else raise ArgumentError, "Unexpected type #{quantity.class} provided to Quantity initializer"
|
53
|
-
end
|
54
|
-
super()
|
55
|
-
end
|
56
|
-
|
57
|
-
#
|
58
|
-
# Convert this quantity into a another unit
|
59
|
-
#
|
60
|
-
# @param [Object] other String or Unit to convert to
|
61
|
-
#
|
62
|
-
# @return [Quantity] This quantity converted to another unit
|
63
|
-
#
|
64
|
-
def |(other)
|
65
|
-
other = SimpleUnitFormat.instance.unitFor(other) if other.is_a? String
|
66
|
-
|
67
|
-
Quantity.new(quantity.to_unit(other))
|
68
|
-
end
|
69
|
-
|
70
|
-
#
|
71
|
-
# Compare this quantity
|
72
|
-
#
|
73
|
-
# @param [Object] other object to compare to
|
74
|
-
#
|
75
|
-
# @return [Integer] -1,0,1 if this object is less than, equal to, or greater than the supplied object,
|
76
|
-
# nil if it cannot be compared
|
77
|
-
#
|
78
|
-
def <=>(other)
|
79
|
-
logger.trace("Comparing #{self} to #{other}")
|
80
|
-
my_qt, other_qt = unitize(*to_qt(coerce(other).reverse))
|
81
|
-
my_qt.compare_to(other_qt)
|
82
|
-
end
|
83
|
-
|
84
|
-
#
|
85
|
-
# Coerce other object into a Quantity
|
86
|
-
#
|
87
|
-
# @param [Object] other object to convert to Quantity
|
88
|
-
#
|
89
|
-
# @return [Array] of self and other object as Quantity types, nil if object cannot be coerced
|
90
|
-
#
|
91
|
-
def coerce(other)
|
92
|
-
logger.trace("Coercing #{self} as a request from #{other.class}")
|
93
|
-
case other
|
94
|
-
when Quantity then [other.quantity, quantity]
|
95
|
-
when QuantityType then [other, quantity]
|
96
|
-
when NumberItem then [other.to_qt.quantity, quantity]
|
97
|
-
when Numeric, String then [Quantity.new(other), self]
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
#
|
102
|
-
# Forward missing methods to Openhab Quantity Item if they are defined
|
103
|
-
#
|
104
|
-
# @param [String] meth name of method invoked
|
105
|
-
# @param [Array] args arguments to invoked method
|
106
|
-
# @param [Proc] block block passed ot method
|
107
|
-
#
|
108
|
-
# @return [Object] result of delegation
|
109
|
-
#
|
110
|
-
def method_missing(meth, *args, &block)
|
111
|
-
logger.trace("Method missing, performing dynamic lookup for: #{meth}")
|
112
|
-
if quantity.respond_to?(meth)
|
113
|
-
quantity.__send__(meth, *args, &block)
|
114
|
-
elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
|
115
|
-
::Kernel.instance_method(meth).bind_call(self, *args, &block)
|
116
|
-
else
|
117
|
-
super(meth, *args, &block)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
#
|
122
|
-
# Checks if this method responds to the missing method
|
123
|
-
#
|
124
|
-
# @param [String] method_name Name of the method to check
|
125
|
-
# @param [Boolean] _include_private boolean if private methods should be checked
|
126
|
-
#
|
127
|
-
# @return [Boolean] true if this object will respond to the supplied method, false otherwise
|
128
|
-
#
|
129
|
-
def respond_to_missing?(method_name, _include_private = false)
|
130
|
-
quantity.respond_to?(method_name) ||
|
131
|
-
::Kernel.method_defined?(method_name) ||
|
132
|
-
::Kernel.private_method_defined?(method_name)
|
133
|
-
end
|
134
|
-
|
135
|
-
#
|
136
|
-
# Negate the quantity
|
137
|
-
#
|
138
|
-
# @return [Quantity] This quantity negated
|
139
|
-
#
|
140
|
-
def -@
|
141
|
-
Quantity.new(quantity.negate)
|
142
|
-
end
|
143
|
-
|
144
|
-
OPERATIONS.each do |operation, method|
|
145
|
-
define_method(operation) do |other|
|
146
|
-
logger.trace("Executing math operation '#{operation}' on quantity #{inspect} "\
|
147
|
-
"with other type #{other.class} and value #{other.inspect}")
|
148
|
-
|
149
|
-
a, b = to_qt(coerce(other).reverse)
|
150
|
-
logger.trace("Coerced a='#{a}' with b='#{b}'")
|
151
|
-
a, b = unitize(a, b, operation)
|
152
|
-
logger.trace("Unitized a='#{a}' b='#{b}'")
|
153
|
-
logger.trace("Performing operation '#{operation}' with method '#{method}' on a='#{a}' with b='#{b}'")
|
154
|
-
Quantity.new(a.public_send(method, b))
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
#
|
159
|
-
# Provide details about quantity object
|
160
|
-
#
|
161
|
-
# @return [String] Representing details about the quantity object
|
162
|
-
#
|
163
|
-
def inspect
|
164
|
-
if @quantity.unit == AbstractUnit::ONE
|
165
|
-
"unit=#{@quantity.unit}, value=#{@quantity.to_string}"
|
166
|
-
else
|
167
|
-
@quantity.to_string
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
private
|
172
|
-
|
173
|
-
# @return [Array] Array of strings for operations for which the operands will not be unitized
|
174
|
-
DIMENSIONLESS_NON_UNITIZED_OPERATIONS = %w[* /].freeze
|
175
|
-
|
176
|
-
# Dimensionless numbers should only be unitzed for addition and subtraction
|
177
|
-
|
178
|
-
#
|
179
|
-
# Convert one or more Quantity obects to the underlying quantitytypes
|
180
|
-
#
|
181
|
-
# @param [Array] quanities Array of either Quantity or QuantityType objects
|
182
|
-
#
|
183
|
-
# @return [Array] Array of QuantityType objects
|
184
|
-
#
|
185
|
-
def to_qt(*quanities)
|
186
|
-
[quanities].flatten.compact.map { |item| item.is_a?(Quantity) ? item.quantity : item }
|
187
|
-
end
|
188
|
-
|
189
|
-
#
|
190
|
-
# Checks if an item should be unitized
|
191
|
-
#
|
192
|
-
# @param [Quantity] quantity to check
|
193
|
-
# @param [String] operation quantity is being used with
|
194
|
-
#
|
195
|
-
# @return [Boolean] True if the quantity should be unitzed based on the unit and operation, false otherwise
|
196
|
-
#
|
197
|
-
def unitize?(quantity, operation)
|
198
|
-
!(quantity.unit == AbstractUnit::ONE && DIMENSIONLESS_NON_UNITIZED_OPERATIONS.include?(operation))
|
199
|
-
end
|
200
|
-
|
201
|
-
#
|
202
|
-
# Convert the unit for the quantity
|
203
|
-
#
|
204
|
-
# @param [Quantity] quantity being converted
|
205
|
-
#
|
206
|
-
# @return [Quantity] Quantity coverted to unit set by unit block
|
207
|
-
#
|
208
|
-
def convert_unit(quantity)
|
209
|
-
return quantity unless unit?
|
210
|
-
|
211
|
-
case quantity.unit
|
212
|
-
when unit
|
213
|
-
quantity
|
214
|
-
when AbstractUnit::ONE
|
215
|
-
convert_unit_from_dimensionless(quantity, unit)
|
216
|
-
else
|
217
|
-
convert_unit_from_dimensioned(quantity, unit)
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
#
|
222
|
-
# Converts a dimensioned quantity to a specific unit
|
223
|
-
#
|
224
|
-
# @param [Quantity] quantity to convert
|
225
|
-
# @param [Unit] unit to convert to
|
226
|
-
#
|
227
|
-
# @return [Java::org::openhab::core::library::types::QuantityType] converted quantity
|
228
|
-
#
|
229
|
-
def convert_unit_from_dimensioned(quantity, unit)
|
230
|
-
logger.trace("Converting dimensioned item #{inspect} to #{unit}")
|
231
|
-
quantity.to_unit(unit).tap do |converted|
|
232
|
-
raise "Conversion from #{quantity.unit} to #{unit} failed" unless converted
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
#
|
237
|
-
# Converts a dimensionless quantity to a unit
|
238
|
-
#
|
239
|
-
# @param [Quantity] quantity to convert
|
240
|
-
# @param [Unit] unit to convert to
|
241
|
-
#
|
242
|
-
# @return [Java::org::openhab::core::library::types::QuantityType] converted quantity
|
243
|
-
#
|
244
|
-
def convert_unit_from_dimensionless(quantity, unit)
|
245
|
-
logger.trace("Converting dimensionless #{quantity} to #{unit}")
|
246
|
-
QuantityType.new(quantity.to_big_decimal, unit)
|
247
|
-
end
|
248
|
-
|
249
|
-
#
|
250
|
-
# Convert quantities to appropriate units
|
251
|
-
#
|
252
|
-
# @param [Quantity] quantity_a Quantity on left side of operation
|
253
|
-
# @param [Quantity] quantity_b Quantity on right side of operation
|
254
|
-
# @param [String] operation Math operation
|
255
|
-
# @yield [quantity_a, quantity_b] yields unitized versions of supplied quantities
|
256
|
-
#
|
257
|
-
# @return [Array, Object] of quantites in correct units for the supplied operation and the unit
|
258
|
-
# or the result of the block if a block is given
|
259
|
-
#
|
260
|
-
def unitize(quantity_a, quantity_b, operation = nil)
|
261
|
-
logger.trace("Unitizing (#{quantity_a}) and (#{quantity_b})")
|
262
|
-
quantity_a, quantity_b = [quantity_a, quantity_b].map do |qt|
|
263
|
-
unitize?(qt, operation) ? convert_unit(qt) : qt
|
264
|
-
end
|
265
|
-
return yield quantity_a, quantity_b if block_given?
|
266
|
-
|
267
|
-
[quantity_a, quantity_b]
|
268
|
-
end
|
269
|
-
|
270
|
-
#
|
271
|
-
# Get the unit from the current thread local variable
|
272
|
-
#
|
273
|
-
# @return [Object] Unit or string representation of Unit, or nil if not set
|
274
|
-
#
|
275
|
-
def unit
|
276
|
-
Thread.current.thread_variable_get(:unit)
|
277
|
-
end
|
278
|
-
|
279
|
-
#
|
280
|
-
# Is a unit set for this thread
|
281
|
-
#
|
282
|
-
# @return [boolean] true if a unit is set by this thread, false otherwise
|
283
|
-
#
|
284
|
-
def unit?
|
285
|
-
unit != nil
|
286
|
-
end
|
287
|
-
end
|
288
|
-
end
|
289
|
-
end
|
290
|
-
end
|
291
|
-
end
|
292
|
-
# rubocop: enable Metrics/ClassLength
|
@@ -1,41 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
|
5
|
-
# Import Imperial and SI Units overriding provided values
|
6
|
-
%i[ImperialUnits SIUnits].each do |type|
|
7
|
-
Object.send(:remove_const, type)
|
8
|
-
java_import "org.openhab.core.library.unit.#{type}"
|
9
|
-
end
|
10
|
-
|
11
|
-
Object.send(:remove_const, :QuantityType)
|
12
|
-
java_import org.openhab.core.library.types.QuantityType
|
13
|
-
|
14
|
-
module OpenHAB
|
15
|
-
module Core
|
16
|
-
module DSL
|
17
|
-
#
|
18
|
-
# Provides support for interacting with OpenHAB Units of Measurement
|
19
|
-
#
|
20
|
-
module Units
|
21
|
-
java_import 'tec.uom.se.format.SimpleUnitFormat'
|
22
|
-
|
23
|
-
#
|
24
|
-
# Sets a thread local variable to the supplied unit such that classes operating inside the block
|
25
|
-
# can perform automatic conversions to the supplied unit for NumberItems
|
26
|
-
#
|
27
|
-
# @param [Object] unit OpenHAB Unit or String representing unit
|
28
|
-
# @yield [] Block executed in context of the supplied unit
|
29
|
-
#
|
30
|
-
#
|
31
|
-
def unit(unit)
|
32
|
-
unit = SimpleUnitFormat.instance.unitFor(unit) if unit.is_a? String
|
33
|
-
Thread.current.thread_variable_set(:unit, unit)
|
34
|
-
yield
|
35
|
-
ensure
|
36
|
-
Thread.current.thread_variable_set(:unit, nil)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
data/lib/openhab/core/log.rb
DELETED
@@ -1,170 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'openhab/configuration'
|
4
|
-
require 'java'
|
5
|
-
require 'pp'
|
6
|
-
|
7
|
-
#
|
8
|
-
# Provides access to the OpenHAB logging using a Ruby logging methods
|
9
|
-
#
|
10
|
-
module Logging
|
11
|
-
#
|
12
|
-
# Ruby Logger that forwards messages at appropriate levels to OpenHAB Logger
|
13
|
-
#
|
14
|
-
class Logger
|
15
|
-
java_import org.slf4j.LoggerFactory
|
16
|
-
|
17
|
-
# @return [Array] Supported logging levels
|
18
|
-
LEVELS = %i[TRACE DEBUG WARN INFO ERROR].freeze
|
19
|
-
|
20
|
-
#
|
21
|
-
# Create a new logger
|
22
|
-
#
|
23
|
-
# @param [String] name of the logger
|
24
|
-
#
|
25
|
-
def initialize(name)
|
26
|
-
@sl4fj_logger = LoggerFactory.getLogger(name)
|
27
|
-
end
|
28
|
-
|
29
|
-
# Dynamically define the methods for each level as identified by the levels constant
|
30
|
-
# This creates a method for each level that looks like this
|
31
|
-
# def <level>(msg=nil, &block)
|
32
|
-
# log(severity: <level>, msg: msg, &block)
|
33
|
-
# end
|
34
|
-
LEVELS.each do |level|
|
35
|
-
method = level.to_s.downcase
|
36
|
-
define_method(method.to_s) do |msg = nil, &block|
|
37
|
-
log(severity: level, msg: msg, &block)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
#
|
44
|
-
# Log a message to the OpenHAB Logger
|
45
|
-
#
|
46
|
-
# @param [Symbol] severity Severity to log message at
|
47
|
-
# @param [Object] msg to log, if no msg supplied and a block is provided,
|
48
|
-
# the msg is taken from the result of the block
|
49
|
-
#
|
50
|
-
def log(severity:, msg: nil)
|
51
|
-
severity = severity.to_sym
|
52
|
-
|
53
|
-
raise ArgumentError, "Unknown Severity #{severity}" unless LEVELS.include? severity
|
54
|
-
|
55
|
-
# Dynamically check enablement of underlying logger, this expands to "is_<level>_enabled"
|
56
|
-
return unless @sl4fj_logger.send("is_#{severity.to_s.downcase}_enabled")
|
57
|
-
|
58
|
-
# Process block if no message provided
|
59
|
-
msg = yield if msg.nil? && block_given?
|
60
|
-
|
61
|
-
msg = message_to_string(msg: msg)
|
62
|
-
|
63
|
-
# Dynamically invoke underlying logger, this expands to "<level>(message)"
|
64
|
-
@sl4fj_logger.send(severity.to_s.downcase, msg)
|
65
|
-
end
|
66
|
-
|
67
|
-
#
|
68
|
-
# Conver the supplied message object to a String
|
69
|
-
#
|
70
|
-
# @param [object] msg object to convert
|
71
|
-
#
|
72
|
-
# @return [String] Msg object as a string
|
73
|
-
#
|
74
|
-
def message_to_string(msg:)
|
75
|
-
case msg
|
76
|
-
when ::String
|
77
|
-
msg
|
78
|
-
when ::Exception
|
79
|
-
"#{msg.message} (#{msg.class})\n#{msg.backtrace&.join("\n")}"
|
80
|
-
else
|
81
|
-
msg.inspect
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
@loggers = {}
|
87
|
-
|
88
|
-
# Return a logger with the configured log prefix plus the calling scripts name
|
89
|
-
|
90
|
-
#
|
91
|
-
# Create a logger for the current class
|
92
|
-
#
|
93
|
-
# @return [Logger] for the current class
|
94
|
-
#
|
95
|
-
def logger
|
96
|
-
Logging.logger(self.class.name)
|
97
|
-
end
|
98
|
-
|
99
|
-
class << self
|
100
|
-
#
|
101
|
-
# Injects a logger into the base class
|
102
|
-
#
|
103
|
-
# @param [String] name of the logger
|
104
|
-
#
|
105
|
-
# @return [Logger] for the supplied name
|
106
|
-
#
|
107
|
-
def logger(name)
|
108
|
-
name ||= self.class.name
|
109
|
-
@loggers[name] ||= Logging.logger_for(name)
|
110
|
-
end
|
111
|
-
|
112
|
-
#
|
113
|
-
# Configure a logger for the supplied class name
|
114
|
-
#
|
115
|
-
# @param [String] classname to configure logger for
|
116
|
-
#
|
117
|
-
# @return [Logger] for the supplied classname
|
118
|
-
#
|
119
|
-
def logger_for(classname)
|
120
|
-
configure_logger_for(classname)
|
121
|
-
end
|
122
|
-
|
123
|
-
private
|
124
|
-
|
125
|
-
#
|
126
|
-
# Configure a logger for the supplied classname
|
127
|
-
#
|
128
|
-
# @param [String] classname to create logger for
|
129
|
-
#
|
130
|
-
# @return [Logger] Logger for the supplied classname
|
131
|
-
#
|
132
|
-
def configure_logger_for(classname)
|
133
|
-
log_prefix = Configuration.log_prefix
|
134
|
-
log_prefix += if classname
|
135
|
-
".#{classname}"
|
136
|
-
else
|
137
|
-
".#{log_caller}"
|
138
|
-
end
|
139
|
-
Logger.new(log_prefix)
|
140
|
-
end
|
141
|
-
|
142
|
-
#
|
143
|
-
# Figure out the log prefix
|
144
|
-
#
|
145
|
-
# @return [String] Prefix for log messages
|
146
|
-
#
|
147
|
-
def log_caller
|
148
|
-
caller_locations.map(&:path)
|
149
|
-
.grep_v(%r{openhab/core/})
|
150
|
-
.grep_v(/rubygems/)
|
151
|
-
.grep_v(%r{lib/ruby})
|
152
|
-
.first
|
153
|
-
.yield_self { |caller| File.basename(caller, '.*') }
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
#
|
158
|
-
# Add logger method to the object that includes this module
|
159
|
-
#
|
160
|
-
# @param [Object] base Object to add method to
|
161
|
-
#
|
162
|
-
#
|
163
|
-
def self.included(base)
|
164
|
-
class << base
|
165
|
-
def logger
|
166
|
-
Logging.logger(self.class.name)
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|