class Dovado::Router::Services

Router Services.

@since 1.0.0

Attributes

home_automation[R]

Get status of home automation service

@return [String] a string with “enabled” or “disabled” @since 1.0.3

sms[R]

Get status of sms service

@return [String] a string with “enabled” or “disabled” @since 1.0.3

Public Class Methods

new(args=nil) click to toggle source

Create a new {Services} object.

@param [Hash] args optional argiments

# File lib/dovado/router/services.rb, line 27
def initialize(args=nil)
  @list = ThreadSafe::Cache.new
  @last_update = nil
  unless args.nil?
    args.each do |k,v|
      @list[Utilities.name_to_sym(k)] = v
    end
    touch!
  end
end
setup_supervision!() click to toggle source

@api private

# File lib/dovado/router/services.rb, line 122
def self.setup_supervision!
  return supervise as: :router_services, size: 1 unless Actor[:router_services]
  return supervise as: :router_services, size: 1 if Actor[:router_services] and Actor[:router_services].dead?
end

Public Instance Methods

[](key) click to toggle source

Fetch an entry from the {Services} object.

@param [Symbol] key The key to fetch.

# File lib/dovado/router/services.rb, line 70
def [](key)
  @list[key]
end
create_from_string(data_string=nil) click to toggle source

Create a new {Services} object from a string with values from the router API.

@param [String] data_string String with data from fetched from the

router.

@return [Services] a new {Services} object.

# File lib/dovado/router/services.rb, line 53
def create_from_string(data_string=nil)
  data_array = data_string.split("\n")
  data_array.each do |data_entry|
    entry_array = data_entry.split('=')
    if entry_array.length == 2
      key = entry_array[0].downcase
      val = entry_array[1]
      keysym = Utilities.name_to_sym(key)
      @list[keysym] = val
    end
  end
  touch!
end
has_key?(key) click to toggle source

Check if the {Services} object has a given key.

@param [Symbol] key the key to check for. @return [Boolean] true or false

# File lib/dovado/router/services.rb, line 85
def has_key?(key)
  keys.member?(key)
end
home_automation?() click to toggle source

Boolean check if home automation is enabled

@return [Boolean] true or false @since 1.0.3

# File lib/dovado/router/services.rb, line 117
def home_automation?
  home_automation ? (home_automation == "enabled") : false
end
keys() click to toggle source

Fetch the list of entries in the {Services} object.

@return [Array<Symbol>]

# File lib/dovado/router/services.rb, line 77
def keys
  @list.keys
end
sms?() click to toggle source

Boolean check if sms service is enabled

@return [Boolean] true or false @since 1.0.3

# File lib/dovado/router/services.rb, line 105
def sms?
  sms ? (sms == "enabled") : false
end
update!() click to toggle source

Update the data in this {Services} object.

# File lib/dovado/router/services.rb, line 39
def update!
  client = Actor[:client]
  client.connect unless client.connected?
  client.authenticate unless client.authenticated?
  string = client.command('services')
  create_from_string string
end
valid?() click to toggle source

Checks if this {Services} object is still valid.

@return [Boolean] true or false.

# File lib/dovado/router/services.rb, line 92
def valid?
  return false if @last_update.nil?
  (@last_update + SecureRandom.random_number(9) + 1 <= Time.now.to_i)
end

Private Instance Methods

touch!() click to toggle source
# File lib/dovado/router/services.rb, line 129
def touch!
  @last_update = Time.now.to_i
end