Touch to Identify
An example of how to use the REST API with a simple script that identifies devices by touch.
Overview
A feature in DT Studio popular with our customers is the ability to locate a sensor by simply touching it. Like all functions in DT Studio, the implementation is based on our REST API. We will in this example show how simple it is to recreate using the stream endpoint.
Preliminaries
Basic Auth For simplicity, we here use Basic Auth for authentication. We recommend replacing this with an OAuth2 flow for integrations more complex than local experimentation.
Service Account Credentials You must create and know the credentials of a Service Account. Any role will suffice.
Streaming Best Practices While this example is based on our other example for Streaming Events, it does not implement the same retry policy or other best practices as it is not the focus here. You are, however, free to combine the two examples for a more robust touch-event-listening loop.
Example Code
The following points summarize the provided example code.
Sends a GET request to the REST API to initialize an event stream.
Keep the TCP connection open while receiving events.
When receiving a Touch Event, fetch and print the source device information.
Break the stream.
Environment Setup
If you wish to run the code locally, make sure you have a working runtime environment.
The following packages are required by the example code and must be installed.
The latest version of our Python API can be installed through pip.
The following modules are required by the example code and must be installed.
Add the following environment variables as they will be used to authenticate the API.
Source
The following code snippet implements the touch-to-identify listening loop.
Expected Output
Once a touch event has been received, the device information is printed and stream terminated.
Last updated
Was this helpful?