Class: PulseMeter::Sensor::Remote

Inherits:
Base
  • Object
show all
Defined in:
lib/pulse-meter/sensor/remote.rb

Overview

Remote sensor, i.e. a simple UDP proxy for sending data without taking in account backend performance issues

Constant Summary collapse

DEFAULT_PORT =
27182
DEFAULT_HOST =
'localhost'

Constants included from Mixins::Dumper

Mixins::Dumper::DUMP_REDIS_KEY

Instance Attribute Summary collapse

Attributes inherited from Base

#redis

Instance Method Summary collapse

Methods inherited from Base

#annotate, #annotation, #cleanup

Methods included from Mixins::Dumper

included

Constructor Details

#initialize(name, options = {}) ⇒ Remote

Initializes sensor and creates UDP socket

Parameters:

  • name (String)

    sensor name

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :host (Symbol)

    host for remote pulse-meter daemon

  • :port (Symbol)

    port for remote pulse-meter daemon

Raises:

  • (BadSensorName)

    if sensor name is malformed

  • (ConnectionError)

    if invalid host or port are provided



24
25
26
27
28
29
30
# File 'lib/pulse-meter/sensor/remote.rb', line 24

def initialize(name, options={})
  @name = name.to_s
  raise BadSensorName, @name unless @name =~ /\A\w+\z/
  @host = options[:host].to_s || DEFAULT_HOST
  @port = options[:port].to_i || DEFAULT_PORT
  @socket = UDPSocket.new
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/pulse-meter/sensor/remote.rb', line 16

def name
  @name
end

Instance Method Details

#event(value) ⇒ Object

Send value to remote sensor

Parameters:

  • value

    value for remote sensor

Raises:

  • (ConnectionError)

    if remote daemon is not available

  • (MessageTooLarge)

    if event data is too large to be serialized into a UDP datagram



36
37
38
# File 'lib/pulse-meter/sensor/remote.rb', line 36

def event(value)
  events(name => value)
end

#events(event_data) ⇒ Object

Send values to multiple remote sensors

Parameters:

  • event_data

    hash with remote sensor names as keys end event value for each value as sensor

Raises:

  • (ArgumentError)


42
43
44
45
46
47
# File 'lib/pulse-meter/sensor/remote.rb', line 42

def events(event_data)
  raise ArgumentError unless event_data.is_a?(Hash)
  socket_action do
    @socket.send(event_data.to_json, 0, @host, @port)
  end
end