Class: M2X::MQTT::Stream

Inherits:
Resource show all
Defined in:
lib/m2x/mqtt/stream.rb

Overview

Wrapper for AT&T M2X Data Streams API m2x.att.com/developer/documentation/v2/device

Instance Attribute Summary

Attributes inherited from Resource

#attributes

Instance Method Summary collapse

Methods inherited from Resource

#inspect, #refresh, #view

Constructor Details

#initialize(client, device, attributes) ⇒ Stream

Returns a new instance of Stream.



5
6
7
8
9
# File 'lib/m2x/mqtt/stream.rb', line 5

def initialize(client, device, attributes)
  @client     = client
  @device     = device
  @attributes = attributes
end

Instance Method Details

#pathObject



11
12
13
# File 'lib/m2x/mqtt/stream.rb', line 11

def path
  @path ||= "#{@device.path}/streams/#{ URI.encode(@attributes.fetch("name")) }"
end

#post_values(values) ⇒ Object

Post multiple values to the stream

The ‘values` parameter is an array with the following format:

[
  { "timestamp": <Time in ISO8601>, "value": x },
  { "timestamp": <Time in ISO8601>, "value": y },
  [ ... ]
]

m2x.att.com/developer/documentation/v2/device#Post-Data-Stream-Values



48
49
50
51
52
# File 'lib/m2x/mqtt/stream.rb', line 48

def post_values(values)
  params = { values: values }

  @client.post("#{path}/values", params)
end

#update!(params = {}) ⇒ Object

Update stream properties (if the stream does not exist it gets created).

m2x.att.com/developer/documentation/v2/device#Create-Update-Data-Stream



19
20
21
22
23
# File 'lib/m2x/mqtt/stream.rb', line 19

def update!(params={})
  @client.put(path, params)

  @attributes.merge!(params)
end

#update_value(value, timestamp = nil) ⇒ Object

Update the current value of the stream. The timestamp is optional. If ommited, the current server time will be used

m2x.att.com/developer/documentation/v2/device#Update-Data-Stream-Value



29
30
31
32
33
34
35
# File 'lib/m2x/mqtt/stream.rb', line 29

def update_value(value, timestamp=nil)
  params = { value: value }

  params[:timestamp] = timestamp if timestamp

  @client.put("#{path}/value", params)
end