Sha256: d5d2c0d29bed2e5ba113cdbf48fb69571226a767dd25e83c2fa6568dc1d2c5c0

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

module Helium
  class Sensor < Resource
    attr_reader :name, :mac, :ports, :last_seen

    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

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

    # NOTE: currently this just returns the ids of labels. Once core returns
    # all label info via includes=label, we can get rid of the
    # label_relationships stuff
    def labels
      labels_params = Array(@params.dig('relationships', 'label', 'data'))
      @labels ||= labels_params.map{ |label_params|
         Label.new(client: @client, params: label_params)
      }
    end

    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

    # @return [DateTime, nil] when the resource was last seen
    def last_seen
      return nil if @last_seen.nil?
      @_last_seen ||= DateTime.parse(@last_seen)
    end

    # TODO can probably generalize this a bit more
    def as_json
      super.merge({
        name: name,
        mac: mac,
        ports: ports,
        last_seen: last_seen,
        labels: labels
      })
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
helium-ruby-0.12.0 lib/helium/sensor.rb
helium-ruby-0.11.0 lib/helium/sensor.rb
helium-ruby-0.10.0 lib/helium/sensor.rb