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.
-
#initialize(thread, sys_id, origin = 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) ⇒ SystemProxy
Returns a new instance of SystemProxy.
7 8 9 10 11 |
# File 'lib/orchestrator/core/system_proxy.rb', line 7 def initialize(thread, sys_id, origin = nil) @system = sys_id.to_sym @thread = thread @origin = origin # This is the module that requested the proxy end |
Instance Method Details
#[](mod) ⇒ Object
Alias for get
14 15 16 |
# File 'lib/orchestrator/core/system_proxy.rb', line 14 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
46 47 48 49 |
# File 'lib/orchestrator/core/system_proxy.rb', line 46 def all(mod) name = mod.to_sym RequestsProxy.new(@thread, system.all(name)) end |
#count(mod) ⇒ Integer
Grabs the number of a particular device type
69 70 71 72 |
# File 'lib/orchestrator/core/system_proxy.rb', line 69 def count(mod) name = mod.to_sym system.count(name) 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
35 36 37 38 39 40 |
# File 'lib/orchestrator/core/system_proxy.rb', line 35 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
23 24 25 26 27 28 |
# File 'lib/orchestrator/core/system_proxy.rb', line 23 def get(mod, index = 1) index -= 1 # Get the real index name = mod.to_sym RequestProxy.new(@thread, system.get(name, index)) end |
#modules ⇒ Array
Returns a list of all the module names in the system
77 78 79 |
# File 'lib/orchestrator/core/system_proxy.rb', line 77 def modules system.modules end |
#name ⇒ String
Returns the system name as defined in the database
84 85 86 |
# File 'lib/orchestrator/core/system_proxy.rb', line 84 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
95 96 97 98 99 100 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 |
# File 'lib/orchestrator/core/system_proxy.rb', line 95 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 |