Class: M2X::MQTT::Device

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

Overview

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

Constant Summary collapse

PATH =
"/devices"

Instance Attribute Summary

Attributes inherited from Resource

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#initialize, #inspect, #refresh, #view

Constructor Details

This class inherits a constructor from M2X::MQTT::Resource

Class Method Details

.create!(client, params) ⇒ Object



11
12
13
14
15
# File 'lib/m2x/mqtt/device.rb', line 11

def create!(client, params)
  client.post(PATH, params)

  new(client, params)
end

Instance Method Details

#commands(params = {}) ⇒ Object

Return a list of recently received commands.

Most commonly, this method can be used to fetch unacknowledged commands by filtering by delivery status, using the parameters:

{ status: “sent” }

MQTT clients that are subscribed to command delivery notifications should still use this method periodically to check for unacknowledged commands that were missed while offline or during a network partition.

m2x.att.com/developer/documentation/v2/commands#Device-List-of-Received-Commands



38
39
40
41
42
43
44
45
# File 'lib/m2x/mqtt/device.rb', line 38

def commands(params={})
  @client.get("#{path}/commands", params)

  res      = @client.get_response
  commands = res["body"]["commands"] if res["status"] < 300

  commands.map { |data| M2X::MQTT::Command.new(@client, data) } if commands
end

#pathObject



18
19
20
# File 'lib/m2x/mqtt/device.rb', line 18

def path
  @path ||= "#{ PATH }/#{ URI.encode(@attributes.fetch("id")) }"
end

#post_update(params) ⇒ Object

Post Device Update (Single Value to Multiple Streams)

This method allows posting a single value to multiple streams belonging to a device and optionally, the device’s location.

All the streams should be created before posting values using this method.

The ‘params` parameter accepts a Hash which can contain the following keys:

- values:    A Hash in which the keys are the stream names and the values
             hold the stream values.
- location:  (optional) A hash with the current location of the specified
             device.
- timestamp: (optional) The timestamp for all the passed values and
             location. If ommited, the M2X server's time will be used.

   {
      values: {
          temperature: 30,
          humidity:    80
      },
      location: {
        name:      "Storage Room",
        latitude:  -37.9788423562422,
        longitude: -57.5478776916862,
        elevation: 5
      }
   }

m2x.att.com/developer/documentation/v2/device#Post-Device-Update–Single-Values-to-Multiple-Streams-



113
114
115
# File 'lib/m2x/mqtt/device.rb', line 113

def post_update(params)
  @client.post("#{path}/update", params)
end

#post_updates(params) ⇒ Object

Post Device Updates (Multiple Values to Multiple Streams)

This method allows posting multiple values to multiple streams belonging to a device and optionally, the device location.

All the streams should be created before posting values using this method.

The ‘values` parameter contains an object with one attribute per each stream to be updated. The value of each one of these attributes is an array of timestamped values.

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

}

The optional location attribute can contain location information that will be used to update the current location of the specified device

staging.m2x.sl.attcompute.com/developer/documentation/v2/device#Post-Device-Updates–Multiple-Values-to-Multiple-Streams-



80
81
82
# File 'lib/m2x/mqtt/device.rb', line 80

def post_updates(params)
  @client.post("#{path}/updates", params)
end

#stream(name) ⇒ Object



22
23
24
# File 'lib/m2x/mqtt/device.rb', line 22

def stream(name)
  M2X::MQTT::Stream.new(@client, self, "name" => name)
end

#update_location(params) ⇒ Object

Update the current location of the specified device.

m2x.att.com/developer/documentation/v2/device#Update-Device-Location



50
51
52
# File 'lib/m2x/mqtt/device.rb', line 50

def update_location(params)
  @client.put("#{path}/location", params)
end