Sha256: 795bad1df5cd1537a57bb43aadeaa3d366f50f188971af2df8250058afcd8b4c

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

module PulseMeter
  module Visualize
    class Sensor
      attr_reader :name
      attr_reader :color

      def initialize(args) 
        raise ArgumentError unless args.respond_to?('[]')
        @name = args[:sensor] or raise ArgumentError, ":sensor_name not specified"
        @color = args[:color]
      end

      def last_value(need_incomplete=false)
        sensor = real_sensor

        sensor_data = if need_incomplete
          sensor.timeline(sensor.interval).first
        else
          sensor.timeline(sensor.interval * 2).first
        end

        if sensor_data.is_a?(PulseMeter::SensorData)
          sensor_data.value
        else
          nil
        end
      end

      def last_point_data(need_incomplete=false)
        res = {
            name: real_sensor.annotation,
            y: to_float(last_value(need_incomplete))
        }
        res[:color] = color if color
        res
      end

      def timeline_data(time_span, need_incomplete = false)
        sensor = real_sensor
        data = sensor.timeline(time_span).map{|sd| {x: sd.start_time.to_i*1000, y: to_float(sd.value)}}
        data.pop unless need_incomplete
        res = {
            name: sensor.annotation,
            data: data
        }
        res[:color] = color if color
        res
      end

      protected

      def real_sensor
        # TODO add !temporarily! caching if this will be called too frequently
        PulseMeter::Sensor::Base.restore(@name)
      end

      def to_float(val)
        val && val.to_f
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pulse-meter-0.1.6 lib/pulse-meter/visualize/sensor.rb
pulse-meter-0.1.5 lib/pulse-meter/visualize/sensor.rb
pulse-meter-0.1.4 lib/pulse-meter/visualize/sensor.rb
pulse-meter-0.1.3 lib/pulse-meter/visualize/sensor.rb
pulse-meter-0.1.2 lib/pulse-meter/visualize/sensor.rb