Class: CS::Relation::SensorRelation
- Inherits:
-
Object
- Object
- CS::Relation::SensorRelation
- Includes:
- CS::Relation
- Defined in:
- lib/cs/relation/sensor_relation.rb
Overview
Class that used to query List of sensor data from CS.
parameters
The relation object has serveral parameter
-
page : Integer, number of page in pagination. starts from 0
-
per_page : Integer, number of sensor per page, default 1000, max: 1000
-
shared : Boolean, filter only sensor that is shared.
-
onwed : Boolean, filter only sensor that user own.
-
physical : Boolean, filter only physical sensor (sensor that connected to device).
-
details : String “no” or “full”, gives full description of sensor
Examples
This is an example how would you use the sensors relation object
Create Sensors Relation
client = CS::Client.new
client.login('user', 'password')
session = client.session
# create sensors relation
sensors = client.sensors
# is the same as
sensors = CS::Relation::Sensors.new
sensors.session = session
Get all sensor
sensors = client.sensors
sensors.to_a
Get sensor by specifying parameters
client.sensors.where(page: 0, per_page: 1000)
client.sensors.where(owned: true)
client.sensors.where(physical: true)
client.sensors.where(page: 0, per_page: 1000, physical: true, owned: true, details: "full")
Chain parameters
client.sensors.where(page:0, per_page: 10).where(physical: true)
Find sensor by name
client.sensors.find_by_name(/position/)
client.sensors.find_by_name(/position/, owned: true) # or
client.sensors.where(owned: true).find_by_name(/position/)
Get first sensor or last sensor
sensor = client.sensors.first
sensor = client.sensors.last
Get number of sensors
client.sensors.count
client.sensors.where(owned: true).count
Instance Method Summary collapse
-
#clone_from(other_sensor) ⇒ Object
Create new sensor with properties from other sensor.
- #find(id) ⇒ Object
-
#find!(id) ⇒ Object
Find Sensor by id.
-
#find_by_name(regex, parameters = {}) ⇒ Object
Find sensor by name in regular expression.
- #triggers ⇒ Object
Methods included from CS::Relation
#all, #build, #check_session!, #count, #each, #each_batch, #find_or_create, #find_or_create!, #find_or_new, #first, #get_data, #get_data!, #get_options, #initialize, #inspect, #last, #limit, #parameter, #parameters, #where
Instance Method Details
#clone_from(other_sensor) ⇒ Object
Create new sensor with properties from other sensor
example :
client.sensors.clone_from(client.sensors.find(123))
112 113 114 115 116 117 118 |
# File 'lib/cs/relation/sensor_relation.rb', line 112 def clone_from(other_sensor) sensor = EndPoint::Sensor.new(other_sensor.to_cs_value) sensor.id = nil sensor.session = self.session sensor end |
#find(id) ⇒ Object
86 87 88 |
# File 'lib/cs/relation/sensor_relation.rb', line 86 def find(id) find!(id) rescue false end |
#find!(id) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/cs/relation/sensor_relation.rb', line 79 def find!(id) check_session! sensor = EndPoint::Sensor.new(id: id) sensor.session = self.session sensor.retrieve! ? sensor : false end |
#find_by_name(regex, parameters = {}) ⇒ Object
Find sensor by name in regular expression. The second argument is parameter that is usualy us in where
example:
client.sensors.find_by_name(/position/, owned: true)
96 97 98 99 100 101 |
# File 'lib/cs/relation/sensor_relation.rb', line 96 def find_by_name(regex, parameters={}) check_session! self.where(parameters) regex = /\A#{regex}\z/ if regex.kind_of?(String) self.select { |sensor| sensor.name =~ regex } end |
#triggers ⇒ Object
103 104 105 |
# File 'lib/cs/relation/sensor_relation.rb', line 103 def triggers() Relation::SensorTriggersRelation.new(@session) end |