openhab-scripting 4.46.2 → 5.0.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/core/actions/audio.rb +47 -0
- data/lib/openhab/core/actions/ephemeris.rb +39 -0
- data/lib/openhab/core/actions/exec.rb +51 -0
- data/lib/openhab/core/actions/http.rb +80 -0
- data/lib/openhab/core/actions/ping.rb +30 -0
- data/lib/openhab/core/actions/transformation.rb +32 -0
- data/lib/openhab/core/actions/voice.rb +36 -0
- data/lib/openhab/core/actions.rb +82 -0
- data/lib/openhab/core/dependency_tracking.rb +34 -0
- data/lib/openhab/core/dto/item_channel_link.rb +33 -0
- data/lib/openhab/core/dto/thing.rb +27 -0
- data/lib/openhab/core/dto.rb +11 -0
- data/lib/openhab/core/entity_lookup.rb +152 -70
- data/lib/openhab/core/events/abstract_event.rb +18 -0
- data/lib/openhab/core/events/abstract_item_registry_event.rb +36 -0
- data/lib/openhab/core/events/abstract_thing_registry_event.rb +40 -0
- data/lib/openhab/core/events/item_command_event.rb +78 -0
- data/lib/openhab/core/events/item_event.rb +22 -0
- data/lib/openhab/core/events/item_state_changed_event.rb +75 -0
- data/lib/openhab/core/events/item_state_event.rb +79 -0
- data/lib/openhab/core/events/thing_status_info_event.rb +55 -0
- data/lib/openhab/core/events.rb +10 -0
- data/lib/openhab/core/items/accepted_data_types.rb +29 -0
- data/lib/openhab/core/items/color_item.rb +52 -0
- data/lib/openhab/core/items/contact_item.rb +52 -0
- data/lib/openhab/core/items/date_time_item.rb +59 -0
- data/lib/openhab/core/items/dimmer_item.rb +148 -0
- data/lib/openhab/core/items/generic_item.rb +292 -0
- data/lib/openhab/core/items/group_item.rb +176 -0
- data/lib/openhab/{dsl → core}/items/image_item.rb +35 -29
- data/lib/openhab/core/items/item.rb +273 -0
- data/lib/openhab/core/items/location_item.rb +34 -0
- data/lib/openhab/core/items/metadata/hash.rb +433 -0
- data/lib/openhab/core/items/metadata/namespace_hash.rb +475 -0
- data/lib/openhab/core/items/metadata/provider.rb +48 -0
- data/lib/openhab/core/items/metadata.rb +11 -0
- data/lib/openhab/core/items/number_item.rb +62 -0
- data/lib/openhab/core/items/numeric_item.rb +22 -0
- data/lib/openhab/core/items/persistence.rb +416 -0
- data/lib/openhab/core/items/player_item.rb +66 -0
- data/lib/openhab/core/items/provider.rb +44 -0
- data/lib/openhab/core/items/proxy.rb +136 -0
- data/lib/openhab/core/items/registry.rb +86 -0
- data/lib/openhab/core/items/rollershutter_item.rb +68 -0
- data/lib/openhab/core/items/semantics/enumerable.rb +177 -0
- data/lib/openhab/core/items/semantics.rb +473 -0
- data/lib/openhab/core/items/state_storage.rb +53 -0
- data/lib/openhab/core/items/string_item.rb +28 -0
- data/lib/openhab/core/items/switch_item.rb +78 -0
- data/lib/openhab/core/items.rb +108 -0
- data/lib/openhab/{dsl → core}/lazy_array.rb +9 -3
- data/lib/openhab/core/profile_factory.rb +132 -0
- data/lib/openhab/core/provider.rb +230 -0
- data/lib/openhab/core/proxy.rb +130 -0
- data/lib/openhab/core/registry.rb +40 -0
- data/lib/openhab/core/rules/module.rb +26 -0
- data/lib/openhab/core/rules/provider.rb +25 -0
- data/lib/openhab/core/rules/registry.rb +76 -0
- data/lib/openhab/core/rules/rule.rb +150 -0
- data/lib/openhab/core/rules.rb +25 -0
- data/lib/openhab/core/script_handling.rb +78 -20
- data/lib/openhab/core/things/channel.rb +48 -0
- data/lib/openhab/core/things/channel_uid.rb +51 -0
- data/lib/openhab/core/things/item_channel_link.rb +33 -0
- data/lib/openhab/core/things/links/provider.rb +78 -0
- data/lib/openhab/core/things/profile_callback.rb +52 -0
- data/lib/openhab/core/things/provider.rb +29 -0
- data/lib/openhab/core/things/proxy.rb +87 -0
- data/lib/openhab/core/things/registry.rb +73 -0
- data/lib/openhab/core/things/thing.rb +194 -0
- data/lib/openhab/core/things.rb +22 -0
- data/lib/openhab/core/timer.rb +148 -0
- data/lib/openhab/{dsl → core}/types/comparable_type.rb +5 -3
- data/lib/openhab/{dsl → core}/types/date_time_type.rb +55 -127
- data/lib/openhab/{dsl → core}/types/decimal_type.rb +50 -48
- data/lib/openhab/{dsl → core}/types/hsb_type.rb +35 -83
- data/lib/openhab/core/types/increase_decrease_type.rb +34 -0
- data/lib/openhab/core/types/next_previous_type.rb +34 -0
- data/lib/openhab/{dsl → core}/types/numeric_type.rb +20 -7
- data/lib/openhab/core/types/on_off_type.rb +46 -0
- data/lib/openhab/core/types/open_closed_type.rb +41 -0
- data/lib/openhab/{dsl → core}/types/percent_type.rb +19 -20
- data/lib/openhab/core/types/play_pause_type.rb +38 -0
- data/lib/openhab/core/types/point_type.rb +117 -0
- data/lib/openhab/core/types/quantity_type.rb +325 -0
- data/lib/openhab/core/types/raw_type.rb +26 -0
- data/lib/openhab/core/types/refresh_type.rb +27 -0
- data/lib/openhab/core/types/rewind_fastforward_type.rb +38 -0
- data/lib/openhab/core/types/stop_move_type.rb +34 -0
- data/lib/openhab/{dsl → core}/types/string_type.rb +17 -28
- data/lib/openhab/{dsl → core}/types/type.rb +42 -40
- data/lib/openhab/core/types/un_def_type.rb +38 -0
- data/lib/openhab/core/types/up_down_type.rb +50 -0
- data/lib/openhab/core/types.rb +82 -0
- data/lib/openhab/{dsl → core}/uid.rb +4 -23
- data/lib/openhab/core/value_cache.rb +188 -0
- data/lib/openhab/core.rb +98 -0
- data/lib/openhab/core_ext/between.rb +32 -0
- data/lib/openhab/core_ext/ephemeris.rb +53 -0
- data/lib/openhab/core_ext/java/class.rb +34 -0
- data/lib/openhab/core_ext/java/duration.rb +142 -0
- data/lib/openhab/core_ext/java/list.rb +436 -0
- data/lib/openhab/core_ext/java/local_date.rb +104 -0
- data/lib/openhab/core_ext/java/local_time.rb +118 -0
- data/lib/openhab/core_ext/java/map.rb +66 -0
- data/lib/openhab/core_ext/java/month.rb +71 -0
- data/lib/openhab/core_ext/java/month_day.rb +119 -0
- data/lib/openhab/core_ext/java/period.rb +103 -0
- data/lib/openhab/core_ext/java/temporal_amount.rb +34 -0
- data/lib/openhab/core_ext/java/time.rb +62 -0
- data/lib/openhab/core_ext/java/unit.rb +15 -0
- data/lib/openhab/core_ext/java/zoned_date_time.rb +213 -0
- data/lib/openhab/core_ext/ruby/array.rb +21 -0
- data/lib/openhab/core_ext/ruby/date.rb +96 -0
- data/lib/openhab/core_ext/ruby/date_time.rb +55 -0
- data/lib/openhab/core_ext/ruby/module.rb +15 -0
- data/lib/openhab/core_ext/ruby/numeric.rb +195 -0
- data/lib/openhab/core_ext/ruby/range.rb +70 -0
- data/lib/openhab/core_ext/ruby/symbol.rb +7 -0
- data/lib/openhab/core_ext/ruby/time.rb +108 -0
- data/lib/openhab/core_ext.rb +18 -0
- data/lib/openhab/dsl/debouncer.rb +259 -0
- data/lib/openhab/dsl/events/watch_event.rb +18 -0
- data/lib/openhab/dsl/events.rb +9 -0
- data/lib/openhab/dsl/gems.rb +1 -1
- data/lib/openhab/dsl/items/builder.rb +578 -0
- data/lib/openhab/dsl/items/ensure.rb +73 -82
- data/lib/openhab/dsl/items/timed_command.rb +214 -159
- data/lib/openhab/dsl/rules/automation_rule.rb +126 -115
- data/lib/openhab/dsl/rules/builder.rb +1935 -0
- data/lib/openhab/dsl/rules/guard.rb +51 -114
- data/lib/openhab/dsl/rules/name_inference.rb +66 -25
- data/lib/openhab/dsl/rules/property.rb +48 -75
- data/lib/openhab/dsl/rules/rule_triggers.rb +22 -27
- data/lib/openhab/dsl/rules/terse.rb +58 -14
- data/lib/openhab/dsl/rules/triggers/changed.rb +48 -94
- data/lib/openhab/dsl/rules/triggers/channel.rb +9 -40
- data/lib/openhab/dsl/rules/triggers/command.rb +14 -63
- data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +34 -69
- data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +6 -14
- data/lib/openhab/dsl/rules/triggers/cron/cron.rb +48 -82
- data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +30 -47
- data/lib/openhab/dsl/rules/triggers/trigger.rb +7 -28
- data/lib/openhab/dsl/rules/triggers/updated.rb +21 -45
- data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +257 -102
- data/lib/openhab/dsl/rules/triggers.rb +12 -0
- data/lib/openhab/dsl/rules.rb +8 -0
- data/lib/openhab/dsl/things/builder.rb +299 -0
- data/lib/openhab/{core → dsl}/thread_local.rb +27 -17
- data/lib/openhab/dsl/timer_manager.rb +204 -0
- data/lib/openhab/dsl/version.rb +9 -0
- data/lib/openhab/dsl.rb +979 -0
- data/lib/openhab/log.rb +355 -0
- data/lib/openhab/osgi.rb +68 -0
- data/lib/openhab/rspec/configuration.rb +56 -0
- data/lib/openhab/rspec/example_group.rb +132 -0
- data/lib/openhab/rspec/helpers.rb +458 -0
- data/lib/openhab/rspec/hooks.rb +113 -0
- data/lib/openhab/rspec/jruby.rb +46 -0
- data/lib/openhab/rspec/karaf.rb +851 -0
- data/lib/openhab/rspec/mocks/bundle_install_support.rb +25 -0
- data/lib/openhab/rspec/mocks/bundle_resolver.rb +30 -0
- data/lib/openhab/rspec/mocks/event_admin.rb +146 -0
- data/lib/openhab/rspec/mocks/instance_method_stasher.rb +22 -0
- data/lib/openhab/rspec/mocks/persistence_service.rb +155 -0
- data/lib/openhab/rspec/mocks/safe_caller.rb +40 -0
- data/lib/openhab/rspec/mocks/space.rb +23 -0
- data/lib/openhab/rspec/mocks/synchronous_executor.rb +63 -0
- data/lib/openhab/rspec/mocks/thing_handler.rb +76 -0
- data/lib/openhab/rspec/mocks/timer.rb +134 -0
- data/lib/openhab/rspec/openhab/core/actions.rb +38 -0
- data/lib/openhab/rspec/openhab/core/items/proxy.rb +15 -0
- data/lib/openhab/rspec/openhab/core/things/proxy.rb +27 -0
- data/lib/openhab/rspec/shell.rb +31 -0
- data/lib/openhab/rspec/suspend_rules.rb +50 -0
- data/lib/openhab/rspec.rb +26 -0
- data/lib/openhab/yard/base_helper.rb +19 -0
- data/lib/openhab/yard/cli/stats.rb +23 -0
- data/lib/openhab/yard/code_objects/group_object.rb +23 -0
- data/lib/openhab/yard/code_objects/java/base.rb +31 -0
- data/lib/openhab/yard/code_objects/java/class_object.rb +11 -0
- data/lib/openhab/yard/code_objects/java/field_object.rb +15 -0
- data/lib/openhab/yard/code_objects/java/interface_object.rb +15 -0
- data/lib/openhab/yard/code_objects/java/package_object.rb +11 -0
- data/lib/openhab/yard/code_objects/java/proxy.rb +23 -0
- data/lib/openhab/yard/coderay.rb +17 -0
- data/lib/openhab/yard/handlers/jruby/base.rb +58 -0
- data/lib/openhab/yard/handlers/jruby/class_handler.rb +18 -0
- data/lib/openhab/yard/handlers/jruby/constant_handler.rb +18 -0
- data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +30 -0
- data/lib/openhab/yard/handlers/jruby/mixin_handler.rb +23 -0
- data/lib/openhab/yard/html_helper.rb +78 -0
- data/lib/openhab/yard/markdown_helper.rb +148 -0
- data/lib/openhab/yard/tags/constant_directive.rb +20 -0
- data/lib/openhab/yard/tags/group_directive.rb +24 -0
- data/lib/openhab/yard/tags/library.rb +3 -0
- data/lib/openhab/yard.rb +38 -0
- metadata +475 -106
- data/lib/openhab/core/item_proxy.rb +0 -29
- data/lib/openhab/core/load_path.rb +0 -19
- data/lib/openhab/core/openhab_setup.rb +0 -29
- data/lib/openhab/core/osgi.rb +0 -58
- data/lib/openhab/core/services.rb +0 -24
- data/lib/openhab/dsl/actions.rb +0 -114
- data/lib/openhab/dsl/between.rb +0 -25
- data/lib/openhab/dsl/channel.rb +0 -43
- data/lib/openhab/dsl/dsl.rb +0 -59
- data/lib/openhab/dsl/group.rb +0 -54
- data/lib/openhab/dsl/imports.rb +0 -21
- data/lib/openhab/dsl/items/color_item.rb +0 -76
- data/lib/openhab/dsl/items/comparable_item.rb +0 -62
- data/lib/openhab/dsl/items/contact_item.rb +0 -41
- data/lib/openhab/dsl/items/date_time_item.rb +0 -65
- data/lib/openhab/dsl/items/dimmer_item.rb +0 -65
- data/lib/openhab/dsl/items/generic_item.rb +0 -229
- data/lib/openhab/dsl/items/group_item.rb +0 -127
- data/lib/openhab/dsl/items/item_equality.rb +0 -59
- data/lib/openhab/dsl/items/item_registry.rb +0 -54
- data/lib/openhab/dsl/items/items.rb +0 -109
- data/lib/openhab/dsl/items/location_item.rb +0 -59
- data/lib/openhab/dsl/items/metadata.rb +0 -326
- data/lib/openhab/dsl/items/number_item.rb +0 -17
- data/lib/openhab/dsl/items/numeric_item.rb +0 -87
- data/lib/openhab/dsl/items/persistence.rb +0 -307
- data/lib/openhab/dsl/items/player_item.rb +0 -58
- data/lib/openhab/dsl/items/rollershutter_item.rb +0 -51
- data/lib/openhab/dsl/items/semantics/enumerable.rb +0 -91
- data/lib/openhab/dsl/items/semantics.rb +0 -227
- data/lib/openhab/dsl/items/string_item.rb +0 -51
- data/lib/openhab/dsl/items/switch_item.rb +0 -70
- data/lib/openhab/dsl/monkey_patch/actions/actions.rb +0 -4
- data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +0 -39
- data/lib/openhab/dsl/monkey_patch/events/events.rb +0 -7
- data/lib/openhab/dsl/monkey_patch/events/item_command.rb +0 -85
- data/lib/openhab/dsl/monkey_patch/events/item_event.rb +0 -28
- data/lib/openhab/dsl/monkey_patch/events/item_state.rb +0 -61
- data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +0 -60
- data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +0 -33
- data/lib/openhab/dsl/monkey_patch/java/java.rb +0 -4
- data/lib/openhab/dsl/monkey_patch/java/local_time.rb +0 -44
- data/lib/openhab/dsl/monkey_patch/java/time_extensions.rb +0 -50
- data/lib/openhab/dsl/monkey_patch/java/zoned_date_time.rb +0 -45
- data/lib/openhab/dsl/monkey_patch/ruby/number.rb +0 -104
- data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -6
- data/lib/openhab/dsl/monkey_patch/ruby/string.rb +0 -47
- data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -61
- data/lib/openhab/dsl/openhab.rb +0 -30
- data/lib/openhab/dsl/persistence.rb +0 -27
- data/lib/openhab/dsl/rules/item_event.rb +0 -19
- data/lib/openhab/dsl/rules/rule.rb +0 -160
- data/lib/openhab/dsl/rules/rule_config.rb +0 -147
- data/lib/openhab/dsl/rules/triggers/generic.rb +0 -31
- data/lib/openhab/dsl/rules/triggers/triggers.rb +0 -11
- data/lib/openhab/dsl/rules/triggers/watch/watch.rb +0 -81
- data/lib/openhab/dsl/states.rb +0 -89
- data/lib/openhab/dsl/things.rb +0 -147
- data/lib/openhab/dsl/time/month_day.rb +0 -180
- data/lib/openhab/dsl/time/time_of_day.rb +0 -235
- data/lib/openhab/dsl/timers/manager.rb +0 -119
- data/lib/openhab/dsl/timers/reentrant_timer.rb +0 -38
- data/lib/openhab/dsl/timers/timer.rb +0 -132
- data/lib/openhab/dsl/timers.rb +0 -77
- data/lib/openhab/dsl/types/increase_decrease_type.rb +0 -23
- data/lib/openhab/dsl/types/next_previous_type.rb +0 -23
- data/lib/openhab/dsl/types/on_off_type.rb +0 -28
- data/lib/openhab/dsl/types/open_closed_type.rb +0 -29
- data/lib/openhab/dsl/types/play_pause_type.rb +0 -27
- data/lib/openhab/dsl/types/point_type.rb +0 -180
- data/lib/openhab/dsl/types/quantity_type.rb +0 -265
- data/lib/openhab/dsl/types/refresh_type.rb +0 -18
- data/lib/openhab/dsl/types/rewind_fastforward_type.rb +0 -33
- data/lib/openhab/dsl/types/stop_move_type.rb +0 -23
- data/lib/openhab/dsl/types/types.rb +0 -83
- data/lib/openhab/dsl/types/un_def_type.rb +0 -22
- data/lib/openhab/dsl/types/up_down_type.rb +0 -32
- data/lib/openhab/dsl/units.rb +0 -45
- data/lib/openhab/log/configuration.rb +0 -21
- data/lib/openhab/log/logger.rb +0 -282
- data/lib/openhab/version.rb +0 -9
- data/lib/openhab.rb +0 -36
@@ -1,28 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'java'
|
4
|
-
require 'openhab/core/thread_local'
|
5
|
-
require 'openhab/log/logger'
|
6
|
-
require 'openhab/dsl/between'
|
7
|
-
|
8
|
-
require_relative 'item_event'
|
9
|
-
|
10
3
|
module OpenHAB
|
11
4
|
module DSL
|
12
|
-
#
|
13
|
-
# Creates and manages OpenHAB Rules
|
14
|
-
#
|
15
5
|
module Rules
|
16
6
|
#
|
17
|
-
#
|
7
|
+
# OpenHAB rules engine object
|
18
8
|
#
|
19
|
-
#
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
9
|
+
# @!visibility private
|
10
|
+
class AutomationRule < org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRule
|
11
|
+
# @!visibility private
|
12
|
+
INPUT_KEY_PATTERN = /^[a-z_]+[a-zA-Z0-9_]*$/.freeze
|
13
|
+
|
14
|
+
class << self
|
15
|
+
#
|
16
|
+
# Caches dynamically generated Struct classes so that they don't have
|
17
|
+
# to be re-generated for every event, blowing the method cache.
|
18
|
+
#
|
19
|
+
# @!visibility private
|
20
|
+
# @return [java.util.Map<Array<Symbol>, Class>]
|
21
|
+
attr_reader :event_structs
|
22
|
+
end
|
23
|
+
@event_structs = java.util.concurrent.ConcurrentHashMap.new
|
26
24
|
|
27
25
|
field_writer :uid
|
28
26
|
|
@@ -32,63 +30,94 @@ module OpenHAB
|
|
32
30
|
# @param [Config] config Rule configuration
|
33
31
|
#
|
34
32
|
# Constructor sets a number of variables, no further decomposition necessary
|
35
|
-
|
36
|
-
|
33
|
+
#
|
34
|
+
def initialize(config)
|
37
35
|
super()
|
38
36
|
set_name(config.name)
|
39
37
|
set_description(config.description)
|
40
|
-
|
38
|
+
tags = DSL::Items::ItemBuilder.normalize_tags(*config.tags)
|
39
|
+
set_tags(tags.to_set)
|
41
40
|
set_triggers(config.triggers)
|
42
41
|
self.uid = config.uid
|
43
42
|
@run_context = config.caller
|
44
43
|
@run_queue = config.run
|
45
44
|
@guard = config.guard
|
46
|
-
|
47
|
-
between = config.between&.then { between(config.between) }
|
48
|
-
@between = between
|
45
|
+
@between = config.between && DSL.between(config.between)
|
49
46
|
@trigger_conditions = config.trigger_conditions
|
47
|
+
@trigger_conditions.each_value do |condition|
|
48
|
+
condition.rule = self if condition.respond_to?(:rule=)
|
49
|
+
end
|
50
50
|
@attachments = config.attachments
|
51
|
+
@thread_locals = ThreadLocal.persist
|
52
|
+
@cleanup_hooks = Set.new
|
53
|
+
@listener = nil
|
54
|
+
debounce_settings = config.debounce_settings || { for: nil }
|
55
|
+
@debouncer = Debouncer.new(**debounce_settings)
|
51
56
|
end
|
52
57
|
|
53
58
|
#
|
54
59
|
# Execute the rule
|
55
60
|
#
|
56
|
-
# @param [Map] mod map provided by
|
57
|
-
# @param [Map] inputs map provided by
|
58
|
-
#
|
61
|
+
# @param [java.util.Map] mod map provided by openHAB rules engine
|
62
|
+
# @param [java.util.Map] inputs map provided by openHAB rules engine containing event and other information
|
59
63
|
#
|
60
64
|
def execute(mod = nil, inputs = nil)
|
61
|
-
|
62
|
-
|
65
|
+
execute!(mod, inputs)
|
66
|
+
end
|
67
|
+
|
68
|
+
# @!visibility private
|
69
|
+
def on_removal(listener)
|
70
|
+
@cleanup_hooks << listener
|
71
|
+
listen_for_removal unless @listener
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
# This method gets called in rspec's SuspendRules as well
|
77
|
+
def execute!(mod, inputs)
|
78
|
+
ThreadLocal.thread_local(**@thread_locals) do
|
63
79
|
logger.trace { "Execute called with mod (#{mod&.to_string}) and inputs (#{inputs.inspect})" }
|
64
|
-
logger.trace { "Event details #{inputs[
|
80
|
+
logger.trace { "Event details #{inputs["event"].inspect}" } if inputs&.key?("event")
|
65
81
|
trigger_conditions(inputs).process(mod: mod, inputs: inputs) do
|
66
|
-
|
82
|
+
event = extract_event(inputs)
|
83
|
+
@debouncer.call { process_queue(create_queue(event), mod, event) }
|
67
84
|
end
|
85
|
+
rescue Exception => e
|
86
|
+
raise if defined?(::RSpec) && ::RSpec.current_example.example_group.propagate_exceptions?
|
87
|
+
|
88
|
+
@run_context.send(:logger).log_exception(e)
|
68
89
|
end
|
69
90
|
end
|
70
91
|
|
71
|
-
#
|
72
|
-
# Cleanup any resources associated with automation rule
|
73
|
-
#
|
74
92
|
def cleanup
|
75
|
-
|
93
|
+
@cleanup_hooks.each(&:cleanup)
|
76
94
|
end
|
77
95
|
|
78
|
-
|
96
|
+
def listen_for_removal
|
97
|
+
@listener ||= org.openhab.core.common.registry.RegistryChangeListener.impl do |method, element|
|
98
|
+
next unless method == :removed
|
99
|
+
|
100
|
+
logger.trace("Rule #{element.inspect} removed from registry")
|
101
|
+
next unless element.uid == uid
|
102
|
+
|
103
|
+
cleanup
|
104
|
+
$rules.remove_registry_change_listener(@listener)
|
105
|
+
end
|
106
|
+
$rules.add_registry_change_listener(@listener)
|
107
|
+
end
|
79
108
|
|
80
109
|
#
|
81
110
|
# Create the run queue based on guards
|
82
111
|
#
|
83
|
-
# @param [Map]
|
112
|
+
# @param [Map] event Event object
|
84
113
|
# @return [Queue] <description>
|
85
114
|
#
|
86
|
-
def create_queue(
|
87
|
-
case check_guards(event:
|
115
|
+
def create_queue(event)
|
116
|
+
case check_guards(event: event)
|
88
117
|
when true
|
89
|
-
@run_queue.dup.grep_v(
|
118
|
+
@run_queue.dup.grep_v(BuilderDSL::Otherwise)
|
90
119
|
when false
|
91
|
-
@run_queue.dup.grep(
|
120
|
+
@run_queue.dup.grep(BuilderDSL::Otherwise)
|
92
121
|
end
|
93
122
|
end
|
94
123
|
|
@@ -101,12 +130,30 @@ module OpenHAB
|
|
101
130
|
# @return [Object] event object
|
102
131
|
#
|
103
132
|
def extract_event(inputs)
|
104
|
-
|
105
|
-
|
106
|
-
event =
|
107
|
-
event
|
133
|
+
attachment = @attachments[trigger_id(inputs)]
|
134
|
+
if inputs&.key?("event")
|
135
|
+
event = inputs["event"]
|
136
|
+
unless event
|
137
|
+
if attachment
|
138
|
+
logger.warn("Unable to attach #{attachment.inspect} to event " \
|
139
|
+
"object for rule #{uid} since the event is nil.")
|
140
|
+
end
|
141
|
+
return nil
|
142
|
+
end
|
143
|
+
|
144
|
+
event.attachment = attachment
|
145
|
+
return event
|
108
146
|
end
|
109
|
-
|
147
|
+
|
148
|
+
inputs = inputs.to_h
|
149
|
+
.select { |key, _value| key != "module" && INPUT_KEY_PATTERN.match?(key) }
|
150
|
+
.transform_keys(&:to_sym)
|
151
|
+
inputs[:attachment] = attachment
|
152
|
+
keys = inputs.keys.sort
|
153
|
+
struct_class = self.class.event_structs.compute_if_absent(keys) do
|
154
|
+
Struct.new(*keys, keyword_init: true)
|
155
|
+
end
|
156
|
+
struct_class.new(**inputs)
|
110
157
|
end
|
111
158
|
|
112
159
|
#
|
@@ -115,85 +162,61 @@ module OpenHAB
|
|
115
162
|
# @return [Hash] Input hash potentially containing trigger id
|
116
163
|
#
|
117
164
|
def trigger_id(inputs)
|
118
|
-
inputs&.dig(
|
165
|
+
inputs&.dig("module")
|
119
166
|
end
|
120
167
|
|
121
168
|
#
|
122
169
|
# Returns trigger conditions from inputs if it exists
|
123
170
|
#
|
124
|
-
# @param [Map] inputs map from
|
171
|
+
# @param [java.util.Map] inputs map from openHAB containing UID
|
125
172
|
#
|
126
173
|
# @return [Array] Array of trigger conditions that match rule UID
|
127
174
|
#
|
128
175
|
def trigger_conditions(inputs)
|
129
|
-
# Parse this to get the trigger UID:
|
130
|
-
# ["72698819-83cb-498a-8e61-5aab8b812623.event", "oldState", "module", \
|
131
|
-
# "72698819-83cb-498a-8e61-5aab8b812623.oldState", "event", "newState",\
|
132
|
-
# "72698819-83cb-498a-8e61-5aab8b812623.newState"]
|
133
176
|
@trigger_conditions[trigger_id(inputs)]
|
134
177
|
end
|
135
178
|
|
136
|
-
# If an attachment exists for the trigger for this event add it to the event object
|
137
|
-
# @param [Object] event Event
|
138
|
-
# @param [Hash] inputs Inputs into event
|
139
|
-
# @return [Object] Event with attachment added
|
140
|
-
#
|
141
|
-
def add_attachment(event, inputs)
|
142
|
-
attachment = @attachments[trigger_id(inputs)]
|
143
|
-
return event unless attachment
|
144
|
-
|
145
|
-
event.attachment = attachment
|
146
|
-
event
|
147
|
-
end
|
148
|
-
|
149
179
|
#
|
150
180
|
# Check if any guards prevent execution
|
151
181
|
#
|
152
|
-
# @param [
|
182
|
+
# @param [Object] event openHAB rule trigger event
|
153
183
|
#
|
154
|
-
# @return [
|
184
|
+
# @return [true,false] True if guards says rule should execute, false otherwise
|
155
185
|
#
|
156
|
-
# rubocop:disable Metrics/MethodLength
|
157
186
|
# Loggging inflates method length
|
158
187
|
def check_guards(event:)
|
159
|
-
if @guard.
|
160
|
-
return true if @between.nil?
|
188
|
+
return true if @guard.nil?
|
161
189
|
|
190
|
+
unless @between.nil?
|
162
191
|
now = Time.now
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
logger.trace("Skipped execution of rule '#{name}' because of guard #{@guard}")
|
192
|
+
unless @between.cover?(now)
|
193
|
+
logger.trace("Skipped execution of rule '#{name}' because the current time #{now} " \
|
194
|
+
"is not between #{@between.begin} and #{@between.end}")
|
195
|
+
return false
|
196
|
+
end
|
169
197
|
end
|
198
|
+
|
199
|
+
return true if @guard.should_run?(event)
|
200
|
+
|
201
|
+
logger.trace("Skipped execution of rule '#{name}' because of guard #{@guard}")
|
170
202
|
false
|
171
|
-
rescue StandardError => e
|
172
|
-
logger.log_exception(e, name)
|
173
203
|
end
|
174
|
-
# rubocop:enable Metrics/MethodLength
|
175
204
|
|
176
205
|
#
|
177
206
|
# Process the run queue
|
178
207
|
#
|
179
208
|
# @param [Array] run_queue array of procs of various types to execute
|
180
|
-
# @param [Map] mod
|
181
|
-
# @param [
|
209
|
+
# @param [java.util/Map] mod openHAB map object describing rule trigger
|
210
|
+
# @param [Object] event openHAB map object describing rule trigger
|
182
211
|
#
|
183
|
-
|
184
|
-
# No logical way to break this method up
|
185
|
-
def process_queue(run_queue, mod, inputs)
|
186
|
-
event = extract_event(inputs)
|
187
|
-
|
212
|
+
def process_queue(run_queue, mod, event)
|
188
213
|
while (task = run_queue.shift)
|
189
|
-
if task.is_a?
|
190
|
-
process_delay_task(
|
214
|
+
if task.is_a?(BuilderDSL::Delay)
|
215
|
+
process_delay_task(event, mod, run_queue, task)
|
191
216
|
else
|
192
217
|
process_task(event, task)
|
193
218
|
end
|
194
219
|
end
|
195
|
-
rescue StandardError => e
|
196
|
-
logger.log_exception(e, name)
|
197
220
|
end
|
198
221
|
|
199
222
|
#
|
@@ -203,11 +226,12 @@ module OpenHAB
|
|
203
226
|
# @param [Task] task task containing otherwise block to execute
|
204
227
|
#
|
205
228
|
def process_task(event, task)
|
206
|
-
thread_local(
|
229
|
+
ThreadLocal.thread_local(**@thread_locals) do
|
207
230
|
case task
|
208
|
-
when
|
209
|
-
when
|
210
|
-
when
|
231
|
+
when BuilderDSL::Run then process_run_task(event, task)
|
232
|
+
when BuilderDSL::Script then process_script_task(task)
|
233
|
+
when BuilderDSL::Trigger then process_trigger_task(event, task)
|
234
|
+
when BuilderDSL::Otherwise then process_otherwise_task(event, task)
|
211
235
|
end
|
212
236
|
end
|
213
237
|
end
|
@@ -227,15 +251,15 @@ module OpenHAB
|
|
227
251
|
#
|
228
252
|
# Process delay task
|
229
253
|
#
|
230
|
-
# @param [Map]
|
254
|
+
# @param [Map] event Rule trigger event
|
231
255
|
# @param [Map] mod Rule modes
|
232
256
|
# @param [Queue] run_queue Queue of tasks for this rule
|
233
257
|
# @param [Delay] task to process
|
234
258
|
#
|
235
259
|
#
|
236
|
-
def process_delay_task(
|
260
|
+
def process_delay_task(event, mod, run_queue, task)
|
237
261
|
remaining_queue = run_queue.slice!(0, run_queue.length)
|
238
|
-
after(task.duration) { process_queue(remaining_queue, mod,
|
262
|
+
DSL.after(task.duration) { process_queue(remaining_queue, mod, event) }
|
239
263
|
end
|
240
264
|
|
241
265
|
#
|
@@ -265,25 +289,13 @@ module OpenHAB
|
|
265
289
|
end
|
266
290
|
|
267
291
|
#
|
268
|
-
#
|
269
|
-
# Convert Semantics classes to their simple name
|
292
|
+
# Process a script task
|
270
293
|
#
|
271
|
-
# @
|
272
|
-
# to_string_set("tag1", Semantics::LivingRoom)
|
294
|
+
# @param [Script] task to execute
|
273
295
|
#
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
#
|
278
|
-
def to_string_set(*tags)
|
279
|
-
tags = tags.flatten.map do |tag|
|
280
|
-
if tag.respond_to?(:java_class) && tag < org.openhab.core.semantics.Tag
|
281
|
-
tag.java_class.simple_name
|
282
|
-
else
|
283
|
-
tag.to_s
|
284
|
-
end
|
285
|
-
end
|
286
|
-
Set.new(tags)
|
296
|
+
def process_script_task(task)
|
297
|
+
logger.trace { "Executing script '#{name}' run block" }
|
298
|
+
@run_context.instance_exec(&task.block)
|
287
299
|
end
|
288
300
|
|
289
301
|
#
|
@@ -317,4 +329,3 @@ module OpenHAB
|
|
317
329
|
end
|
318
330
|
end
|
319
331
|
end
|
320
|
-
# rubocop: enable Metrics/ClassLength
|