rspec-openhab-scripting 0.0.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rspec/bundler/inline.rb +12 -0
  3. data/lib/rspec/openhab/api.rb +34 -0
  4. data/lib/rspec/openhab/core/cron_scheduler.rb +13 -0
  5. data/lib/rspec/openhab/core/load_path.rb +13 -0
  6. data/lib/rspec/openhab/core/logger.rb +24 -0
  7. data/lib/rspec/openhab/core/openhab_setup.rb +11 -0
  8. data/lib/rspec/openhab/core/osgi.rb +19 -0
  9. data/lib/rspec/openhab/core/script_handling.rb +21 -0
  10. data/lib/rspec/openhab/dsl/imports.rb +231 -0
  11. data/lib/rspec/openhab/dsl/timers/timer.rb +86 -0
  12. data/lib/rspec/openhab/version.rb +7 -0
  13. data/lib/rspec-openhab-scripting.rb +115 -0
  14. data/lib/rspec-openhab-scripting_jars.rb +14 -0
  15. data/vendor/gems/jar-dependencies-1.0.0/lib/jar-dependencies.rb +24 -0
  16. data/vendor/gems/jar-dependencies-1.0.0/lib/jar_dependencies.rb +397 -0
  17. data/vendor/gems/jar-dependencies-1.0.0/lib/jar_install_post_install_hook.rb +29 -0
  18. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/attach_jars_pom.rb +35 -0
  19. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/classpath.rb +92 -0
  20. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/gemspec_artifacts.rb +220 -0
  21. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/gemspec_pom.rb +11 -0
  22. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/installer.rb +221 -0
  23. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/lock.rb +75 -0
  24. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/lock_down.rb +102 -0
  25. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/lock_down_pom.rb +35 -0
  26. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/maven_exec.rb +88 -0
  27. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/maven_factory.rb +138 -0
  28. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/maven_require.rb +48 -0
  29. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/maven_settings.rb +124 -0
  30. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/output_jars_pom.rb +16 -0
  31. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/post_install_hook.rb +33 -0
  32. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/setup.rb +9 -0
  33. data/vendor/gems/jar-dependencies-1.0.0/lib/jars/version.rb +7 -0
  34. data/vendor/gems/jar-dependencies-1.0.0/lib/rubygems_plugin.rb +24 -0
  35. metadata +217 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0469b486b84d65883d60fa79dbea29b5c5c86792a8f430d222c87bbb76b69d5
