Class: AutomateEm::System
- Inherits:
-
Object
- Object
- AutomateEm::System
- Defined in:
- lib/automate-em/core/system.rb,
lib/automate-em/interfaces/html5.rb
Constant Summary collapse
- @@systems =
system_name => system instance
{:'---GOD---' => self}
- @@controllers =
controller_id => system instance
{}
- @@logger =
nil
- @@communicator =
AutomateEm::Communicator.new(self)
- @@god_lock =
Mutex.new
- @@socket_server =
nil
Instance Attribute Summary collapse
-
#communicator ⇒ Object
readonly
Returns the value of attribute communicator.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#modules ⇒ Object
readonly
Returns the value of attribute modules.
Class Method Summary collapse
- .[](system) ⇒ Object
-
.communicator ⇒ Object
def self.systems @@systems end.
-
.force_load_file(path) ⇒ Object
Allows for system updates on the fly Dangerous (Could be used to add on the fly interfaces).
- .instance ⇒ Object
-
.logger ⇒ Object
System Logger.
- .logger=(log) ⇒ Object
-
.modules ⇒ Object
For access via communicator as a super user.
- .new_system(controller, log_level = Logger::INFO) ⇒ Object
-
.reload(dep) ⇒ Object
Reloads a dependency live This is the re-load code function (live bug fixing - removing functions does not work).
- .start_websockets ⇒ Object
- .stop_websockets ⇒ Object
Instance Method Summary collapse
-
#[](mod) ⇒ Object
Module accessor.
-
#delete ⇒ Object
Unload and then destroy self.
- #instance ⇒ Object
-
#log_level(level) ⇒ Object
Log level changing on the fly.
-
#start(force = false) ⇒ Object
Starts the control system if not running.
-
#stop ⇒ Object
Stops the current control system Loops through the module instances.
Instance Attribute Details
#communicator ⇒ Object (readonly)
Returns the value of attribute communicator.
172 173 174 |
# File 'lib/automate-em/core/system.rb', line 172 def communicator @communicator end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
173 174 175 |
# File 'lib/automate-em/core/system.rb', line 173 def controller @controller end |
#logger ⇒ Object
Returns the value of attribute logger.
174 175 176 |
# File 'lib/automate-em/core/system.rb', line 174 def logger @logger end |
#modules ⇒ Object (readonly)
Returns the value of attribute modules.
171 172 173 |
# File 'lib/automate-em/core/system.rb', line 171 def modules @modules end |
Class Method Details
.[](system) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/automate-em/core/system.rb', line 138 def self.[] (system) system = system.to_sym if system.class == String @@god_lock.synchronize { @@systems[system] } end |
.communicator ⇒ Object
def self.systems @@systems end
134 135 136 |
# File 'lib/automate-em/core/system.rb', line 134 def self.communicator @@communicator end |
.force_load_file(path) ⇒ Object
Allows for system updates on the fly Dangerous (Could be used to add on the fly interfaces)
110 111 112 |
# File 'lib/automate-em/core/system.rb', line 110 def self.force_load_file(path) load path if File.exists?(path) && File.extname(path) == '.rb' end |
.instance ⇒ Object
153 154 155 |
# File 'lib/automate-em/core/system.rb', line 153 def self.instance self end |
.logger ⇒ Object
System Logger
118 119 120 |
# File 'lib/automate-em/core/system.rb', line 118 def self.logger @@logger end |
.logger=(log) ⇒ Object
122 123 124 |
# File 'lib/automate-em/core/system.rb', line 122 def self.logger=(log) @@logger = log end |
.modules ⇒ Object
For access via communicator as a super user
150 151 152 |
# File 'lib/automate-em/core/system.rb', line 150 def self.modules self end |
.new_system(controller, log_level = Logger::INFO) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/automate-em/core/system.rb', line 12 def self.new_system(controller, log_level = Logger::INFO) begin if controller.class == Fixnum controller = ControlSystem.find(controller) elsif controller.class == String controller = ControlSystem.where('name = ?', controller).first end if controller.class != ControlSystem raise 'invalid controller identifier' end @@god_lock.lock if @@controllers[controller.id].nil? @@god_lock.unlock sys = System.new(controller, log_level) if controller.active begin sys.start(true) # as this is loading the first time we ignore controller active rescue => e AutomateEm.print_error(@@logger, e, { :message => "Error starting system in new_system, stopping..", :level => Logger::ERROR }) begin sys.stop EM.schedule do EM.add_timer(60) do # Attempt a single restart EM.defer do begin sys.start(true) rescue => e begin AutomateEm.print_error(@@logger, e, { :message => "Second start attempt failed..", :level => Logger::ERROR }) sys.stop rescue end end end end end rescue => e AutomateEm.print_error(@@logger, e, { :message => "Error stopping system after problems starting..", :level => Logger::ERROR }) end end end else @@god_lock.unlock end ensure ActiveRecord::Base.clear_active_connections! # Clear any unused connections end end |
.reload(dep) ⇒ Object
Reloads a dependency live This is the re-load code function (live bug fixing - removing functions does not work)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/automate-em/core/system.rb', line 77 def self.reload(dep) System.logger.info "reloading dependency: #{dep}" dep = Dependency.find(dep) Modules.load_module(dep) updated = {} dep.devices.select('id').each do |dev| begin inst = DeviceModule.instance_of(dev.id) inst.on_update if (!!!updated[inst]) && inst.respond_to?(:on_update) ensure updated[inst] = true end end updated = {} dep.logics.select('id').each do |log| begin inst = LogicModule.instance_of(log.id) inst.on_update if (!!!updated[inst]) && inst.respond_to?(:on_update) ensure updated[inst] = true end end ActiveRecord::Base.clear_active_connections! # Clear any unused connections end |
.start_websockets ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/automate-em/interfaces/html5.rb', line 249 def self.start_websockets EM.schedule do if @@socket_server.nil? @@socket_server = EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 81) do |socket| # , :debug => true socket.onopen { # # This socket represents a connected device # HTML5Monitor.register(socket) } socket. { |data| # # Attach socket here to system # then process commands # HTML5Monitor.receive(socket, data) } socket.onclose { HTML5Monitor.unregister(socket) } socket.onerror { |error| if !error.kind_of?(EM::WebSocket::WebSocketError) EM.defer do AutomateEm.print_error(AutomateEm::System.logger, error, { :message => "in html5.rb, onerror : issue with websocket data", :level => Logger::ERROR }) end else EM.defer do AutomateEm::System.logger.info "in html5.rb, onerror : invalid handshake received - #{error.inspect}" end end } end end end EM.defer do AutomateEm::System.logger.info 'running HTML5 socket server on port 81' end end |
.stop_websockets ⇒ Object
295 296 297 298 299 300 |
# File 'lib/automate-em/interfaces/html5.rb', line 295 def self.stop_websockets EM.schedule do EventMachine::stop_server(@@socket_server) unless @@socket_server.nil? @@socket_server = nil end end |
Instance Method Details
#[](mod) ⇒ Object
Module accessor
166 167 168 169 |
# File 'lib/automate-em/core/system.rb', line 166 def [] (mod) mod = mod.to_sym if mod.class == String @modules[mod].instance end |
#delete ⇒ Object
Unload and then destroy self
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/automate-em/core/system.rb', line 236 def delete System.logger.info "deleting #{@controller.name}" @sys_lock.synchronize { stop_nolock @@god_lock.synchronize { @@systems.delete(@controller.name.to_sym) @@controllers.delete(@controller.id) } begin @controller.destroy! rescue # Controller may already be deleted end @modules = nil } end |
#instance ⇒ Object
156 157 158 |
# File 'lib/automate-em/core/system.rb', line 156 def instance self end |
#log_level(level) ⇒ Object
Log level changing on the fly
259 260 261 262 263 264 265 266 |
# File 'lib/automate-em/core/system.rb', line 259 def log_level(level) @sys_lock.synchronize { @log_level = AutomateEm::get_log_level(level) if @controller.active @logger.level = @log_level end } end |
#start(force = false) ⇒ Object
Starts the control system if not running
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/automate-em/core/system.rb', line 181 def start(force = false) System.logger.info "starting #{@controller.name}" @sys_lock.synchronize { @@god_lock.synchronize { @@systems.delete(@controller.name.to_sym) @controller.reload #(:lock => true) @@systems[@controller.name.to_sym] = self } if !@controller.active || force if @logger.nil? if Rails.env.production? @logger = Logger.new(Rails.root.join("log/system_#{@controller.id}.log").to_s, 10, 4194304) else @logger = Logger.new(STDOUT) end @logger.formatter = proc { |severity, datetime, progname, msg| "#{datetime.strftime("%d/%m/%Y @ %I:%M%p")} #{severity}: #{@controller.name} - #{msg}\n" } end @controller.devices.includes(:dependency).each do |device| load_hooks(device, DeviceModule.new(self, device)) end @controller.services.includes(:dependency).each do |service| load_hooks(service, ServiceModule.new(self, service)) end @controller.logics.includes(:dependency).each do |logic| load_hooks(logic, LogicModule.new(self, logic)) end end @controller.active = true @controller.save @communicator.start } end |