Class: CS::EndPoint::Sensor

Inherits:
Object
  • Object
show all
Includes:
CS::EndPoint
Defined in:
lib/cs/end_point/sensor.rb

Instance Attribute Summary

Attributes included from CS::EndPoint

#session

Instance Method Summary collapse

Methods included from CS::EndPoint

#create, #create!, #delete, #delete!, #duplicate, #inspect, #parameter, #reload, #reload!, #retrieve, #save, #save!, #update, #update!, #url_for

Methods included from Serializer

#from_hash, #to_h

Constructor Details

#initialize(hash = {}) ⇒ Sensor

Returns a new instance of Sensor.



10
11
12
13
# File 'lib/cs/end_point/sensor.rb', line 10

def initialize(hash={})
  from_hash(hash)
  parse_data_structure
end

Instance Method Details

#copy_data(sensor, parameters = {}) ⇒ Object

Copy data from other sensor

example :

source = client.sensors.find(1234)
destination = clint.sensors.find(2345)

destination.copy_data(source, start_date: 12345, end_date: 12350)


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cs/end_point/sensor.rb', line 59

def copy_data(sensor, parameters={})
  source = sensor.data
  parameters.each do |k,v|
    source.send(k.to_sym, v)
  end

  collection = self.data.collection
  source.each do |point|
    collection.push self.data.build(date: point.date, value: point.value)
  end

  collection.save!
end

#dataObject



46
47
48
# File 'lib/cs/end_point/sensor.rb', line 46

def data
  Relation::SensorDataRelation.new(self.id, self.session)
end

#parse_data_structureObject



21
22
23
24
25
26
27
# File 'lib/cs/end_point/sensor.rb', line 21

def parse_data_structure
  if self.data_type == "json"
    if self.data_structure && self.data_structure.kind_of?(String)
      self.data_structure = JSON.parse(self.data_structure) rescue nil
    end
  end
end

#retrieve!Object



15
16
17
18
19
# File 'lib/cs/end_point/sensor.rb', line 15

def retrieve!
  super
  parse_data_structure
  true
end

#to_cs_valueObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/cs/end_point/sensor.rb', line 30

def to_cs_value
  param = self.to_h(false)
  if param[:data_type] == "json"
    if param[:data_structure] && !param[:data_structure].kind_of?(String)
      param[:data_structure] = param[:data_structure].to_json
    end
  end

  param
end

#to_parametersObject

overide Endpoint#to_parameters



42
43
44
# File 'lib/cs/end_point/sensor.rb', line 42

def to_parameters
  {sensor: to_cs_value}
end