Class: TriggerSwitchD::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/trigger_switch_d/application.rb

Overview

starts an instance of the daemon.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ Application

Prepares the run by loading up the environment located in config/config.rb



32
33
34
35
36
37
# File 'lib/trigger_switch_d/application.rb', line 32

def initialize(config_file)
  @current_date = Date.new
  ConfigFactory.new(config_file)
  @logger = Logger.new(Config.log_object)
  logger.info("Environment prepared")
end

Instance Attribute Details

#devicesObject (readonly) Also known as: list_devices

Returns the value of attribute devices.



27
28
29
# File 'lib/trigger_switch_d/application.rb', line 27

def devices
  @devices
end

#loggerObject (readonly)

Returns the value of attribute logger.



27
28
29
# File 'lib/trigger_switch_d/application.rb', line 27

def logger
  @logger
end

#scheduleObject (readonly)

Returns the value of attribute schedule.



27
28
29
# File 'lib/trigger_switch_d/application.rb', line 27

def schedule
  @schedule
end

Instance Method Details

#list_scheduleObject

:nodoc:



67
68
69
# File 'lib/trigger_switch_d/application.rb', line 67

def list_schedule #:nodoc:
  schedule.by_name.keys
end

#startObject

Runs the daemon untill process killed or it receives stop from client connected via unix socket



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
# File 'lib/trigger_switch_d/application.rb', line 41

def start
  com_thread = IPC.create(Config.unix_socket_path)
  logger.info("IPC started")
  com_thread["received"] = "anything"
  while ((command = com_thread["received"].to_sym) != :stop)
    now_time = Time.new
    load_db(now_time.strftime("%Y-%m-%d %H:%M:%S"))
    hhmm_now_time = now_time.strftime("%H:%M")
    if methods.include? command.to_s
      com_thread["response"] = send(command).to_yaml
      logger.info("Received #{command} via IPC")
      Config.log_object.flush
      com_thread["received"] = "anything"
      com_thread.run
    end
    if (schedule.cronological.keys.include? hhmm_now_time)
      logger.info("Executed #{schedule.cronological[hhmm_now_time].length} action(s) at #{hhmm_now_time}")
      Config.log_object.flush
      schedule.execute(hhmm_now_time,Config.output)
    end
    Kernel.sleep(1)
    com_thread["received"] = "anything" if com_thread["received"] == ""
  end
  logger.info("Received #{command} via IPC")
end