Class: CS::EndPoint::SensorData

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

Overview

This object represent a sensor data point usage example:

client = CS::Client.new
client.login('username', 'password')

# Find the first position sensor
sensor = client.sensors.find_by_name(/position/).first

# save data point
data = sensor.data.build
data.date = Time.now
data.value = {"lux": 1}
data.save!

# more compact version
sensor.data.build(date: Time.now, value: {"lux => 1}).save!

Instance Attribute Summary collapse

Attributes included from CS::EndPoint

#session

Instance Method Summary collapse

Methods included from CS::EndPoint

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

Methods included from Serializer

#from_hash, #to_h

Instance Attribute Details

#monthObject

Returns the value of attribute month.



24
25
26
# File 'lib/cs/end_point/sensor_data.rb', line 24

def month
  @month
end

#weekObject

Returns the value of attribute week.



24
25
26
# File 'lib/cs/end_point/sensor_data.rb', line 24

def week
  @week
end

#yearObject

Returns the value of attribute year.



24
25
26
# File 'lib/cs/end_point/sensor_data.rb', line 24

def year
  @year
end

Instance Method Details

#date_humanObject



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

def date_human
  Time.new(@date)
end

#delete_urlObject



76
77
78
# File 'lib/cs/end_point/sensor_data.rb', line 76

def delete_url
  "/sensors/#{sensor_id}/data/#{id}.json"
end

#get_urlObject



68
69
70
# File 'lib/cs/end_point/sensor_data.rb', line 68

def get_url
  "/sensors/#{sensor_id}/data/#{id}.json"
end

#post_urlObject



64
65
66
# File 'lib/cs/end_point/sensor_data.rb', line 64

def post_url
  "/sensors/#{sensor_id}/data.json"
end

#put_urlObject



72
73
74
# File 'lib/cs/end_point/sensor_data.rb', line 72

def put_url
  "/sensors/#{sensor_id}/data/#{id}.json"
end

#retrieve!Object

there is no currently end point for geting data by id



51
52
53
# File 'lib/cs/end_point/sensor_data.rb', line 51

def retrieve!
  raise Error::NotImplementedError, "There is no current end point to get sensor data by id"
end

#scan_header_for_id(location_header) ⇒ Object



60
61
62
# File 'lib/cs/end_point/sensor_data.rb', line 60

def scan_header_for_id(location_header)
  location_header.scan(/.*\/sensors\/(.*)\/(.*)/)[1] if location_header
end

#to_cs_valueObject



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

def to_cs_value
  param = self.to_h(false)
  param.delete(:sensor_id)
  value = param[:value]
  if value
    param[:value] = value.to_json unless value.kind_of?(String) || value.kind_of?(Numeric)
  end

  param[:date] = CS::Time.new(date).to_f if param[:date]

  param
end

#to_parametersObject



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

def to_parameters
  {data: [to_cs_value]}
end

#update!Object

there is no currently end point for updating data



56
57
58
# File 'lib/cs/end_point/sensor_data.rb', line 56

def update!
  raise Error::NotImplementedError, "There is no current end point to update sensor data by id"
end