Class: M2X::Client::Stream

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

Overview

Wrapper for M2X Data Streams API.

Instance Attribute Summary

Attributes inherited from Resource

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#delete!, #inspect, #refresh, #view

Constructor Details

#initialize(client, device, attributes) ⇒ Stream

Returns a new instance of Stream.



35
36
37
38
39
# File 'lib/m2x/stream.rb', line 35

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

Class Method Details

.fetch(client, device, name) ⇒ Stream

Method for View Data Streams endpoint.

Parameters:

  • client (Client)

    Client API

  • device (String)

    device to fetch

  • name (String)

    of the stream to be fetched

Returns:



15
16
17
18
19
# File 'lib/m2x/stream.rb', line 15

def fetch(client, device, name)
  res = client.get("#{device.path}/streams/#{URI.encode(name)}")

  new(client, device, res.json) if res.success?
end

.list(client, device) ⇒ Array

Method for List Data Streams endpoint.

Parameters:

  • client (Client)

    Client API

  • device (String)

    device to get its data streams

Returns:



28
29
30
31
32
# File 'lib/m2x/stream.rb', line 28

def list(client, device)
  res = client.get("#{device.path}/streams")

  res.json["streams"].map{ |atts| new(client, device, atts) } if res.success?
end

Instance Method Details

#delete_values!(start, stop) ⇒ Response

Method for Delete Data Stream Values endpoint. The ‘start` and `stop` parameters should be ISO8601 timestamps

Parameters:

  • start (String)

    from time to delete the data

  • stop (String)

    end time to delete the data

Returns:

  • (Response)

    The API response, see M2X API docs for details



134
135
136
137
138
# File 'lib/m2x/stream.rb', line 134

def delete_values!(start, stop)
  params = { from: start, end: stop }

  @client.delete("#{path}/values", nil, params, "Content-Type" => "application/json")
end

#pathObject



41
42
43
# File 'lib/m2x/stream.rb', line 41

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

#post_values(values) ⇒ Response

Method for Post Data Stream Values endpoint. 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 },
  [ ... ]
]

Parameters:

  • values

    The values being posted, formatted according to the API docs

Returns:

  • (Response)

    The API response, see M2X API docs for details



120
121
122
123
124
# File 'lib/m2x/stream.rb', line 120

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

  @client.post("#{path}/values", nil, params, "Content-Type" => "application/json")
end

#sampling(params) ⇒ Array

Method for Data Stream Sampling endpoint. This method only works for numeric streams

Parameters:

  • params

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Array)

    List of sample values from the stream



74
75
76
# File 'lib/m2x/stream.rb', line 74

def sampling(params)
  @client.get("#{path}/sampling", params)
end

#stats(params = {}) ⇒ Array

Method for Data Stream Stats endpoint. Returns the count, min, max, average and standard deviation stats for the values of the stream. This method only works for numeric streams

Parameters:

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Array)

    Stats of the stream



87
88
89
# File 'lib/m2x/stream.rb', line 87

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

#update!(params) ⇒ Stream

Method for Create Update Data Stream endpoint.

Parameters:

  • params

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Stream)

    The newly created stream



51
52
53
54
55
# File 'lib/m2x/stream.rb', line 51

def update!(params)
  @client.put(path, {}, params, "Content-Type" => "application/json").tap do |res|
    @attributes = res.json if res.status == 201
  end
end

#update_value(value, timestamp = nil) ⇒ Object

Method for Update Data Stream Value endpoint. The timestamp is optional. If omitted, the current server time will be used

Parameters:

  • value (String)

    Value to be updated

  • timestamp (String) (defaults to: nil)

    Current Timestamp

Returns:

  • void



99
100
101
102
103
104
105
# File 'lib/m2x/stream.rb', line 99

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

  params[:timestamp] = timestamp if timestamp

  @client.put("#{path}/value", nil, params, "Content-Type" => "application/json")
end

#values(params = {}) ⇒ Array

Method for List Data Stream Values endpoint.

Parameters:

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Array)

    List of values from the stream



63
64
65
# File 'lib/m2x/stream.rb', line 63

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