Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision Next revisionBoth sides next revision | ||
ezeio2:apiref:subscribe [2021-01-26 20:34] – created andreh | ezeio2:apiref:subscribe [2021-01-27 00:32] – andreh | ||
---|---|---|---|
Line 15: | Line 15: | ||
The '' | The '' | ||
- | |||
=== Example usage === | === Example usage === | ||
Line 21: | Line 20: | ||
Using the subscribe API requires the following steps: | Using the subscribe API requires the following steps: | ||
- | 1) | + | **Step |
- | Request | + | |
+ | Call the following API endpoint using valid API credentials: | ||
< | < | ||
- | This will return a JSON object with metadata listing the systems that will be accessible through the websocket. | + | This call will return a JSON object |
- | + | ||
- | The '' | + | |
- | 2) | + | //(whitespaces |
- | Use the '' | + | |
- | The websocket will automatically receive all updates to any system (ezeio) covered by the API call credentials given in the original call to subscribe/ | + | |
- | + | ||
- | The following update types will be sent over the websocket connection: | + | |
- | LOGDATA - log interval data | + | |
- | STATUS - 10 minute interval data. Always sent from all systems. | + | |
- | + | ||
- | 3) | + | |
- | Call '' | + | |
- | + | ||
- | === Return value === | + | |
- | + | ||
- | JSON formatted data from the '' | + | |
- | + | ||
- | //(below example has added whitespaces | + | |
<code javascript> | <code javascript> | ||
Line 111: | Line 94: | ||
</ | </ | ||
+ | The '' | ||
+ | The '' | ||
+ | |||
+ | **Step 2 : Open websocket and start receiving data** | ||
+ | |||
+ | Use the '' | ||
+ | The websocket will automatically receive all updates to any system (ezeio) covered by the initial API call, as the data becomes available to the cloud servers. | ||
+ | |||
+ | 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 | ||
+ | |||
+ | The STATUS updates will be sent every 10 minutes even if a faster subscription is active. | ||
+ | |||
+ | **Step 3 : Request subscription changes** | ||
+ | |||
+ | 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 unbuffered log updates, the call is: | ||
+ | < | ||
+ | The log updates will revert to normal (buffered) mode after 30 minutes. | ||
+ | |||
+ | To request 10s status updates, the call is: | ||
+ | < | ||
+ | The status updates will revert to normal (10 minute) updates after 5 minutes. | ||
+ | |||
+ | To cancel updates, the call is: | ||
+ | < | ||
+ | |||
+ | Multiple requests can be sent in the same command: | ||
+ | < | ||
+ | |||
+ | |||
+ | === Example code (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-> | ||
+ | </ |