openhab-scripting 4.47.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (281) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/actions/audio.rb +47 -0
  3. data/lib/openhab/core/actions/ephemeris.rb +39 -0
  4. data/lib/openhab/core/actions/exec.rb +51 -0
  5. data/lib/openhab/core/actions/http.rb +80 -0
  6. data/lib/openhab/core/actions/ping.rb +30 -0
  7. data/lib/openhab/core/actions/transformation.rb +32 -0
  8. data/lib/openhab/core/actions/voice.rb +36 -0
  9. data/lib/openhab/core/actions.rb +82 -0
  10. data/lib/openhab/core/dependency_tracking.rb +34 -0
  11. data/lib/openhab/core/dto/item_channel_link.rb +33 -0
  12. data/lib/openhab/core/dto/thing.rb +27 -0
  13. data/lib/openhab/core/dto.rb +11 -0
  14. data/lib/openhab/core/entity_lookup.rb +152 -70
  15. data/lib/openhab/core/events/abstract_event.rb +18 -0
  16. data/lib/openhab/core/events/abstract_item_registry_event.rb +36 -0
  17. data/lib/openhab/core/events/abstract_thing_registry_event.rb +40 -0
  18. data/lib/openhab/core/events/item_command_event.rb +78 -0
  19. data/lib/openhab/core/events/item_event.rb +22 -0
  20. data/lib/openhab/core/events/item_state_changed_event.rb +75 -0
  21. data/lib/openhab/core/events/item_state_event.rb +79 -0
  22. data/lib/openhab/core/events/thing_status_info_event.rb +55 -0
  23. data/lib/openhab/core/events.rb +10 -0
  24. data/lib/openhab/core/items/accepted_data_types.rb +29 -0
  25. data/lib/openhab/core/items/color_item.rb +52 -0
  26. data/lib/openhab/core/items/contact_item.rb +52 -0
  27. data/lib/openhab/core/items/date_time_item.rb +59 -0
  28. data/lib/openhab/core/items/dimmer_item.rb +148 -0
  29. data/lib/openhab/core/items/generic_item.rb +292 -0
  30. data/lib/openhab/core/items/group_item.rb +176 -0
  31. data/lib/openhab/{dsl → core}/items/image_item.rb +35 -29
  32. data/lib/openhab/core/items/item.rb +273 -0
  33. data/lib/openhab/core/items/location_item.rb +34 -0
  34. data/lib/openhab/core/items/metadata/hash.rb +433 -0
  35. data/lib/openhab/core/items/metadata/namespace_hash.rb +475 -0
  36. data/lib/openhab/core/items/metadata/provider.rb +48 -0
  37. data/lib/openhab/core/items/metadata.rb +11 -0
  38. data/lib/openhab/core/items/number_item.rb +62 -0
  39. data/lib/openhab/core/items/numeric_item.rb +22 -0
  40. data/lib/openhab/core/items/persistence.rb +416 -0
  41. data/lib/openhab/core/items/player_item.rb +66 -0
  42. data/lib/openhab/core/items/provider.rb +44 -0
  43. data/lib/openhab/core/items/proxy.rb +136 -0
  44. data/lib/openhab/core/items/registry.rb +86 -0
  45. data/lib/openhab/core/items/rollershutter_item.rb +68 -0
  46. data/lib/openhab/core/items/semantics/enumerable.rb +177 -0
  47. data/lib/openhab/core/items/semantics.rb +473 -0
  48. data/lib/openhab/core/items/state_storage.rb +53 -0
  49. data/lib/openhab/core/items/string_item.rb +28 -0
  50. data/lib/openhab/core/items/switch_item.rb +78 -0
  51. data/lib/openhab/core/items.rb +108 -0
  52. data/lib/openhab/{dsl → core}/lazy_array.rb +9 -3
  53. data/lib/openhab/core/profile_factory.rb +132 -0
  54. data/lib/openhab/core/provider.rb +230 -0
  55. data/lib/openhab/core/proxy.rb +130 -0
  56. data/lib/openhab/core/registry.rb +40 -0
  57. data/lib/openhab/core/rules/module.rb +26 -0
  58. data/lib/openhab/core/rules/provider.rb +25 -0
  59. data/lib/openhab/core/rules/registry.rb +76 -0
  60. data/lib/openhab/core/rules/rule.rb +150 -0
  61. data/lib/openhab/core/rules.rb +25 -0
  62. data/lib/openhab/core/script_handling.rb +78 -20
  63. data/lib/openhab/core/things/channel.rb +48 -0
  64. data/lib/openhab/core/things/channel_uid.rb +51 -0
  65. data/lib/openhab/core/things/item_channel_link.rb +33 -0
  66. data/lib/openhab/core/things/links/provider.rb +78 -0
  67. data/lib/openhab/core/things/profile_callback.rb +52 -0
  68. data/lib/openhab/core/things/provider.rb +29 -0
  69. data/lib/openhab/core/things/proxy.rb +87 -0
  70. data/lib/openhab/core/things/registry.rb +73 -0
  71. data/lib/openhab/core/things/thing.rb +194 -0
  72. data/lib/openhab/core/things.rb +22 -0
  73. data/lib/openhab/core/timer.rb +148 -0
  74. data/lib/openhab/{dsl → core}/types/comparable_type.rb +5 -3
  75. data/lib/openhab/{dsl → core}/types/date_time_type.rb +55 -127
  76. data/lib/openhab/{dsl → core}/types/decimal_type.rb +50 -48
  77. data/lib/openhab/{dsl → core}/types/hsb_type.rb +35 -83
  78. data/lib/openhab/core/types/increase_decrease_type.rb +34 -0
  79. data/lib/openhab/core/types/next_previous_type.rb +34 -0
  80. data/lib/openhab/{dsl → core}/types/numeric_type.rb +20 -7
  81. data/lib/openhab/core/types/on_off_type.rb +46 -0
  82. data/lib/openhab/core/types/open_closed_type.rb +41 -0
  83. data/lib/openhab/{dsl → core}/types/percent_type.rb +19 -20
  84. data/lib/openhab/core/types/play_pause_type.rb +38 -0
  85. data/lib/openhab/core/types/point_type.rb +117 -0
  86. data/lib/openhab/core/types/quantity_type.rb +325 -0
  87. data/lib/openhab/core/types/raw_type.rb +26 -0
  88. data/lib/openhab/core/types/refresh_type.rb +27 -0
  89. data/lib/openhab/core/types/rewind_fastforward_type.rb +38 -0
  90. data/lib/openhab/core/types/stop_move_type.rb +34 -0
  91. data/lib/openhab/{dsl → core}/types/string_type.rb +17 -28
  92. data/lib/openhab/{dsl → core}/types/type.rb +42 -40
  93. data/lib/openhab/core/types/un_def_type.rb +38 -0
  94. data/lib/openhab/core/types/up_down_type.rb +50 -0
  95. data/lib/openhab/core/types.rb +82 -0
  96. data/lib/openhab/{dsl → core}/uid.rb +4 -23
  97. data/lib/openhab/core/value_cache.rb +188 -0
  98. data/lib/openhab/core.rb +98 -0
  99. data/lib/openhab/core_ext/between.rb +32 -0
  100. data/lib/openhab/core_ext/ephemeris.rb +53 -0
  101. data/lib/openhab/core_ext/java/class.rb +34 -0
  102. data/lib/openhab/core_ext/java/duration.rb +142 -0
  103. data/lib/openhab/core_ext/java/list.rb +436 -0
  104. data/lib/openhab/core_ext/java/local_date.rb +104 -0
  105. data/lib/openhab/core_ext/java/local_time.rb +118 -0
  106. data/lib/openhab/core_ext/java/map.rb +66 -0
  107. data/lib/openhab/core_ext/java/month.rb +71 -0
  108. data/lib/openhab/core_ext/java/month_day.rb +119 -0
  109. data/lib/openhab/core_ext/java/period.rb +103 -0
  110. data/lib/openhab/core_ext/java/temporal_amount.rb +34 -0
  111. data/lib/openhab/core_ext/java/time.rb +62 -0
  112. data/lib/openhab/core_ext/java/unit.rb +15 -0
  113. data/lib/openhab/core_ext/java/zoned_date_time.rb +213 -0
  114. data/lib/openhab/core_ext/ruby/array.rb +21 -0
  115. data/lib/openhab/core_ext/ruby/date.rb +96 -0
  116. data/lib/openhab/core_ext/ruby/date_time.rb +55 -0
  117. data/lib/openhab/core_ext/ruby/module.rb +15 -0
  118. data/lib/openhab/core_ext/ruby/numeric.rb +195 -0
  119. data/lib/openhab/core_ext/ruby/range.rb +70 -0
  120. data/lib/openhab/core_ext/ruby/symbol.rb +7 -0
  121. data/lib/openhab/core_ext/ruby/time.rb +108 -0
  122. data/lib/openhab/core_ext.rb +18 -0
  123. data/lib/openhab/dsl/debouncer.rb +259 -0
  124. data/lib/openhab/dsl/events/watch_event.rb +18 -0
  125. data/lib/openhab/dsl/events.rb +9 -0
  126. data/lib/openhab/dsl/gems.rb +1 -1
  127. data/lib/openhab/dsl/items/builder.rb +578 -0
  128. data/lib/openhab/dsl/items/ensure.rb +73 -82
  129. data/lib/openhab/dsl/items/timed_command.rb +214 -159
  130. data/lib/openhab/dsl/rules/automation_rule.rb +126 -115
  131. data/lib/openhab/dsl/rules/builder.rb +1935 -0
  132. data/lib/openhab/dsl/rules/guard.rb +51 -114
  133. data/lib/openhab/dsl/rules/name_inference.rb +66 -25
  134. data/lib/openhab/dsl/rules/property.rb +48 -75
  135. data/lib/openhab/dsl/rules/rule_triggers.rb +22 -27
  136. data/lib/openhab/dsl/rules/terse.rb +58 -14
  137. data/lib/openhab/dsl/rules/triggers/changed.rb +48 -94
  138. data/lib/openhab/dsl/rules/triggers/channel.rb +9 -40
  139. data/lib/openhab/dsl/rules/triggers/command.rb +14 -63
  140. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +34 -69
  141. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +6 -14
  142. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +48 -82
  143. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +30 -47
  144. data/lib/openhab/dsl/rules/triggers/trigger.rb +7 -28
  145. data/lib/openhab/dsl/rules/triggers/updated.rb +21 -45
  146. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +257 -102
  147. data/lib/openhab/dsl/rules/triggers.rb +12 -0
  148. data/lib/openhab/dsl/rules.rb +8 -0
  149. data/lib/openhab/dsl/things/builder.rb +299 -0
  150. data/lib/openhab/{core → dsl}/thread_local.rb +27 -17
  151. data/lib/openhab/dsl/timer_manager.rb +204 -0
  152. data/lib/openhab/dsl/version.rb +9 -0
  153. data/lib/openhab/dsl.rb +979 -0
  154. data/lib/openhab/log.rb +355 -0
  155. data/lib/openhab/osgi.rb +68 -0
  156. data/lib/openhab/rspec/configuration.rb +56 -0
  157. data/lib/openhab/rspec/example_group.rb +132 -0
  158. data/lib/openhab/rspec/helpers.rb +458 -0
  159. data/lib/openhab/rspec/hooks.rb +113 -0
  160. data/lib/openhab/rspec/jruby.rb +46 -0
  161. data/lib/openhab/rspec/karaf.rb +851 -0
  162. data/lib/openhab/rspec/mocks/bundle_install_support.rb +25 -0
  163. data/lib/openhab/rspec/mocks/bundle_resolver.rb +30 -0
  164. data/lib/openhab/rspec/mocks/event_admin.rb +146 -0
  165. data/lib/openhab/rspec/mocks/instance_method_stasher.rb +22 -0
  166. data/lib/openhab/rspec/mocks/persistence_service.rb +155 -0
  167. data/lib/openhab/rspec/mocks/safe_caller.rb +40 -0
  168. data/lib/openhab/rspec/mocks/space.rb +23 -0
  169. data/lib/openhab/rspec/mocks/synchronous_executor.rb +63 -0
  170. data/lib/openhab/rspec/mocks/thing_handler.rb +76 -0
  171. data/lib/openhab/rspec/mocks/timer.rb +134 -0
  172. data/lib/openhab/rspec/openhab/core/actions.rb +38 -0
  173. data/lib/openhab/rspec/openhab/core/items/proxy.rb +15 -0
  174. data/lib/openhab/rspec/openhab/core/things/proxy.rb +27 -0
  175. data/lib/openhab/rspec/shell.rb +31 -0
  176. data/lib/openhab/rspec/suspend_rules.rb +50 -0
  177. data/lib/openhab/rspec.rb +26 -0
  178. data/lib/openhab/yard/base_helper.rb +19 -0
  179. data/lib/openhab/yard/cli/stats.rb +23 -0
  180. data/lib/openhab/yard/code_objects/group_object.rb +23 -0
  181. data/lib/openhab/yard/code_objects/java/base.rb +31 -0
  182. data/lib/openhab/yard/code_objects/java/class_object.rb +11 -0
  183. data/lib/openhab/yard/code_objects/java/field_object.rb +15 -0
  184. data/lib/openhab/yard/code_objects/java/interface_object.rb +15 -0
  185. data/lib/openhab/yard/code_objects/java/package_object.rb +11 -0
  186. data/lib/openhab/yard/code_objects/java/proxy.rb +23 -0
  187. data/lib/openhab/yard/coderay.rb +17 -0
  188. data/lib/openhab/yard/handlers/jruby/base.rb +58 -0
  189. data/lib/openhab/yard/handlers/jruby/class_handler.rb +18 -0
  190. data/lib/openhab/yard/handlers/jruby/constant_handler.rb +18 -0
  191. data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +30 -0
  192. data/lib/openhab/yard/handlers/jruby/mixin_handler.rb +23 -0
  193. data/lib/openhab/yard/html_helper.rb +78 -0
  194. data/lib/openhab/yard/markdown_helper.rb +148 -0
  195. data/lib/openhab/yard/tags/constant_directive.rb +20 -0
  196. data/lib/openhab/yard/tags/group_directive.rb +24 -0
  197. data/lib/openhab/yard/tags/library.rb +3 -0
  198. data/lib/openhab/yard.rb +38 -0
  199. metadata +475 -106
  200. data/lib/openhab/core/item_proxy.rb +0 -29
  201. data/lib/openhab/core/load_path.rb +0 -19
  202. data/lib/openhab/core/openhab_setup.rb +0 -29
  203. data/lib/openhab/core/osgi.rb +0 -58
  204. data/lib/openhab/core/services.rb +0 -24
  205. data/lib/openhab/dsl/actions.rb +0 -114
  206. data/lib/openhab/dsl/between.rb +0 -25
  207. data/lib/openhab/dsl/channel.rb +0 -43
  208. data/lib/openhab/dsl/dsl.rb +0 -59
  209. data/lib/openhab/dsl/group.rb +0 -54
  210. data/lib/openhab/dsl/imports.rb +0 -21
  211. data/lib/openhab/dsl/items/color_item.rb +0 -76
  212. data/lib/openhab/dsl/items/comparable_item.rb +0 -62
  213. data/lib/openhab/dsl/items/contact_item.rb +0 -41
  214. data/lib/openhab/dsl/items/date_time_item.rb +0 -65
  215. data/lib/openhab/dsl/items/dimmer_item.rb +0 -65
  216. data/lib/openhab/dsl/items/generic_item.rb +0 -229
  217. data/lib/openhab/dsl/items/group_item.rb +0 -127
  218. data/lib/openhab/dsl/items/item_equality.rb +0 -59
  219. data/lib/openhab/dsl/items/item_registry.rb +0 -54
  220. data/lib/openhab/dsl/items/items.rb +0 -109
  221. data/lib/openhab/dsl/items/location_item.rb +0 -59
  222. data/lib/openhab/dsl/items/metadata.rb +0 -326
  223. data/lib/openhab/dsl/items/number_item.rb +0 -17
  224. data/lib/openhab/dsl/items/numeric_item.rb +0 -87
  225. data/lib/openhab/dsl/items/persistence.rb +0 -307
  226. data/lib/openhab/dsl/items/player_item.rb +0 -58
  227. data/lib/openhab/dsl/items/rollershutter_item.rb +0 -51
  228. data/lib/openhab/dsl/items/semantics/enumerable.rb +0 -91
  229. data/lib/openhab/dsl/items/semantics.rb +0 -227
  230. data/lib/openhab/dsl/items/string_item.rb +0 -51
  231. data/lib/openhab/dsl/items/switch_item.rb +0 -70
  232. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +0 -4
  233. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +0 -39
  234. data/lib/openhab/dsl/monkey_patch/events/events.rb +0 -7
  235. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +0 -85
  236. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +0 -28
  237. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +0 -61
  238. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +0 -60
  239. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +0 -33
  240. data/lib/openhab/dsl/monkey_patch/java/java.rb +0 -4
  241. data/lib/openhab/dsl/monkey_patch/java/local_time.rb +0 -44
  242. data/lib/openhab/dsl/monkey_patch/java/time_extensions.rb +0 -50
  243. data/lib/openhab/dsl/monkey_patch/java/zoned_date_time.rb +0 -45
  244. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +0 -104
  245. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -6
  246. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +0 -47
  247. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -61
  248. data/lib/openhab/dsl/openhab.rb +0 -30
  249. data/lib/openhab/dsl/persistence.rb +0 -27
  250. data/lib/openhab/dsl/rules/item_event.rb +0 -19
  251. data/lib/openhab/dsl/rules/rule.rb +0 -160
  252. data/lib/openhab/dsl/rules/rule_config.rb +0 -147
  253. data/lib/openhab/dsl/rules/triggers/generic.rb +0 -31
  254. data/lib/openhab/dsl/rules/triggers/triggers.rb +0 -11
  255. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +0 -81
  256. data/lib/openhab/dsl/states.rb +0 -89
  257. data/lib/openhab/dsl/things.rb +0 -147
  258. data/lib/openhab/dsl/time/month_day.rb +0 -180
  259. data/lib/openhab/dsl/time/time_of_day.rb +0 -235
  260. data/lib/openhab/dsl/timers/manager.rb +0 -119
  261. data/lib/openhab/dsl/timers/reentrant_timer.rb +0 -38
  262. data/lib/openhab/dsl/timers/timer.rb +0 -132
  263. data/lib/openhab/dsl/timers.rb +0 -77
  264. data/lib/openhab/dsl/types/increase_decrease_type.rb +0 -23
  265. data/lib/openhab/dsl/types/next_previous_type.rb +0 -23
  266. data/lib/openhab/dsl/types/on_off_type.rb +0 -28
  267. data/lib/openhab/dsl/types/open_closed_type.rb +0 -29
  268. data/lib/openhab/dsl/types/play_pause_type.rb +0 -27
  269. data/lib/openhab/dsl/types/point_type.rb +0 -180
  270. data/lib/openhab/dsl/types/quantity_type.rb +0 -265
  271. data/lib/openhab/dsl/types/refresh_type.rb +0 -18
  272. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +0 -33
  273. data/lib/openhab/dsl/types/stop_move_type.rb +0 -23
  274. data/lib/openhab/dsl/types/types.rb +0 -83
  275. data/lib/openhab/dsl/types/un_def_type.rb +0 -22
  276. data/lib/openhab/dsl/types/up_down_type.rb +0 -32
  277. data/lib/openhab/dsl/units.rb +0 -45
  278. data/lib/openhab/log/configuration.rb +0 -21
  279. data/lib/openhab/log/logger.rb +0 -282
  280. data/lib/openhab/version.rb +0 -9
  281. data/lib/openhab.rb +0 -38
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delegate"
4
+
5
+ module OpenHAB
6
+ module RSpec
7
+ # @!visibility private
8
+ module Mocks
9
+ class BundleInstallSupport < SimpleDelegator
10
+ include org.apache.karaf.features.internal.service.BundleInstallSupport
11
+
12
+ def initialize(parent, karaf_wrapper)
13
+ super(parent)
14
+ @karaf_wrapper = karaf_wrapper
15
+ end
16
+
17
+ def set_bundle_start_level(bundle, _start_level)
18
+ return if @karaf_wrapper.send(:blocked_bundle?, bundle)
19
+
20
+ super
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ module OpenHAB
6
+ module RSpec
7
+ module Mocks
8
+ class BundleResolver
9
+ include org.openhab.core.util.BundleResolver
10
+ include Singleton
11
+
12
+ def initialize
13
+ @classes = {}
14
+ end
15
+
16
+ def register_class(klass, bundle)
17
+ # ensure we have an individual java class already
18
+ @classes[klass.become_java!] = bundle
19
+ end
20
+
21
+ def resolve_bundle(clazz)
22
+ bundle = @classes[clazz]
23
+ return bundle if bundle
24
+
25
+ org.osgi.framework.FrameworkUtil.get_bundle(clazz)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ module OpenHAB
6
+ module RSpec
7
+ module Mocks
8
+ # reimplement to not use a thread
9
+ class OSGiEventManager
10
+ attr_reader :logger
11
+
12
+ def initialize(typed_event_factories, typed_event_subscribers)
13
+ @typed_event_factories = typed_event_factories
14
+ @typed_event_subscribers = typed_event_subscribers
15
+ @logger = org.slf4j.LoggerFactory.get_logger("rspec.openhab.core.mocks.event_handler")
16
+ end
17
+
18
+ def handle_event(osgi_event)
19
+ type = osgi_event.get_property("type")
20
+ payload = osgi_event.get_property("payload")
21
+ topic = osgi_event.get_property("topic")
22
+ source = osgi_event.get_property("source")
23
+
24
+ if type.is_a?(String) && payload.is_a?(String) && topic.is_a?(String)
25
+ handle_event_internal(type, payload, topic, source) unless type.empty? || payload.empty? || topic.empty?
26
+ else
27
+ logger.error("The handled OSGi event is invalid. " \
28
+ "Expect properties as string named 'type', 'payload', and 'topic'. " \
29
+ "Received event properties are: #{osgi_event.property_names.inspect}")
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def handle_event_internal(type, payload, topic, source)
36
+ event_factory = @typed_event_factories[type]
37
+ unless event_factory
38
+ logger.debug("Could not find an Event Factory for the event type '#{type}'.")
39
+ return
40
+ end
41
+
42
+ event_subscribers = event_subscribers(type)
43
+ return if event_subscribers.empty?
44
+
45
+ event = create_event(event_factory, type, payload, topic, source)
46
+ return unless event
47
+
48
+ dispatch_event(event_subscribers, event)
49
+ end
50
+
51
+ def event_subscribers(event_type)
52
+ event_type_subscribers = @typed_event_subscribers[event_type]
53
+ all_event_type_subscribers = @typed_event_subscribers["ALL"]
54
+
55
+ subscribers = java.util.HashSet.new
56
+ subscribers.add_all(event_type_subscribers) if event_type_subscribers
57
+ subscribers.add_all(all_event_type_subscribers) if all_event_type_subscribers
58
+ subscribers
59
+ end
60
+
61
+ def create_event(event_factory, type, payload, topic, source)
62
+ event_factory.create_event(type, topic, payload, source)
63
+ rescue Exception => e
64
+ logger.warn("Creation of event failed, because one of the " \
65
+ "registered event factories has thrown an exception: #{e.inspect}")
66
+ nil
67
+ end
68
+
69
+ def dispatch_event(event_subscribers, event)
70
+ event_subscribers.each do |event_subscriber|
71
+ filter = event_subscriber.event_filter
72
+ if filter.nil? || filter.apply(event)
73
+ begin
74
+ event_subscriber.receive(event)
75
+ rescue Exception => e
76
+ logger.warn(
77
+ "Dispatching/filtering event for subscriber '#{event_subscriber.class}' failed: #{e.inspect}"
78
+ )
79
+ end
80
+ else
81
+ logger.trace("Skip event subscriber (#{event_subscriber.class}) because of its filter.")
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ class EventAdmin < org.osgi.util.tracker.ServiceTracker
88
+ include org.osgi.service.event.EventAdmin
89
+
90
+ def initialize(bundle_context)
91
+ super(bundle_context, "org.osgi.service.event.EventHandler", nil)
92
+
93
+ @handlers_matching_all_events = []
94
+ @handlers_matching_topics = Hash.new { |h, k| h[k] = [] }
95
+ open
96
+ end
97
+
98
+ def addingService(reference) # rubocop:disable Naming/MethodName
99
+ topics = Array(reference.get_property(org.osgi.service.event.EventConstants::EVENT_TOPIC))
100
+ topics = nil if topics.empty? || topics.include?("*")
101
+
102
+ service = OSGi.send(:bundle_context).get_service(reference)
103
+
104
+ if reference.get_property("component.name") == "org.openhab.core.internal.events.OSGiEventManager"
105
+ # OSGiEventManager will create a ThreadedEventHandler on OSGi activation;
106
+ # we're skipping that, and directly sending to a non-threaded event handler.
107
+ service.class.field_reader :typedEventFactories, :typedEventSubscribers
108
+ service = OSGiEventManager.new(service.typedEventFactories, service.typedEventSubscribers)
109
+ end
110
+ if topics.nil?
111
+ @handlers_matching_all_events << service
112
+ else
113
+ topics.each do |topic|
114
+ @handlers_matching_topics[topic] << service
115
+ end
116
+ end
117
+ service
118
+ end
119
+
120
+ def postEvent(event) # rubocop:disable Naming/MethodName
121
+ sendEvent(event)
122
+ end
123
+
124
+ def sendEvent(event) # rubocop:disable Naming/MethodName
125
+ # prevent re-entrancy
126
+ if (pending_events = Thread.current[:event_admin_pending_events])
127
+ pending_events << event
128
+ return
129
+ end
130
+
131
+ pending_events = Thread.current[:event_admin_pending_events] = []
132
+ handle_event(event)
133
+ handle_event(pending_events.shift) until pending_events.empty?
134
+ Thread.current[:event_admin_pending_events] = nil
135
+ end
136
+
137
+ private
138
+
139
+ def handle_event(event)
140
+ @handlers_matching_all_events.each { |h| h.handle_event(event) }
141
+ @handlers_matching_topics[event.topic].each { |h| h.handle_event(event) }
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module RSpec
5
+ module Mocks
6
+ module InstanceMethodStasher
7
+ ::RSpec::Mocks::InstanceMethodStasher.prepend(self)
8
+
9
+ # Disable "singleton on non-persistent Java type"
10
+ # it doesn't matter during specs
11
+ def initialize(*)
12
+ original_verbose = $VERBOSE
13
+ $VERBOSE = nil
14
+
15
+ super
16
+ ensure
17
+ $VERBOSE = original_verbose
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module RSpec
5
+ module Mocks
6
+ class PersistenceService
7
+ include org.openhab.core.persistence.ModifiablePersistenceService
8
+ include Singleton
9
+
10
+ class HistoricItem
11
+ include org.openhab.core.persistence.HistoricItem
12
+
13
+ attr_reader :timestamp, :state, :name
14
+
15
+ def initialize(timestamp, state, name)
16
+ @timestamp = timestamp
17
+ @state = state
18
+ @name = name
19
+ end
20
+ end
21
+
22
+ module HistoricState
23
+ def timestamp
24
+ # PersistenceExtensions uses an anonymous class to wrap the current
25
+ # state if that happens to be an answer. Except it calls
26
+ # ZonedDateTime.now in Java land, bypassing Timecop.
27
+ # Detect that and make the call in Ruby
28
+ #
29
+ jc = @historic_item.class.java_class
30
+ return ZonedDateTime.now if jc.anonymous? && jc.enclosing_class == PersistenceExtensions.java_class
31
+
32
+ super
33
+ end
34
+ end
35
+ Core::Items::Persistence::HistoricState.prepend(HistoricState)
36
+
37
+ attr_reader :id
38
+
39
+ def initialize
40
+ @id = "default"
41
+ reset
42
+ end
43
+
44
+ def reset
45
+ @data = Hash.new { |h, k| h[k] = [] }
46
+ end
47
+
48
+ def store(item, date = nil, state = nil)
49
+ date = nil if date.is_a?(String) # alias overload
50
+ state ||= item.state
51
+ date ||= ZonedDateTime.now
52
+
53
+ new_item = HistoricItem.new(date, state, item.name)
54
+
55
+ item_history = @data[item.name]
56
+
57
+ insert_index = item_history.bsearch_index do |i|
58
+ i.timestamp.compare_to(date).positive?
59
+ end
60
+
61
+ return item_history << new_item unless insert_index
62
+
63
+ return item_history[insert_index].state = state if item_history[insert_index].timestamp == date
64
+
65
+ item_history.insert(insert_index, new_item)
66
+ end
67
+
68
+ def remove(filter)
69
+ query_internal(filter) do |item_history, index|
70
+ historic_item = item_history.delete_at(index)
71
+ @data.delete(historic_item.name) if item_history.empty?
72
+ end
73
+ end
74
+
75
+ def query(filter)
76
+ result = []
77
+
78
+ query_internal(filter) do |item_history, index|
79
+ result << item_history[index]
80
+
81
+ return result if filter.page_number.zero? && result.length == filter.page_size && filter.item_name
82
+ end
83
+
84
+ result.sort_by!(&:timestamp) unless filter.item_name
85
+
86
+ result = result.slice(filter.page_number * filter.page_size, filter.page_size) unless filter.page_number.zero?
87
+
88
+ result
89
+ end
90
+
91
+ def get_item_info # rubocop:disable Naming/AccessorMethodName must match Java interface
92
+ @data.to_set do |(n, entries)|
93
+ [n, entries.length, entries.first.timestamp, entries.last.timestamp]
94
+ end
95
+ end
96
+
97
+ def get_default_strategies # rubocop:disable Naming/AccessorMethodName must match Java interface
98
+ [org.openhab.core.persistence.strategy.PersistenceStrategy::Globals::CHANGE]
99
+ end
100
+
101
+ private
102
+
103
+ def query_internal(filter, &block)
104
+ if filter.item_name
105
+ return unless @data.key?(filter.item_name)
106
+
107
+ query_item_internal(@data[filter.item_name], filter, &block)
108
+ else
109
+ @data.each_value do |item_history|
110
+ query_item_internal(item_history, filter, &block)
111
+ end
112
+ end
113
+ end
114
+
115
+ def query_item_internal(item_history, filter)
116
+ first_index = 0
117
+ last_index = item_history.length
118
+
119
+ if filter.begin_date
120
+ first_index = item_history.bsearch_index do |i|
121
+ i.timestamp.compare_to(filter.begin_date).positive?
122
+ end
123
+ return if first_index.nil?
124
+ end
125
+
126
+ if filter.end_date
127
+ last_index = item_history.bsearch_index do |i|
128
+ i.timestamp.compare_to(filter.end_date).positive?
129
+ end
130
+ return if last_index.zero?
131
+
132
+ last_index ||= item_history.length
133
+ end
134
+
135
+ range = first_index...last_index
136
+
137
+ operator = filter.operator.symbol
138
+ operator = "==" if operator == "="
139
+
140
+ block = lambda do |i|
141
+ next if filter.state && !item_history[i].state.send(operator, filter.state)
142
+
143
+ yield(item_history, i)
144
+ end
145
+
146
+ if filter.ordering == filter.class::Ordering::DESCENDING
147
+ range.reverse_each(&block)
148
+ else
149
+ range.each(&block)
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ module OpenHAB
6
+ module RSpec
7
+ module Mocks
8
+ class SafeCaller
9
+ include Singleton
10
+ include org.openhab.core.common.SafeCaller
11
+
12
+ class Builder
13
+ include org.openhab.core.common.SafeCallerBuilder
14
+
15
+ def initialize(target)
16
+ @target = target
17
+ end
18
+
19
+ def build
20
+ @target
21
+ end
22
+
23
+ def chain(*)
24
+ self
25
+ end
26
+
27
+ alias_method :withTimeout, :chain
28
+ alias_method :withIdentifier, :chain
29
+ alias_method :onException, :chain
30
+ alias_method :onTimeout, :chain
31
+ alias_method :withAsync, :chain
32
+ end
33
+
34
+ def create(target, _klass)
35
+ Builder.new(target)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module RSpec
5
+ module Mocks
6
+ module Space
7
+ ::RSpec::Mocks::Space.prepend(self)
8
+
9
+ #
10
+ # When setting expectations on {Items::Proxy proxies}, set them against the item
11
+ # themselves, so that it will catch calls against `self` from within the instance.
12
+ #
13
+ def proxy_for(object)
14
+ return super unless ::RSpec.current_example&.example_group&.consistent_proxies?
15
+
16
+ object = object.__getobj__ if object.is_a?(Core::Items::Proxy)
17
+
18
+ super
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ module OpenHAB
6
+ module RSpec
7
+ module Mocks
8
+ class CallbacksMap < java.util.HashMap
9
+ def put(_rule_uid, trigger_handler)
10
+ if trigger_handler.executor
11
+ trigger_handler.executor.shutdown_now
12
+ trigger_handler.executor = SynchronousExecutor.instance
13
+ end
14
+ super
15
+ end
16
+ end
17
+
18
+ class SynchronousExecutor < java.util.concurrent.ScheduledThreadPoolExecutor
19
+ include Singleton
20
+
21
+ attr_accessor :main_thread
22
+
23
+ def initialize
24
+ super(1)
25
+ end
26
+
27
+ def submit(runnable)
28
+ return super unless Thread.current == main_thread
29
+
30
+ runnable.respond_to?(:run) ? runnable.run : runnable.call
31
+
32
+ java.util.concurrent.CompletableFuture.completed_future(nil)
33
+ end
34
+
35
+ def execute(runnable)
36
+ return super unless Thread.current == main_thread
37
+
38
+ runnable.run
39
+ end
40
+
41
+ def shutdown; end
42
+
43
+ def shutdown_now
44
+ []
45
+ end
46
+ alias_method :shutdownNow, :shutdown_now
47
+ end
48
+
49
+ class SynchronousExecutorMap
50
+ include java.util.Map
51
+ include Singleton
52
+
53
+ def get(_key)
54
+ SynchronousExecutor.instance
55
+ end
56
+
57
+ def key_set
58
+ java.util.HashSet.new
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Naming/MethodName, Naming/AccessorMethodName
4
+
5
+ require "singleton"
6
+
7
+ module OpenHAB
8
+ module RSpec
9
+ module Mocks
10
+ class ThingHandler
11
+ include org.openhab.core.thing.binding.BridgeHandler
12
+
13
+ attr_reader :thing, :callback
14
+
15
+ def initialize(thing = nil)
16
+ # have to handle the interface method
17
+ if thing.nil?
18
+ status_info = org.openhab.core.thing.binding.builder.ThingStatusInfoBuilder
19
+ .create(org.openhab.core.thing.ThingStatus::ONLINE).build
20
+ @callback.status_updated(self.thing, status_info)
21
+ return
22
+ end
23
+
24
+ # ruby initializer here
25
+ @thing = thing
26
+ end
27
+
28
+ def thing_updated(thing)
29
+ @thing = thing
30
+ end
31
+
32
+ def handle_command(channel, command); end
33
+
34
+ def set_callback(callback)
35
+ @callback = callback
36
+ end
37
+
38
+ def child_handler_initialized(child_handler, child_thing); end
39
+ def child_handler_disposed(child_handler, child_thing); end
40
+
41
+ def channel_linked(_channel_uid); end
42
+ def channel_unlinked(_channel_uid); end
43
+
44
+ def dispose; end
45
+ end
46
+
47
+ class ThingHandlerFactory < org.openhab.core.thing.binding.BaseThingHandlerFactory
48
+ include Singleton
49
+
50
+ class ComponentContext
51
+ include org.osgi.service.component.ComponentContext
52
+ include Singleton
53
+
54
+ def getBundleContext
55
+ org.osgi.framework.FrameworkUtil.get_bundle(org.openhab.core.thing.Thing.java_class).bundle_context
56
+ end
57
+ end
58
+ private_constant :ComponentContext
59
+
60
+ def initialize
61
+ super
62
+ activate(ComponentContext.instance)
63
+ end
64
+
65
+ def supportsThingType(_type)
66
+ true
67
+ end
68
+
69
+ def createHandler(thing)
70
+ ThingHandler.new(thing)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ # rubocop:enable Naming/MethodName, Naming/AccessorMethodName