openhab-scripting 4.1.4 → 4.5.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/core/entity_lookup.rb +1 -57
- data/lib/openhab/dsl/actions.rb +2 -3
- data/lib/openhab/dsl/dsl.rb +8 -12
- data/lib/openhab/dsl/group.rb +1 -5
- data/lib/openhab/dsl/items/color_item.rb +60 -0
- data/lib/openhab/dsl/items/comparable_item.rb +49 -0
- data/lib/openhab/dsl/items/contact_item.rb +41 -0
- data/lib/openhab/dsl/items/date_time_item.rb +64 -0
- data/lib/openhab/dsl/items/dimmer_item.rb +59 -0
- data/lib/openhab/dsl/items/ensure.rb +93 -0
- data/lib/openhab/dsl/items/generic_item.rb +174 -0
- data/lib/openhab/dsl/items/group_item.rb +121 -89
- data/lib/openhab/dsl/items/image_item.rb +5 -41
- data/lib/openhab/dsl/items/item_equality.rb +36 -0
- data/lib/openhab/dsl/items/item_registry.rb +49 -0
- data/lib/openhab/dsl/items/items.rb +81 -35
- data/lib/openhab/dsl/items/metadata.rb +325 -0
- data/lib/openhab/dsl/items/number_item.rb +6 -312
- data/lib/openhab/dsl/items/numeric_item.rb +68 -0
- data/lib/openhab/dsl/items/persistence.rb +122 -0
- data/lib/openhab/dsl/items/player_item.rb +49 -40
- data/lib/openhab/dsl/items/rollershutter_item.rb +25 -77
- data/lib/openhab/dsl/items/string_item.rb +16 -58
- data/lib/openhab/dsl/items/switch_item.rb +62 -0
- data/lib/openhab/dsl/lazy_array.rb +8 -6
- data/lib/openhab/dsl/monkey_patch/events/events.rb +2 -2
- data/lib/openhab/dsl/monkey_patch/events/item_command.rb +67 -24
- data/lib/openhab/dsl/monkey_patch/events/item_event.rb +5 -5
- data/lib/openhab/dsl/monkey_patch/events/item_state.rb +10 -11
- data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +10 -11
- data/lib/openhab/dsl/monkey_patch/ruby/number.rb +25 -2
- data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -3
- data/lib/openhab/dsl/monkey_patch/ruby/string.rb +24 -24
- data/lib/openhab/dsl/rules/terse.rb +24 -0
- data/lib/openhab/dsl/states.rb +1 -1
- data/lib/openhab/dsl/time_of_day.rb +3 -5
- data/lib/openhab/dsl/types/comparable_type.rb +21 -0
- data/lib/openhab/dsl/types/date_time_type.rb +334 -0
- data/lib/openhab/dsl/types/decimal_type.rb +187 -0
- data/lib/openhab/dsl/types/hsb_type.rb +201 -0
- data/lib/openhab/dsl/types/increase_decrease_type.rb +23 -0
- data/lib/openhab/dsl/types/next_previous_type.rb +23 -0
- data/lib/openhab/dsl/types/numeric_type.rb +39 -0
- data/lib/openhab/dsl/types/on_off_type.rb +29 -0
- data/lib/openhab/dsl/types/open_closed_type.rb +29 -0
- data/lib/openhab/dsl/types/percent_type.rb +70 -0
- data/lib/openhab/dsl/types/play_pause_type.rb +27 -0
- data/lib/openhab/dsl/types/quantity_type.rb +275 -0
- data/lib/openhab/dsl/types/refresh_type.rb +18 -0
- data/lib/openhab/dsl/types/rewind_fastforward_type.rb +33 -0
- data/lib/openhab/dsl/types/stop_move_type.rb +23 -0
- data/lib/openhab/dsl/types/string_type.rb +88 -0
- data/lib/openhab/dsl/types/type.rb +72 -0
- data/lib/openhab/dsl/types/types.rb +78 -0
- data/lib/openhab/dsl/types/un_def_type.rb +22 -0
- data/lib/openhab/dsl/types/up_down_type.rb +32 -0
- data/lib/openhab/dsl/units.rb +11 -6
- data/lib/openhab/version.rb +1 -1
- data/lib/openhab.rb +0 -1
- metadata +36 -28
- data/lib/openhab/dsl/items/datetime_item.rb +0 -75
- data/lib/openhab/dsl/items/item_command.rb +0 -90
- data/lib/openhab/dsl/items/item_delegate.rb +0 -125
- data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +0 -51
- data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +0 -140
- data/lib/openhab/dsl/monkey_patch/items/items.rb +0 -142
- data/lib/openhab/dsl/monkey_patch/items/metadata.rb +0 -328
- data/lib/openhab/dsl/monkey_patch/items/persistence.rb +0 -123
- data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +0 -71
- data/lib/openhab/dsl/monkey_patch/ruby/range.rb +0 -47
- data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -32
- data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +0 -97
- data/lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/next_previous_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +0 -79
- data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +0 -71
- data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +0 -77
- data/lib/openhab/dsl/monkey_patch/types/play_pause_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +0 -69
- data/lib/openhab/dsl/monkey_patch/types/refresh_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/stop_move_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/types.rb +0 -15
- data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +0 -72
- data/lib/openhab/dsl/types/datetime.rb +0 -338
- data/lib/openhab/dsl/types/quantity.rb +0 -300
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'java'
|
|
4
|
-
require 'openhab/log/logger'
|
|
5
|
-
require 'bigdecimal'
|
|
6
|
-
|
|
7
|
-
# Monkey patch items
|
|
8
|
-
require 'openhab/dsl/monkey_patch/items/metadata'
|
|
9
|
-
require 'openhab/dsl/monkey_patch/items/persistence'
|
|
10
|
-
require 'openhab/dsl/monkey_patch/items/contact_item'
|
|
11
|
-
require 'openhab/dsl/monkey_patch/items/dimmer_item'
|
|
12
|
-
require 'openhab/dsl/monkey_patch/items/switch_item'
|
|
13
|
-
|
|
14
|
-
module OpenHAB
|
|
15
|
-
module DSL
|
|
16
|
-
module MonkeyPatch
|
|
17
|
-
#
|
|
18
|
-
# Monkeypatches Items
|
|
19
|
-
#
|
|
20
|
-
module Items
|
|
21
|
-
#
|
|
22
|
-
# Extensions for Items
|
|
23
|
-
#
|
|
24
|
-
module ItemExtensions
|
|
25
|
-
include OpenHAB::Log
|
|
26
|
-
java_import Java::OrgOpenhabCoreModelScriptActions::BusEvent
|
|
27
|
-
java_import Java::OrgOpenhabCoreTypes::UnDefType
|
|
28
|
-
|
|
29
|
-
#
|
|
30
|
-
# Send a command to this item
|
|
31
|
-
#
|
|
32
|
-
# @param [Object] command to send to object
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
def command(command)
|
|
36
|
-
command = command.to_java.strip_trailing_zeros.to_plain_string if command.is_a? BigDecimal
|
|
37
|
-
logger.trace "Sending Command #{command} to #{id}"
|
|
38
|
-
BusEvent.sendCommand(self, command.to_s)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
alias << command
|
|
42
|
-
|
|
43
|
-
#
|
|
44
|
-
# Send an update to this item
|
|
45
|
-
#
|
|
46
|
-
# @param [Object] update the item
|
|
47
|
-
#
|
|
48
|
-
#
|
|
49
|
-
def update(update)
|
|
50
|
-
update = update.to_java.strip_trailing_zeros.to_plain_string if update.is_a? BigDecimal
|
|
51
|
-
logger.trace "Sending Update #{update} to #{id}"
|
|
52
|
-
BusEvent.postUpdate(self, update.to_s)
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
#
|
|
56
|
-
# Check if the item state == UNDEF
|
|
57
|
-
#
|
|
58
|
-
# @return [Boolean] True if the state is UNDEF, false otherwise
|
|
59
|
-
#
|
|
60
|
-
def undef?
|
|
61
|
-
# Need to explicitly call the super method version because this state will never return UNDEF
|
|
62
|
-
method(:state).super_method.call == UnDefType::UNDEF
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
#
|
|
66
|
-
# Check if the item state == NULL
|
|
67
|
-
#
|
|
68
|
-
# @return [Boolean] True if the state is NULL, false otherwise
|
|
69
|
-
def null?
|
|
70
|
-
# Need to explicitly call the super method version because this state will never return NULL
|
|
71
|
-
method(:state).super_method.call == UnDefType::NULL
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
#
|
|
75
|
-
# Check if the item has a state (not UNDEF or NULL)
|
|
76
|
-
#
|
|
77
|
-
# @return [Boolean] True if state is not UNDEF or NULL
|
|
78
|
-
#
|
|
79
|
-
def state?
|
|
80
|
-
undef? == false && null? == false
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
#
|
|
84
|
-
# Get the item state
|
|
85
|
-
#
|
|
86
|
-
# @return [State] OpenHAB item state if state is not UNDEF or NULL, nil otherwise
|
|
87
|
-
#
|
|
88
|
-
def state
|
|
89
|
-
super if state?
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
#
|
|
93
|
-
# Get an ID for the item, using the item label if set, otherwise item name
|
|
94
|
-
#
|
|
95
|
-
# @return [String] label if set otherwise name
|
|
96
|
-
#
|
|
97
|
-
def id
|
|
98
|
-
label || name
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
#
|
|
102
|
-
# Get the string representation of the state of the item
|
|
103
|
-
#
|
|
104
|
-
# @return [String] State of the item as a string
|
|
105
|
-
#
|
|
106
|
-
def to_s
|
|
107
|
-
method(:state).super_method.call.to_s # call the super state to include UNDEF/NULL
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
#
|
|
111
|
-
# Inspect the item
|
|
112
|
-
#
|
|
113
|
-
# @return [String] details of the item
|
|
114
|
-
#
|
|
115
|
-
def inspect
|
|
116
|
-
toString
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
#
|
|
120
|
-
# Return all groups that this item is part of
|
|
121
|
-
#
|
|
122
|
-
# @return [Array<Group>] All groups that this item is part of
|
|
123
|
-
#
|
|
124
|
-
def groups
|
|
125
|
-
group_names.map { |name| OpenHAB::DSL::Groups.groups[name] }
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
java_import Java::OrgOpenhabCoreItems::GenericItem
|
|
130
|
-
|
|
131
|
-
#
|
|
132
|
-
# Monkey patches all OpenHAB items
|
|
133
|
-
#
|
|
134
|
-
class GenericItem
|
|
135
|
-
prepend OpenHAB::DSL::MonkeyPatch::Items::ItemExtensions
|
|
136
|
-
prepend OpenHAB::DSL::MonkeyPatch::Items::Metadata
|
|
137
|
-
prepend OpenHAB::DSL::MonkeyPatch::Items::Persistence
|
|
138
|
-
end
|
|
139
|
-
end
|
|
140
|
-
end
|
|
141
|
-
end
|
|
142
|
-
end
|
|
@@ -1,328 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'java'
|
|
4
|
-
require 'delegate'
|
|
5
|
-
require 'pp'
|
|
6
|
-
require 'forwardable'
|
|
7
|
-
require 'openhab/core/osgi'
|
|
8
|
-
require 'openhab/log/logger'
|
|
9
|
-
|
|
10
|
-
module OpenHAB
|
|
11
|
-
module DSL
|
|
12
|
-
module MonkeyPatch
|
|
13
|
-
module Items
|
|
14
|
-
#
|
|
15
|
-
# Metadata extension for Items
|
|
16
|
-
#
|
|
17
|
-
module Metadata
|
|
18
|
-
include OpenHAB::Log
|
|
19
|
-
|
|
20
|
-
java_import Java::OrgOpenhabCoreItems::Metadata
|
|
21
|
-
java_import Java::OrgOpenhabCoreItems::MetadataKey
|
|
22
|
-
|
|
23
|
-
#
|
|
24
|
-
# Provide the interface to access namespace's value and configuration
|
|
25
|
-
#
|
|
26
|
-
class MetadataItem < SimpleDelegator
|
|
27
|
-
extend Forwardable
|
|
28
|
-
|
|
29
|
-
def_delegator :@metadata, :value
|
|
30
|
-
|
|
31
|
-
def initialize(metadata: nil, key: nil, value: nil, config: nil)
|
|
32
|
-
@metadata = metadata || Metadata.new(key || MetadataKey.new('', ''), value&.to_s, config)
|
|
33
|
-
super(to_ruby(@metadata&.configuration))
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
#
|
|
37
|
-
# Updates the metadata configuration associated with the key
|
|
38
|
-
#
|
|
39
|
-
def []=(key, value)
|
|
40
|
-
configuration = {}.merge(@metadata&.configuration || {}).merge({ key => value })
|
|
41
|
-
metadata = Metadata.new(@metadata&.uID, @metadata&.value, configuration)
|
|
42
|
-
NamespaceAccessor.registry.update(metadata) if @metadata&.uID
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
#
|
|
46
|
-
# Delete the configuration with the given key
|
|
47
|
-
#
|
|
48
|
-
# @return [Java::Org::openhab::core::items::Metadata] the old metadata
|
|
49
|
-
#
|
|
50
|
-
def delete(key)
|
|
51
|
-
configuration = {}.merge(@metadata&.configuration || {})
|
|
52
|
-
configuration.delete(key)
|
|
53
|
-
metadata = Metadata.new(@metadata&.uID, @metadata&.value, configuration)
|
|
54
|
-
NamespaceAccessor.registry.update(metadata) if @metadata&.uID
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
#
|
|
58
|
-
# Set the metadata value
|
|
59
|
-
#
|
|
60
|
-
# @return [Java::Org::openhab::core::items::Metadata] the old metadata
|
|
61
|
-
#
|
|
62
|
-
def value=(value)
|
|
63
|
-
metadata = Metadata.new(@metadata&.uID, value&.to_s, @metadata&.configuration)
|
|
64
|
-
NamespaceAccessor.registry.update(metadata) if @metadata&.uID
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
#
|
|
68
|
-
# Set the entire configuration to a hash
|
|
69
|
-
#
|
|
70
|
-
# @return [Java::Org::openhab::core::items::Metadata] the old metadata
|
|
71
|
-
#
|
|
72
|
-
def config=(config)
|
|
73
|
-
raise ArgumentError, 'Configuration must be a hash' unless config.is_a? Hash
|
|
74
|
-
|
|
75
|
-
metadata = Metadata.new(@metadata&.uID, @metadata&.value, config)
|
|
76
|
-
NamespaceAccessor.registry.update(metadata) if @metadata&.uID
|
|
77
|
-
end
|
|
78
|
-
alias configuration= config=
|
|
79
|
-
|
|
80
|
-
#
|
|
81
|
-
# Convert the metadata to an array
|
|
82
|
-
#
|
|
83
|
-
# @return [Array[2]] An array of [value, configuration]
|
|
84
|
-
#
|
|
85
|
-
def to_a
|
|
86
|
-
[@metadata&.value, @metadata&.configuration || {}]
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
private
|
|
90
|
-
|
|
91
|
-
#
|
|
92
|
-
# Recursively convert the supplied Hash object into a Ruby Hash and recreate the keys and values
|
|
93
|
-
#
|
|
94
|
-
# @param [Hash] Hash to convert
|
|
95
|
-
#
|
|
96
|
-
# @return [Hash] The converted hash
|
|
97
|
-
#
|
|
98
|
-
def to_ruby_hash(hash)
|
|
99
|
-
return unless hash.respond_to? :each_with_object
|
|
100
|
-
|
|
101
|
-
hash.each_with_object({}) { |(key, value), ruby_hash| ruby_hash[to_ruby(key)] = to_ruby(value) }
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
#
|
|
105
|
-
# Recursively convert the supplied array to a Ruby array and recreate all String values
|
|
106
|
-
#
|
|
107
|
-
# @param [Object] array to convert
|
|
108
|
-
#
|
|
109
|
-
# @return [Array] The converted array
|
|
110
|
-
#
|
|
111
|
-
def to_ruby_array(array)
|
|
112
|
-
return unless array.respond_to? :each_with_object
|
|
113
|
-
|
|
114
|
-
array.each_with_object([]) { |value, ruby_array| ruby_array << to_ruby(value) }
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
# Convert the given object to Ruby equivalent
|
|
118
|
-
def to_ruby(value)
|
|
119
|
-
case value
|
|
120
|
-
when Hash, Java::JavaUtil::Map then to_ruby_hash(value)
|
|
121
|
-
when Array, Java::JavaUtil::List then to_ruby_array(value)
|
|
122
|
-
when String then String.new(value)
|
|
123
|
-
else value
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
#
|
|
129
|
-
# Provide the interface to access item metadata
|
|
130
|
-
#
|
|
131
|
-
class NamespaceAccessor
|
|
132
|
-
include Enumerable
|
|
133
|
-
|
|
134
|
-
def initialize(item_name:)
|
|
135
|
-
@item_name = item_name
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
#
|
|
139
|
-
# Return the metadata namespace
|
|
140
|
-
#
|
|
141
|
-
# @return [OpenHAB::DSL::MonkeyPatch::Items::MetadataItem], or nil if the namespace doesn't exist
|
|
142
|
-
#
|
|
143
|
-
def [](namespace)
|
|
144
|
-
logger.trace("Getting metadata for item: #{@item_name}, namespace '#{namespace}'")
|
|
145
|
-
metadata = NamespaceAccessor.registry.get(MetadataKey.new(namespace, @item_name))
|
|
146
|
-
MetadataItem.new(metadata: metadata) if metadata
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
#
|
|
150
|
-
# Set the metadata namespace. If the namespace does not exist, it will be created
|
|
151
|
-
#
|
|
152
|
-
# @param value [Object] The assigned value can be a OpenHAB::DSL::MonkeyPatch::Items::MetadataItem,
|
|
153
|
-
# Java::Org::openhab::core::items::Metadata, Array[2] of [value, configuration],
|
|
154
|
-
# A String to set the value and clear the configuration,
|
|
155
|
-
# or a Hash to set the configuration and set the value to nil
|
|
156
|
-
#
|
|
157
|
-
# @return [OpenHAB::DSL::MonkeyPatch::Items::MetadataItem]
|
|
158
|
-
#
|
|
159
|
-
def []=(namespace, value)
|
|
160
|
-
meta_value, configuration = update_from_value(value)
|
|
161
|
-
|
|
162
|
-
key = MetadataKey.new(namespace, @item_name)
|
|
163
|
-
metadata = Metadata.new(key, meta_value&.to_s, configuration)
|
|
164
|
-
# registry.get can be omitted, but registry.update will log a warning for nonexistent metadata
|
|
165
|
-
if NamespaceAccessor.registry.get(key)
|
|
166
|
-
NamespaceAccessor.registry.update(metadata)
|
|
167
|
-
else
|
|
168
|
-
NamespaceAccessor.registry.add(metadata)
|
|
169
|
-
end
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
#
|
|
173
|
-
# Implements Hash#dig-like functionaity to metadata
|
|
174
|
-
#
|
|
175
|
-
# @param [String] key The first key
|
|
176
|
-
# @param [Array<String, Symbol>] keys More keys to dig deeper
|
|
177
|
-
#
|
|
178
|
-
# @return [OpenHAB::DSL::MonkeyPatch::Items::MetadataItem], or nil if the namespace doesn't exist
|
|
179
|
-
#
|
|
180
|
-
def dig(key, *keys)
|
|
181
|
-
keys.empty? ? self[key]&.value : self[key]&.dig(*keys)
|
|
182
|
-
end
|
|
183
|
-
|
|
184
|
-
#
|
|
185
|
-
# Enumerates through all the namespaces
|
|
186
|
-
#
|
|
187
|
-
def each
|
|
188
|
-
return unless block_given?
|
|
189
|
-
|
|
190
|
-
NamespaceAccessor.registry.getAll.each do |meta|
|
|
191
|
-
yield meta.uID.namespace, meta.value, meta.configuration if meta.uID.itemName == @item_name
|
|
192
|
-
end
|
|
193
|
-
end
|
|
194
|
-
|
|
195
|
-
#
|
|
196
|
-
# Remove all the namespaces
|
|
197
|
-
#
|
|
198
|
-
def clear
|
|
199
|
-
NamespaceAccessor.registry.removeItemMetadata @item_name
|
|
200
|
-
end
|
|
201
|
-
|
|
202
|
-
#
|
|
203
|
-
# Delete a specific namespace
|
|
204
|
-
#
|
|
205
|
-
# @param namespace [String] The namespace to delete
|
|
206
|
-
#
|
|
207
|
-
def delete(namespace)
|
|
208
|
-
NamespaceAccessor.registry.remove(MetadataKey.new(namespace, @item_name))
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
alias delete_all clear
|
|
212
|
-
|
|
213
|
-
#
|
|
214
|
-
# @return [Boolean] True if the given namespace exists, false otherwise
|
|
215
|
-
#
|
|
216
|
-
def key?(namespace)
|
|
217
|
-
!NamespaceAccessor.registry.get(MetadataKey.new(namespace, @item_name)).nil?
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
alias has_key? key?
|
|
221
|
-
alias include? key?
|
|
222
|
-
|
|
223
|
-
#
|
|
224
|
-
# Merge the given hash with the current metadata. Existing namespace that matches the name
|
|
225
|
-
# of the new namespace will be overwritten. Others will be added.
|
|
226
|
-
#
|
|
227
|
-
def merge!(*others)
|
|
228
|
-
return self if others.empty?
|
|
229
|
-
|
|
230
|
-
others.each do |other|
|
|
231
|
-
case other
|
|
232
|
-
when Hash then merge_hash!(other)
|
|
233
|
-
when self.class then merge_metadata!(other)
|
|
234
|
-
else raise ArgumentError, "merge only supports Hash, or another item's metadata"
|
|
235
|
-
end
|
|
236
|
-
end
|
|
237
|
-
self
|
|
238
|
-
end
|
|
239
|
-
|
|
240
|
-
#
|
|
241
|
-
# @return [String] the string representation of all the namespaces with their value and config
|
|
242
|
-
#
|
|
243
|
-
def to_s
|
|
244
|
-
namespaces = []
|
|
245
|
-
each { |ns, value, config| namespaces << "\"#{ns}\"=>[\"#{value}\",#{config}]" }
|
|
246
|
-
"{#{namespaces.join(',')}}"
|
|
247
|
-
end
|
|
248
|
-
|
|
249
|
-
#
|
|
250
|
-
# @return [Java::org::openhab::core::items::MetadataRegistry]
|
|
251
|
-
#
|
|
252
|
-
def self.registry
|
|
253
|
-
@registry ||= OpenHAB::Core::OSGI.service('org.openhab.core.items.MetadataRegistry')
|
|
254
|
-
end
|
|
255
|
-
|
|
256
|
-
private
|
|
257
|
-
|
|
258
|
-
#
|
|
259
|
-
# perform an updated based on the supplied value
|
|
260
|
-
#
|
|
261
|
-
# @param [MetadataItem,Metadata,Array,Hash] value to perform update from
|
|
262
|
-
#
|
|
263
|
-
# @return [Array<Object,Hash>] Array containing the value and configuration based on the
|
|
264
|
-
# the supplied object
|
|
265
|
-
#
|
|
266
|
-
def update_from_value(value)
|
|
267
|
-
case value
|
|
268
|
-
when MetadataItem then [value.value, value.__getobj__]
|
|
269
|
-
when Metadata then [value.value, value.configuration]
|
|
270
|
-
when Array
|
|
271
|
-
raise ArgumentError, 'Array must contain 2 elements: value, config' if value.length != 2
|
|
272
|
-
|
|
273
|
-
value
|
|
274
|
-
when Hash then [nil, value]
|
|
275
|
-
else [value, nil]
|
|
276
|
-
end
|
|
277
|
-
end
|
|
278
|
-
|
|
279
|
-
#
|
|
280
|
-
# Merge the metadata from the supplied other metadata object
|
|
281
|
-
#
|
|
282
|
-
# @param [Hash] other metadata object to merge
|
|
283
|
-
# @yield [key, current_metadata, new_meta] to process merge
|
|
284
|
-
#
|
|
285
|
-
#
|
|
286
|
-
def merge_metadata!(other)
|
|
287
|
-
other.each do |key, new_value, new_config|
|
|
288
|
-
new_meta = new_value, new_config
|
|
289
|
-
if block_given?
|
|
290
|
-
current_meta = self[key]&.to_a
|
|
291
|
-
new_meta = yield key, current_meta, new_meta unless current_meta.nil?
|
|
292
|
-
end
|
|
293
|
-
self[key] = new_meta
|
|
294
|
-
end
|
|
295
|
-
end
|
|
296
|
-
|
|
297
|
-
#
|
|
298
|
-
# Merge a hash into the metadata
|
|
299
|
-
#
|
|
300
|
-
# @param [Hash] other to merge into metadata
|
|
301
|
-
# @yield [key, current_metadata, new_meta] to process merge
|
|
302
|
-
#
|
|
303
|
-
#
|
|
304
|
-
def merge_hash!(other)
|
|
305
|
-
other.each do |key, new_meta|
|
|
306
|
-
if block_given?
|
|
307
|
-
current_meta = self[key]&.to_a
|
|
308
|
-
new_meta = yield key, current_meta, new_meta unless current_meta.nil?
|
|
309
|
-
end
|
|
310
|
-
self[key] = new_meta
|
|
311
|
-
end
|
|
312
|
-
end
|
|
313
|
-
end
|
|
314
|
-
|
|
315
|
-
#
|
|
316
|
-
# Accessor to the item's metadata
|
|
317
|
-
#
|
|
318
|
-
# @return [NamespaceAccessor] an Enumerable object to access item's namespaces
|
|
319
|
-
#
|
|
320
|
-
def meta
|
|
321
|
-
@meta ||= NamespaceAccessor.new(item_name: name)
|
|
322
|
-
end
|
|
323
|
-
alias metadata meta
|
|
324
|
-
end
|
|
325
|
-
end
|
|
326
|
-
end
|
|
327
|
-
end
|
|
328
|
-
end
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module OpenHAB
|
|
4
|
-
module DSL
|
|
5
|
-
module MonkeyPatch
|
|
6
|
-
module Items
|
|
7
|
-
#
|
|
8
|
-
# Persistence extension for Items
|
|
9
|
-
#
|
|
10
|
-
module Persistence
|
|
11
|
-
java_import Java::OrgOpenhabCoreTypesUtil::UnitUtils
|
|
12
|
-
|
|
13
|
-
# A wrapper for OpenHAB's HistoricItem that returns the state directly
|
|
14
|
-
class HistoricState < SimpleDelegator
|
|
15
|
-
attr_reader :timestamp, :state
|
|
16
|
-
|
|
17
|
-
def initialize(state, timestamp)
|
|
18
|
-
@state = state
|
|
19
|
-
@timestamp = timestamp
|
|
20
|
-
super(@state)
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# All persistence methods that could return a Quantity
|
|
25
|
-
QUANTITY_METHODS = %i[average_since
|
|
26
|
-
delta_since
|
|
27
|
-
deviation_since
|
|
28
|
-
sum_since
|
|
29
|
-
variance_since].freeze
|
|
30
|
-
|
|
31
|
-
# All persistence methods that require a timestamp
|
|
32
|
-
PERSISTENCE_METHODS = (QUANTITY_METHODS +
|
|
33
|
-
%i[changed_since
|
|
34
|
-
evolution_rate
|
|
35
|
-
historic_state
|
|
36
|
-
maximum_since
|
|
37
|
-
minimum_since
|
|
38
|
-
updated_since]).freeze
|
|
39
|
-
|
|
40
|
-
%i[persist last_update].each do |method|
|
|
41
|
-
define_method(method) do |service = nil|
|
|
42
|
-
service ||= persistence_service
|
|
43
|
-
PersistenceExtensions.public_send(method, self, service&.to_s)
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
#
|
|
48
|
-
# Return the previous state of the item
|
|
49
|
-
#
|
|
50
|
-
# @param skip_equal [Boolean] if true, skips equal state values and
|
|
51
|
-
# searches the first state not equal the current state
|
|
52
|
-
# @param service [String] the name of the PersistenceService to use
|
|
53
|
-
#
|
|
54
|
-
# @return the previous state or nil if no previous state could be found,
|
|
55
|
-
# or if the default persistence service is not configured or
|
|
56
|
-
# does not refer to a valid service
|
|
57
|
-
#
|
|
58
|
-
def previous_state(service = nil, skip_equal: false)
|
|
59
|
-
service ||= persistence_service
|
|
60
|
-
result = PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
|
|
61
|
-
HistoricState.new(quantify(result.state), result.timestamp)
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
PERSISTENCE_METHODS.each do |method|
|
|
65
|
-
define_method(method) do |timestamp, service = nil|
|
|
66
|
-
service ||= persistence_service
|
|
67
|
-
result = PersistenceExtensions.public_send(method, self, to_zdt(timestamp), service&.to_s)
|
|
68
|
-
if result.is_a? Java::OrgOpenhabCorePersistence::HistoricItem
|
|
69
|
-
return HistoricState.new(quantify(result.state), result.timestamp)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
QUANTITY_METHODS.include?(method) ? quantify(result) : result
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
private
|
|
77
|
-
|
|
78
|
-
#
|
|
79
|
-
# Convert timestamp to ZonedDateTime if it's a TemporalAmount
|
|
80
|
-
#
|
|
81
|
-
# @param [Object] timestamp to convert
|
|
82
|
-
#
|
|
83
|
-
# @return [ZonedDateTime]
|
|
84
|
-
#
|
|
85
|
-
def to_zdt(timestamp)
|
|
86
|
-
if timestamp.is_a? Java::JavaTimeTemporal::TemporalAmount
|
|
87
|
-
logger.trace("Converting #{timestamp} (#{timestamp.class}) to ZonedDateTime")
|
|
88
|
-
Java::JavaTime::ZonedDateTime.now.minus(timestamp)
|
|
89
|
-
else
|
|
90
|
-
timestamp
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
#
|
|
95
|
-
# Convert value to Quantity if it is a DecimalType and a unit is defined
|
|
96
|
-
#
|
|
97
|
-
# @param [Object] value The value to convert
|
|
98
|
-
#
|
|
99
|
-
# @return [Object] Quantity or the original value
|
|
100
|
-
#
|
|
101
|
-
def quantify(value)
|
|
102
|
-
if value.is_a?(Java::OrgOpenhabCoreLibraryTypes::DecimalType) && state_description&.pattern
|
|
103
|
-
item_unit = UnitUtils.parse_unit(state_description.pattern)
|
|
104
|
-
logger.trace("Unitizing #{value} with unit #{item_unit}")
|
|
105
|
-
Quantity.new(Java::OrgOpenhabCoreLibraryTypes::QuantityType.new(value.to_big_decimal, item_unit))
|
|
106
|
-
else
|
|
107
|
-
value
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
#
|
|
112
|
-
# Get the specified persistence service from the current thread local variable
|
|
113
|
-
#
|
|
114
|
-
# @return [Object] Persistence service name as String or Symbol, or nil if not set
|
|
115
|
-
#
|
|
116
|
-
def persistence_service
|
|
117
|
-
Thread.current.thread_variable_get(:persistence_service)
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
end
|
|
122
|
-
end
|
|
123
|
-
end
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'java'
|
|
4
|
-
require 'openhab/dsl/items/item_command'
|
|
5
|
-
|
|
6
|
-
module OpenHAB
|
|
7
|
-
module DSL
|
|
8
|
-
module MonkeyPatch
|
|
9
|
-
#
|
|
10
|
-
# Patches OpenHAB items
|
|
11
|
-
#
|
|
12
|
-
module Items
|
|
13
|
-
java_import Java::OrgOpenhabCoreLibraryItems::SwitchItem
|
|
14
|
-
|
|
15
|
-
# Alias class names for easy is_a? comparisons
|
|
16
|
-
::Switch = SwitchItem
|
|
17
|
-
|
|
18
|
-
#
|
|
19
|
-
# Monkeypatching SwitchItem to add Ruby Support methods
|
|
20
|
-
#
|
|
21
|
-
class SwitchItem
|
|
22
|
-
extend OpenHAB::DSL::Items::ItemCommand
|
|
23
|
-
|
|
24
|
-
java_import Java::OrgOpenhabCoreLibraryTypes::OnOffType
|
|
25
|
-
|
|
26
|
-
item_type Java::OrgOpenhabCoreLibraryItems::SwitchItem
|
|
27
|
-
|
|
28
|
-
alias truthy? on?
|
|
29
|
-
|
|
30
|
-
#
|
|
31
|
-
# Send a command to invert the state of the switch
|
|
32
|
-
#
|
|
33
|
-
# @return [OnOffType] Inverted state
|
|
34
|
-
#
|
|
35
|
-
def toggle
|
|
36
|
-
self << !self
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
#
|
|
40
|
-
# Return the inverted state of the switch: ON if the switch is OFF, UNDEF or NULL; OFF if the switch is ON
|
|
41
|
-
#
|
|
42
|
-
# @return [OnOffType] Inverted state
|
|
43
|
-
#
|
|
44
|
-
def !
|
|
45
|
-
return !state if state?
|
|
46
|
-
|
|
47
|
-
OnOffType::ON
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
#
|
|
51
|
-
# Check for equality against supplied object
|
|
52
|
-
#
|
|
53
|
-
# @param [Object] other object to compare to
|
|
54
|
-
#
|
|
55
|
-
# @return [Boolean] True if other is a OnOffType and other equals state for this switch item,
|
|
56
|
-
# otherwise result from super
|
|
57
|
-
#
|
|
58
|
-
def ==(other)
|
|
59
|
-
other = other.get_state_as(OnOffType) if other.respond_to?(:get_state_as)
|
|
60
|
-
|
|
61
|
-
if other.is_a? OnOffType
|
|
62
|
-
state? && state == other
|
|
63
|
-
else
|
|
64
|
-
super
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
end
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'java'
|
|
4
|
-
|
|
5
|
-
# Monkey patch range to support case equality of OpenHab "Numeric" Objects
|
|
6
|
-
|
|
7
|
-
module OpenHAB
|
|
8
|
-
module DSL
|
|
9
|
-
#
|
|
10
|
-
# MonkeyPatch both Ruby and OpenHAB Objects to support DSL
|
|
11
|
-
#
|
|
12
|
-
module MonkeyPatch
|
|
13
|
-
#
|
|
14
|
-
# MonkeyPatch ruby object to support DSL
|
|
15
|
-
#
|
|
16
|
-
module Ruby
|
|
17
|
-
#
|
|
18
|
-
# Extensions for Range Class to support DimmerItems
|
|
19
|
-
#
|
|
20
|
-
module RangeExtensions
|
|
21
|
-
java_import Java::OrgOpenhabCoreLibraryItems::DimmerItem
|
|
22
|
-
|
|
23
|
-
#
|
|
24
|
-
# Override range === method to support DimmerItems
|
|
25
|
-
#
|
|
26
|
-
# @param [Object] other object to compare for case equals
|
|
27
|
-
#
|
|
28
|
-
# @return [Boolean] if other is DimmerItem and state is covered by range,
|
|
29
|
-
# result from parent Range class if not DimmerItem
|
|
30
|
-
#
|
|
31
|
-
def ===(other)
|
|
32
|
-
return super unless other.is_a? DimmerItem
|
|
33
|
-
|
|
34
|
-
cover? other.state.to_i
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
#
|
|
43
|
-
# Prepend Range class with RangeExtensions
|
|
44
|
-
#
|
|
45
|
-
class Range
|
|
46
|
-
prepend OpenHAB::DSL::MonkeyPatch::Ruby::RangeExtensions
|
|
47
|
-
end
|