Class: ActiveSensor::Equipment

Inherits:
Object
  • Object
show all
Defined in:
lib/active_sensor/equipment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vendor, model, options = {}) ⇒ Equipment

Initialize an equipment with given parameters


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_sensor/equipment.rb', line 82

def initialize(vendor, model, options = {})
  # options.symbolize_keys!
  @vendor = vendor.to_sym
  raise 'Need vendor' unless @vendor
  @model = model.to_sym
  raise 'Need model' unless @model
  if options[:indicators]
    @indicators = options[:indicators].collect do |i|
      raise "Invalid indicator: #{i.inspect}" unless Nomen::Indicator.find(i)
      i.to_sym
    end
  end
  store_translation(:label, options[:label])
  store_translation(:description, options[:description])
  if options[:image_path]
    path = Pathname.new(options[:image_path])
    raise "Cannot find image #{options[:image_path]}" unless path.exist?
    @image_path = path
  end
  @controller = options[:controller].constantize if options[:controller]
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller


77
78
79
# File 'lib/active_sensor/equipment.rb', line 77

def controller
  @controller
end

#image_pathObject (readonly)

Returns the value of attribute image_path


76
77
78
# File 'lib/active_sensor/equipment.rb', line 76

def image_path
  @image_path
end

#indicatorsObject (readonly)

Returns the value of attribute indicators


75
76
77
# File 'lib/active_sensor/equipment.rb', line 75

def indicators
  @indicators
end

#modelObject (readonly)

Returns the value of attribute model


74
75
76
# File 'lib/active_sensor/equipment.rb', line 74

def model
  @model
end

#vendorObject (readonly)

Returns the value of attribute vendor


73
74
75
# File 'lib/active_sensor/equipment.rb', line 73

def vendor
  @vendor
end

Class Method Details

.connect(vendor, model, access_parameters = {}) ⇒ Object


67
68
69
70
# File 'lib/active_sensor/equipment.rb', line 67

def connect(vendor, model, access_parameters = {})
  equipment = find!(vendor, model)
  equipment.connect(access_parameters)
end

.equipments_of(vendor) ⇒ Object


41
42
43
44
45
# File 'lib/active_sensor/equipment.rb', line 41

def equipments_of(vendor)
  list.select do |equipment|
    equipment.vendor == vendor.to_sym
  end
end

.erase(vendor, model) ⇒ Object


47
48
49
50
# File 'lib/active_sensor/equipment.rb', line 47

def erase(vendor, model)
  equipment = find(vendor, model)
  list.delete(equipment)
end

.find(vendor, model) ⇒ Object


52
53
54
55
56
57
# File 'lib/active_sensor/equipment.rb', line 52

def find(vendor, model)
  return nil unless vendor && model
  list.detect do |equipment|
    equipment.vendor == vendor.to_sym && equipment.model == model.to_sym
  end
end

.find!(vendor, model) ⇒ Object


59
60
61
62
63
64
65
# File 'lib/active_sensor/equipment.rb', line 59

def find!(vendor, model)
  equipment = find(vendor, model)
  unless equipment
    raise EquipmentNotFound, "Cannot find vendor=#{vendor.inspect}, model=#{model.inspect}"
  end
  equipment
end

.register(vendor, model, options = {}) ⇒ Object

Register an equipment in global base


9
10
11
12
13
14
15
16
17
# File 'lib/active_sensor/equipment.rb', line 9

def register(vendor, model, options = {})
  # puts "Register #{vendor.to_s.yellow} #{model.to_s.green}"
  if ActiveSensor::Equipment.find(vendor, model)
    Rails.logger.warn "Equipment #{vendor}:#{model} already exists. Will be overriden"
    erase(vendor, model)
  end
  list << ActiveSensor::Equipment.new(vendor, model, options)
  true
end

.register_many(path, _options = {}) ⇒ Object

Register a YAML file of many equipment

<vendor>:
  <model>:
    label: ...
    description: ...
    controller: ...
    indicators: ...

26
27
28
29
30
31
32
33
34
# File 'lib/active_sensor/equipment.rb', line 26

def register_many(path, _options = {})
  raise "Cannot find #{path}" unless Pathname(path).exist?
  sensors = YAML.load_file(path).deep_symbolize_keys
  sensors.each do |vendor, models|
    models.each do |model, options|
      register(vendor, model, options)
    end
  end
end

.vendorsObject

List all vendors


37
38
39
# File 'lib/active_sensor/equipment.rb', line 37

def vendors
  list.collect(&:vendor).uniq
end

Instance Method Details

#connect(access_parameters = {}) ⇒ Object

Returns ActiveSensor::Connection with permit to retrieve data from sensor


106
107
108
# File 'lib/active_sensor/equipment.rb', line 106

def connect(access_parameters = {})
  ActiveSensor::Connection.new(self, access_parameters)
end

#description(options = {}) ⇒ Object

Returns i18nized description


120
121
122
# File 'lib/active_sensor/equipment.rb', line 120

def description(options = {})
  translate(:description, options)
end

#label(options = {}) ⇒ Object

Returns i18nized label


115
116
117
# File 'lib/active_sensor/equipment.rb', line 115

def label(options = {})
  translate(:label, options) || @model.to_s.humanize
end

#unique_nameObject


110
111
112
# File 'lib/active_sensor/equipment.rb', line 110

def unique_name
  "#{@vendor}##{@model}"
end