openhab-jrubyscripting 5.0.0.rc3 → 5.0.0.rc4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce249607265a008aa969e2edbd56302edd70a67a01dfebaec9ad67b7e5f1ad27
4
- data.tar.gz: ac0c8d25509a8be8374a3f15fecd2e575da8cb08b82268aef41414f64f7a3b38
3
+ metadata.gz: 2e06b71ae524165363db99771e7ef601f542ed14c979572d152b079d70ad82a2
4
+ data.tar.gz: 0d6002391ed26a68bbb907d94787f510df30ef6ef9a3a7cd445ac9c47122851b
5
5
  SHA512:
6
- metadata.gz: 7c082f0949b6bc55ebf48b1489eade3f6bbcf25181d6741e29697dbb5ef5221abf583c78fb8ad46596b5ea10957e9d4b5d2d5dfe35097eea004f005d06683d48
7
- data.tar.gz: ee7e740d0dd1233462521313c31145971bed68e2830c765bad27b8762d064bdf154993a2d733fc7743240bd55aec9e0eecd8cb4f2d569dbd0c599e765140cd06
6
+ metadata.gz: 05a3016219eaf41fc4af71f1476029e2b8d0b320afc7bed99e9777b7726afc6897328d9aa92f643132e66d647aae8742eca36f26c703ab3763e1738c55d249e1
7
+ data.tar.gz: d5a7d65543b3628e5c3abfa61cb2aa6d6f10e2ab2bb3334d9f38e1ee8952da655da6360c156d926bd53b25c013c86c7672dc1b492c85aaa056e898579a7be7dd
@@ -66,11 +66,15 @@ module OpenHAB
66
66
  #
67
67
  module Actions
68
68
  OSGi.services("org.openhab.core.model.script.engine.action.ActionService")&.each do |service|
69
- java_import service.action_class.ruby_class
69
+ klass = (java_import service.action_class.ruby_class).first
70
70
  logger.trace("Loaded ACTION: #{service.action_class}")
71
+ Object.const_set(service.action_class.simple_name, klass)
71
72
  end
72
73
  # Import common actions
73
- %w[Exec HTTP Ping].each { |action| java_import "org.openhab.core.model.script.actions.#{action}" }
74
+ %w[Exec HTTP Ping].each do |action|
75
+ klass = (java_import "org.openhab.core.model.script.actions.#{action}").first
76
+ Object.const_set(action, klass)
77
+ end
74
78
 
75
79
  module_function
76
80
 
@@ -380,6 +380,7 @@ module OpenHAB
380
380
 
381
381
  [value, to_h].inspect
382
382
  end
383
+ remove_method :to_s
383
384
  alias_method :to_s, :inspect
384
385
 
385
386
  #
@@ -399,7 +400,7 @@ module OpenHAB
399
400
  return preferred_provider unless provider
400
401
 
401
402
  unless provider.is_a?(org.openhab.core.common.registry.ManagedProvider)
402
- raise FrozenError, "Cannot modify metadata from provider #{provider.inspect}"
403
+ raise FrozenError, "Cannot modify metadata from provider #{provider.inspect} for #{uid}."
403
404
  end
404
405
 
405
406
  if preferred_provider != provider
@@ -286,7 +286,7 @@ module OpenHAB
286
286
  # @return [Object] QuantityType or the original value
287
287
  #
288
288
  def quantify(value)
289
- if value.is_a?(DecimalType) && unit
289
+ if value.is_a?(DecimalType) && respond_to?(:unit) && unit
290
290
  logger.trace("Unitizing #{value} with unit #{unit}")
291
291
  QuantityType.new(value.to_big_decimal, unit)
292
292
  else
@@ -187,11 +187,6 @@ module OpenHAB
187
187
  # end
188
188
  #
189
189
 
190
- # @!visibility private
191
- # import the actual semantics action
192
- SemanticsAction = org.openhab.core.model.script.actions.Semantics
193
- private_constant :SemanticsAction
194
-
195
190
  # import all the semantics constants
