Module: OpenHAB::DSL::Imports

Defined in:
lib/rspec/openhab/dsl/imports.rb

Defined Under Namespace

Classes: Bundle, BundleContext, EventAdmin, EventManager, ScriptExtensionManagerWrapper, VolatileStorage, VolatileStorageService

Class Method Summary collapse

Class Method Details

.import_presetsObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/rspec/openhab/dsl/imports.rb', line 111

def import_presets
  return if @imported

  @imported = true

  # some background java threads get created; kill them at_exit
  at_exit do
    status = 0
    status = $!.status if $!.is_a?(SystemExit)
    exit!(status)
  end

  # OSGiEventManager will create a ThreadedEventHandler on OSGi activation;
  # we're skipping that, and directly sending to a non-threaded event handler.
  em = EventManager.new
  eh = org.openhab.core.internal.events.EventHandler.new(em.typedEventSubscribers, em.typedEventFactories)
  at_exit { eh.close }
  ea = EventAdmin.new(eh)
  ep = org.openhab.core.internal.events.OSGiEventPublisher.new(ea)

  # the registries!
  mr = org.openhab.core.internal.items.MetadataRegistryImpl.new
  OpenHAB::Core::OSGI.register_service("org.openhab.core.items.MetadataRegistry", mr)
  ir = org.openhab.core.internal.items.ItemRegistryImpl.new(mr)
  ss = VolatileStorageService.new
  ir.managed_provider = mip = org.openhab.core.items.ManagedItemProvider.new(ss, nil)
  ir.add_provider(mip)
  tr = org.openhab.core.thing.internal.ThingRegistryImpl.new
  mtr = org.openhab.core.automation.internal.type.ModuleTypeRegistryImpl.new
  rr = org.openhab.core.automation.internal.RuleRegistryImpl.new
  rr.module_type_registry = mtr
  rr.managed_provider = mrp = org.openhab.core.automation.ManagedRuleProvider.new(ss)
  rr.add_provider(mrp)
  iclr = org.openhab.core.thing.link.ItemChannelLinkRegistry.new(tr, ir)

  # set up stuff accessed from rules
  preset = org.openhab.core.automation.module.script.internal.defaultscope
              .DefaultScriptScopeProvider.new(ir, tr, rr, ep)
  preset.default_presets.each do |preset_name|
    preset.import_preset(nil, preset_name).each do |(name, value)|
      next if name == "File"

      if value.respond_to?(:ruby_class)
        Object.const_set(name, value.ruby_class)
      elsif /[[:upper:]]/.match?(name[0])
        Object.const_set(name, value)
      else
        eval("$#{name} = value", binding, __FILE__, __LINE__) # $ir = value # rubocop:disable Security/Eval
      end
    end
  end

  # set up the rules engine part 1
  mtrbp = org.openhab.core.automation.internal.provider.ModuleTypeResourceBundleProvider.new(nil)
  mtgp = org.openhab.core.automation.internal.parser.gson.ModuleTypeGSONParser.new
  mtrbp.add_parser(mtgp, {})
  bundle = Bundle.new("org.openhab.core.bundles",
                      "org.openhab.core.automation.module.script.rulesupport",
                      OpenHAB::Core.openhab_version)
  mtrbp.process_automation_provider(bundle)
  bundle = Bundle.new("org.openhab.core.bundles",
                      "org.openhab.core.automation",
                      OpenHAB::Core.openhab_version)
  mtrbp.process_automation_provider(bundle)
  mtr.add_provider(mtrbp)

  # set up script support stuff
  srp = org.openhab.core.automation.module.script.rulesupport.shared.ScriptedRuleProvider.new
  rr.add_provider(srp)
  scmhf = org.openhab.core.automation.module.script.rulesupport.internal.ScriptedCustomModuleHandlerFactory.new
  scmtp = org.openhab.core.automation.module.script.rulesupport.internal.ScriptedCustomModuleTypeProvider.new
  mtr.add_provider(scmtp)
  spmhf = org.openhab.core.automation.module.script.rulesupport.internal.ScriptedPrivateModuleHandlerFactory.new
  se = org.openhab.core.automation.module.script.rulesupport.internal
          .RuleSupportScriptExtension.new(rr, srp, scmhf, scmtp, spmhf)
  sew = ScriptExtensionManagerWrapper.new(se)
  $se = $scriptExtension = sew # rubocop:disable Style/GlobalVars

  # need to create some singletons referencing registries
  org.openhab.core.model.script.ScriptServiceUtil.new(ir, tr, ep, nil, nil)
  org.openhab.core.model.script.internal.engine.action.SemanticsActionService.new(ir)

  # link up event bus infrastructure
  iu = org.openhab.core.internal.items.ItemUpdater.new(ir)
  ief = org.openhab.core.items.events.ItemEventFactory.new

  sc = org.openhab.core.internal.common.SafeCallerImpl.new({})
  aum = org.openhab.core.thing.internal.AutoUpdateManager.new({ "enabled" => "true" }, nil, ep, iclr, mr, tr)
  cm = org.openhab.core.thing.internal.CommunicationManager.new(aum, nil, nil, iclr, ir, nil, ep, sc, tr)

  em.add_event_subscriber(iu)
  em.add_event_subscriber(cm)
  em.add_event_factory(ief)

  # set up the rules engine part 2
  bc = BundleContext.new(em)
  k = org.openhab.core.automation.internal.module.factory.CoreModuleHandlerFactory
  # depending on OH version, this class is set up differently
  cmhf = begin
    cmhf = k.new
    cmhf.item_registry = ir
    cmhf.event_publisher = ep
    cmhf.activate(bc)
    cmhf
  rescue ArgumentError
    k.new(bc, ep, ir)
  end

  rs = org.openhab.core.internal.service.ReadyServiceImpl.new
  re = org.openhab.core.automation.internal.RuleEngineImpl.new(mtr, rr, ss, rs)
  re.add_module_handler_factory(cmhf)
  re.add_module_handler_factory(scmhf)
  re.add_module_handler_factory(spmhf)
  re.onReadyMarkerAdded(nil)
end