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
# 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')
end

Instance Attribute Details

#last_seenDateTime? (readonly)

Returns when the resource was last seen

Returns:

  • (DateTime, nil)

    when the resource was last seen



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

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



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

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

Instance Method Details

#as_jsonObject

TODO can probably generalize this a bit more



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

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

#device_configurationObject



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

def device_configuration
  @client.sensor_device_configuration(self)
end

#elementObject



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

def element
  @client.sensor_element(self)
end

#labelsObject



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

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

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



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

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

#timeseries(opts = {}) ⇒ Object



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

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)


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

def virtual?
  mac.nil?
end