openhab-jrubyscripting 5.0.0.rc1 → 5.0.0.rc3
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/entity_lookup.rb +1 -12
- data/lib/openhab/core/items/generic_item.rb +15 -7
- data/lib/openhab/core/items/metadata/hash.rb +81 -39
- data/lib/openhab/core/items/metadata/namespace_hash.rb +17 -19
- data/lib/openhab/core/items/metadata/provider.rb +48 -0
- data/lib/openhab/core/items/persistence.rb +2 -0
- data/lib/openhab/core/items/provider.rb +40 -0
- data/lib/openhab/core/items/proxy.rb +10 -0
- data/lib/openhab/core/items/registry.rb +16 -7
- data/lib/openhab/core/items/semantics/enumerable.rb +6 -4
- data/lib/openhab/core/items/state_storage.rb +3 -3
- data/lib/openhab/core/profile_factory.rb +3 -1
- data/lib/openhab/core/provider.rb +223 -0
- data/lib/openhab/core/registry.rb +30 -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 +50 -0
- data/lib/openhab/core/things/links/provider.rb +40 -0
- data/lib/openhab/core/things/provider.rb +25 -0
- data/lib/openhab/core/things/proxy.rb +10 -0
- data/lib/openhab/core/things/registry.rb +25 -2
- data/lib/openhab/core/timer.rb +17 -7
- data/lib/openhab/core/types/quantity_type.rb +5 -2
- data/lib/openhab/core/types.rb +1 -1
- data/lib/openhab/core.rb +3 -30
- data/lib/openhab/core_ext/java/class.rb +34 -0
- data/lib/openhab/core_ext/java/list.rb +436 -0
- data/lib/openhab/core_ext/java/local_time.rb +2 -1
- data/lib/openhab/core_ext/java/map.rb +66 -0
- data/lib/openhab/core_ext/java/month.rb +2 -1
- data/lib/openhab/core_ext/java/zoned_date_time.rb +1 -2
- data/lib/openhab/core_ext/ruby/date.rb +2 -0
- data/lib/openhab/core_ext/ruby/date_time.rb +53 -0
- data/lib/openhab/core_ext/ruby/time.rb +88 -86
- data/lib/openhab/dsl/events/watch_event.rb +1 -1
- data/lib/openhab/dsl/items/builder.rb +38 -100
- data/lib/openhab/dsl/items/ensure.rb +6 -2
- data/lib/openhab/dsl/items/timed_command.rb +10 -11
- data/lib/openhab/dsl/rules/automation_rule.rb +36 -13
- data/lib/openhab/dsl/rules/builder.rb +126 -8
- data/lib/openhab/dsl/rules/name_inference.rb +0 -5
- data/lib/openhab/dsl/rules/terse.rb +1 -2
- data/lib/openhab/dsl/rules/triggers/changed.rb +7 -4
- data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +17 -53
- data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +0 -3
- data/lib/openhab/dsl/rules/triggers/cron/cron.rb +1 -1
- data/lib/openhab/dsl/rules/triggers/trigger.rb +1 -1
- data/lib/openhab/dsl/rules/triggers/updated.rb +7 -3
- data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +1 -1
- data/lib/openhab/dsl/rules.rb +0 -21
- data/lib/openhab/dsl/script_handling.rb +0 -49
- data/lib/openhab/dsl/things/builder.rb +8 -31
- data/lib/openhab/dsl/thread_local.rb +3 -2
- data/lib/openhab/dsl/timer_manager.rb +16 -8
- data/lib/openhab/dsl/version.rb +1 -1
- data/lib/openhab/dsl.rb +137 -120
- data/lib/openhab/log.rb +3 -3
- data/lib/openhab/rspec/example_group.rb +42 -0
- data/lib/openhab/rspec/helpers.rb +33 -27
- data/lib/openhab/rspec/hooks.rb +17 -23
- data/lib/openhab/rspec/karaf.rb +45 -27
- data/lib/openhab/rspec/mocks/synchronous_executor.rb +11 -4
- data/lib/openhab/rspec/mocks/timer.rb +7 -1
- data/lib/openhab/rspec/suspend_rules.rb +4 -2
- metadata +30 -3
- data/lib/openhab/rspec/mocks/metadata_provider.rb +0 -75
@@ -0,0 +1,223 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "singleton"
|
4
|
+
|
5
|
+
module OpenHAB
|
6
|
+
module Core
|
7
|
+
# @interface
|
8
|
+
java_import org.openhab.core.common.registry.ManagedProvider
|
9
|
+
|
10
|
+
# @!visibility private
|
11
|
+
module ManagedProvider
|
12
|
+
# Maps actual element types to the symbol used in Thread.local[:openhab_providers]
|
13
|
+
TYPE_TO_PROVIDER_TYPE = {
|
14
|
+
org.openhab.core.items.Item.java_class => :items,
|
15
|
+
org.openhab.core.items.Metadata.java_class => :metadata,
|
16
|
+
org.openhab.core.automation.Rule.java_class => :rules,
|
17
|
+
org.openhab.core.thing.Thing.java_class => :things,
|
18
|
+
org.openhab.core.thing.link.ItemChannelLink.java_class => :links
|
19
|
+
}.freeze
|
20
|
+
private_constant :TYPE_TO_PROVIDER_TYPE
|
21
|
+
|
22
|
+
# @return [Symbol, nil]
|
23
|
+
def type
|
24
|
+
java_class.generic_ancestors.each do |klass|
|
25
|
+
next unless klass.respond_to?(:raw_type)
|
26
|
+
next unless klass.raw_type == org.openhab.core.common.registry.Provider.java_class
|
27
|
+
|
28
|
+
type_arg = klass.actual_type_arguments.first
|
29
|
+
next unless type_arg.is_a?(java.lang.Class)
|
30
|
+
next unless klass.actual_type_arguments.first.is_a?(java.lang.Class)
|
31
|
+
|
32
|
+
return TYPE_TO_PROVIDER_TYPE[type_arg]
|
33
|
+
end
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# @abstract
|
39
|
+
class Provider < org.openhab.core.common.registry.AbstractProvider
|
40
|
+
include org.openhab.core.common.registry.ManagedProvider
|
41
|
+
include Enumerable
|
42
|
+
include Singleton
|
43
|
+
public_class_method :new
|
44
|
+
|
45
|
+
# Known supported provider types
|
46
|
+
# @return [Array<Symbol>]
|
47
|
+
KNOWN_TYPES = %i[items metadata things links].freeze
|
48
|
+
|
49
|
+
class << self
|
50
|
+
#
|
51
|
+
# Determines the current provider that should be used to create elements belonging to this registry.
|
52
|
+
#
|
53
|
+
# @param [org.openhab.core.common.registry.Provider, Proc, :persistent, :transient, nil] preferred_provider
|
54
|
+
# An optional preferred provider to use. Can be one of several types:
|
55
|
+
# * An explicit instance of {org.openhab.core.common.registry.ManagedProvider ManagedProvider}
|
56
|
+
# * A Proc, which can calculate the preferred provider based on whatever conditions it wants,
|
57
|
+
# and then is further processed as this parameter.
|
58
|
+
# * `:persistent`, meaning the default {org.openhab.core.common.registry.ManagedProvider ManagedProvider}
|
59
|
+
# for this registry. Managed providers persist their objects to JSON, and will survive after the
|
60
|
+
# Ruby script is unloaded. This is where objects you configure with MainUI are stored. You should
|
61
|
+
# use this provider when you're creating something in response to a one-time event.
|
62
|
+
# * `:transient`, meaning a {org.openhab.core.common.registry.ManagedProvider ManagedProvider} that
|
63
|
+
# will remove all of its contents when the Ruby script is unloaded. You should use this if you're
|
64
|
+
# generating objects dynamically, either based on some sort of other configuration, or simply
|
65
|
+
# hard coded and you're using Ruby as a more expressive way to define things than a `.items` or
|
66
|
+
# `.things` file. If you _don't_ use this provider for something such as metadata, then you
|
67
|
+
# may have issues such as metadata still showing up even though you're no longer creating items
|
68
|
+
# with it anymore.
|
69
|
+
# * `nil`, meaning to fall back to the current thread setting. See {OpenHAB::DSL.provider}.
|
70
|
+
# If there is no thread setting (or the thread setting was Proc that returned `nil`),
|
71
|
+
# it defaults to `:transient`.
|
72
|
+
# @return [org.openhab.core.common.registry.Provider]
|
73
|
+
#
|
74
|
+
def current(preferred_provider = nil, element = nil)
|
75
|
+
preferred_provider ||= Thread.current[:openhab_providers]&.[](type)
|
76
|
+
if preferred_provider.is_a?(Proc)
|
77
|
+
preferred_provider = if preferred_provider.arity.zero? || element.nil?
|
78
|
+
preferred_provider.call
|
79
|
+
else
|
80
|
+
preferred_provider.call(element)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
case preferred_provider
|
85
|
+
when nil, :transient
|
86
|
+
instance
|
87
|
+
when :persistent
|
88
|
+
registry.managed_provider.get
|
89
|
+
when org.openhab.core.common.registry.ManagedProvider
|
90
|
+
preferred_provider
|
91
|
+
else
|
92
|
+
raise ArgumentError, "#{preferred_provider.inspect} is not a ManagedProvider"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# @abstract
|
97
|
+
# @!attribute [r] registry
|
98
|
+
#
|
99
|
+
# The registry that this provider provides elements for.
|
100
|
+
#
|
101
|
+
# @return [org.openhab.core.common.registry.Registry]
|
102
|
+
#
|
103
|
+
def registry
|
104
|
+
raise NotImplementedError
|
105
|
+
end
|
106
|
+
|
107
|
+
#
|
108
|
+
# Creates a new instance of a provider, registers it, sets it as the
|
109
|
+
# default for the thread, calls the block, and then unregisters it.
|
110
|
+
#
|
111
|
+
# @param [true, false] thread_provider Set this new provider as the default for the thread
|
112
|
+
# @yieldparam [Provider] provider The provider
|
113
|
+
# @return [Object] The result of the block
|
114
|
+
#
|
115
|
+
# @!visibility private
|
116
|
+
def new(thread_provider: true)
|
117
|
+
unless @singleton__instance__.nil? || block_given?
|
118
|
+
raise NoMethodError,
|
119
|
+
"private method `new' called for #{self}:Class"
|
120
|
+
end
|
121
|
+
|
122
|
+
r = provider = super()
|
123
|
+
if block_given?
|
124
|
+
if thread_provider
|
125
|
+
DSL.provider(provider) do
|
126
|
+
r = yield provider
|
127
|
+
end
|
128
|
+
else
|
129
|
+
r = yield provider
|
130
|
+
end
|
131
|
+
provider.unregister
|
132
|
+
end
|
133
|
+
r
|
134
|
+
end
|
135
|
+
|
136
|
+
# @!attribute [r] type
|
137
|
+
# @!visibility private
|
138
|
+
# @return [Symbol]
|
139
|
+
#
|
140
|
+
def type
|
141
|
+
name.split("::")[-2].downcase.to_sym
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# @!visibility private
|
146
|
+
def each(&block)
|
147
|
+
@elements.each_value(&block)
|
148
|
+
end
|
149
|
+
|
150
|
+
# @return [String]
|
151
|
+
def inspect
|
152
|
+
"#<#{self.class.name}:#{object_id}>"
|
153
|
+
end
|
154
|
+
|
155
|
+
# @!visibility private
|
156
|
+
def add(element)
|
157
|
+
@elements[element.uid] = element
|
158
|
+
notify_listeners_about_added_element(element)
|
159
|
+
element
|
160
|
+
end
|
161
|
+
|
162
|
+
#
|
163
|
+
# Get an element from this provider
|
164
|
+
#
|
165
|
+
# @param [Object] key The proper key type for the elements in this provider.
|
166
|
+
# @return [Object]
|
167
|
+
#
|
168
|
+
def [](key)
|
169
|
+
@elements[key]
|
170
|
+
end
|
171
|
+
alias_method :get, :[]
|
172
|
+
|
173
|
+
#
|
174
|
+
# Get all elements in this provider
|
175
|
+
#
|
176
|
+
# @return [Array<Object>]
|
177
|
+
#
|
178
|
+
def all
|
179
|
+
@elements.values
|
180
|
+
end
|
181
|
+
alias_method :getAll, :all
|
182
|
+
|
183
|
+
#
|
184
|
+
# Remove an element from this provider
|
185
|
+
#
|
186
|
+
# @return [Object, nil] the removed element
|
187
|
+
#
|
188
|
+
# @!visibility private
|
189
|
+
#
|
190
|
+
def remove(key)
|
191
|
+
@elements.delete(key)&.tap do |element|
|
192
|
+
notify_listeners_about_removed_element(element)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
# @return [Object, nil] the previous version of the element
|
197
|
+
# @!visibility private
|
198
|
+
#
|
199
|
+
def update(element)
|
200
|
+
old_element = @elements[element.uid]
|
201
|
+
if old_element
|
202
|
+
@elements[element.uid] = element
|
203
|
+
notify_listeners_about_updated_element(old_element, element)
|
204
|
+
end
|
205
|
+
old_element
|
206
|
+
end
|
207
|
+
|
208
|
+
# @!visibility private
|
209
|
+
def unregister
|
210
|
+
self.class.registry.remove_provider(self)
|
211
|
+
end
|
212
|
+
|
213
|
+
private
|
214
|
+
|
215
|
+
def initialize
|
216
|
+
super
|
217
|
+
@elements = {}
|
218
|
+
self.class.registry.add_provider(self)
|
219
|
+
ScriptHandling.script_unloaded { unregister }
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenHAB
|
4
|
+
module Core
|
5
|
+
Registry = org.openhab.core.common.registry.AbstractRegistry
|
6
|
+
|
7
|
+
Registry.field_reader :elementToProvider, :elementReadLock, :identifierToElement
|
8
|
+
|
9
|
+
# @abstract
|
10
|
+
#
|
11
|
+
# The base class for all registries in openHAB.
|
12
|
+
#
|
13
|
+
class Registry
|
14
|
+
#
|
15
|
+
# Determines which provider an element is associated with.
|
16
|
+
#
|
17
|
+
# @param [Object] key
|
18
|
+
# @return [org.openhab.core.common.registry.Provider]
|
19
|
+
#
|
20
|
+
def provider_for(key)
|
21
|
+
elementReadLock.lock
|
22
|
+
return nil unless (element = identifierToElement[key])
|
23
|
+
|
24
|
+
elementToProvider[element]
|
25
|
+
ensure
|
26
|
+
elementReadLock.unlock
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenHAB
|
4
|
+
module Core
|
5
|
+
module Rules
|
6
|
+
#
|
7
|
+
# Provides rules created in Ruby to openHAB
|
8
|
+
#
|
9
|
+
class Provider < Core::Provider
|
10
|
+
include org.openhab.core.automation.RuleProvider
|
11
|
+
|
12
|
+
class << self
|
13
|
+
#
|
14
|
+
# The Rule registry
|
15
|
+
#
|
16
|
+
# @return [org.openhab.core.automation.RuleRegistry]
|
17
|
+
#
|
18
|
+
def registry
|
19
|
+
$rules
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "singleton"
|
4
|
+
|
5
|
+
module OpenHAB
|
6
|
+
module Core
|
7
|
+
module Rules
|
8
|
+
#
|
9
|
+
# Provides access to all OpenHAB {Rule rules}, and acts like an array.
|
10
|
+
#
|
11
|
+
class Registry
|
12
|
+
include LazyArray
|
13
|
+
include Singleton
|
14
|
+
|
15
|
+
#
|
16
|
+
# Gets a specific Rule
|
17
|
+
#
|
18
|
+
# @param [String] uid Rule UID
|
19
|
+
# @return [Rule, nil]
|
20
|
+
#
|
21
|
+
def [](uid)
|
22
|
+
$rules.get(uid)
|
23
|
+
end
|
24
|
+
alias_method :include?, :[]
|
25
|
+
alias_method :key?, :[]
|
26
|
+
# @deprecated
|
27
|
+
alias_method :has_key?, :[]
|
28
|
+
|
29
|
+
#
|
30
|
+
# Explicit conversion to array
|
31
|
+
#
|
32
|
+
# @return [Array<Rule>]
|
33
|
+
#
|
34
|
+
def to_a
|
35
|
+
$rules.all.to_a
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# Enter the Rule Builder DSL.
|
40
|
+
# @param (see Core::Provider.current)
|
41
|
+
# @yield Block executed in the context of a {DSL::Rules::Builder}.
|
42
|
+
# @return [Object] The result of the block.
|
43
|
+
#
|
44
|
+
def build(preferred_provider = nil, &block)
|
45
|
+
DSL::Rules::Builder.new(preferred_provider).instance_eval(&block)
|
46
|
+
end
|
47
|
+
|
48
|
+
#
|
49
|
+
# Remove a Rule.
|
50
|
+
#
|
51
|
+
# The rule must be a managed thing (typically created by Ruby or in the UI).
|
52
|
+
#
|
53
|
+
# @param [String, Rule] rule_uid
|
54
|
+
# @return [Rule, nil] The removed rule, if found.
|
55
|
+
#
|
56
|
+
# @example
|
57
|
+
# my_rule = rule do
|
58
|
+
# every :day
|
59
|
+
# run { nil }
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# rules.remove(my_rule)
|
63
|
+
#
|
64
|
+
def remove(rule_uid)
|
65
|
+
rule_uid = rule_uid.uid if rule_uid.is_a?(Rule)
|
66
|
+
provider = Provider.registry.provider_for(rule_uid)
|
67
|
+
unless provider.is_a?(org.openhab.core.common.registry.ManagedProvider)
|
68
|
+
raise "Cannot remove rule #{rule_uid} from non-managed provider #{provider.inspect}"
|
69
|
+
end
|
70
|
+
|
71
|
+
provider.remove(rule_uid)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenHAB
|
4
|
+
module Core
|
5
|
+
module Rules
|
6
|
+
# @interface
|
7
|
+
java_import org.openhab.core.automation.Rule
|
8
|
+
|
9
|
+
#
|
10
|
+
# A {Rule} is a chunk of code that can execute when certain conditions are
|
11
|
+
# met, enabling the core dynamic functionality of openHAB.
|
12
|
+
#
|
13
|
+
module Rule
|
14
|
+
#
|
15
|
+
# @!method visible?
|
16
|
+
# Check if visibility == `VISIBLE`
|
17
|
+
# @return [true,false]
|
18
|
+
#
|
19
|
+
|
20
|
+
#
|
21
|
+
# @!method hidden?
|
22
|
+
# Check if visibility == `HIDDEN`
|
23
|
+
# @return [true,false]
|
24
|
+
#
|
25
|
+
|
26
|
+
#
|
27
|
+
# @!method expert?
|
28
|
+
# Check if visibility == `EXPERT`
|
29
|
+
# @return [true,false]
|
30
|
+
#
|
31
|
+
|
32
|
+
#
|
33
|
+
# @!method initializing?
|
34
|
+
# Check if rule status == `INITIALIZING`
|
35
|
+
# @return [true,false]
|
36
|
+
#
|
37
|
+
#
|
38
|
+
# @!method idle?
|
39
|
+
# Check if rule status == `IDLE`
|
40
|
+
# @return [true,false]
|
41
|
+
#
|
42
|
+
#
|
43
|
+
# @!method running?
|
44
|
+
# Check if rule status == `RUNNING`
|
45
|
+
# @return [true,false]
|
46
|
+
#
|
47
|
+
|
48
|
+
Visibility.constants.each do |visibility|
|
49
|
+
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
50
|
+
def #{visibility.to_s.downcase}? # def visibile?
|
51
|
+
visibility == Visibility::#{visibility} # visibility == Visibility::VISIBLE
|
52
|
+
end # end
|
53
|
+
RUBY
|
54
|
+
end
|
55
|
+
|
56
|
+
RuleStatus.constants.each do |status|
|
57
|
+
next if status == :UNINITIALIZED
|
58
|
+
|
59
|
+
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
60
|
+
def #{status.to_s.downcase}? # def initializing?
|
61
|
+
status == RuleStatus::#{status} # status == RuleStatus::INITIALIZING
|
62
|
+
end # end
|
63
|
+
RUBY
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# Check if rule status == `UNINITIALIZED`
|
68
|
+
#
|
69
|
+
# @return [true,false]
|
70
|
+
#
|
71
|
+
def uninitialized?
|
72
|
+
s = status
|
73
|
+
s.nil? || s == RuleStatus::UNINITIALIZED
|
74
|
+
end
|
75
|
+
|
76
|
+
#
|
77
|
+
# Enable the Rule
|
78
|
+
#
|
79
|
+
# @param [true, false] enabled
|
80
|
+
# @return [void]
|
81
|
+
#
|
82
|
+
def enable(enabled: true)
|
83
|
+
Rules.manager.set_enabled(uid, enabled)
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# Disable the Rule
|
88
|
+
#
|
89
|
+
# @return [void]
|
90
|
+
#
|
91
|
+
def disable
|
92
|
+
enable(enabled: false)
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# Check if the rule's status detail == `DISABLED`
|
97
|
+
#
|
98
|
+
# @return [true, false]
|
99
|
+
#
|
100
|
+
def disabled?
|
101
|
+
info = status_info
|
102
|
+
info.nil? || info.status_detail == RuleStatusDetail::DISABLED
|
103
|
+
end
|
104
|
+
|
105
|
+
#
|
106
|
+
# @!attribute [r] status
|
107
|
+
# @return [RuleStatus nil]
|
108
|
+
#
|
109
|
+
def status
|
110
|
+
Rules.manager.get_status(uid)
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# @!attribute [r] status_info
|
115
|
+
# @return [RuleStatusInfo, nil]
|
116
|
+
#
|
117
|
+
def status_info
|
118
|
+
Rules.manager.get_status_info(uid)
|
119
|
+
end
|
120
|
+
|
121
|
+
# @return [String]
|
122
|
+
def inspect
|
123
|
+
r = "#<OpenHAB::Core::Rules::Rule #{uid}"
|
124
|
+
r += " #{name.inspect}" if name
|
125
|
+
r += " #{visibility}" unless visible?
|
126
|
+
r += " #{status || "<detached>"}"
|
127
|
+
r += " (#{status_info.status_detail})" if status_info && status_info.status_detail != RuleStatusDetail::NONE
|
128
|
+
r += " tags=#{tags.to_a.inspect}" unless tags.empty?
|
129
|
+
r += " configuration=#{configuration.properties.to_h}" if configuration && !configuration.properties.empty?
|
130
|
+
"#{r}>"
|
131
|
+
end
|
132
|
+
|
133
|
+
# @return [String]
|
134
|
+
def to_s
|
135
|
+
uid
|
136
|
+
end
|
137
|
+
|
138
|
+
#
|
139
|
+
# Manually trigger the rule
|
140
|
+
#
|
141
|
+
# @param [Object, nil] event The event to pass to the rule's execution blocks.
|
142
|
+
# @return [void]
|
143
|
+
#
|
144
|
+
def trigger(event = nil)
|
145
|
+
Rules.manager.run_now(uid, false, { "event" => event })
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenHAB
|
4
|
+
module Core
|
5
|
+
#
|
6
|
+
# Contains the core {Rule} as well as related infrastructure.
|
7
|
+
#
|
8
|
+
module Rules
|
9
|
+
java_import org.openhab.core.automation.RuleStatus,
|
10
|
+
org.openhab.core.automation.RuleStatusInfo,
|
11
|
+
org.openhab.core.automation.RuleStatusDetail,
|
12
|
+
org.openhab.core.automation.Visibility
|
13
|
+
|
14
|
+
class << self
|
15
|
+
#
|
16
|
+
# @!attribute [r] rule_manager
|
17
|
+
# @return [org.openhab.core.automation.RuleManager] The OpenHAB rule manager/engine
|
18
|
+
#
|
19
|
+
def manager
|
20
|
+
@manager ||= OSGi.service("org.openhab.core.automation.RuleManager")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -2,6 +2,56 @@
|
|
2
2
|
|
3
3
|
module OpenHAB
|
4
4
|
module Core
|
5
|
+
#
|
6
|
+
# Provide callback mechanisms for script handling
|
7
|
+
#
|
8
|
+
module ScriptHandling
|
9
|
+
module_function
|
10
|
+
|
11
|
+
#
|
12
|
+
# Add a block of code to be executed once the rule script has finished loading.
|
13
|
+
#
|
14
|
+
# This can occur on OpenHAB start up, when the script is first created, or updated.
|
15
|
+
#
|
16
|
+
# Multiple hooks can be added by calling {#script_loaded} multiple times.
|
17
|
+
# They can be used to perform final initializations.
|
18
|
+
#
|
19
|
+
# @return [void]
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
# script_loaded do
|
23
|
+
# logger.info 'Hi, this script has just finished loading'
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
# script_loaded do
|
28
|
+
# logger.info 'I will be called after the script finished loading too'
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
def script_loaded(&block)
|
32
|
+
Core::ScriptHandlingCallbacks.script_loaded_hooks << block
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Add a block of code to be executed when the script is unloaded.
|
37
|
+
#
|
38
|
+
# This can occur when OpenHAB shuts down, or when the script is being reloaded.
|
39
|
+
#
|
40
|
+
# Multiple hooks can be added by calling {#script_unloaded} multiple times.
|
41
|
+
# They can be used to perform final cleanup.
|
42
|
+
#
|
43
|
+
# @return [void]
|
44
|
+
#
|
45
|
+
# @example
|
46
|
+
# script_unloaded do
|
47
|
+
# logger.info 'Hi, this script has been unloaded'
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
def script_unloaded(&block)
|
51
|
+
Core::ScriptHandlingCallbacks.script_unloaded_hooks << block
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
5
55
|
#
|
6
56
|
# Manages script loading and unloading
|
7
57
|
#
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenHAB
|
4
|
+
module Core
|
5
|
+
module Things
|
6
|
+
#
|
7
|
+
# Contains the link between a {Thing Thing's} {Channel Channels} and {GenericItem Items}.
|
8
|
+
#
|
9
|
+
module Links
|
10
|
+
#
|
11
|
+
# Provides {Items::GenericItem items} linked to {Channel channels} in Ruby to openHAB.
|
12
|
+
#
|
13
|
+
class Provider < Core::Provider
|
14
|
+
include org.openhab.core.thing.link.ItemChannelLinkProvider
|
15
|
+
|
16
|
+
class << self
|
17
|
+
#
|
18
|
+
# The ItemChannelLink registry
|
19
|
+
#
|
20
|
+
# @return [org.openhab.core.thing.link.ItemChanneLinkRegistry]
|
21
|
+
#
|
22
|
+
def registry
|
23
|
+
@registry ||= OSGi.service("org.openhab.core.thing.link.ItemChannelLinkRegistry")
|
24
|
+
end
|
25
|
+
|
26
|
+
# @!visibility private
|
27
|
+
def link(item, channel, config = {})
|
28
|
+
config = org.openhab.core.config.core.Configuration.new(config.transform_keys(&:to_s))
|
29
|
+
channel = ChannelUID.new(channel) if channel.is_a?(String)
|
30
|
+
channel = channel.uid if channel.is_a?(Channel)
|
31
|
+
link = org.openhab.core.thing.link.ItemChannelLink.new(item.name, channel, config)
|
32
|
+
|
33
|
+
current.add(link)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenHAB
|
4
|
+
module Core
|
5
|
+
module Things
|
6
|
+
#
|
7
|
+
# Provides {Thing Things} created in Ruby to openHAB
|
8
|
+
#
|
9
|
+
class Provider < Core::Provider
|
10
|
+
include org.openhab.core.thing.ThingProvider
|
11
|
+
|
12
|
+
class << self
|
13
|
+
#
|
14
|
+
# The Thing registry
|
15
|
+
#
|
16
|
+
# @return [org.openhab.core.thing.ThingRegistry]
|
17
|
+
#
|
18
|
+
def registry
|
19
|
+
$things
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|