Class: Orchestrator::Core::SystemProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/orchestrator/core/system_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(thread, sys_id, origin = nil, user = nil) ⇒ SystemProxy

Returns a new instance of SystemProxy.



7
8
9
10
11
12
# File 'lib/orchestrator/core/system_proxy.rb', line 7

def initialize(thread, sys_id, origin = nil, user = nil)
    @system = sys_id.to_sym
    @thread = thread
    @origin = origin    # This is the module that requested the proxy
    @user = user
end

Instance Method Details

#[](mod) ⇒ Object

Alias for get



15
16
17
# File 'lib/orchestrator/core/system_proxy.rb', line 15

def [](mod)
    get mod
end

#all(mod) ⇒ ::Orchestrator::Core::RequestsProxy

Provides a proxy to multiple modules. A simple way to send commands to multiple devices

Parameters:

  • module (String, Symbol)

    the name of the module in the system

Returns:



47
48
49
# File 'lib/orchestrator/core/system_proxy.rb', line 47

def all(mod)
    RequestsProxy.new(@thread, system.all(mod.to_sym), @user)
end

#count(mod) ⇒ Integer

Grabs the number of a particular device type

Parameters:

  • module (String, Symbol)

    the name of the module in the system

Returns:

  • (Integer)

    the number of modules with a shared name



69
70
71
# File 'lib/orchestrator/core/system_proxy.rb', line 69

def count(mod)
    system.count(mod.to_sym)
end

#each(*args) {|Module Instance, Symbol, Integer| ... } ⇒ Object

Iterates over the modules in the system. Can also specify module types.

Parameters:

  • mod_name (String, Symbol)

    the optional names of modules to iterate over

Yields:

  • (Module Instance, Symbol, Integer)

    yields the modules with their name and index



55
56
57
58
59
60
61
62
63
# File 'lib/orchestrator/core/system_proxy.rb', line 55

def each(*args)
    mods = args.empty? ? modules : args
    mods.each do |mod|
        number = count(mod)
        (1..number).each do |index|
            yield(get(mod, index), mod, index)
        end
    end
end

#exists?(mod, index = 1) ⇒ true, false

Checks for the existence of a particular module

Parameters:

  • module (String, Symbol)

    the name of the module in the system

  • index (Integer) (defaults to: 1)

    the index of the desired module (starting at 1)

Returns:

  • (true, false)

    does the module exist?



36
37
38
39
40
41
# File 'lib/orchestrator/core/system_proxy.rb', line 36

def exists?(mod, index = 1)
    index -= 1  # Get the real index
    name = mod.to_sym

    !system.get(name, index).nil?
end

#get(mod, index = 1) ⇒ ::Orchestrator::Core::RequestsProxy

Provides a proxy to a module for a safe way to communicate across threads

Parameters:

  • module (String, Symbol)

    the name of the module in the system

  • index (Integer) (defaults to: 1)

    the index of the desired module (starting at 1)

Returns:



24
25
26
27
28
29
# File 'lib/orchestrator/core/system_proxy.rb', line 24

def get(mod, index = 1)
    index -= 1  # Get the real index
    name = mod.to_sym

    RequestProxy.new(@thread, system.get(name, index), @user)
end

#idSymbol

Returns the system id as defined in the database

Returns:

  • (Symbol)

    the id of the system



90
91
92
# File 'lib/orchestrator/core/system_proxy.rb', line 90

def id
    @system
end

#modulesArray

Returns a list of all the module names in the system

Returns:

  • (Array)

    a list of all the module names



76
77
78
# File 'lib/orchestrator/core/system_proxy.rb', line 76

def modules
    system.modules
end

#nameString

Returns the system name as defined in the database

Returns:

  • (String)

    the name of the system



83
84
85
# File 'lib/orchestrator/core/system_proxy.rb', line 83

def name
    system.config.name
end

#subscribe(mod_name, index, status = nil, callback = nil, &block) ⇒ Object

Used to be notified when an update to a status value occurs

Parameters:

  • module (String, Symbol)

    the name of the module in the system

  • index (Integer)

    the index of the module as there may be more than one

  • status (String, Symbol) (defaults to: nil)

    the name of the status variable

  • callback (Proc) (defaults to: nil)

    method, block, proc or lambda to be called when a change occurs

Returns:

  • (Object)

    a reference to the subscription for un-subscribing



101
102
103
104
105
106
107
108
109
110
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
# File 'lib/orchestrator/core/system_proxy.rb', line 101

def subscribe(mod_name, index, status = nil, callback = nil, &block)
    # Allow index to be optional
    if not index.is_a?(Integer)
        callback = status || block
        status = index.to_sym
        index = 1
    else
        callback ||= block
    end
    mod_name = mod_name.to_sym

    raise 'callback required' unless callback.respond_to? :call

    # We need to get the system to schedule threads
    sys = system
    options = {
        sys_id: @system,
        sys_name: sys.config.name,
        mod_name: mod_name,
        index: index,
        status: status,
        callback: callback,
        on_thread: @thread
    }

    # if the module exists, subscribe on the correct thread
    # use a bit of promise magic as required
    mod_man = sys.get(mod_name, index - 1)
    sub = if mod_man
        defer = @thread.defer

        options[:mod_id] = mod_man.settings.id.to_sym
        options[:mod] = mod_man
        thread = mod_man.thread
        thread.schedule do
            defer.resolve (
                thread.observer.subscribe(options)
            )
        end

        defer.promise
    else
        @thread.observer.subscribe(options)
    end

    @origin.add_subscription sub if @origin
    sub
end