Class: FSEvent::FailSafeDevice

Inherits:
AbstractDevice show all
Defined in:
lib/fsevent/failsafedevice.rb

Overview

failsafedevice.rb — fail safe device class

Copyright © 2014 National Institute of Advanced Industrial Science and Technology (AIST)

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Instance Attribute Summary

Attributes inherited from AbstractDevice

#framework, #name, #schedule

Instance Method Summary collapse

Methods inherited from AbstractDevice

#add_watch, #define_status, #set_elapsed_time, #status_changed, #unregister_device, #unregistered

Constructor Details

#initialize(device_name, initial_status, *watchee_device_names) ⇒ FailSafeDevice

Returns a new instance of FailSafeDevice.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fsevent/failsafedevice.rb', line 19

def initialize(device_name, initial_status, *watchee_device_names)
  super device_name
  raise "One devices required at least" if watchee_device_names.empty?
  @current_status = {} # status_name -> value
  @current_status_list = {} # status_name -> watchee_device_name status_name -> value
  @status_merger = {}
  initial_status.each {|k, v, merger|
    @current_status[k] = v
    @status_merger[k] = merger_callable(merger)
    @current_status_list[k] = {}
    watchee_device_names.each {|watchee_device_name|
      @current_status_list[k][watchee_device_name] = v
    }
  }
  @watchee_device_names = watchee_device_names
end

Instance Method Details

#merger_callable(merger) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fsevent/failsafedevice.rb', line 36

def merger_callable(merger)
  case merger
  when :max
    method(:merger_max)
  when :min
    method(:merger_min)
  when :lazy
    method(:merger_lazy)
  else
    merger
  end
end

#merger_lazy(cur, *values) ⇒ Object



51
# File 'lib/fsevent/failsafedevice.rb', line 51

def merger_lazy(cur, *values) values.uniq.length == 1 ? values[0] : cur end

#merger_max(cur, *values) ⇒ Object



49
# File 'lib/fsevent/failsafedevice.rb', line 49

def merger_max(cur, *values) values.max end

#merger_min(cur, *values) ⇒ Object



50
# File 'lib/fsevent/failsafedevice.rb', line 50

def merger_min(cur, *values) values.min end

#registeredObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/fsevent/failsafedevice.rb', line 53

def registered
  @watchee_device_names.each {|n|
    @current_status.each {|k, v|
      add_watch n, k
    }
  }
  @current_status.each {|k, v|
    define_status k, v
  }
end

#run(watched_status_change) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fsevent/failsafedevice.rb', line 64

def run(watched_status_change)
  updated = {}
  watched_status_change.each {|watchee_device_name, h|
    h.each {|status_name, value|
      unless updated.has_key? status_name
        updated[status_name] = @current_status_list[status_name]
      end
      updated[status_name][watchee_device_name] = value
    }
  }
  updated.each {|status_name, h|
    merger = @status_merger[status_name]
    cur_val = @current_status[status_name]
    values = @watchee_device_names.map {|d| h[d] }
    new_val = merger.call(cur_val, *values)
    if cur_val != new_val
      @current_status[status_name] = new_val
      status_changed status_name, new_val
    end
  }
end