openhab-scripting 2.16.2 → 2.16.3
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 +162 -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 +47 -0
- data/lib/openhab/{core/dsl → dsl}/gems.rb +0 -1
- data/lib/openhab/dsl/group.rb +100 -0
- data/lib/openhab/dsl/items/items.rb +46 -0
- data/lib/openhab/dsl/items/number_item.rb +352 -0
- data/lib/openhab/dsl/items/string_item.rb +120 -0
- data/lib/openhab/dsl/monkey_patch/actions/actions.rb +4 -0
- data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +32 -0
- data/lib/openhab/dsl/monkey_patch/events/events.rb +5 -0
- data/lib/openhab/dsl/monkey_patch/events/item_command.rb +23 -0
- data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +35 -0
- data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +33 -0
- data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +61 -0
- data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +193 -0
- data/lib/openhab/dsl/monkey_patch/items/group_item.rb +37 -0
- data/lib/openhab/dsl/monkey_patch/items/items.rb +133 -0
- data/lib/openhab/dsl/monkey_patch/items/metadata.rb +281 -0
- data/lib/openhab/dsl/monkey_patch/items/persistence.rb +70 -0
- data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +95 -0
- data/lib/openhab/dsl/monkey_patch/ruby/number.rb +39 -0
- data/lib/openhab/dsl/monkey_patch/ruby/range.rb +47 -0
- data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +7 -0
- data/lib/openhab/dsl/monkey_patch/ruby/string.rb +41 -0
- data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +70 -0
- data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +51 -0
- data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +36 -0
- data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +32 -0
- data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +69 -0
- data/lib/openhab/dsl/monkey_patch/types/types.rb +8 -0
- data/lib/openhab/dsl/persistence.rb +25 -0
- data/lib/openhab/dsl/rules/automation_rule.rb +342 -0
- data/lib/openhab/dsl/rules/guard.rb +134 -0
- data/lib/openhab/dsl/rules/property.rb +102 -0
- data/lib/openhab/dsl/rules/rule.rb +116 -0
- data/lib/openhab/dsl/rules/rule_config.rb +151 -0
- data/lib/openhab/dsl/rules/triggers/changed.rb +143 -0
- data/lib/openhab/dsl/rules/triggers/channel.rb +53 -0
- data/lib/openhab/dsl/rules/triggers/command.rb +104 -0
- data/lib/openhab/dsl/rules/triggers/cron.rb +177 -0
- data/lib/openhab/dsl/rules/triggers/trigger.rb +124 -0
- data/lib/openhab/dsl/rules/triggers/updated.rb +98 -0
- data/lib/openhab/dsl/states.rb +61 -0
- data/lib/openhab/dsl/things.rb +91 -0
- data/lib/openhab/dsl/time_of_day.rb +228 -0
- data/lib/openhab/dsl/timers.rb +77 -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 +55 -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
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
%w[persist last_update].each do |method|
|
|
12
|
+
define_method(method) do |service = nil|
|
|
13
|
+
service ||= persistence_service
|
|
14
|
+
PersistenceExtensions.public_send(method, self, service&.to_s)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
#
|
|
19
|
+
# Return the previous state of the item
|
|
20
|
+
#
|
|
21
|
+
# @param skip_equal [Boolean] if true, skips equal state values and
|
|
22
|
+
# searches the first state not equal the current state
|
|
23
|
+
# @param service [String] the name of the PersistenceService to use
|
|
24
|
+
#
|
|
25
|
+
# @return the previous state or nil if no previous state could be found,
|
|
26
|
+
# or if the default persistence service is not configured or
|
|
27
|
+
# does not refer to a valid service
|
|
28
|
+
#
|
|
29
|
+
def previous_state(service = nil, skip_equal: false)
|
|
30
|
+
service ||= persistence_service
|
|
31
|
+
PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
%w[
|
|
35
|
+
average_since
|
|
36
|
+
changed_since
|
|
37
|
+
delta_since
|
|
38
|
+
deviation_since
|
|
39
|
+
evolution_rate
|
|
40
|
+
historic_state
|
|
41
|
+
maximum_since
|
|
42
|
+
minimum_since
|
|
43
|
+
sum_since
|
|
44
|
+
updated_since
|
|
45
|
+
variance_since
|
|
46
|
+
].each do |method|
|
|
47
|
+
define_method(method) do |timestamp, service = nil|
|
|
48
|
+
service ||= persistence_service
|
|
49
|
+
if timestamp.is_a? Java::JavaTimeTemporal::TemporalAmount
|
|
50
|
+
timestamp = Java::JavaTime::ZonedDateTime.now.minus(timestamp)
|
|
51
|
+
end
|
|
52
|
+
PersistenceExtensions.public_send(method, self, timestamp, service&.to_s)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
#
|
|
59
|
+
# Get the specified persistence service from the current thread local variable
|
|
60
|
+
#
|
|
61
|
+
# @return [Object] Persistence service name as String or Symbol, or nil if not set
|
|
62
|
+
#
|
|
63
|
+
def persistence_service
|
|
64
|
+
Thread.current.thread_variable_get(:persistence_service)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OpenHAB
|
|
4
|
+
module DSL
|
|
5
|
+
module MonkeyPatch
|
|
6
|
+
#
|
|
7
|
+
# Patches OpenHAB items
|
|
8
|
+
#
|
|
9
|
+
module Items
|
|
10
|
+
java_import Java::OrgOpenhabCoreLibraryItems::SwitchItem
|
|
11
|
+
|
|
12
|
+
# Alias class names for easy is_a? comparisons
|
|
13
|
+
::Switch = SwitchItem
|
|
14
|
+
|
|
15
|
+
#
|
|
16
|
+
# Monkeypatching SwitchItem to add Ruby Support methods
|
|
17
|
+
#
|
|
18
|
+
class SwitchItem
|
|
19
|
+
java_import Java::OrgOpenhabCoreLibraryTypes::OnOffType
|
|
20
|
+
#
|
|
21
|
+
# Send the OFF command to the switch
|
|
22
|
+
#
|
|
23
|
+
#
|
|
24
|
+
def off
|
|
25
|
+
command(OnOffType::OFF)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
#
|
|
29
|
+
# Send the OFF command to the switch
|
|
30
|
+
#
|
|
31
|
+
#
|
|
32
|
+
def on
|
|
33
|
+
command(OnOffType::ON)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
#
|
|
37
|
+
# Check if a switch is on
|
|
38
|
+
#
|
|
39
|
+
# @return [Boolean] True if the switch is on, false otherwise
|
|
40
|
+
#
|
|
41
|
+
def on?
|
|
42
|
+
state? && state == OnOffType::ON
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
alias truthy? on?
|
|
46
|
+
|
|
47
|
+
#
|
|
48
|
+
# Check if a switch is off
|
|
49
|
+
#
|
|
50
|
+
# @return [Boolean] True if the switch is off, false otherwise
|
|
51
|
+
#
|
|
52
|
+
def off?
|
|
53
|
+
state? && state == OnOffType::OFF
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
#
|
|
57
|
+
# Send a command to invert the state of the switch
|
|
58
|
+
#
|
|
59
|
+
# @return [OnOffType] Inverted state
|
|
60
|
+
#
|
|
61
|
+
def toggle
|
|
62
|
+
self << !self
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
#
|
|
66
|
+
# Return the inverted state of the switch: ON if the switch is OFF, UNDEF or NULL; OFF if the switch is ON
|
|
67
|
+
#
|
|
68
|
+
# @return [OnOffType] Inverted state
|
|
69
|
+
#
|
|
70
|
+
def !
|
|
71
|
+
return !state if state?
|
|
72
|
+
|
|
73
|
+
OnOffType::ON
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
#
|
|
77
|
+
# Check for equality against supplied object
|
|
78
|
+
#
|
|
79
|
+
# @param [Object] other object to compare to
|
|
80
|
+
#
|
|
81
|
+
# @return [Boolean] True if other is a OnOffType and other equals state for this switch item,
|
|
82
|
+
# otherwise result from super
|
|
83
|
+
#
|
|
84
|
+
def ==(other)
|
|
85
|
+
if other.is_a? OnOffType
|
|
86
|
+
state? && state == other
|
|
87
|
+
else
|
|
88
|
+
super
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OpenHAB
|
|
4
|
+
module DSL
|
|
5
|
+
module MonkeyPatch
|
|
6
|
+
module Ruby
|
|
7
|
+
#
|
|
8
|
+
# Extend integer to create duration object
|
|
9
|
+
#
|
|
10
|
+
module IntegerExtensions
|
|
11
|
+
include OpenHAB::Core
|
|
12
|
+
|
|
13
|
+
#
|
|
14
|
+
# Create Duration with the specified unit
|
|
15
|
+
#
|
|
16
|
+
# @return [Java::JavaTime::Duration] Duration with number of units from self
|
|
17
|
+
#
|
|
18
|
+
%w[millis seconds minutes hours].each do |unit|
|
|
19
|
+
define_method(unit) { Java::JavaTime::Duration.public_send("of_#{unit}", self) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
alias second seconds
|
|
23
|
+
alias millisecond millis
|
|
24
|
+
alias milliseconds millis
|
|
25
|
+
alias ms millis
|
|
26
|
+
alias minute minutes
|
|
27
|
+
alias hour hours
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
#
|
|
35
|
+
# Prepend Integer class with duration extensions
|
|
36
|
+
#
|
|
37
|
+
class Integer
|
|
38
|
+
prepend OpenHAB::DSL::MonkeyPatch::Ruby::IntegerExtensions
|
|
39
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'openhab/dsl/types/quantity'
|
|
4
|
+
|
|
5
|
+
module OpenHAB
|
|
6
|
+
module DSL
|
|
7
|
+
module MonkeyPatch
|
|
8
|
+
module Ruby
|
|
9
|
+
#
|
|
10
|
+
# Extend String class
|
|
11
|
+
#
|
|
12
|
+
module StringExtensions
|
|
13
|
+
include OpenHAB::Core
|
|
14
|
+
|
|
15
|
+
#
|
|
16
|
+
# Compares String to another object
|
|
17
|
+
#
|
|
18
|
+
# @param [Object] other object to compare to
|
|
19
|
+
#
|
|
20
|
+
# @return [Boolean] true if the two objects contain the same value, false otherwise
|
|
21
|
+
#
|
|
22
|
+
def ==(other)
|
|
23
|
+
case other
|
|
24
|
+
when OpenHAB::DSL::Types::Quantity, QuantityType
|
|
25
|
+
other == self
|
|
26
|
+
else
|
|
27
|
+
super
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
#
|
|
37
|
+
# Prepend String class with comparison extensions
|
|
38
|
+
#
|
|
39
|
+
class String
|
|
40
|
+
prepend OpenHAB::DSL::MonkeyPatch::Ruby::StringExtensions
|
|
41
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'java'
|
|
4
|
+
|
|
5
|
+
module OpenHAB
|
|
6
|
+
module DSL
|
|
7
|
+
module MonkeyPatch
|
|
8
|
+
#
|
|
9
|
+
# Patches OpenHAB types
|
|
10
|
+
#
|
|
11
|
+
module Types
|
|
12
|
+
java_import Java::OrgOpenhabCoreLibraryTypes::DecimalType
|
|
13
|
+
|
|
14
|
+
#
|
|
15
|
+
# MonkeyPatching Decimal Type
|
|
16
|
+
#
|
|
17
|
+
class DecimalType
|
|
18
|
+
#
|
|
19
|
+
# @param [Object] other object to compare to
|
|
20
|
+
#
|
|
21
|
+
# @return [Integer] -1,0,1 or nil depending on value supplied,
|
|
22
|
+
# nil comparison to supplied object is not possible.
|
|
23
|
+
#
|
|
24
|
+
def <=>(other)
|
|
25
|
+
logger.trace("#{self.class} #{self} <=> #{other} (#{other.class})")
|
|
26
|
+
case other
|
|
27
|
+
when Numeric
|
|
28
|
+
to_big_decimal.compare_to(other.to_d)
|
|
29
|
+
when Java::OrgOpenhabCoreTypes::UnDefType
|
|
30
|
+
1
|
|
31
|
+
else
|
|
32
|
+
other = other.state if other.respond_to? :state
|
|
33
|
+
compare_to(other)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
#
|
|
38
|
+
# Coerce objects into a DecimalType
|
|
39
|
+
#
|
|
40
|
+
# @param [Object] other object to coerce to a DecimalType if possible
|
|
41
|
+
#
|
|
42
|
+
# @return [Object] Numeric when applicable
|
|
43
|
+
#
|
|
44
|
+
def coerce(other)
|
|
45
|
+
logger.trace("Coercing #{self} as a request from #{other.class}")
|
|
46
|
+
case other
|
|
47
|
+
when Numeric
|
|
48
|
+
[other.to_d, to_big_decimal]
|
|
49
|
+
else
|
|
50
|
+
[other, self]
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
#
|
|
55
|
+
# Compare self to other through the spaceship operator
|
|
56
|
+
# Compare self to other using Java BigDecimal compare method
|
|
57
|
+
#
|
|
58
|
+
# @param [Object] other object to compare to
|
|
59
|
+
#
|
|
60
|
+
# @return [Boolean] True if have the same BigDecimal representation, false otherwise
|
|
61
|
+
#
|
|
62
|
+
def ==(other)
|
|
63
|
+
logger.trace("#{self.class} #{self} == #{other} (#{other.class})")
|
|
64
|
+
(self <=> other).zero?
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'java'
|
|
4
|
+
|
|
5
|
+
module OpenHAB
|
|
6
|
+
module DSL
|
|
7
|
+
module MonkeyPatch
|
|
8
|
+
#
|
|
9
|
+
# Patches OpenHAB types
|
|
10
|
+
#
|
|
11
|
+
module Types
|
|
12
|
+
java_import Java::OrgOpenhabCoreLibraryTypes::OnOffType
|
|
13
|
+
|
|
14
|
+
#
|
|
15
|
+
# Monkey patching OnOffType
|
|
16
|
+
#
|
|
17
|
+
class OnOffType
|
|
18
|
+
#
|
|
19
|
+
# Invert the type
|
|
20
|
+
#
|
|
21
|
+
# @return [Java::OrgOpenhabCoreLibraryTypes::OnOffType] OFF if ON, ON if OFF
|
|
22
|
+
#
|
|
23
|
+
def !
|
|
24
|
+
return OFF if self == ON
|
|
25
|
+
return ON if self == OFF
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Check if the supplied object is case equals to self
|
|
29
|
+
#
|
|
30
|
+
# @param [Object] other object to compare
|
|
31
|
+
#
|
|
32
|
+
# @return [Boolean] True if the other object responds to on?/off? and is in the same state as this object,
|
|
33
|
+
# nil if object cannot be compared
|
|
34
|
+
#
|
|
35
|
+
def ===(other)
|
|
36
|
+
# A case statement here causes and infinite loop
|
|
37
|
+
# rubocop:disable Style/CaseLikeIf
|
|
38
|
+
if self == ON
|
|
39
|
+
other.on? if other.respond_to? :on?
|
|
40
|
+
elsif self == OFF
|
|
41
|
+
other.off? if other.respond_to?('off?')
|
|
42
|
+
else
|
|
43
|
+
false
|
|
44
|
+
end
|
|
45
|
+
# rubocop:enable Style/CaseLikeIf
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'java'
|
|
4
|
+
|
|
5
|
+
module OpenHAB
|
|
6
|
+
module DSL
|
|
7
|
+
module MonkeyPatch
|
|
8
|
+
#
|
|
9
|
+
# Patches OpenHAB types
|
|
10
|
+
#
|
|
11
|
+
module Types
|
|
12
|
+
java_import Java::OrgOpenhabCoreLibraryTypes::OpenClosedType
|
|
13
|
+
|
|
14
|
+
#
|
|
15
|
+
# Monkey patch for DSL use
|
|
16
|
+
#
|
|
17
|
+
class OpenClosedType
|
|
18
|
+
java_import Java::OrgOpenhabCoreLibraryItems::ContactItem
|
|
19
|
+
|
|
20
|
+
#
|
|
21
|
+
# Check if the supplied object is case equals to self
|
|
22
|
+
#
|
|
23
|
+
# @param [Object] other object to compare
|
|
24
|
+
#
|
|
25
|
+
# @return [Boolean] True if the other object is a ContactItem and has the same state
|
|
26
|
+
#
|
|
27
|
+
def ===(other)
|
|
28
|
+
super unless other.is_a? ContactItem
|
|
29
|
+
|
|
30
|
+
self == other.state
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|