openhab-scripting 4.1.4 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/openhab/core/entity_lookup.rb +1 -57
- data/lib/openhab/dsl/dsl.rb +6 -12
- data/lib/openhab/dsl/group.rb +1 -5
- 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/generic_item.rb +197 -0
- data/lib/openhab/dsl/items/group_item.rb +56 -92
- data/lib/openhab/dsl/items/image_item.rb +5 -41
- data/lib/openhab/dsl/items/item_registry.rb +49 -0
- data/lib/openhab/dsl/items/items.rb +71 -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 +66 -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/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/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 +68 -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 +77 -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 +31 -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,140 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
require 'openhab/dsl/items/item_command'
|
5
|
-
require 'forwardable'
|
6
|
-
|
7
|
-
module OpenHAB
|
8
|
-
module DSL
|
9
|
-
module MonkeyPatch
|
10
|
-
#
|
11
|
-
# Patches OpenHAB items
|
12
|
-
#
|
13
|
-
module Items
|
14
|
-
java_import Java::OrgOpenhabCoreLibraryItems::DimmerItem
|
15
|
-
java_import Java::OrgOpenhabCoreItems::GenericItem
|
16
|
-
|
17
|
-
#
|
18
|
-
# Alias class for is_a? testing
|
19
|
-
#
|
20
|
-
::Dimmer = DimmerItem
|
21
|
-
|
22
|
-
#
|
23
|
-
# Monkey Patch DimmerItem
|
24
|
-
#
|
25
|
-
class DimmerItem
|
26
|
-
include Comparable
|
27
|
-
java_import Java::OrgOpenhabCoreLibraryTypes::DecimalType
|
28
|
-
java_import Java::OrgOpenhabCoreLibraryTypes::IncreaseDecreaseType
|
29
|
-
|
30
|
-
extend Forwardable
|
31
|
-
extend OpenHAB::DSL::Items::ItemCommand
|
32
|
-
|
33
|
-
item_type Java::OrgOpenhabCoreLibraryItems::DimmerItem
|
34
|
-
|
35
|
-
def_delegator :state, :to_s
|
36
|
-
|
37
|
-
#
|
38
|
-
# Dim the dimmer
|
39
|
-
#
|
40
|
-
# @param [Integer] amount to dim by
|
41
|
-
#
|
42
|
-
# @return [Integer] level target for dimmer
|
43
|
-
#
|
44
|
-
def dim(amount = 1)
|
45
|
-
[state&.to_big_decimal&.intValue&.-(amount), 0].compact
|
46
|
-
.max
|
47
|
-
.tap { |target| command(target) }
|
48
|
-
end
|
49
|
-
|
50
|
-
#
|
51
|
-
# Brighten the dimmer
|
52
|
-
#
|
53
|
-
# @param [Integer] amount to brighten by
|
54
|
-
#
|
55
|
-
# @return [Integer] level target for dimmer
|
56
|
-
#
|
57
|
-
def brighten(amount = 1)
|
58
|
-
state&.to_big_decimal&.intValue&.+(amount)&.tap { |target| command(target) }
|
59
|
-
end
|
60
|
-
|
61
|
-
#
|
62
|
-
# Compare DimmerItem to supplied object
|
63
|
-
#
|
64
|
-
# @param [Object] other object to compare to
|
65
|
-
#
|
66
|
-
# @return [Integer] -1,0,1 or nil depending on value supplied,
|
67
|
-
# nil comparison to supplied object is not possible.
|
68
|
-
#
|
69
|
-
def <=>(other)
|
70
|
-
logger.trace("Comparing #{self} to #{other}")
|
71
|
-
case other
|
72
|
-
when GenericItem, NumberItem then state <=> other.state
|
73
|
-
when DecimalType then state <=> other
|
74
|
-
when Numeric then state.to_big_decimal.to_d <=> other.to_d
|
75
|
-
else compare_to(other)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
#
|
80
|
-
# Coerce objects into a DimmerItem
|
81
|
-
#
|
82
|
-
# @param [Object] other object to coerce to a DimmerItem if possible
|
83
|
-
#
|
84
|
-
# @return [Object] Numeric when applicable
|
85
|
-
#
|
86
|
-
def coerce(other)
|
87
|
-
logger.trace("Coercing #{self} as a request from #{other.class}")
|
88
|
-
case other
|
89
|
-
when Numeric then [other, state.to_big_decimal.to_d]
|
90
|
-
else [other, state]
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
#
|
95
|
-
# Compare DimmerItem to supplied object.
|
96
|
-
# The == operator needs to be overridden because the parent java object
|
97
|
-
# has .equals which overrides the <=> operator above
|
98
|
-
#
|
99
|
-
# @param [Object] other object to compare to
|
100
|
-
#
|
101
|
-
# @return [Integer] true if the two objects contain the same value, false otherwise
|
102
|
-
#
|
103
|
-
def ==(other)
|
104
|
-
(self <=> other).zero?
|
105
|
-
end
|
106
|
-
|
107
|
-
#
|
108
|
-
# Define math operations
|
109
|
-
#
|
110
|
-
%i[+ - / *].each do |operator|
|
111
|
-
define_method(operator) do |other|
|
112
|
-
right, left = coerce(other)
|
113
|
-
left.send(operator, right)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
#
|
118
|
-
# Check if dimmer has a state and state is not zero
|
119
|
-
#
|
120
|
-
# @return [Boolean] True if dimmer is not NULL or UNDEF and value is not 0
|
121
|
-
#
|
122
|
-
def truthy?
|
123
|
-
state? && state != DecimalType::ZERO
|
124
|
-
end
|
125
|
-
|
126
|
-
#
|
127
|
-
# Value of dimmer
|
128
|
-
#
|
129
|
-
# @return [Integer] Value of dimmer or nil if state is UNDEF or NULL
|
130
|
-
#
|
131
|
-
def to_i
|
132
|
-
state&.to_big_decimal&.intValue
|
133
|
-
end
|
134
|
-
|
135
|
-
alias to_int to_i
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
@@ -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
|