class Orchestrator::Core::SystemProxy
Public Class Methods
# 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
Public Instance Methods
Alias for get
# File lib/orchestrator/core/system_proxy.rb, line 15 def [](mod) get mod end
Provides a proxy to multiple modules. A simple way to send commands to multiple devices
@param module [String, Symbol] the name of the module in the system @return [::Orchestrator::Core::RequestsProxy] proxies requests to multiple modules
# File lib/orchestrator/core/system_proxy.rb, line 47 def all(mod) RequestsProxy.new(@thread, system.all(mod.to_sym), @user) end
Grabs the number of a particular device type
@param module [String, Symbol] the name of the module in the system @return [Integer] the number of modules with a shared name
# File lib/orchestrator/core/system_proxy.rb, line 69 def count(mod) system.count(mod.to_sym) end
Iterates over the modules in the system. Can also specify module types.
@param mod_name [String, Symbol] the optional names of modules to iterate over @yield [Module Instance, Symbol, Integer] yields the modules with their name and index
# 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
Checks for the existence of a particular module
@param module [String, Symbol] the name of the module in the system @param index [Integer] the index of the desired module (starting at 1) @return [true, false] does the module exist?
# 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
Provides a proxy to a module for a safe way to communicate across threads
@param module [String, Symbol] the name of the module in the system @param index [Integer] the index of the desired module (starting at 1) @return [::Orchestrator::Core::RequestsProxy] proxies requests to a single module
# 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
Returns the system id as defined in the database
@return [Symbol] the id of the system
# File lib/orchestrator/core/system_proxy.rb, line 90 def id @system end
Returns a list of all the module names in the system
@return [Array] a list of all the module names
# File lib/orchestrator/core/system_proxy.rb, line 76 def modules system.modules end
Returns the system name as defined in the database
@return [String] the name of the system
# File lib/orchestrator/core/system_proxy.rb, line 83 def name system.config.name end
Used to be notified when an update to a status value occurs
@param module [String, Symbol] the name of the module in the system @param index [Integer] the index of the module as there may be more than one @param status [String, Symbol] the name of the status variable @param callback [Proc] method, block, proc or lambda to be called when a change occurs @return [Object] a reference to the subscription for un-subscribing
# 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
Protected Instance Methods
# File lib/orchestrator/core/system_proxy.rb, line 154 def system ::Orchestrator::System.get(@system) end