openhab-scripting 2.16.2 → 2.19.0
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 +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,345 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'java'
|
|
4
|
-
|
|
5
|
-
module OpenHAB
|
|
6
|
-
module Core
|
|
7
|
-
module DSL
|
|
8
|
-
#
|
|
9
|
-
# Creates and manages OpenHAB Rules
|
|
10
|
-
#
|
|
11
|
-
module Rule
|
|
12
|
-
#
|
|
13
|
-
# JRuby extension to OpenHAB Rule
|
|
14
|
-
#
|
|
15
|
-
# rubocop: disable Metrics/ClassLength
|
|
16
|
-
# Disabled because this class has a single responsibility, there does not appear a logical
|
|
17
|
-
# way of breaking it up into multiple classes
|
|
18
|
-
class AutomationRule < Java::OrgOpenhabCoreAutomationModuleScriptRulesupportSharedSimple::SimpleRule
|
|
19
|
-
include Logging
|
|
20
|
-
include OpenHAB::Core::DSL::Tod
|
|
21
|
-
java_import java.time.ZonedDateTime
|
|
22
|
-
|
|
23
|
-
#
|
|
24
|
-
# Create a new Rule
|
|
25
|
-
#
|
|
26
|
-
# @param [Config] config Rule configuration
|
|
27
|
-
#
|
|
28
|
-
def initialize(config:)
|
|
29
|
-
super()
|
|
30
|
-
set_name(config.name)
|
|
31
|
-
set_description(config.description)
|
|
32
|
-
set_triggers(config.triggers)
|
|
33
|
-
@run_queue = config.run
|
|
34
|
-
@guard = config.guard
|
|
35
|
-
between = config.between&.yield_self { between(config.between) }
|
|
36
|
-
@between = between || OpenHAB::Core::DSL::Tod::ALL_DAY
|
|
37
|
-
# Convert between to correct range or nil if not set
|
|
38
|
-
@trigger_delays = config.trigger_delays
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
#
|
|
42
|
-
# Execute the rule
|
|
43
|
-
#
|
|
44
|
-
# @param [Map] mod map provided by OpenHAB rules engine
|
|
45
|
-
# @param [Map] inputs map provided by OpenHAB rules engine containing event and other information
|
|
46
|
-
#
|
|
47
|
-
#
|
|
48
|
-
def execute(mod = nil, inputs = nil)
|
|
49
|
-
logger.trace { "Execute called with mod (#{mod&.to_string}) and inputs (#{inputs&.pretty_inspect}" }
|
|
50
|
-
logger.trace { "Event details #{inputs['event'].pretty_inspect}" } if inputs&.key?('event')
|
|
51
|
-
if trigger_delay inputs
|
|
52
|
-
trigger_delay = trigger_delay(inputs)
|
|
53
|
-
process_trigger_delay(trigger_delay, mod, inputs)
|
|
54
|
-
else
|
|
55
|
-
# If guards are satisfied execute the run type blocks
|
|
56
|
-
# If they are not satisfied, execute the Othewise blocks
|
|
57
|
-
queue = create_queue(inputs)
|
|
58
|
-
process_queue(queue, mod, inputs)
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
private
|
|
63
|
-
|
|
64
|
-
#
|
|
65
|
-
# Create the run queue based on guards
|
|
66
|
-
#
|
|
67
|
-
# @param [Map] inputs rule inputs
|
|
68
|
-
#
|
|
69
|
-
# @return [Queue] <description>
|
|
70
|
-
#
|
|
71
|
-
def create_queue(inputs)
|
|
72
|
-
case check_guards(event: inputs&.dig('event'))
|
|
73
|
-
when true
|
|
74
|
-
@run_queue.dup
|
|
75
|
-
when false
|
|
76
|
-
@run_queue.dup.grep(RuleConfig::Otherwise)
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
#
|
|
81
|
-
# Returns trigger delay from inputs if it exists
|
|
82
|
-
#
|
|
83
|
-
# @param [Map] inputs map from OpenHAB containing UID
|
|
84
|
-
#
|
|
85
|
-
# @return [Array] Array of trigger delays that match rule UID
|
|
86
|
-
#
|
|
87
|
-
def trigger_delay(inputs)
|
|
88
|
-
# Parse this to get the trigger UID:
|
|
89
|
-
# ["72698819-83cb-498a-8e61-5aab8b812623.event", "oldState", "module", \
|
|
90
|
-
# "72698819-83cb-498a-8e61-5aab8b812623.oldState", "event", "newState",\
|
|
91
|
-
# "72698819-83cb-498a-8e61-5aab8b812623.newState"]
|
|
92
|
-
@trigger_delays[inputs&.keys&.grep(/\.event$/)&.first&.chomp('.event')]
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
#
|
|
96
|
-
# Check if trigger guards prevent rule execution
|
|
97
|
-
#
|
|
98
|
-
# @param [Delay] trigger_delay rules delaying trigger because of
|
|
99
|
-
# @param [Map] inputs OpenHAB map object describing rule trigger
|
|
100
|
-
#
|
|
101
|
-
# @return [Boolean] True if the rule should execute, false if trigger guard prevents execution
|
|
102
|
-
#
|
|
103
|
-
def check_trigger_guards(trigger_delay, inputs)
|
|
104
|
-
old_state = inputs['oldState']
|
|
105
|
-
new_state = inputs['newState']
|
|
106
|
-
if check_from(trigger_delay, old_state)
|
|
107
|
-
return true if check_to(trigger_delay, new_state)
|
|
108
|
-
|
|
109
|
-
logger.trace("Skipped execution of rule '#{name}' because to state #{new_state}"\
|
|
110
|
-
" does not equal specified state(#{trigger_delay.to})")
|
|
111
|
-
else
|
|
112
|
-
logger.trace("Skipped execution of rule '#{name}' because old state #{old_state}"\
|
|
113
|
-
" does not equal specified state(#{trigger_delay.from})")
|
|
114
|
-
end
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
#
|
|
118
|
-
# Check the from state against the trigger delay
|
|
119
|
-
#
|
|
120
|
-
# @param [TriggerDelay] trigger_delay Information about the trigger delay
|
|
121
|
-
# @param [Item State] state from state to check
|
|
122
|
-
#
|
|
123
|
-
# @return [Boolean] true if no from state is defined or defined state equals supplied state
|
|
124
|
-
#
|
|
125
|
-
def check_from(trigger_delay, state)
|
|
126
|
-
trigger_delay.from.nil? || trigger_delay.from == state
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
#
|
|
130
|
-
# Check the to state against the trigger delay
|
|
131
|
-
#
|
|
132
|
-
# @param [TriggerDelay] trigger_delay Information about the trigger delay
|
|
133
|
-
# @param [Item State] state to-state to check
|
|
134
|
-
#
|
|
135
|
-
# @return [Boolean] true if no to state is defined or defined state equals supplied state
|
|
136
|
-
#
|
|
137
|
-
def check_to(trigger_delay, state)
|
|
138
|
-
trigger_delay.to.nil? || trigger_delay.to == state
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
#
|
|
142
|
-
# Process any matching trigger delays
|
|
143
|
-
#
|
|
144
|
-
# @param [Map] mod OpenHAB map object describing rule trigger
|
|
145
|
-
# @param [Map] inputs OpenHAB map object describing rule trigger
|
|
146
|
-
#
|
|
147
|
-
#
|
|
148
|
-
def process_trigger_delay(trigger_delay, mod, inputs)
|
|
149
|
-
if check_trigger_guards(trigger_delay, inputs)
|
|
150
|
-
logger.trace("Trigger Guards Matched for #{trigger_delay}, delaying rule execution")
|
|
151
|
-
# Add timer and attach timer to delay object, and also state being tracked to so timer can be cancelled if
|
|
152
|
-
# state changes
|
|
153
|
-
# Also another timer should not be created if changed to same value again but instead rescheduled
|
|
154
|
-
if trigger_delay.timer_active?
|
|
155
|
-
process_active_timer(inputs, mod, trigger_delay)
|
|
156
|
-
else
|
|
157
|
-
create_trigger_delay_timer(inputs, mod, trigger_delay)
|
|
158
|
-
end
|
|
159
|
-
else
|
|
160
|
-
logger.trace("Trigger Guards did not match for #{trigger_delay}, ignoring trigger.")
|
|
161
|
-
end
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
#
|
|
165
|
-
# Creatas a timer for trigger delays
|
|
166
|
-
#
|
|
167
|
-
# @param [Hash] inputs rule trigger inputs
|
|
168
|
-
# @param [Hash] mod rule trigger mods
|
|
169
|
-
# @param [TriggerDelay] trigger_delay specifications
|
|
170
|
-
#
|
|
171
|
-
#
|
|
172
|
-
def create_trigger_delay_timer(inputs, mod, trigger_delay)
|
|
173
|
-
logger.trace("Creating timer for rule #{name} and trigger delay #{trigger_delay}")
|
|
174
|
-
trigger_delay.timer = after(trigger_delay.duration) do
|
|
175
|
-
logger.trace("Delay Complete for #{trigger_delay}, executing rule")
|
|
176
|
-
trigger_delay.timer = nil
|
|
177
|
-
process_queue(@run_queue.dup, mod, inputs)
|
|
178
|
-
end
|
|
179
|
-
trigger_delay.tracking_to = inputs['newState']
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
#
|
|
183
|
-
# Process an active trigger timer
|
|
184
|
-
#
|
|
185
|
-
# @param [Hash] inputs rule trigger inputs
|
|
186
|
-
# @param [Hash] mod rule trigger mods
|
|
187
|
-
# @param [TriggerDelay] trigger_delay specifications
|
|
188
|
-
#
|
|
189
|
-
#
|
|
190
|
-
def process_active_timer(inputs, mod, trigger_delay)
|
|
191
|
-
state = inputs['newState']
|
|
192
|
-
if state == trigger_delay.tracking_to
|
|
193
|
-
logger.trace("Item changed to #{state} for #{trigger_delay}, rescheduling timer.")
|
|
194
|
-
trigger_delay.timer.reschedule(ZonedDateTime.now.plus(duration))
|
|
195
|
-
else
|
|
196
|
-
logger.trace("Item changed to #{state} for #{trigger_delay}, cancelling timer.")
|
|
197
|
-
trigger_delay.timer.cancel
|
|
198
|
-
# Reprocess trigger delay after cancelling to track new state (if guards matched, etc)
|
|
199
|
-
process_trigger_delay(trigger_delay, mod, inputs)
|
|
200
|
-
end
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
#
|
|
204
|
-
# Check if any guards prevent execution
|
|
205
|
-
#
|
|
206
|
-
# @param [Map] event OpenHAB rule trigger event
|
|
207
|
-
#
|
|
208
|
-
# @return [Boolean] True if guards says rule should execute, false otherwise
|
|
209
|
-
#
|
|
210
|
-
def check_guards(event:)
|
|
211
|
-
if @guard.should_run? event
|
|
212
|
-
now = TimeOfDay.now
|
|
213
|
-
return true if @between.cover? now
|
|
214
|
-
|
|
215
|
-
logger.trace("Skipped execution of rule '#{name}' because the current time #{now} "\
|
|
216
|
-
"is not between #{@between.begin} and #{@between.end}")
|
|
217
|
-
else
|
|
218
|
-
logger.trace("Skipped execution of rule '#{name}' because of guard #{@guard}")
|
|
219
|
-
end
|
|
220
|
-
false
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
#
|
|
224
|
-
# Patch event to decorate event.item with our item wrapper
|
|
225
|
-
#
|
|
226
|
-
# @param [OpenHAB Event] event patch
|
|
227
|
-
#
|
|
228
|
-
def decorate_event_item(event)
|
|
229
|
-
return if event.nil?
|
|
230
|
-
|
|
231
|
-
class << event
|
|
232
|
-
def item
|
|
233
|
-
EntityLookup.lookup_item(item_name)
|
|
234
|
-
end
|
|
235
|
-
end
|
|
236
|
-
end
|
|
237
|
-
|
|
238
|
-
#
|
|
239
|
-
# Process the run queue
|
|
240
|
-
#
|
|
241
|
-
# @param [Array] run_queue array of procs of various types to execute
|
|
242
|
-
# @param [Map] mod OpenHAB map object describing rule trigger
|
|
243
|
-
# @param [Map] inputs OpenHAB map object describing rule trigge
|
|
244
|
-
#
|
|
245
|
-
#
|
|
246
|
-
def process_queue(run_queue, mod, inputs)
|
|
247
|
-
event = inputs&.dig('event')
|
|
248
|
-
|
|
249
|
-
while (task = run_queue.shift)
|
|
250
|
-
case task
|
|
251
|
-
when RuleConfig::Run then process_run_task(event, inputs, task)
|
|
252
|
-
when RuleConfig::Trigger then process_trigger_task(event, task)
|
|
253
|
-
when RuleConfig::Delay then process_delay_task(inputs, mod, run_queue, task)
|
|
254
|
-
when RuleConfig::Otherwise then process_otherwise_task(event, task)
|
|
255
|
-
end
|
|
256
|
-
end
|
|
257
|
-
end
|
|
258
|
-
|
|
259
|
-
#
|
|
260
|
-
# Process an otherwise block
|
|
261
|
-
#
|
|
262
|
-
# @param [OpenHab Event] event that triggered the rule
|
|
263
|
-
# @param [Task] task task containing otherwise block to execute
|
|
264
|
-
#
|
|
265
|
-
#
|
|
266
|
-
def process_otherwise_task(event, task)
|
|
267
|
-
decorate_event_item(event)
|
|
268
|
-
logger.trace { "Executing rule '#{name}' otherwise block with event(#{event})" }
|
|
269
|
-
task.block.call(event)
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
#
|
|
273
|
-
# Process delay task
|
|
274
|
-
#
|
|
275
|
-
# @param [Map] inputs Rule trigger inputs
|
|
276
|
-
# @param [Map] mod Rule modes
|
|
277
|
-
# @param [Queue] run_queue Queue of tasks for this rule
|
|
278
|
-
# @param [Delay] task to process
|
|
279
|
-
#
|
|
280
|
-
#
|
|
281
|
-
def process_delay_task(inputs, mod, run_queue, task)
|
|
282
|
-
remaining_queue = run_queue.slice!(0, run_queue.length)
|
|
283
|
-
after(task.duration) { process_queue(remaining_queue, mod, inputs) }
|
|
284
|
-
end
|
|
285
|
-
|
|
286
|
-
#
|
|
287
|
-
# Process a task that is caused by a group item
|
|
288
|
-
#
|
|
289
|
-
# @param [Map] event Rule event map
|
|
290
|
-
# @param [Trigger] task to execute
|
|
291
|
-
#
|
|
292
|
-
#
|
|
293
|
-
def process_trigger_task(event, task)
|
|
294
|
-
triggering_item = EntityLookup.lookup_item(event&.itemName)
|
|
295
|
-
logger.trace { "Executing rule '#{name}' trigger block with item (#{triggering_item})" }
|
|
296
|
-
task.block.call(triggering_item) if triggering_item
|
|
297
|
-
end
|
|
298
|
-
|
|
299
|
-
#
|
|
300
|
-
# Process a run task
|
|
301
|
-
#
|
|
302
|
-
# @param [OpenHab Event] event information
|
|
303
|
-
# @param [Map] inputs of rule trigger information
|
|
304
|
-
# @param [Run] task to execute
|
|
305
|
-
#
|
|
306
|
-
#
|
|
307
|
-
def process_run_task(event, _inputs, task)
|
|
308
|
-
decorate_event_item(event)
|
|
309
|
-
logger.trace { "Executing rule '#{name}' run block with event(#{event})" }
|
|
310
|
-
task.block.call(event)
|
|
311
|
-
end
|
|
312
|
-
|
|
313
|
-
#
|
|
314
|
-
# Create a new hash in which all elements are converted to strings
|
|
315
|
-
#
|
|
316
|
-
# @param [Map] hash in which all elements should be converted to strings
|
|
317
|
-
#
|
|
318
|
-
# @return [Map] new map with values converted to strings
|
|
319
|
-
#
|
|
320
|
-
def inspect_hash(hash)
|
|
321
|
-
hash.each_with_object({}) do |(key, value), new_hash|
|
|
322
|
-
new_hash[inspect_item(key)] = inspect_item(value)
|
|
323
|
-
end
|
|
324
|
-
end
|
|
325
|
-
|
|
326
|
-
#
|
|
327
|
-
# Convert an individual element into a string based on if it a Ruby or Java object
|
|
328
|
-
#
|
|
329
|
-
# @param [Object] item to convert to a string
|
|
330
|
-
#
|
|
331
|
-
# @return [String] representation of item
|
|
332
|
-
#
|
|
333
|
-
def inspect_item(item)
|
|
334
|
-
if item.respond_to? :to_string
|
|
335
|
-
item.to_string
|
|
336
|
-
elsif item.respond_to? :to_str
|
|
337
|
-
item.to_str
|
|
338
|
-
end
|
|
339
|
-
end
|
|
340
|
-
end
|
|
341
|
-
end
|
|
342
|
-
end
|
|
343
|
-
end
|
|
344
|
-
end
|
|
345
|
-
# rubocop: enable Metrics/ClassLength
|
|
@@ -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
|