openhab-scripting 3.2.0 → 3.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0be2b09569b3f3e96c5a79dbec4c6b0be6616a46d013831650c1e5d60cee374f
4
- data.tar.gz: f71890c321e188f6322191e0c40099a6de22bc086d89085743b0c20f6b211649
3
+ metadata.gz: f309bd0e53d6ef4d8e738fb95507b79a3467e386b031a7e087c15502c20442cf
4
+ data.tar.gz: 0fbf71682ac4809b5d804f47ff3e847f8632d91a2749514c04abb6e7b5e02713
5
5
  SHA512:
6
- metadata.gz: ff1e955329247a70b39158dff4a3694f51e2974fb11ce18260ff7048b686ac1181a99c7e941299def6f77697cacdaabf8f67e72d5c8bc0193f3b4334df761f94
7
- data.tar.gz: db0fb2c35af26aaa963107b7971e83bfbfdd43c42d951cc470dec1192e8f743978ed91e04859953b5970e076516c275f7133784d12545a842e856181e76ef5af
6
+ metadata.gz: 7b323582ae4bb7e8e5d5501353f0021437fa53f4170e098f51d518f30103d6a4241474678f80d1330d1c6257de354e58633ad16ea3e5e928315a2235aba51389
7
+ data.tar.gz: 56ea9c5ec2b80d16c4f9ccdd08fbab04a45aee6460947de4df219c800e2120fa6f169290886984c25b942408ebc62c13913f03bd9a7e54aa52468e5b7e308c72
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'base64'
4
+ require 'pathname'
4
5
  require 'net/http'
5
6
  require 'java'
6
- require 'mimemagic'
7
+ require 'marcel'
7
8
  require 'openhab/dsl/items/item_command'
8
9
  require 'openhab/dsl/items/item_delegate'
9
10
 
@@ -57,7 +58,7 @@ module OpenHAB
57
58
  #
58
59
  def update_from_file(file, mime_type: nil)
59
60
  file_data = IO.binread(file)
60
- mime_type ||= (MimeMagic.by_path(file) || MimeMagic.by_magic(file_data))&.type
61
+ mime_type ||= Marcel::MimeType.for(Pathname.new(file)) || Marcel::MimeType.for(file_data)
61
62
  update_from_bytes(file_data, mime_type: mime_type)
62
63
  end
63
64
 
@@ -130,7 +131,7 @@ module OpenHAB
130
131
  #
131
132
  def detect_mime_from_bytes(bytes:)
132
133
  logger.trace('Detecting mime type from file image contents')
133
- MimeMagic.by_magic(bytes)&.type
134
+ Marcel::MimeType.for(bytes)
134
135
  end
135
136
  end
136
137
  end
@@ -30,7 +30,7 @@ module OpenHAB
30
30
 
31
31
  def initialize(metadata: nil, key: nil, value: nil, config: nil)
32
32
  @metadata = metadata || Metadata.new(key || MetadataKey.new('', ''), value&.to_s, config)
33
- super(@metadata&.configuration)
33
+ super(to_ruby(@metadata&.configuration))
34
34
  end
35
35
 
36
36
  #
@@ -85,6 +85,44 @@ module OpenHAB
85
85
  def to_a
86
86
  [@metadata&.value, @metadata&.configuration || {}]
87
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
88
126
  end
89
127
 
90
128
  #
@@ -9,6 +9,18 @@ module OpenHAB
9
9
  #
10
10
  module Persistence
11
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
+
12
24
  # All persistence methods that could return a Quantity
13
25
  QUANTITY_METHODS = %i[average_since
14
26
  delta_since
@@ -45,13 +57,18 @@ module OpenHAB
45
57
  #
46
58
  def previous_state(service = nil, skip_equal: false)
47
59
  service ||= persistence_service
48
- PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
60
+ result = PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
61
+ HistoricState.new(quantify(result.state), result.timestamp)
49
62
  end
50
63
 
51
64
  PERSISTENCE_METHODS.each do |method|
52
65
  define_method(method) do |timestamp, service = nil|
53
66
  service ||= persistence_service
54
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
+
55
72
  QUANTITY_METHODS.include?(method) ? quantify(result) : result
56
73
  end
57
74
  end
@@ -30,11 +30,8 @@ module OpenHAB
30
30
  dow: '?'
31
31
  }
