Class: OpenHAB::Core::Proxy::EventSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/openhab/core/proxy.rb

Overview

Registers and listens to openHAB bus events for objects getting added/updated/removed, and updates references from proxy objects to real objects.

Proxies are tracked (via a WeakRef), and their underlying object is if it has changed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventSubscriber

Returns a new instance of EventSubscriber.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/openhab/core/proxy.rb', line 39

def initialize
  @proxies = java.util.concurrent.ConcurrentHashMap.new
  @parent_module = Object.const_get(self.class.name.split("::")[0..-3].join("::"), false)
  @object_type = @parent_module.name.split("::").last.downcase[0..-2].to_sym

  @event_types = @parent_module::Proxy::EVENTS
  @uid_method = @parent_module::Proxy::UID_METHOD
  @registry = @parent_module::Provider.registry
  @registration = OSGi.register_service(self, "event.topics": "openhab/*")
  ScriptHandling.script_unloaded { @registration.unregister }
end

Instance Attribute Details

#subscribed_event_typesSet<String> (readonly)

Returns:

  • (Set<String>)


55
56
57
# File 'lib/openhab/core/proxy.rb', line 55

def subscribed_event_types
  @event_types.to_set
end

Instance Method Details

#event_filterorg.openhab.core.events.EventFilter?

Returns:

  • (org.openhab.core.events.EventFilter, nil)


60
61
62
# File 'lib/openhab/core/proxy.rb', line 60

def event_filter
  nil
end

#fetch(object) ⇒ Object

Get or create a Proxy for the given raw openHAB object.



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/openhab/core/proxy.rb', line 84

def fetch(object)
  result = nil

  @proxies.compute(object.__send__(@uid_method)) do |_k, proxy_ref|
    result = resolve_ref(proxy_ref)
    proxy_ref = nil unless result
    result ||= yield

    proxy_ref || WeakRef.new(result)
  end

  result
end

#receive(event) ⇒ void

This method returns an undefined value.

Parameters:



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/openhab/core/proxy.rb', line 68

def receive(event)
  uid = event.__send__(@object_type).__send__(@uid_method)
  object = @registry.get(uid) unless event.class.simple_name == @event_types.last

  @proxies.compute_if_present(uid) do |_, proxy_ref|
    proxy = resolve_ref(proxy_ref)
    next nil unless proxy

    proxy.__setobj__(object)
    proxy_ref
  end
end