196
191
  [org.openhab.core.semantics.model.point.Points,
197
192
  org.openhab.core.semantics.model.property.Properties,
@@ -223,6 +218,7 @@ module OpenHAB
223
218
  # Property = org.openhab.core.semantics.Property
224
219
 
225
220
  # put ourself into the global namespace, replacing the action
221
+ Object.send(:remove_const, :Semantics)
226
222
  ::Semantics = self # rubocop:disable Naming/ConstantName
227
223
 
228
224
  #
@@ -234,7 +230,7 @@ module OpenHAB
234
230
  # @return [true, false]
235
231
  #
236
232
  def location?
237
- SemanticsAction.location?(self)
233
+ Actions::Semantics.location?(self)
238
234
  end
239
235
 
240
236
  #
@@ -246,7 +242,7 @@ module OpenHAB
246
242
  # @return [true, false]
247
243
  #
248
244
  def equipment?
249
- SemanticsAction.equipment?(self)
245
+ Actions::Semantics.equipment?(self)
250
246
  end
251
247
 
252
248
  # Checks if this Item is a {Point}
@@ -257,7 +253,7 @@ module OpenHAB
257
253
  # @return [true, false]
258
254
  #
259
255
  def point?
260
- SemanticsAction.point?(self)
256
+ Actions::Semantics.point?(self)
261
257
  end
262
258
 
263
259
  #
@@ -280,7 +276,7 @@ module OpenHAB
280
276
  # @return [GenericItem, nil]
281
277
  #
282
278
  def location
283
- SemanticsAction.get_location(self)&.then(&Proxy.method(:new))
279
+ Actions::Semantics.get_location(self)&.then(&Proxy.method(:new))
284
280
  end
285
281
 
286
282
  #
@@ -293,7 +289,7 @@ module OpenHAB
293
289
  # @return [Class, nil]
294
290
  #
295
291
  def location_type
296
- SemanticsAction.get_location_type(self)&.ruby_class
292
+ Actions::Semantics.get_location_type(self)&.ruby_class
297
293
  end
298
294
 
299
295
  #
@@ -306,7 +302,7 @@ module OpenHAB
306
302
  # @return [GenericItem, nil]
307
303
  #
308
304
  def equipment
309
- SemanticsAction.get_equipment(self)&.then(&Proxy.method(:new))
305
+ Actions::Semantics.get_equipment(self)&.then(&Proxy.method(:new))
310
306
  end
311
307
 
312
308
  #
@@ -319,7 +315,7 @@ module OpenHAB
319
315
  # @return [Class, nil]
320
316
  #
321
317
  def equipment_type
322
- SemanticsAction.get_equipment_type(self)&.ruby_class
318
+ Actions::Semantics.get_equipment_type(self)&.ruby_class
323
319
  end
324
320
 
325
321
  #
@@ -330,7 +326,7 @@ module OpenHAB
330
326
  # @return [Class, nil]
331
327
  #
332
328
  def point_type
333
- SemanticsAction.get_point_type(self)&.ruby_class
329
+ Actions::Semantics.get_point_type(self)&.ruby_class
334
330
  end
335
331
 
336
332
  #
@@ -341,7 +337,7 @@ module OpenHAB
341
337
  # @return [Class, nil]
342
338
  #
343
339
  def property_type
344
- SemanticsAction.get_property_type(self)&.ruby_class
340
+ Actions::Semantics.get_property_type(self)&.ruby_class
345
341
  end
346
342
 
347
343
  # @!attribute [r] semantic_type
@@ -355,7 +351,7 @@ module OpenHAB
355
351
  # @return [Class, nil]
356
352
  #
357
353
  def semantic_type
358
- SemanticsAction.get_semantic_type(self)&.ruby_class
354
+ Actions::Semantics.get_semantic_type(self)&.ruby_class
359
355
  end
360
356
 
361
357
  #
@@ -409,7 +405,7 @@ module Enumerable
409
405
  # Returns a new array of items that are a semantics Location (optionally of the given type)
410
406
  # @return [Array<GenericItem>]
411
407
  def locations(type = nil)
412
- if type && (!type.is_a?(Module) || !(type < OpenHAB::Core::Items::Semantics::Location))
408
+ if type && (!type.is_a?(Module) || !(type < Semantics::Location))
413
409
  raise ArgumentError, "type must be a subclass of Location"
414
410
  end
415
411
 
@@ -432,7 +428,7 @@ module Enumerable
432
428
  # @example Get all TVs in a room
433
429
  # lGreatRoom.equipments(Semantics::Screen)
434
430
  def equipments(type = nil)
435
- if type && (!type.is_a?(Module) || !(type < OpenHAB::Core::Items::Semantics::Equipment))
431
+ if type && (!type.is_a?(Module) || !(type < Semantics::Equipment))
436
432
  raise ArgumentError, "type must be a subclass of Equipment"
437
433
  end
438
434
 
@@ -457,19 +453,19 @@ module Enumerable
457
453
  end
458
454
  unless point_or_property_types.all? do |tag|
459
455
  tag.is_a?(Module) &&
460
- (tag < OpenHAB::Core::Items::Semantics::Point || tag < OpenHAB::Core::Items::Semantics::Property)
456
+ (tag < Semantics::Point || tag < Semantics::Property)
461
457
  end
462
458
  raise ArgumentError, "point_or_property_types must all be a subclass of Point or Property"
463
459
  end
464
- if point_or_property_types.count { |tag| tag < OpenHAB::Core::Items::Semantics::Point } > 1 ||
465
- point_or_property_types.count { |tag| tag < OpenHAB::Core::Items::Semantics::Property } > 1
460
+ if point_or_property_types.count { |tag| tag < Semantics::Point } > 1 ||
461
+ point_or_property_types.count { |tag| tag < Semantics::Property } > 1
466
462
  raise ArgumentError, "point_or_property_types cannot both be a subclass of Point or Property"
467
463
  end
468
464
 
469
465
  select do |point|
470
466
  point.point? && point_or_property_types.all? do |tag|
471
- (tag < OpenHAB::Core::Items::Semantics::Point && point.point_type <= tag) ||
472
- (tag < OpenHAB::Core::Items::Semantics::Property && point.property_type&.<=(tag))
467
+ (tag < Semantics::Point && point.point_type <= tag) ||
468
+ (tag < Semantics::Property && point.property_type&.<=(tag))
473
469
  end
474
470
  end
475
471
  end
@@ -98,6 +98,19 @@ module OpenHAB
98
98
 
99
99
  def initialize
100
100
  @profiles = {}
101
+
102
+ @registration = OSGi.register_service(self)
103
+ ScriptHandling.script_unloaded { unregister }
104
+ end
105
+
106
+ #
107
+ # Unregister the ProfileFactory OSGi service
108
+ #
109
+ # @!visibility private
110
+ # @return [void]
111
+ #
112
+ def unregister
113
+ @registration.unregister
101
114
  end
102
115
 
103
116
  # @!visibility private
@@ -113,8 +126,5 @@ module OpenHAB
113
126
  @profiles.keys
114
127
  end
115
128
  end
116
-
117
- registration = OSGi.register_service(ProfileFactory.instance)
118
- ScriptHandling.script_unloaded { registration.unregister }
119
129
  end
120
130
  end
@@ -212,11 +212,11 @@ module OpenHAB
212
212
 
213
213
  private
214
214
 
215
- def initialize
216
- super
215
+ def initialize(script_unloaded_before: nil)
216
+ super()
217
217
  @elements = {}
218
218
  self.class.registry.add_provider(self)
219
- ScriptHandling.script_unloaded { unregister }
219
+ ScriptHandling.script_unloaded(before: script_unloaded_before) { unregister }
220
220
  end
221
221
  end
222
222
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Rules
6
+ # @interface
7
+ java_import org.openhab.core.automation.Module
8
+
9
+ # @!visibility private
10
+ module Module
11
+ # @return [String]
12
+ def inspect
13
+ r = "#<OpenHAB::Core::Rules::#{self.class.simple_name} #{id} (#{type_uid})"
14
+ r += " #{label.inspect}" if label
15
+ r += " configuration=#{configuration.properties.to_h}" unless configuration.properties.empty?
16
+ "#{r}>"
17
+ end
18
+
19
+ # @return [String]
20
+ def to_s
21
+ id
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -19,6 +19,21 @@ module OpenHAB
19
19
  $rules
20
20
  end
21
21
  end
22
+
23
+ def initialize
24
+ super(script_unloaded_before: lambda do |callbacks|
25
+ callbacks.index do |cb|
26
+ case cb.binding.receiver
27
+ when Items::Provider,
28
+ Things::Provider,
29
+ DSL::TimerManager
30
+ true
31
+ else
32
+ false
33
+ end
34
+ end
35
+ end)
36
+ end
22
37
  end
23
38
  end
24
39
  end
@@ -29,9 +29,11 @@ module OpenHAB
29
29
  # end
30
30
  #
31
31
  def script_loaded(&block)
32
- Core::ScriptHandlingCallbacks.script_loaded_hooks << block
32
+ ScriptHandlingCallbacks.script_loaded_hooks << block
33
33
  end
34
34
 
35
+ #
36
+ # @!method script_unloaded(&block)
35
37
  #
36
38
  # Add a block of code to be executed when the script is unloaded.
37
39
  #
@@ -47,8 +49,10 @@ module OpenHAB
47
49
  # logger.info 'Hi, this script has been unloaded'
48
50
  # end
49
51
  #
50
- def script_unloaded(&block)
51
- Core::ScriptHandlingCallbacks.script_unloaded_hooks << block
52
+ def script_unloaded(before: nil, &block)
53
+ # `before` is as yet undocumented, because I'm not set on its interface
54
+ index = before.call(ScriptHandlingCallbacks.script_unloaded_hooks) if before
55
+ ScriptHandlingCallbacks.script_unloaded_hooks.insert(index || -1, block)
52
56
  end
53
57
  end
54
58
 
@@ -58,6 +62,13 @@ module OpenHAB
58
62
  # @!visibility private
59
63
  module ScriptHandlingCallbacks
60
64
  class << self
65
+ #
66
+ # Has the script completed loading?
67
+ #
68
+ # @!visibility private
69
+ # @return [true, false]
70
+ attr_accessor :script_loaded
71
+
61
72
  #
62
73
  # Return script_loaded_hooks
63
74
  #
@@ -74,6 +85,7 @@ module OpenHAB
74
85
  @script_unloaded_hooks ||= []
75
86
  end
76
87
  end
88
+ self.script_loaded = false
77
89
 
78
90
  #
79
91
  # Executed when OpenHAB unloads a script file
@@ -85,6 +97,22 @@ module OpenHAB
85
97
  rescue => e
86
98
  logger.error("Failed to call script_unloaded hook #{hook}: #{e}")
87
99
  end
100
+
101
+ return if ScriptHandlingCallbacks.script_loaded
102
+
103
+ # Make sure we terminate the main thread if it's still set up, in case
104
+ # it's timing out and that's why we're unloading.
105
+ #
106
+ # It would seem simpler to just record Thread.current when this file
107
+ # loads, but if the user is using the autorequire feature of the
108
+ # jrubyscripting addon, this file will load before the main script.
109
+ #
110
+ # Note that Thread.list only includes threads that have had Ruby
111
+ # execute in them, so we don't need to worry about accidentally killing
112
+ # a random Java thread.
113
+ #
114
+ main_thread = Thread.list.find { |t| t != Thread.current && t.name.include?("-safeCall-") }
115
+ main_thread&.raise(Interrupt.new)
88
116
  end
89
117
 
90
118
  #
@@ -97,6 +125,7 @@ module OpenHAB
97
125
  rescue => e
98
126
  logger.error("Failed to call script_loaded hook #{hook}: #{e}")
99
127
  end
128
+ ScriptHandlingCallbacks.script_loaded = true
100
129
  end
101
130
  end
102
131
  end
@@ -5,7 +5,7 @@ require_relative "time"
5
5
  module OpenHAB
6
6
  module CoreExt
7
7
  module Java
8
- java_import java.time.Month
8
+ Month = java.time.Month
9
9
 
10
10
  # Extensions to Month
11
11
  class Month
@@ -87,5 +87,6 @@ class Date
87
87
  [other.to_date, self]
88
88
  end
89
89
 
90
+ remove_method :inspect
90
91
  alias_method :inspect, :to_s
91
92
  end
@@ -1417,7 +1417,7 @@ module OpenHAB
1417
1417
  added_rule = add_rule(provider, rule)
1418
1418
  # add config so that MainUI can show the script
1419
1419
  added_rule.actions.first.configuration.put("type", "application/x-ruby")
1420
- added_rule.actions.first.configuration.put("script", script)
1420
+ added_rule.actions.first.configuration.put("script", script) if script
1421
1421
 
1422
1422
  rule.execute(nil, { "event" => Struct.new(:attachment).new(start_attachment) }) if on_start?
1423
1423
  added_rule
@@ -44,7 +44,7 @@ module OpenHAB
44
44
  def append_trigger(type:, config:, attach: nil, conditions: nil)
45
45
  config.transform_keys!(&:to_s)
46
46
  RuleTriggers.trigger(type: type, config: config).tap do |trigger|
47
- logger.trace("Appending trigger (#{trigger}) attach (#{attach}) conditions(#{conditions})")
47
+ logger.trace("Appending trigger (#{trigger.inspect}) attach (#{attach}) conditions(#{conditions})")
48
48
  @triggers << trigger
49
49
  @attachments[trigger.id] = attach if attach
50
50
  @trigger_conditions[trigger.id] = conditions if conditions
@@ -109,7 +109,7 @@ module OpenHAB
109
109
  state, = retrieve_states(inputs)
110
110
  if state == @tracking_to
111
111
  logger.trace("Item changed to #{state} for #{self}, rescheduling timer.")
112
- @timer.reschedule(ZonedDateTime.now.plus(@duration))
112
+ @timer.reschedule(@duration)
113
113
  else
114
114
  logger.trace("Item changed to #{state} for #{self}, canceling timer.")
115
115
  @timer.cancel
@@ -4,6 +4,6 @@ module OpenHAB
4
4
  module DSL
5
5
  # Version of OpenHAB helper libraries
6
6
  # @return [String]
7
- VERSION = "5.0.0.rc3"
7
+ VERSION = "5.0.0.rc4"
8
8
  end
9
9
  end
data/lib/openhab/log.rb CHANGED
@@ -235,12 +235,19 @@ module OpenHAB
235
235
  alias_method :to_s, :inspect
236
236
 
237
237
  # @!attribute [rw] level
238
+ #
239
+ # @note When a logger's level is modified, the logging infrastructure has
240
+ # to reload, and logging may be completely unavailable for a short time.
241
+ #
238
242
  # @return [:error,:warn,:info,:debug,:trace] The current log level
243
+ #
239
244
  def level
240
245
  Logger.log_service.get_level(name)[name]&.downcase&.to_sym
241
246
  end
242
247
 
243
248
  def level=(level)
249
+ return if self.level == level
250
+
244
251
  Logger.log_service.set_level(name, level.to_s)
245
252
  end
246
253
 
@@ -57,9 +57,8 @@ module OpenHAB
57
57
  tm.class.field_reader :storage
58
58
  tm.storage.keys.each { |k| tm.storage.remove(k) } # rubocop:disable Style/HashEachMethods not a hash
59
59
 
60
- profile_factory = Core::ProfileFactory.send(:new)
61
- @profile_factory_registration = OSGi.register_service(profile_factory)
62
- allow(Core::ProfileFactory).to receive(:instance).and_return(profile_factory)
60
+ @profile_factory = Core::ProfileFactory.send(:new)
61
+ allow(Core::ProfileFactory).to receive(:instance).and_return(@profile_factory)
63
62
 
64
63
  stub_const("OpenHAB::Core::Timer", Mocks::Timer) if self.class.mock_timers?
65
64
 
@@ -72,7 +71,7 @@ module OpenHAB
72
71
  config.after do
73
72
  Core::Items::Proxy.reset_cache
74
73
  Core::Things::Proxy.reset_cache
75
- @profile_factory_registration.unregister
74
+ @profile_factory.unregister
76
75
  timers.cancel_all
77
76
  # timers and rules have already been canceled, so we can safely just
78
77
  # wipe this
@@ -429,6 +429,11 @@ module OpenHAB
429
429
 
430
430
  tm.bundleResolver = Mocks::BundleResolver.instance
431
431
 
432
+ require_relative "mocks/safe_caller"
433
+ field = tm.class.java_class.declared_field :safeCaller
434
+ field.accessible = true
435
+ field.set(tm, Mocks::SafeCaller.instance)
436
+
432
437
  require_relative "mocks/thing_handler"
433
438
  thf = Mocks::ThingHandlerFactory.instance
434
439
  bundle = org.osgi.framework.FrameworkUtil.get_bundle(org.openhab.core.thing.Thing)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-jrubyscripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0.rc3
4
+ version: 5.0.0.rc4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-26 00:00:00.000000000 Z
11
+ date: 2022-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -369,6 +369,7 @@ files:
369
369
  - lib/openhab/core/provider.rb
370
370
  - lib/openhab/core/registry.rb
371
371
  - lib/openhab/core/rules.rb
372
+ - lib/openhab/core/rules/module.rb
372
373
  - lib/openhab/core/rules/provider.rb
373
374
  - lib/openhab/core/rules/registry.rb
374
375
  - lib/openhab/core/rules/rule.rb
@@ -455,7 +456,6 @@ files:
455
456
  - lib/openhab/dsl/rules/triggers/updated.rb
456
457
  - lib/openhab/dsl/rules/triggers/watch/watch.rb
457
458
  - lib/openhab/dsl/rules/triggers/watch/watch_handler.rb
458
- - lib/openhab/dsl/script_handling.rb
459
459
  - lib/openhab/dsl/things/builder.rb
460
460
  - lib/openhab/dsl/thread_local.rb
461
461
  - lib/openhab/dsl/timer_manager.rb
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OpenHAB
4
- module DSL
5
- end
6
- end