Class: M2X::Client::Stream
Overview
Wrapper for M2X Data Streams API.
Instance Attribute Summary
Attributes inherited from Resource
Class Method Summary collapse
-
.fetch(client, device, name) ⇒ Stream
Method for View Data Streams endpoint.
-
.list(client, device) ⇒ Array
Method for List Data Streams endpoint.
Instance Method Summary collapse
-
#delete_values!(start, stop) ⇒ Response
Method for Delete Data Stream Values endpoint.
-
#initialize(client, device, attributes) ⇒ Stream
constructor
A new instance of Stream.
- #path ⇒ Object
-
#post_values(values) ⇒ Response
Method for Post Data Stream Values endpoint.
-
#sampling(params) ⇒ Array
Method for Data Stream Sampling endpoint.
-
#stats(params = {}) ⇒ Array
Method for Data Stream Stats endpoint.
-
#update!(params) ⇒ Stream
Method for Create Update Data Stream endpoint.
-
#update_value(value, timestamp = nil) ⇒ Object
Method for Update Data Stream Value endpoint.
-
#values(params = {}) ⇒ Array
Method for List Data Stream Values endpoint.
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.
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.
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
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 |
#path ⇒ Object
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 },
[ ... ]
]
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
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
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.
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
99 100 101 102 103 104 105 |
# File 'lib/m2x/stream.rb', line 99 def update_value(value, =nil) params = { value: value } params[:timestamp] = if @client.put("#{path}/value", nil, params, "Content-Type" => "application/json") end |
#values(params = {}) ⇒ Array
Method for List Data Stream Values endpoint.
63 64 65 |
# File 'lib/m2x/stream.rb', line 63 def values(params={}) @client.get("#{path}/values", params) end |