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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openhab/core/proxy.rb', line 47

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[0...-1]
  @object_type = object_type.downcase.to_sym
  @type = @parent_module.const_get(object_type, false)

  @event_types = @parent_module::Proxy::EVENTS
  @uid_method = @parent_module::Proxy::UID_METHOD
  @uid_type = @parent_module::Proxy::UID_TYPE
  @registry = @parent_module::Provider.registry
  @registration = OSGi.register_service(self)
  ScriptHandling.script_unloaded { @registration.unregister }
end

Instance Attribute Details

#subscribed_event_typesSet<String> (readonly)

Returns:

  • (Set<String>)


66
67
68
# File 'lib/openhab/core/proxy.rb', line 66

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)


71
72
73
# File 'lib/openhab/core/proxy.rb', line 71

def event_filter
  nil
end

#fetch(object) ⇒ Object

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/openhab/core/proxy.rb', line 96

def fetch(object)
  result = nil

  uid = if object.is_a?(@type)
          object.__send__(@uid_method)
        else
          object
        end
  @proxies.compute(uid) 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:



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openhab/core/proxy.rb', line 79

def receive(event)
  uid = event.__send__(@object_type).__send__(@uid_method)
  uid = @uid_type.new(uid) unless @uid_type == String

  @proxies.compute_if_present(uid) do |_, proxy_ref|
    object = @registry.get(uid) unless event.class.simple_name == @event_types.last
    proxy = resolve_ref(proxy_ref)
    next nil unless proxy

    proxy.__setobj__(object)
    proxy_ref
  end
end