Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
ezeio2:apiref:subscribe [2021-01-26 22:48] – andreh | ezeio2:apiref:subscribe [2021-01-28 23:49] (current) – andreh | ||
---|---|---|---|
Line 1: | Line 1: | ||
==== subscribe ==== | ==== subscribe ==== | ||
- | Request a websocket data stream, and commands to manage the data stream | + | Request a websocket data stream, and commands to manage the data stream. |
=== Description === | === Description === | ||
Line 11: | Line 11: | ||
Subsequent calls using the subscribe command are used to manage the data stream. | Subsequent calls using the subscribe command are used to manage the data stream. | ||
+ | |||
+ | <WRAP center round important 60%> | ||
+ | This functionality requires firmware 21012701 or later in the device. | ||
+ | </ | ||
+ | |||
=== Parameters === | === Parameters === | ||
The '' | The '' | ||
- | |||
=== Example usage === | === Example usage === | ||
Line 96: | Line 100: | ||
The '' | The '' | ||
+ | |||
+ | The '' | ||
**Step 2 : Open websocket and start receiving data** | **Step 2 : Open websocket and start receiving data** | ||
Line 103: | Line 109: | ||
The data received will have a '' | The data received will have a '' | ||
- | | LOGDATA | The field data is from the 'fast log'. The interval is determined by the log interval setting on each field. | | + | | '' |
- | | STATUS | The field data is from the ' | + | | '' |
Note that the ezeio normally buffers data before the data is uploaded to the cloud servers, so the data may be delayed with up to 20 minutes | Note that the ezeio normally buffers data before the data is uploaded to the cloud servers, so the data may be delayed with up to 20 minutes | ||
+ | |||
+ | The STATUS updates will be sent every 10 minutes even if a faster subscription is active. | ||
+ | |||
+ | This is an example of what a '' | ||
+ | <code javascript> | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | }, | ||
+ | { | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | Note that the fields array will include all configured fields for this unit - regardless of their log setting. All fields are always logged every 10 minutes. | ||
+ | |||
+ | A '' | ||
+ | <code javascript> | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | Note that this message only includes the fields that are configured for fast logging (interval less than 10 minutes). Please see below for the meaning of the '' | ||
**Step 3 : Request subscription changes** | **Step 3 : Request subscription changes** | ||
Line 112: | Line 160: | ||
With an open websocket and while receiving data from the ezeio system, you can call the subscribe API to request immediate updates or to cancel updates from a certain ezeio. | With an open websocket and while receiving data from the ezeio system, you can call the subscribe API to request immediate updates or to cancel updates from a certain ezeio. | ||
- | To request | + | To request |
- | < | + | < |
+ | The log updates will revert to normal (buffered) mode after 30 minutes. | ||
To cancel updates, the call is: | To cancel updates, the call is: | ||
- | < | + | < |
Multiple requests can be sent in the same command: | Multiple requests can be sent in the same command: | ||
- | < | + | < |
+ | Up to 50 devices can be included in the same command | ||
+ | === Example code to set up the websocket channel and receive data (PHP) === | ||
+ | <code php> | ||
+ | <?php | ||
+ | define(" | ||
+ | | ||
+ | // API keyID and key needs to be set up in eze.io under Groups-> | ||
+ | define(" | ||
+ | define(" | ||
+ | // Using the textalk/ | ||
+ | // https:// | ||
+ | require(' | ||
+ | use WebSocket\Client; | ||
+ | // Request a websocket key and metadata using cURL | ||
+ | $ch = curl_init(); | ||
+ | curl_setopt($ch, | ||
+ | | ||
+ | // All API calls use Digest AUTH | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | | ||
+ | // Set a 5s timeout, and return any received data | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | | ||
+ | // Execute the cURL request | ||
+ | $response = curl_exec($ch); | ||
+ | curl_close($ch); | ||
+ | |||
+ | // Decode the json reply into an associative array | ||
+ | $json = json_decode( $response, TRUE ); | ||
+ | | ||
+ | // Status should be ' | ||
+ | if($json[" | ||
+ | die(" | ||
+ | |||
+ | // Open the websocket | ||
+ | $wsclient = new WebSocket\Client( $json[" | ||
+ | | ||
+ | // Loop forever (until websocket disconnects) | ||
+ | while ( true ) { | ||
+ | try { | ||
+ | $message = $wsclient-> | ||
+ | |||
+ | // Process the data - here we just print it for testing | ||
+ | print_r( json_decode( $message, TRUE ) ); | ||
+ | } | ||
+ | catch (\WebSocket\ConnectionException $e) { | ||
+ | // If the websocket was disconnected, | ||
+ | if( !$wsclient-> | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | $wsclient-> | ||
+ | </ |