Class: Helium::Sensor

Inherits:
Resource show all
Defined in:
lib/helium/sensor.rb

Instance Attribute Summary collapse

Attributes inherited from Resource

#id, #params, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, all, create, #created_at, #destroy, #eql?, find, #hash, initialize_from_path, #metadata, resource_name, #resource_name, #resource_path, singleton, #to_json, #update, #updated_at

Methods included from Utils

#datetime_to_iso, #kebab_case

Constructor Details

#initialize(opts = {}) ⇒ Sensor

Returns a new instance of Sensor.



5
6
7
8
9
10
11
12
13
# File 'lib/helium/sensor.rb', line 5

def initialize(opts = {})
  super(opts)

  @name        = @params.dig('attributes', 'name')
  @mac         = @params.dig('meta', 'mac')
  @ports       = @params.dig('meta', 'ports')
  @last_seen   = @params.dig('meta', 'last-seen')
  @device_type = @params.dig('meta', 'device-type')
end

Instance Attribute Details

#device_typeObject (readonly)

Returns the value of attribute device_type.



3
4
5
# File 'lib/helium/sensor.rb', line 3

def device_type
  @device_type
end

#last_seenDateTime? (readonly)

Returns when the resource was last seen.

Returns:

  • (DateTime, nil)

    when the resource was last seen



70
71
72
# File 'lib/helium/sensor.rb', line 70

def last_seen
  @last_seen
end

#macObject (readonly)

Returns the value of attribute mac.



3
4
5
# File 'lib/helium/sensor.rb', line 3

def mac
  @mac
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/helium/sensor.rb', line 3

def name
  @name
end

#portsObject (readonly)

Returns the value of attribute ports.



3
4
5
# File 'lib/helium/sensor.rb', line 3

def ports
  @ports
end

Class Method Details

.all_pathObject



19
20
21
# File 'lib/helium/sensor.rb', line 19

def self.all_path
  "/sensor?include=label"
end

Instance Method Details

#add_labels(labels_to_add = []) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/helium/sensor.rb', line 87

def add_labels(labels_to_add = [])
  # There's no first-class support for modifying the labels of a sensor in
  # the API yet, so we modify each label's relationship to the sensor. Once
  # this is supported in the API, this can use #add_relationships instead.
  # Same comment applies for the following 3 functions
  labels_to_add = Array(labels_to_add)
  labels_to_add.each do |label|
    label.add_sensors(self)
  end
  self
end

#as_jsonObject

TODO can probably generalize this a bit more



76
77
78
79
80
81
82
83
84
85
# File 'lib/helium/sensor.rb', line 76

def as_json
  super.merge({
    name: name,
    mac: mac,
    ports: ports,
    last_seen: last_seen,
    virtual: virtual?,
    device_type: device_type
  })
end

#device_configurationObject



27
28
29
# File 'lib/helium/sensor.rb', line 27

def device_configuration
  @client.sensor_device_configuration(self)
end

#elementObject



15
16
17
# File 'lib/helium/sensor.rb', line 15

def element
  @client.sensor_element(self)
end

#labelsObject



23
24
25
# File 'lib/helium/sensor.rb', line 23

def labels
  Collection.new(klass: Label, client: @client, belongs_to: self)
end

#live_timeseries(opts = {}, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/helium/sensor.rb', line 53

def live_timeseries(opts = {}, &block)
  port        = opts.fetch(:port, nil)
  start_time  = opts.fetch(:start_time, nil)
  end_time    = opts.fetch(:end_time, nil)
  aggtype     = opts.fetch(:aggtype, nil)
  aggsize     = opts.fetch(:aggsize, nil)

  @client.sensor_live_timeseries(self, {
    port:       port,
    start_time: start_time,
    end_time:   end_time,
    aggtype:    aggtype,
    aggsize:    aggsize
  }, &block)
end

#remove_labels(labels_to_remove = []) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/helium/sensor.rb', line 112

def remove_labels(labels_to_remove = [])
  labels_to_remove = Array(labels_to_remove)
  labels_to_remove.each do |label|
    label.remove_sensors(self)
  end
  self
end

#replace_labels(labels_to_replace = []) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/helium/sensor.rb', line 99

def replace_labels(labels_to_replace = [])
  # To support replacement, we remove this sensor from each label, and then
  # add it to the specified set
  labels_to_replace = Array(labels_to_replace)
  labels.each do |label|
    label.remove_sensors(self)
  end
  labels_to_replace.each do |label|
    label.add_sensors(self)
  end
  self
end

#sensor_packagesObject

A sensor can be associated with at most 2 sensor-packages: the loaded package and package being loaded



122
123
124
# File 'lib/helium/sensor.rb', line 122

def sensor_packages
  Collection.new(klass: SensorPackage, client: @client, belongs_to: self)
end

#timeseries(opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/helium/sensor.rb', line 35

def timeseries(opts = {})
  size        = opts.fetch(:size, 1000)
  port        = opts.fetch(:port, nil)
  start_time  = opts.fetch(:start_time, nil)
  end_time    = opts.fetch(:end_time, nil)
  aggtype     = opts.fetch(:aggtype, nil)
  aggsize     = opts.fetch(:aggsize, nil)

  @client.sensor_timeseries(self,
    size:       size,
    port:       port,
    start_time: start_time,
    end_time:   end_time,
    aggtype:    aggtype,
    aggsize:    aggsize
  )
end

#virtual?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/helium/sensor.rb', line 31

def virtual?
  mac.nil?
end