4
+ data.tar.gz: 188413ff354ddec32fdfb5c4fb0823efcde0a803b684acd3370070217f3d4f1c
5
+ SHA512:
6
+ metadata.gz: fe7991e666341f3a035ea7bbb59d575affd17da44503eb90209b30a05df2f1802620696e8fc8701afe1f0abfcd64e5e8250415f463e5934a2e82ac15234cba83
7
+ data.tar.gz: 44d8592c747ac3d179b71a7bf05b35fa8e5b061969b7309f6b793df44135ff781d2d476a776e4f1431e214744188386e32aa875512f016589af62838f7709100
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bundler
4
+ class DummyDsl
5
+ def source(*); end
6
+ end
7
+ end
8
+
9
+ def gemfile(*, &block)
10
+ # needs to be a no-op, since we're already running in the context of bundler
11
+ Bundler::DummyDsl.new.instance_eval(&block)
12
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday_middleware"
4
+
5
+ module OpenHAB
6
+ class API
7
+ def initialize(url)
8
+ @faraday = Faraday.new(url) do |f|
9
+ f.request :retry
10
+ f.response :raise_error
11
+ f.response :json
12
+ f.adapter :net_http_persistent
13
+ f.path_prefix = "/rest/"
14
+ end
15
+ end
16
+
17
+ def version
18
+ body = @faraday.get.body
19
+ version = body.dig("runtimeInfo", "version")
20
+ version = "#{version}-SNAPSHOT" if body.dig("runtimeInfo", "buildString")&.start_with?("Build #")
21
+ version
22
+ end
23
+
24
+ def items
25
+ @faraday.get("items").body
26
+ end
27
+
28
+ def item(name)
29
+ @faraday.get("items/#{name}").body
30
+ rescue Faraday::ResourceNotFound
31
+ nil
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ class CronScheduler
6
+ include Singleton
7
+
8
+ def schedule(*); end
9
+ end
10
+
11
+ OpenHAB::Core::OSGI.register_service("org.openhab.core.scheduler.CronScheduler", CronScheduler.instance)
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ OPENHAB_SHARE_PATH = "#{org.openhab.core.OpenHAB.config_folder}/automation/lib/ruby"
6
+
7
+ class << self
8
+ def add_rubylib_to_load_path
9
+ $LOAD_PATH.unshift(OPENHAB_SHARE_PATH) unless $LOAD_PATH.include?(OPENHAB_SHARE_PATH)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ def ch
4
+ Java::Ch
5
+ end
6
+
7
+ ch.qos.logback.classic.Level.class_eval do
8
+ alias_method :inspect, :to_s
9
+ end
10
+
11
+ module OpenHAB
12
+ module Core
13
+ class Logger
14
+ levels = %i[OFF ERROR WARN INFO DEBUG TRACE ALL]
15
+ levels.each { |level| const_set(level, ch.qos.logback.classic.Level.const_get(level)) }
16
+
17
+ extend Forwardable
18
+ delegate %i[level level=] => :@sl4fj_logger
19
+ end
20
+ end
21
+ end
22
+
23
+ root_logger = org.slf4j.LoggerFactory.get_logger(org.slf4j.Logger::ROOT_LOGGER_NAME)
24
+ root_logger.level = OpenHAB::Core::Logger::INFO
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openhab/core/services"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ class << self
8
+ def wait_till_openhab_ready; end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ class OSGI
6
+ class << self
7
+ def register_service(name, service)
8
+ (@services ||= {})[name] = service
9
+ end
10
+
11
+ def service(name)
12
+ @services&.[](name)
13
+ end
14
+
15
+ def services(name, filter: nil); end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module ScriptHandling
6
+ module_function
7
+
8
+ def script_loaded(&block); end
9
+ def script_unloaded(&block); end
10
+ end
11
+
12
+ module ScriptHandlingCallbacks
13
+ end
14
+ end
15
+
16
+ module DSL
17
+ module Core
18
+ ScriptHandling = OpenHAB::Core::ScriptHandling
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,231 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openhab/core/osgi"
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module Imports
8
+ class ScriptExtensionManagerWrapper
9
+ def initialize(manager)
10
+ @manager = manager
11
+ end
12
+
13
+ def get(type)
14
+ @manager.get("jruby", type)
15
+ end
16
+ end
17
+
18
+ class EventAdmin
19
+ include org.osgi.service.event.EventAdmin
20
+
21
+ def initialize(event_manager)
22
+ @event_manager = event_manager
23
+ end
24
+
25
+ def post_event(event)
26
+ send_event(event)
27
+ end
28
+
29
+ def send_event(event)
30
+ @event_manager.handle_event(event)
31
+ end
32
+ end
33
+
34
+ class BundleContext
35
+ include org.osgi.framework.BundleContext
36
+
37
+ def initialize(event_manager)
38
+ @event_manager = event_manager
39
+ end
40
+
41
+ def register_service(klass, service, _properties)
42
+ klass = klass.to_s
43
+ unless klass == "org.openhab.core.events.EventSubscriber"
44
+ raise NotImplementedError "Don't know how to process service #{service.inspect} of type #{klass.name}"
45
+ end
46
+
47
+ @event_manager.add_event_subscriber(service)
48
+ end
49
+ end
50
+
51
+ # don't depend on org.openhab.core.test
52
+ class VolatileStorageService
53
+ include org.openhab.core.storage.StorageService
54
+
55
+ def initialize
56
+ @storages = {}
57
+ end
58
+
59
+ def get_storage(name, *)
60
+ @storages[name] ||= VolatileStorage.new
61
+ end
62
+ end
63
+
64
+ class VolatileStorage < Hash
65
+ include org.openhab.core.storage.Storage
66
+
67
+ alias_method :get, :[]
68
+ alias_method :put, :[]=
69
+ alias_method :remove, :delete
70
+ alias_method :contains_key, :key?
71
+
72
+ alias_method :get_keys, :keys
73
+ alias_method :get_values, :values
74
+ end
75
+
76
+ class Bundle
77
+ include org.osgi.framework.Bundle
78
+ INSTALLED = 2
79
+
80
+ def initialize(*jar_args)
81
+ file = Jars.find_jar(*jar_args)
82
+ @jar = java.util.jar.JarFile.new(file)
83
+ @symbolic_name = jar_args[1]
84
+ @version = org.osgi.framework.Version.new(jar_args[2].tr("-", "."))
85
+ end
86
+
87
+ attr_reader :symbolic_name, :version
88
+
89
+ def state
90
+ INSTALLED
91
+ end
92
+
93
+ def find_entries(path, pattern, recurse)
94
+ pattern ||= recurse ? "**" : "*"
95
+ full_pattern = File.join(path, pattern)
96
+ entries = @jar.entries.select do |e|
97
+ File.fnmatch(full_pattern, e.name)
98
+ end
99
+ java.util.Collections.enumeration(entries.map { |e| java.net.URL.new("jar:file://#{@jar.name}!/#{e.name}") })
100
+ end
101
+ end
102
+
103
+ # subclass to expose private fields
104
+ class EventManager < org.openhab.core.internal.events.OSGiEventManager
105
+ field_reader :typedEventFactories, :typedEventSubscribers
106
+ end
107
+
108
+ @imported = false
109
+
110
+ class << self
111
+ def import_presets
112
+ return if @imported
113
+
114
+ @imported = true
115
+
116
+ # some background java threads get created; kill them at_exit
117
+ at_exit do
118
+ status = 0
119
+ status = $!.status if $!.is_a?(SystemExit)
120
+ exit!(status)
121
+ end
122
+
123
+ # OSGiEventManager will create a ThreadedEventHandler on OSGi activation;
124
+ # we're skipping that, and directly sending to a non-threaded event handler.
125
+ em = EventManager.new
126
+ eh = org.openhab.core.internal.events.EventHandler.new(em.typedEventSubscribers, em.typedEventFactories)
127
+ at_exit { eh.close }
128
+ ea = EventAdmin.new(eh)
129
+ ep = org.openhab.core.internal.events.OSGiEventPublisher.new(ea)
130
+
131
+ # the registries!
132
+ mr = org.openhab.core.internal.items.MetadataRegistryImpl.new
133
+ OpenHAB::Core::OSGI.register_service("org.openhab.core.items.MetadataRegistry", mr)
134
+ ir = org.openhab.core.internal.items.ItemRegistryImpl.new(mr)
135
+ ss = VolatileStorageService.new
136
+ ir.managed_provider = mip = org.openhab.core.items.ManagedItemProvider.new(ss, nil)
137
+ ir.add_provider(mip)
138
+ tr = org.openhab.core.thing.internal.ThingRegistryImpl.new
139
+ mtr = org.openhab.core.automation.internal.type.ModuleTypeRegistryImpl.new
140
+ rr = org.openhab.core.automation.internal.RuleRegistryImpl.new
141
+ rr.module_type_registry = mtr
142
+ rr.managed_provider = mrp = org.openhab.core.automation.ManagedRuleProvider.new(ss)
143
+ rr.add_provider(mrp)
144
+ iclr = org.openhab.core.thing.link.ItemChannelLinkRegistry.new(tr, ir)
145
+
146
+ # set up stuff accessed from rules
147
+ preset = org.openhab.core.automation.module.script.internal.defaultscope
148
+ .DefaultScriptScopeProvider.new(ir, tr, rr, ep)
149
+ preset.default_presets.each do |preset_name|
150
+ preset.import_preset(nil, preset_name).each do |(name, value)|
151
+ next if name == "File"
152
+
153
+ if value.respond_to?(:ruby_class)
154
+ Object.const_set(name, value.ruby_class)
155
+ elsif /[[:upper:]]/.match?(name[0])
156
+ Object.const_set(name, value)
157
+ else
158
+ eval("$#{name} = value", binding, __FILE__, __LINE__) # $ir = value # rubocop:disable Security/Eval
159
+ end
160
+ end
161
+ end
162
+
163
+ # set up the rules engine part 1
164
+ mtrbp = org.openhab.core.automation.internal.provider.ModuleTypeResourceBundleProvider.new(nil)
165
+ mtgp = org.openhab.core.automation.internal.parser.gson.ModuleTypeGSONParser.new
166
+ mtrbp.add_parser(mtgp, {})
167
+ bundle = Bundle.new("org.openhab.core.bundles",
168
+ "org.openhab.core.automation.module.script.rulesupport",
169
+ OpenHAB::Core.openhab_version)
170
+ mtrbp.process_automation_provider(bundle)
171
+ bundle = Bundle.new("org.openhab.core.bundles",
172
+ "org.openhab.core.automation",
173
+ OpenHAB::Core.openhab_version)
174
+ mtrbp.process_automation_provider(bundle)
175
+ mtr.add_provider(mtrbp)
176
+
177
+ # set up script support stuff
178
+ srp = org.openhab.core.automation.module.script.rulesupport.shared.ScriptedRuleProvider.new
179
+ rr.add_provider(srp)
180
+ scmhf = org.openhab.core.automation.module.script.rulesupport.internal.ScriptedCustomModuleHandlerFactory.new
181
+ scmtp = org.openhab.core.automation.module.script.rulesupport.internal.ScriptedCustomModuleTypeProvider.new
182
+ mtr.add_provider(scmtp)
183
+ spmhf = org.openhab.core.automation.module.script.rulesupport.internal.ScriptedPrivateModuleHandlerFactory.new
184
+ se = org.openhab.core.automation.module.script.rulesupport.internal
185
+ .RuleSupportScriptExtension.new(rr, srp, scmhf, scmtp, spmhf)
186
+ sew = ScriptExtensionManagerWrapper.new(se)
187
+ $se = $scriptExtension = sew # rubocop:disable Style/GlobalVars
188
+
189
+ # need to create some singletons referencing registries
190
+ org.openhab.core.model.script.ScriptServiceUtil.new(ir, tr, ep, nil, nil)
191
+ org.openhab.core.model.script.internal.engine.action.SemanticsActionService.new(ir)
192
+
193
+ # link up event bus infrastructure
194
+ iu = org.openhab.core.internal.items.ItemUpdater.new(ir)
195
+ ief = org.openhab.core.items.events.ItemEventFactory.new
196
+
197
+ sc = org.openhab.core.internal.common.SafeCallerImpl.new({})
198
+ aum = org.openhab.core.thing.internal.AutoUpdateManager.new({ "enabled" => "true" }, nil, ep, iclr, mr, tr)
199
+ cm = org.openhab.core.thing.internal.CommunicationManager.new(aum, nil, nil, iclr, ir, nil, ep, sc, tr)
200
+
201
+ em.add_event_subscriber(iu)
202
+ em.add_event_subscriber(cm)
203
+ em.add_event_factory(ief)
204
+
205
+ # set up the rules engine part 2
206
+ bc = BundleContext.new(em)
207
+ k = org.openhab.core.automation.internal.module.factory.CoreModuleHandlerFactory
208
+ # depending on OH version, this class is set up differently
209
+ cmhf = begin
210
+ cmhf = k.new
211
+ cmhf.item_registry = ir
212
+ cmhf.event_publisher = ep
213
+ cmhf.activate(bc)
214
+ cmhf
215
+ rescue ArgumentError
216
+ k.new(bc, ep, ir)
217
+ end
218
+
219
+ rs = org.openhab.core.internal.service.ReadyServiceImpl.new
220
+ re = org.openhab.core.automation.internal.RuleEngineImpl.new(mtr, rr, ss, rs)
221
+ re.add_module_handler_factory(cmhf)
222
+ re.add_module_handler_factory(scmhf)
223
+ re.add_module_handler_factory(spmhf)
224
+ re.onReadyMarkerAdded(nil)
225
+ end
226
+ end
227
+ end
228
+ end
229
+ end
230
+
231
+ OpenHAB::DSL.singleton_class.prepend(OpenHAB::DSL::Imports)
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module DSL
5
+ class Timer
6
+ #
7
+ # Create a new Timer Object
8
+ #
9
+ # @param [Duration] duration Duration until timer should fire
10
+ # @param [Block] block Block to execute when timer fires
11
+ #
12
+ def initialize(duration:, thread_locals: {}, &block) # rubocop:disable Lint/UnusedMethodArgument
13
+ @duration = duration
14
+
15
+ Timers.timer_manager.add(self)
16
+ end
17
+
18
+ def reschedule(duration = nil)
19
+ duration ||= @duration
20
+
21
+ Timers.timer_manager.add(self)
22
+ reschedule(OpenHAB::DSL.to_zdt(duration))
23
+ end
24
+
25
+ #
26
+ # Cancel timer
27
+ #
28
+ # @return [Boolean] True if cancel was successful, false otherwise
29
+ #
30
+ def cancel
31
+ Timers.timer_manager.delete(self)
32
+ end
33
+
34
+ def terminated?; end
35
+ alias_method :has_terminated, :terminated?
36
+
37
+ private
38
+
39
+ def timer_block
40
+ proc do
41
+ Timers.timer_manager.delete(self)
42
+ yield(self)
43
+ end
44
+ end
45
+ end
46
+
47
+ #
48
+ # Convert TemporalAmount (Duration), seconds (float, integer), and Ruby Time to ZonedDateTime
49
+ # Note: TemporalAmount is added to now
50
+ #
51
+ # @param [Object] timestamp to convert
52
+ #
53
+ # @return [ZonedDateTime]
54
+ #
55
+ def self.to_zdt(timestamp)
56
+ logger.trace("Converting #{timestamp} (#{timestamp.class}) to ZonedDateTime")
57
+ return unless timestamp
58
+
59
+ case timestamp
60
+ when Java::JavaTimeTemporal::TemporalAmount then ZonedDateTime.now.plus(timestamp)
61
+ when ZonedDateTime then timestamp
62
+ when Time then timestamp.to_java(ZonedDateTime)
63
+ else
64
+ to_zdt(seconds_to_duration(timestamp)) ||
65
+ raise(ArgumentError, "Timestamp must be a ZonedDateTime, a Duration, a Numeric, or a Time object")
66
+ end
67
+ end
68
+
69
+ #
70
+ # Convert numeric seconds to a Duration object
71
+ #
72
+ # @param [Float, Integer] secs The number of seconds in integer or float
73
+ #
74
+ # @return [Duration]
75
+ #
76
+ def self.seconds_to_duration(secs)
77
+ return unless secs
78
+
79
+ if secs.respond_to?(:to_f)
80
+ secs.to_f.seconds
81
+ elsif secs.respond_to?(:to_i)
82
+ secs.to_i.seconds
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpec
4
+ module OpenHAB
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ # see https://github.com/jruby/jruby/issues/7262
4
+ # in the meantime, I've "vendored" it, and just forcing it on the load path first
5
+ $LOAD_PATH.unshift(File.expand_path("../vendor/gems/jar-dependencies-1.0.0/lib", __dir__))
6
+
7
+ require "rspec/openhab/api"
8
+ api = OpenHAB::API.new("http://#{ENV.fetch("OPENHAB_HOST", "localhost")}:#{ENV.fetch("OPENHAB_HTTP_PORT", "8080")}/")
9
+
10
+ module OpenHAB
11
+ module Core
12
+ class << self
13
+ attr_accessor :openhab_version
14
+ end
15
+ end
16
+ end
17
+
18
+ oh_home = ENV.fetch("OPENHAB_HOME", "/usr/share/openhab")
19
+ oh_runtime = ENV.fetch("OPENHAB_RUNTIME", "#{oh_home}/runtime")
20
+
21
+ ENV["JARS_ADDITIONAL_MAVEN_REPOS"] = File.join(oh_runtime, "system")
22
+
23
+ openhab_version = OpenHAB::Core.openhab_version = api.version
24
+
25
+ require "rspec-openhab-scripting_jars"
26
+
27
+ maven_require do
28
+ # upstream dependencies that I don't know how to infer from the openhab bundles alone
29
+ require "jar com.google.code.gson, gson, 2.8.9"
30
+ require "jar org.eclipse.xtext, org.eclipse.xtext, 2.26.0"
31
+ require "jar org.osgi, osgi.cmpn, 7.0.0"
32
+ require "jar org.osgi, org.osgi.framework, 1.8.0"
33
+ require "jar si.uom, si-units, 2.1"
34
+ require "jar tech.units, indriya, 2.1.3"
35
+
36
+ require "jar org.openhab.core.bundles, org.openhab.core, #{openhab_version}"
37
+ require "jar org.openhab.core.bundles, org.openhab.core.config.core, #{openhab_version}"
38
+ require "jar org.openhab.core.bundles, org.openhab.core.automation, #{openhab_version}"
39
+ require "jar org.openhab.core.bundles, org.openhab.core.automation.module.script, #{openhab_version}"
40
+ require "jar org.openhab.core.bundles, org.openhab.core.automation.module.script.rulesupport, #{openhab_version}"
41
+ require "jar org.openhab.core.bundles, org.openhab.core.model.core, #{openhab_version}"
42
+ require "jar org.openhab.core.bundles, org.openhab.core.model.script, #{openhab_version}"
43
+ require "jar org.openhab.core.bundles, org.openhab.core.semantics, #{openhab_version}"
44
+ require "jar org.openhab.core.bundles, org.openhab.core.thing, #{openhab_version}"
45
+ end
46
+
47
+ require "openhab/version"
48
+
49
+ # we completely override some files from openhab-scripting
50
+ $LOAD_PATH.unshift("#{__dir__}/rspec")
51
+
52
+ oh = org.openhab.core.OpenHAB
53
+ def oh.config_folder
54
+ ENV.fetch("OPENHAB_CONF", "/etc/openhab")
55
+ end
56
+
57
+ # global variables need to be set up before openhab-scripting loads
58
+ require "openhab/log/logger"
59
+ require "rspec/openhab/core/logger"
60
+ require "openhab/dsl/imports"
61
+ OpenHAB::DSL::Imports.import_presets
62
+
63
+ require "openhab"
64
+
65
+ require "rspec/openhab/core/cron_scheduler"
66
+
67
+ # openhab-scripting uses a require_relative, so our override doesn't get used
68
+ OpenHAB::DSL.send(:remove_const, :Timer)
69
+ require_relative "rspec/openhab/dsl/timers/timer"
70
+
71
+ # rubocop:disable Style/GlobalVars
72
+
73
+ # populate item registry
74
+ all_items = api.items
75
+ all_items.each do |item_json|
76
+ type, _dimension = item_json["type"].split(":")
77
+ if type == "Group"
78
+ if item_json["groupType"]
79
+ type, _dimension = item_json["groupType"].split(":")
80
+ klass = OpenHAB::DSL::Items.const_get(:"#{type}Item")
81
+ base_item = klass.new(item_json["name"])
82
+ end
83
+ # TODO: create group function
84
+ item = GroupItem.new(item_json["name"], base_item)
85
+ else
86
+ klass = OpenHAB::DSL::Items.const_get(:"#{type}Item")
87
+ item = klass.new(item_json["name"])
88
+ end
89
+
90
+ item.label = item_json["label"]
91
+ item_json["tags"].each do |tag|
92
+ item.add_tag(tag)
93
+ end
94
+ $ir.add(item)
95
+ end
96
+ all_items.each do |item_json| # rubocop:disable Style/CombinableLoops
97
+ item_json["groupNames"].each do |group_name|
98
+ next unless (group = $ir.get(group_name))
99
+
100
+ group.add_member($ir.get(item_json["name"]))
101
+ end
102
+ end
103
+
104
+ # rubocop:enable Style/GlobalVars
105
+
106
+ # load rules files
107
+ OPENHAB_AUTOMATION_PATH = "#{org.openhab.core.OpenHAB.config_folder}/automation/jsr223/ruby/personal"
108
+
109
+ # set up some environment the rules files expect
110
+ Dir["#{OPENHAB_AUTOMATION_PATH}/*.rb"].each do |f|
111
+ load f
112
+ rescue Exception => e # rubocop:disable Lint/RescueException
113
+ warn "Failed loading #{f}: #{e}"
114
+ warn e.backtrace
115
+ end
@@ -0,0 +1,14 @@
1
+ # this is a generated file, to avoid over-writing it just delete this comment
2
+ begin
3
+ require 'jar_dependencies'
4
+ rescue LoadError
5
+ require 'ch/qos/logback/logback-classic/1.2.9/logback-classic-1.2.9.jar'
6
+ require 'org/slf4j/slf4j-api/1.7.32/slf4j-api-1.7.32.jar'
7
+ require 'ch/qos/logback/logback-core/1.2.9/logback-core-1.2.9.jar'
8
+ end
9
+
10
+ if defined? Jars
11
+ require_jar 'ch.qos.logback', 'logback-classic', '1.2.9'
12
+ require_jar 'org.slf4j', 'slf4j-api', '1.7.32'
13
+ require_jar 'ch.qos.logback', 'logback-core', '1.2.9'
14
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (C) 2014 Christian Meier
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
7
+ # this software and associated documentation files (the "Software"), to deal in
8
+ # the Software without restriction, including without limitation the rights to
9
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10
+ # the Software, and to permit persons to whom the Software is furnished to do so,
11
+ # subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in all
14
+ # copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #
23
+
24
+ require "jar_dependencies"