Class: Dovado::Router::Services

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/dovado/router/services.rb

Overview

Router Services.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Services

Create a new Dovado::Router::Services object.

Parameters:

  • args (Hash) (defaults to: nil)

    optional argiments

Since:

  • 1.0.0


27
28
29
30
31
32
33
34
35
36
# 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

Instance Attribute Details

#home_automationString (readonly)

Get status of home automation service

Returns:

  • (String)

    a string with “enabled” or “disabled”

Since:

  • 1.0.3


22
23
24
# File 'lib/dovado/router/services.rb', line 22

def home_automation
  @home_automation
end

#smsString (readonly)

Get status of sms service

Returns:

  • (String)

    a string with “enabled” or “disabled”

Since:

  • 1.0.3


16
17
18
# File 'lib/dovado/router/services.rb', line 16

def sms
  @sms
end

Class Method Details

.setup_supervision!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0


122
123
124
125
# 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

Instance Method Details

#[](key) ⇒ Object

Fetch an entry from the Dovado::Router::Services object.

Parameters:

  • key (Symbol)

    The key to fetch.

Since:

  • 1.0.0


70
71
72
# File 'lib/dovado/router/services.rb', line 70

def [](key)
  @list[key]
end

#create_from_string(data_string = nil) ⇒ Services

Create a new Dovado::Router::Services object from a string with values from the router API.

Parameters:

  • data_string (String) (defaults to: nil)

    String with data from fetched from the router.

Returns:

Since:

  • 1.0.0


53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Boolean

Check if the Dovado::Router::Services object has a given key.

Parameters:

  • key (Symbol)

    the key to check for.

Returns:

  • (Boolean)

    true or false

Since:

  • 1.0.0


85
86
87
# File 'lib/dovado/router/services.rb', line 85

def has_key?(key)
  keys.member?(key)
end

#home_automation?Boolean

Boolean check if home automation is enabled

Returns:

  • (Boolean)

    true or false

Since:

  • 1.0.3


117
118
119
# File 'lib/dovado/router/services.rb', line 117

def home_automation?
  home_automation ? (home_automation == "enabled") : false
end

#keysArray<Symbol>

Fetch the list of entries in the Dovado::Router::Services object.

Returns:

  • (Array<Symbol>)

Since:

  • 1.0.0


77
78
79
# File 'lib/dovado/router/services.rb', line 77

def keys
  @list.keys
end

#sms?Boolean

Boolean check if sms service is enabled

Returns:

  • (Boolean)

    true or false

Since:

  • 1.0.3


105
106
107
# File 'lib/dovado/router/services.rb', line 105

def sms?
  sms ? (sms == "enabled") : false
end

#update!Object

Update the data in this Dovado::Router::Services object.

Since:

  • 1.0.0


39
40
41
42
43
44
45
# 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?Boolean

Checks if this Dovado::Router::Services object is still valid.

Returns:

  • (Boolean)

    true or false.

Since:

  • 1.0.0


92
93
94
95
# 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