openhab-scripting 2.16.1 → 2.18.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 +168 -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/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 +8 -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 +58 -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 -140
- data/lib/openhab/core/dsl/group.rb +0 -93
- 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 -348
- 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,136 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'core/dsl/property'
|
4
|
-
require 'core/log'
|
5
|
-
|
6
|
-
module OpenHAB
|
7
|
-
module Core
|
8
|
-
module DSL
|
9
|
-
module Rule
|
10
|
-
#
|
11
|
-
# Guards for rules
|
12
|
-
#
|
13
|
-
module Guard
|
14
|
-
include DSLProperty
|
15
|
-
|
16
|
-
prop_array(:only_if) do |item|
|
17
|
-
unless item.is_a?(Proc) || item.respond_to?(:truthy?)
|
18
|
-
raise ArgumentError, "Object passed to only_if must respond_to 'truthy?'"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
prop_array(:not_if) do |item|
|
23
|
-
unless item.is_a?(Proc) || item.respond_to?(:truthy?)
|
24
|
-
raise ArgumentError, "Object passed to not_if must respond_to 'truthy?'"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
#
|
29
|
-
# Guard that can prevent execute of a rule if not satisfied
|
30
|
-
#
|
31
|
-
class Guard
|
32
|
-
include Logging
|
33
|
-
|
34
|
-
#
|
35
|
-
# Create a new Guard
|
36
|
-
#
|
37
|
-
# @param [Object] only_if Item or Proc to use as guard
|
38
|
-
# @param [Object] not_if Item or Proc to use as guard
|
39
|
-
#
|
40
|
-
def initialize(only_if: nil, not_if: nil)
|
41
|
-
@only_if = only_if
|
42
|
-
@not_if = not_if
|
43
|
-
end
|
44
|
-
|
45
|
-
#
|
46
|
-
# Convert the guard into a string
|
47
|
-
#
|
48
|
-
# @return [String] describing the only_of and not_if guards
|
49
|
-
#
|
50
|
-
def to_s
|
51
|
-
"only_if: #{@only_if}, not_if: #{@not_if}"
|
52
|
-
end
|
53
|
-
|
54
|
-
#
|
55
|
-
# Checks if a guard should run
|
56
|
-
#
|
57
|
-
# @param [OpenHAB Trigger Event] event OpenHAB Trigger Event
|
58
|
-
#
|
59
|
-
# @return [Boolean] True if guard is satisfied, false otherwise
|
60
|
-
#
|
61
|
-
def should_run?(event)
|
62
|
-
logger.trace("Checking guards #{self}")
|
63
|
-
check(@only_if, check_type: :only_if, event: event) && check(@not_if, check_type: :not_if, event: event)
|
64
|
-
end
|
65
|
-
|
66
|
-
private
|
67
|
-
|
68
|
-
#
|
69
|
-
# Check if guard is satisfied
|
70
|
-
#
|
71
|
-
# @param [Array] conditions to check
|
72
|
-
# @param [Symbol] check_type type of check to perform (:only_if or :not_if)
|
73
|
-
# @param [Event] event OpenHAB event to see if it satisfies the guard
|
74
|
-
#
|
75
|
-
# @return [Boolean] True if guard is satisfied, false otherwise
|
76
|
-
#
|
77
|
-
def check(conditions, check_type:, event:)
|
78
|
-
return true if conditions.nil? || conditions.empty?
|
79
|
-
|
80
|
-
procs, items = conditions.flatten.partition { |condition| condition.is_a? Proc }
|
81
|
-
logger.trace("Procs: #{procs} Items: #{items}")
|
82
|
-
|
83
|
-
items.each { |item| logger.trace("#{item} truthy? #{item.truthy?}") }
|
84
|
-
|
85
|
-
process_check(check_type: check_type, event: event, items: items, procs: procs)
|
86
|
-
end
|
87
|
-
|
88
|
-
#
|
89
|
-
# Execute the guard check
|
90
|
-
#
|
91
|
-
# @param [Symbol] check_type :only_if or :not_if to check
|
92
|
-
# @param [OpenHAB Event] event event to check if meets guard
|
93
|
-
# @param [Array<Item>] items to check if satisfy criteria
|
94
|
-
# @param [Array] procs to check if satisfy criteria
|
95
|
-
#
|
96
|
-
# @return [Boolean] True if criteria are satisfied, false otherwise
|
97
|
-
#
|
98
|
-
def process_check(check_type:, event:, items:, procs:)
|
99
|
-
case check_type
|
100
|
-
when :only_if then process_only_if(event, items, procs)
|
101
|
-
when :not_if then process_not_if(event, items, procs)
|
102
|
-
else raise ArgumentError, "Unexpected check type: #{check_type}"
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
#
|
107
|
-
# Check not_if guard
|
108
|
-
#
|
109
|
-
# @param [OpenHAB Event] event event to check if meets guard
|
110
|
-
# @param [Array<Item>] items to check if satisfy criteria
|
111
|
-
# @param [Array] procs to check if satisfy criteria
|
112
|
-
#
|
113
|
-
# @return [Boolean] True if criteria are satisfied, false otherwise
|
114
|
-
#
|
115
|
-
def process_not_if(event, items, procs)
|
116
|
-
items.none?(&:truthy?) && procs.none? { |proc| proc.call(event) }
|
117
|
-
end
|
118
|
-
|
119
|
-
#
|
120
|
-
# Check only_if guard
|
121
|
-
#
|
122
|
-
# @param [OpenHAB Event] event event to check if meets guard
|
123
|
-
# @param [Array<Item>] items to check if satisfy criteria
|
124
|
-
# @param [Array] procs to check if satisfy criteria
|
125
|
-
#
|
126
|
-
# @return [Boolean] True if criteria are satisfied, false otherwise
|
127
|
-
#
|
128
|
-
def process_only_if(event, items, procs)
|
129
|
-
items.all?(&:truthy?) && procs.all? { |proc| proc.call(event) }
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
@@ -1,117 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'core/dsl/rule/rule_config'
|
4
|
-
require 'core/dsl/rule/automation_rule'
|
5
|
-
require 'core/dsl/rule/guard'
|
6
|
-
|
7
|
-
module OpenHAB
|
8
|
-
module Core
|
9
|
-
#
|
10
|
-
# Contains code to create an OpenHAB DSL
|
11
|
-
#
|
12
|
-
module DSL
|
13
|
-
#
|
14
|
-
# Creates and manages OpenHAB Rules
|
15
|
-
#
|
16
|
-
module Rule
|
17
|
-
#
|
18
|
-
# Create a new rule
|
19
|
-
#
|
20
|
-
# @param [String] rule_name <description>
|
21
|
-
# @yield [] Block executed in context of a RuleConfig
|
22
|
-
#
|
23
|
-
#
|
24
|
-
def rule(rule_name, &block)
|
25
|
-
config = RuleConfig.new(rule_name, block.binding)
|
26
|
-
config.instance_eval(&block)
|
27
|
-
config.guard = Guard::Guard.new(only_if: config.only_if, not_if: config.not_if)
|
28
|
-
logger.trace { config.inspect }
|
29
|
-
process_rule_config(config)
|
30
|
-
end
|
31
|
-
|
32
|
-
#
|
33
|
-
# Create a logger where name includes rule name if name is set
|
34
|
-
#
|
35
|
-
# @return [Logging::Logger] Logger with name that appended with rule name if rule name is set
|
36
|
-
#
|
37
|
-
def logger
|
38
|
-
if name
|
39
|
-
Logging.logger(name.chomp.gsub(/\s+/, '_'))
|
40
|
-
else
|
41
|
-
super
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
#
|
48
|
-
# Process a rule based on the supplied configuration
|
49
|
-
#
|
50
|
-
# @param [RuleConfig] config for rule
|
51
|
-
#
|
52
|
-
#
|
53
|
-
def process_rule_config(config)
|
54
|
-
return unless create_rule?(config)
|
55
|
-
|
56
|
-
rule = AutomationRule.new(config: config)
|
57
|
-
add_rule(rule)
|
58
|
-
rule.execute if config.on_start?
|
59
|
-
end
|
60
|
-
|
61
|
-
#
|
62
|
-
# Should a rule be created based on rule configuration
|
63
|
-
#
|
64
|
-
# @param [RuleConfig] config to check
|
65
|
-
#
|
66
|
-
# @return [Boolean] true if it should be created, false otherwise
|
67
|
-
#
|
68
|
-
def create_rule?(config)
|
69
|
-
if !triggers?(config)
|
70
|
-
logger.warn "Rule '#{config.name}' has no triggers, not creating rule"
|
71
|
-
elsif !execution_blocks?(config)
|
72
|
-
logger.warn "Rule '#{config.name}' has no execution blocks, not creating rule"
|
73
|
-
elsif !config.enabled
|
74
|
-
logger.debug "Rule '#{config.name}' marked as disabled, not creating rule."
|
75
|
-
else
|
76
|
-
return true
|
77
|
-
end
|
78
|
-
false
|
79
|
-
end
|
80
|
-
|
81
|
-
#
|
82
|
-
# Check if the rule has any triggers
|
83
|
-
#
|
84
|
-
# @param [RuleConfig] config to check for triggers
|
85
|
-
#
|
86
|
-
# @return [Boolean] True if rule has triggers, false otherwise
|
87
|
-
#
|
88
|
-
def triggers?(config)
|
89
|
-
config.on_start? || config.triggers.length.positive?
|
90
|
-
end
|
91
|
-
|
92
|
-
#
|
93
|
-
# Check if the rule has any execution blocks
|
94
|
-
#
|
95
|
-
# @param [RuleConfig] config to check for triggers
|
96
|
-
#
|
97
|
-
# @return [Boolean] True if rule has execution blocks, false otherwise
|
98
|
-
#
|
99
|
-
def execution_blocks?(config)
|
100
|
-
(config.run || []).length.positive?
|
101
|
-
end
|
102
|
-
|
103
|
-
#
|
104
|
-
# Add a rule to the automation managed
|
105
|
-
#
|
106
|
-
# @param [Java::OrgOpenhabCoreAutomationModuleScriptRulesupportSharedSimple::SimpleRule] rule to add
|
107
|
-
#
|
108
|
-
#
|
109
|
-
def add_rule(rule)
|
110
|
-
# rubocop: disable Style/GlobalVars
|
111
|
-
$scriptExtension.get('automationManager').addRule(rule)
|
112
|
-
# rubocop: enable Style/GlobalVars
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
@@ -1,153 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
require 'pp'
|
5
|
-
require 'core/dsl/property'
|
6
|
-
require 'core/dsl/rule/triggers/cron'
|
7
|
-
require 'core/dsl/rule/triggers/changed'
|
8
|
-
require 'core/dsl/rule/triggers/channel'
|
9
|
-
require 'core/dsl/rule/triggers/command'
|
10
|
-
require 'core/dsl/rule/triggers/updated'
|
11
|
-
require 'core/dsl/rule/guard'
|
12
|
-
require 'core/dsl/entities'
|
13
|
-
require 'core/dsl/time_of_day'
|
14
|
-
require 'core/dsl'
|
15
|
-
require 'core/dsl/timers'
|
16
|
-
|
17
|
-
module OpenHAB
|
18
|
-
module Core
|
19
|
-
module DSL
|
20
|
-
#
|
21
|
-
# Creates and manages OpenHAB Rules
|
22
|
-
#
|
23
|
-
module Rule
|
24
|
-
#
|
25
|
-
# Rule configuration for OpenHAB Rules engine
|
26
|
-
#
|
27
|
-
class RuleConfig
|
28
|
-
include EntityLookup
|
29
|
-
include OpenHAB::Core::DSL::Rule::Triggers
|
30
|
-
include Guard
|
31
|
-
include DSLProperty
|
32
|
-
include Logging
|
33
|
-
extend OpenHAB::Core::DSL
|
34
|
-
|
35
|
-
java_import org.openhab.core.library.items.SwitchItem
|
36
|
-
|
37
|
-
# @return [Array] Of triggers
|
38
|
-
attr_reader :triggers
|
39
|
-
|
40
|
-
# @return [Array] Of trigger delays
|
41
|
-
attr_reader :trigger_delays
|
42
|
-
|
43
|
-
# @return [Array] Of trigger guards
|
44
|
-
attr_accessor :guard
|
45
|
-
|
46
|
-
#
|
47
|
-
# Struct holding a run block
|
48
|
-
#
|
49
|
-
Run = Struct.new(:block)
|
50
|
-
|
51
|
-
#
|
52
|
-
# Struct holding a Triggered block
|
53
|
-
#
|
54
|
-
Trigger = Struct.new(:block)
|
55
|
-
|
56
|
-
#
|
57
|
-
# Struct holding an otherwise block
|
58
|
-
#
|
59
|
-
Otherwise = Struct.new(:block)
|
60
|
-
|
61
|
-
#
|
62
|
-
# Struct holding rule delays
|
63
|
-
#
|
64
|
-
Delay = Struct.new(:duration)
|
65
|
-
|
66
|
-
prop_array :run, array_name: :run_queue, wrapper: Run
|
67
|
-
prop_array :triggered, array_name: :run_queue, wrapper: Trigger
|
68
|
-
prop_array :delay, array_name: :run_queue, wrapper: Delay
|
69
|
-
prop_array :otherwise, array_name: :run_queue, wrapper: Otherwise
|
70
|
-
|
71
|
-
prop :name
|
72
|
-
prop :description
|
73
|
-
prop :enabled
|
74
|
-
prop :between
|
75
|
-
|
76
|
-
#
|
77
|
-
# Create a new RuleConfig
|
78
|
-
#
|
79
|
-
# @param [Object] caller_binding The object initializing this configuration.
|
80
|
-
# Used to execute within the object's context
|
81
|
-
#
|
82
|
-
def initialize(rule_name, caller_binding)
|
83
|
-
@triggers = []
|
84
|
-
@trigger_delays = {}
|
85
|
-
@caller = caller_binding.eval 'self'
|
86
|
-
enabled(true)
|
87
|
-
on_start(false)
|
88
|
-
name(rule_name)
|
89
|
-
end
|
90
|
-
|
91
|
-
#
|
92
|
-
# Start this rule on system startup
|
93
|
-
#
|
94
|
-
# @param [Boolean] run_on_start Run this rule on start, defaults to True
|
95
|
-
#
|
96
|
-
#
|
97
|
-
# rubocop: disable Style/OptionalBooleanParameter
|
98
|
-
# Disabled cop due to use in a DSL
|
99
|
-
def on_start(run_on_start = true)
|
100
|
-
@on_start = run_on_start
|
101
|
-
end
|
102
|
-
# rubocop: enable Style/OptionalBooleanParameter
|
103
|
-
|
104
|
-
#
|
105
|
-
# Checks if this rule should run on start
|
106
|
-
#
|
107
|
-
# @return [Boolean] True if rule should run on start, false otherwise.
|
108
|
-
#
|
109
|
-
def on_start?
|
110
|
-
@on_start
|
111
|
-
end
|
112
|
-
|
113
|
-
#
|
114
|
-
# Run the supplied block inside the object instance of the object that created the rule config
|
115
|
-
#
|
116
|
-
# @yield [] Block executed in context of the object creating the rule config
|
117
|
-
#
|
118
|
-
#
|
119
|
-
def my(&block)
|
120
|
-
@caller.instance_eval(&block)
|
121
|
-
end
|
122
|
-
|
123
|
-
#
|
124
|
-
# Create a logger where name includes rule name if name is set
|
125
|
-
#
|
126
|
-
# @return [Logging::Logger] Logger with name that appended with rule name if rule name is set
|
127
|
-
#
|
128
|
-
def logger
|
129
|
-
if name
|
130
|
-
Logging.logger(name.chomp.gsub(/\s+/, '_'))
|
131
|
-
else
|
132
|
-
super
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
#
|
137
|
-
# Inspect the config object
|
138
|
-
#
|
139
|
-
# @return [String] details of the config object
|
140
|
-
#
|
141
|
-
def inspect
|
142
|
-
"Name: (#{name}) " \
|
143
|
-
"Triggers: (#{triggers}) " \
|
144
|
-
"Run blocks: (#{run}) " \
|
145
|
-
"on_start: (#{on_start?}) " \
|
146
|
-
"Trigger Waits: #{trigger_delays} " \
|
147
|
-
"Trigger UIDs: #{triggers.map(&:id).join(', ')}"
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
@@ -1,145 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'core/log'
|
4
|
-
|
5
|
-
module OpenHAB
|
6
|
-
module Core
|
7
|
-
module DSL
|
8
|
-
module Rule
|
9
|
-
#
|
10
|
-
# Module holds rule triggers
|
11
|
-
#
|
12
|
-
module Triggers
|
13
|
-
include Logging
|
14
|
-
|
15
|
-
#
|
16
|
-
# Struct capturing data necessary for a conditional trigger
|
17
|
-
#
|
18
|
-
TriggerDelay = Struct.new(:to, :from, :duration, :timer, :tracking_to, keyword_init: true) do
|
19
|
-
def timer_active?
|
20
|
-
timer&.is_active
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
#
|
25
|
-
# Creates a trigger item, group and thing changed
|
26
|
-
#
|
27
|
-
# @param [Object] items array of objects to create trigger for
|
28
|
-
# @param [to] to state for object to change for
|
29
|
-
# @param [from] from <description>
|
30
|
-
# @param [OpenHAB::Core::Duration] for Duration to delay trigger until to state is met
|
31
|
-
#
|
32
|
-
# @return [Trigger] OpenHAB trigger
|
33
|
-
#
|
34
|
-
def changed(*items, to: nil, from: nil, for: nil)
|
35
|
-
items.flatten.each do |item|
|
36
|
-
logger.trace("Creating changed trigger for entity(#{item}), to(#{to}), from(#{from})")
|
37
|
-
# for is a reserved word in ruby, so use local_variable_get :for
|
38
|
-
if (wait_duration = binding.local_variable_get(:for))
|
39
|
-
changed_wait(item, to: to, from: from, duration: wait_duration)
|
40
|
-
else
|
41
|
-
# Place in array and flatten to support multiple to elements or single or nil
|
42
|
-
[to].flatten.each do |to_state|
|
43
|
-
create_changed_trigger(item, from, to_state)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
#
|
52
|
-
# Create a TriggerDelay for for an item or group that is changed for a specific duration
|
53
|
-
#
|
54
|
-
# @param [Object] item to create trigger delay for
|
55
|
-
# @param [OpenHAB::Core::Duration] duration to delay trigger for until condition is met
|
56
|
-
# @param [Item State] to OpenHAB Item State item or group needs to change to
|
57
|
-
# @param [Item State] from OpenHAB Item State item or group needs to be coming from
|
58
|
-
#
|
59
|
-
# @return [Array] Array of current TriggerDelay objects
|
60
|
-
#
|
61
|
-
def changed_wait(item, duration:, to: nil, from: nil)
|
62
|
-
# If GroupItems specified, use the group state trigger instead
|
63
|
-
if item.is_a? GroupItems
|
64
|
-
config = { 'groupName' => item.group.name }
|
65
|
-
trigger = Trigger::GROUP_STATE_CHANGE
|
66
|
-
else
|
67
|
-
config = { 'itemName' => item.name }
|
68
|
-
trigger = Trigger::ITEM_STATE_CHANGE
|
69
|
-
end
|
70
|
-
logger.trace("Creating Changed Wait Change Trigger for #{config}")
|
71
|
-
trigger = append_trigger(trigger, config)
|
72
|
-
@trigger_delays[trigger.id] = TriggerDelay.new(to: to, from: from, duration: duration)
|
73
|
-
end
|
74
|
-
|
75
|
-
#
|
76
|
-
# Create a changed trigger
|
77
|
-
#
|
78
|
-
# @param [Object] item to create changed trigger on
|
79
|
-
# @param [String] from state to restrict trigger to
|
80
|
-
# @param [String] to state restrict trigger to
|
81
|
-
#
|
82
|
-
#
|
83
|
-
def create_changed_trigger(item, from, to)
|
84
|
-
trigger, config = case item
|
85
|
-
when GroupItems then create_group_changed_trigger(item, from, to)
|
86
|
-
when Thing then create_thing_changed_trigger(item, from, to)
|
87
|
-
else create_item_changed_trigger(item, from, to)
|
88
|
-
end
|
89
|
-
append_trigger(trigger, config)
|
90
|
-
end
|
91
|
-
|
92
|
-
#
|
93
|
-
# Create a changed trigger for a thing
|
94
|
-
#
|
95
|
-
# @param [Thing] thing to detected changed states on
|
96
|
-
# @param [String] from state to restrict trigger to
|
97
|
-
# @param [String] to state to restrict trigger to
|
98
|
-
#
|
99
|
-
# @return [Array<Hash,String>] first element is a String specifying trigger type
|
100
|
-
# second element is a Hash configuring trigger
|
101
|
-
#
|
102
|
-
def create_thing_changed_trigger(thing, from, to)
|
103
|
-
trigger_for_thing(thing, Trigger::THING_CHANGE, to, from)
|
104
|
-
end
|
105
|
-
|
106
|
-
#
|
107
|
-
# Create a changed trigger for an item
|
108
|
-
#
|
109
|
-
# @param [Item] item to detected changed states on
|
110
|
-
# @param [String] from state to restrict trigger to
|
111
|
-
# @param [String] to to restrict trigger to
|
112
|
-
#
|
113
|
-
# @return [Array<Hash,String>] first element is a String specifying trigger type
|
114
|
-
# second element is a Hash configuring trigger
|
115
|
-
#
|
116
|
-
def create_item_changed_trigger(item, from, to)
|
117
|
-
config = { 'itemName' => item.name }
|
118
|
-
config['state'] = to.to_s if to
|
119
|
-
config['previousState'] = from.to_s if from
|
120
|
-
trigger = Trigger::ITEM_STATE_CHANGE
|
121
|
-
[trigger, config]
|
122
|
-
end
|
123
|
-
|
124
|
-
#
|
125
|
-
# Create a changed trigger for group items
|
126
|
-
#
|
127
|
-
# @param [Group] group to detected changed states on
|
128
|
-
# @param [String] from state to restrict trigger to
|
129
|
-
# @param [String] to to restrict trigger to
|
130
|
-
#
|
131
|
-
# @return [Array<Hash,String>] first element is a String specifying trigger type
|
132
|
-
# second element is a Hash configuring trigger
|
133
|
-
#
|
134
|
-
def create_group_changed_trigger(group, from, to)
|
135
|
-
config = { 'groupName' => group.group.name }
|
136
|
-
config['state'] = to.to_s if to
|
137
|
-
config['previousState'] = from.to_s if from
|
138
|
-
trigger = Trigger::GROUP_STATE_CHANGE
|
139
|
-
[trigger, config]
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|