32
32
  end
33
- # Rubocop check is disabled because YARD cannot process the method if placed in front of
34
- # the method definition
35
- # rubocop:disable Style/AccessModifierDeclarations
33
+
36
34
  module_function :cron_expression_map
37
- # rubocop:enable Style/AccessModifierDeclarations
38
35
 
39
36
  # @return [Hash] Map of days of the week from symbols to to OpenHAB cron strings
40
37
  DAY_OF_WEEK_MAP = {
@@ -11,6 +11,7 @@ module OpenHAB
11
11
  # Support for OpenHAB Things
12
12
  #
13
13
  module Things
14
+ java_import Java::OrgOpenhabCoreThing::ThingStatus
14
15
  include OpenHAB::Log
15
16
 
16
17
  #
@@ -25,6 +26,22 @@ module OpenHAB
25
26
  define_action_methods
26
27
  end
27
28
 
29
+ #
30
+ # Defines boolean thing status methods
31
+ # uninitialized?
32
+ # initializing?
33
+ # unknown?
34
+ # online?
35
+ # offline?
36
+ # removing?
37
+ # removed?
38
+ #
39
+ # @return [Boolean] true if the thing status matches the name
40
+ #
41
+ ThingStatus.constants.each do |thingstatus|
42
+ define_method("#{thingstatus.to_s.downcase}?") { status == ThingStatus.value_of(thingstatus) }
43
+ end
44
+
28
45
  private
29
46
 
30
47
  java_import 'org.openhab.core.automation.annotation.RuleAction'
@@ -22,9 +22,21 @@ module OpenHAB
22
22
  #
23
23
  # Regex for matching internal calls in a stack trace
24
24
  #
25
- INTERNAL_CALL_REGEX = %r{(openhab-scripting-.*/lib)|(org/jruby/)}.freeze
25
+ INTERNAL_CALL_REGEX = %r{(openhab-scripting-.*/lib)|org[./]jruby}.freeze
26
26
  private_constant :INTERNAL_CALL_REGEX
27
27
 
28
+ #
29
+ # Regex for matching internal calls in a java stack trace
30
+ #
31
+ EXCLUDED_JAVA_PACKAGES = /jdk\.internal\.reflect|java\.lang\.reflect|org\.openhab|java\.lang\.Thread\.run/.freeze
32
+ private_constant :EXCLUDED_JAVA_PACKAGES
33
+
34
+ #
35
+ # Regex for matching internal calls in a java stack trace
36
+ #
37
+ JAVA_INTERNAL_CALL_REGEX = Regexp.union(INTERNAL_CALL_REGEX, EXCLUDED_JAVA_PACKAGES).freeze
38
+ private_constant :JAVA_INTERNAL_CALL_REGEX
39
+
28
40
  #
29
41
  # Create a new logger
30
42
  #
@@ -60,8 +72,13 @@ module OpenHAB
60
72
  def clean_backtrace(error)
61
73
  return error if debug_enabled?
62
74
 
63
- backtrace = error.backtrace_locations.reject { |line| INTERNAL_CALL_REGEX.match? line.to_s }
64
- error.set_backtrace(backtrace.map(&:to_s))
75
+ if error.respond_to? :backtrace_locations
76
+ backtrace = error.backtrace_locations.map(&:to_s).reject { |line| INTERNAL_CALL_REGEX.match? line }
77
+ error.set_backtrace(backtrace)
78
+ elsif error.respond_to? :stack_trace
79
+ backtrace = error.stack_trace.reject { |line| JAVA_INTERNAL_CALL_REGEX.match? line.to_s }
80
+ error.set_stack_trace(backtrace)
81
+ end
65
82
  error
66
83
  end
67
84
 
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '3.2.0'
8
+ VERSION = '3.4.2'
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 3.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-10 00:00:00.000000000 Z
11
+ date: 2021-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -25,19 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: mimemagic
28
+ name: marcel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.3'
33
+ version: '1.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.3'
40
+ version: '1.0'
41
41
  description: JRuby Helper Libraries for OpenHAB Scripting
42
42
  email:
43
43
  - broconne@gmail.com