SmartThings Schema - Node.js
tip
Find the SmartThings Schema SDK on our GitHub repository.
#
st-schema-nodejsST Schema helper library for Node.js
#
Installing the module#
Connector app structure#
Minimal loopback connector exampleThis simple connector creates a one dimmer device named Test Dimmer. There's no physical device involved. The connector command handler simply returns the state value corresponding to the issued command. The current state of the device is stored in memory, so if the server is restarted the states will revert to their initial value. This implementation does not implement proactive state callbacks.
#
connector.js#
Running as a web-serviceTo run the above connector as a web service using the Express framework create a server like this one. Note that a real application would need to validate the access token passed in each request. This example only checks for the presence of the token.
#
server.js#
Running as an AWS LambdaTo run the connector as an AWS lambda use a handler like this one.
#
index.js#
Proactive state callbacksSensors and devices that can be controlled other than through the SmartThings mobile app can change state at any time. To ensure that the SmartThings platform is made aware of these state changes right away callsbacks can be implemented to call into the SmartThings cloud. These callbacks are secured via a token exchange dependent on the client ID and secret defined for the ST Schema connector in the Developer Workspace. The following example is a minimal implementation of a connector that supports these callback. It builds on the previous example by implementing the callbacks and exposing a web-service endpoint for executing device commands.
#
app.jsThe connector app is now initialized with the ST Schema connector's client ID and secret, which are available from
the Developer workspace. It also declares an accessTokens
map to contain the list of connectors that need to be
called when device state changes. Note that this simple implementation stores the connectors in memory, so restarting
the server will cause them to be lost. The app also has new callbackAccessHandler
and integrationDeletedHandler
handlers defined to add and remove entries from the accessTokens
map.
#
server.jsThe web server is modified to add a new /command
endpoint for turning on and off the switch. It expects
a JSON body of the form {"attribute": "switch", "value": "on"}
.