openhab-scripting 5.6.0 → 5.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,16 +26,15 @@ module OpenHAB
26
26
  # @return [Hash] Map with symbols for :seconds, :minute, :hour, :dom, :month, :dow
27
27
  # configured to fire every second
28
28
  #
29
- CRON_EXPRESSION_MAP =
30
- {
31
- second: "*",
32
- minute: "*",
33
- hour: "*",
34
- dom: "?",
35
- month: "*",
36
- dow: "?",
37
- year: "*"
38
- }.freeze
29
+ CRON_EXPRESSION_MAP = {
30
+ second: "*",
31
+ minute: "*",
32
+ hour: "*",
33
+ dom: "?",
34
+ month: "*",
35
+ dow: "?",
36
+ year: "*"
37
+ }.freeze
39
38
  private_constant :CRON_EXPRESSION_MAP
40
39
 
41
40
  # @return [Hash] Map of days of the week from symbols to to openHAB cron strings
@@ -52,7 +51,6 @@ module OpenHAB
52
51
 
53
52
  # @return [Hash] Converts the DAY_OF_WEEK_MAP to map used by Cron Expression
54
53
  DAY_OF_WEEK_EXPRESSION_MAP = DAY_OF_WEEK_MAP.transform_values { |v| CRON_EXPRESSION_MAP.merge(dow: v) }
55
-
56
54
  private_constant :DAY_OF_WEEK_EXPRESSION_MAP
57
55
 
58
56
  # @return [Hash] Create a set of cron expressions based on different time intervals
@@ -65,9 +63,12 @@ module OpenHAB
65
63
  month: CRON_EXPRESSION_MAP.merge(second: "0", minute: "0", hour: "0", dom: "1"),
66
64
  year: CRON_EXPRESSION_MAP.merge(second: "0", minute: "0", hour: "0", dom: "1", month: "1")
67
65
  }.merge(DAY_OF_WEEK_EXPRESSION_MAP).freeze
68
-
69
66
  private_constant :EXPRESSION_MAP
70
67
 
68
+ # @return [Hash] Translate cron field names to expression keys
69
+ FIELD_TO_EXPRESSION_KEY = Hash.new { |_, key| key }.merge({ dow: :week, dom: :day })
70
+ private_constant :FIELD_TO_EXPRESSION_KEY
71
+
71
72
  #
72
73
  # Create a cron map from a duration
73
74
  #
@@ -124,10 +125,28 @@ module OpenHAB
124
125
  "unknown keyword#{"s" if extra_fields.size > 1}: #{extra_fields.map(&:inspect).join(", ")}"
125
126
  end
126
127
 
127
- fields = fields.transform_values { |value| value.to_s.delete(" ") }
128
+ fields = fields.to_h do |key, value|
129
+ if value.is_a?(Range)
130
+ if value.exclude_end?
131
+ raise ArgumentError,
132
+ "Range must be inclusive for '#{key}'. Try '#{value.begin}..#{value.end}' instead"
133
+ end
134
+
135
+ unless value.begin && value.end
136
+ raise ArgumentError,
137
+ "Range must have a beginning and ending for '#{key}'"
138
+ end
139
+
140
+ [key, "#{value.begin.to_s.upcase}-#{value.end.to_s.upcase}".delete(" ")]
141
+ else
142
+ [key, value.to_s.delete(" ").upcase]
143
+ end
144
+ end
145
+ # convert fields' key dow->week, dom->day to look into EXPRESSION_MAP
146
+ fields_expression = fields.transform_keys { |key| FIELD_TO_EXPRESSION_KEY[key] }
128
147
  # find the first expression map that has a field from fields.
129
148
  # this ensure more-specific fields get set to 0, not *
130
- base_key = EXPRESSION_MAP.keys.find { |field, _| fields.key?(field) }
149
+ base_key = EXPRESSION_MAP.keys.find { |field, _| fields_expression.key?(field) }
131
150
  base_expression = EXPRESSION_MAP[base_key]
132
151
  expression_map = base_expression.merge(fields)
133
152