Class: Orchestrator::Core::SystemProxy
- Inherits:
-
Object
- Object
- Orchestrator::Core::SystemProxy
- Defined in:
- lib/orchestrator/core/system_proxy.rb
Instance Method Summary collapse
-
#[](mod) ⇒ Object
Alias for get.
-
#all(mod) ⇒ ::Orchestrator::Core::RequestsProxy
Provides a proxy to multiple modules.
-
#count(mod) ⇒ Integer
Grabs the number of a particular device type.
-
#each(*args) {|Module Instance, Symbol, Integer| ... } ⇒ Object
Iterates over the modules in the system.
-
#exists?(mod, index = 1) ⇒ true, false
Checks for the existence of a particular module.
-
#get(mod, index = 1) ⇒ ::Orchestrator::Core::RequestsProxy
Provides a proxy to a module for a safe way to communicate across threads.
-
#id ⇒ Symbol
Returns the system id as defined in the database.
-
#initialize(thread, sys_id, origin = nil, user = nil) ⇒ SystemProxy
constructor
A new instance of SystemProxy.
-
#modules ⇒ Array
Returns a list of all the module names in the system.
-
#name ⇒ String
Returns the system name as defined in the database.
-
#subscribe(mod_name, index, status = nil, callback = nil, &block) ⇒ Object
Used to be notified when an update to a status value occurs.
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
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
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.
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
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
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 |
#id ⇒ Symbol
Returns the system id as defined in the database
90 91 92 |
# File 'lib/orchestrator/core/system_proxy.rb', line 90 def id @system end |
#modules ⇒ Array
Returns a list of all the module names in the system
76 77 78 |
# File 'lib/orchestrator/core/system_proxy.rb', line 76 def modules system.modules end |
#name ⇒ String
Returns the system name as defined in the database
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
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 = { 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 [:mod_id] = mod_man.settings.id.to_sym [:mod] = mod_man thread = mod_man.thread thread.schedule do defer.resolve ( thread.observer.subscribe() ) end defer.promise else @thread.observer.subscribe() end @origin.add_subscription sub if @origin sub end |