Skip to content

Commit d56f8f7

Browse files
authored
chore: update examples and link to app-examples (#171)
1 parent c0e927c commit d56f8f7

File tree

5 files changed

+23
-11631
lines changed

5 files changed

+23
-11631
lines changed

README.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,55 +60,51 @@ import SmartApp from '@smartthings/smartapp'
6060

6161
## Examples
6262

63-
The following example is the equivalent of the original SmartThings Groovy _Let There Be Light_ app that turns on and off a light when a door opens and closes.
63+
The following example automation is the equivalent of a simple Rule (if contact sensor opens/closes, turn lights on/off) which is easily achieved via our [Rules API](https://smartthings.developer.samsung.com/docs/rules/overview.html). It is given here as a brief showcase of the SDK, and not meant to be a good candidate for a SmartApp. Be sure to check if your automation is possible with the Rules API, as it will benefit from speed, stability, and security through future local execution support.
6464

6565
### Running it as a web service
6666

6767
To run the app with an HTTP server, like Express.js:
6868

6969
```javascript
70-
const express = require('express');
71-
const SmartApp = require('@smartthings/smartapp');
72-
const server = module.exports = express();
73-
const PORT = 8080;
70+
const SmartApp = require('@smartthings/smartapp');
71+
const express = require('express');
72+
const server = express();
73+
const PORT = 8080;
7474

75-
server.use(express.json());
7675

7776
/* Define the SmartApp */
7877
const smartapp = new SmartApp()
79-
// @smartthings_rsa.pub is your on-disk public key
80-
// If you do not have it yet, omit publicKey()
81-
.publicKey('@smartthings_rsa.pub') // optional until app verified
8278
.enableEventLogging(2) // logs all lifecycle event requests and responses as pretty-printed JSON. Omit in production
83-
.configureI18n()
8479
.page('mainPage', (context, page, configData) => {
8580
page.section('sensors', section => {
8681
section
8782
.deviceSetting('contactSensor')
8883
.capabilities(['contactSensor'])
89-
.required(false);
9084
});
9185
page.section('lights', section => {
9286
section
9387
.deviceSetting('lights')
9488
.capabilities(['switch'])
95-
.multiple(true)
96-
.permissions('rx');
89+
.permissions('rx')
90+
.multiple(true);
9791
});
9892
})
93+
// Called for both INSTALLED and UPDATED lifecycle events if there is no separate installed() handler
9994
.updated(async (context, updateData) => {
100-
// Called for both INSTALLED and UPDATED lifecycle events if there is no separate installed() handler
101-
await context.api.subscriptions.unsubscribeAll()
102-
return context.api.subscriptions.subscribeToDevices(context.config.contactSensor, 'contactSensor', 'contact', 'myDeviceEventHandler');
95+
await context.api.subscriptions.delete() // clear any existing configuration
96+
await context.api.subscriptions.subscribeToDevices(context.config.contactSensor, 'contactSensor', 'contact', 'myDeviceEventHandler');
10397
})
104-
.subscribedEventHandler('myDeviceEventHandler', (context, event) => {
98+
.subscribedEventHandler('myDeviceEventHandler', async (context, event) => {
10599
const value = event.value === 'open' ? 'on' : 'off';
106-
context.api.devices.sendCommands(context.config.lights, 'switch', value);
100+
await context.api.devices.sendCommands(context.config.lights, 'switch', value);
107101
});
108102

103+
server.use(express.json());
104+
109105
/* Handle POST requests */
110-
server.post('/', function(req, res, next) {
111-
smartapp.handleHttpCallback(req, res);
106+
server.post('/', function (req, res, next) {
107+
smartapp.handleHttpCallback(req, res);
112108
});
113109

114110
/* Start listening at your defined PORT */
@@ -119,18 +115,19 @@ server.listen(PORT, () => console.log(`Server is up and running on port ${PORT}`
119115

120116
To run as a Lambda function instead of an HTTP server, ensure that your main entry file exports `smartapp.handleLambdaCallback(...)`.
121117

122-
> **Note:** This snippet is heavily truncated for brevity – see the web service example above a more detailed example of how to define a `smartapp`.
118+
> **Note:** This snippet is heavily truncated for brevity.
123119
124120
```javascript
125-
const SmartApp = require('@smartthings/smartapp')
121+
const SmartApp = require('@smartthings/smartapp');
122+
126123
const smartapp = new SmartApp()
127124
.enableEventLogging() // logs all lifecycle event requests and responses. Omit in production
128125
.page( ... )
129126
.updated(() => { ... })
130127
.subscribedEventHandler( ... );
131128

132-
exports.handler = (event, context) => {
133-
return smartapp.handleLambdaCallback(event, context);
129+
exports.handler = (event, context, callback) => {
130+
smartapp.handleLambdaCallback(event, context, callback);
134131
};
135132
```
136133
There are also a few Glitch examples:
@@ -139,6 +136,8 @@ There are also a few Glitch examples:
139136
- [Simple SmartThings Automation App using Motion Detectors](https://glitch.com/edit/#!/polite-math?path=README.md:1:0)
140137
- [Simple Switch Cloud-to-Cloud (C2C) App](https://glitch.com/edit/#!/aquamarine-crop?path=README.md:1:0)
141138

139+
More detailed examples to use as a starting point can be found in our [app-examples](https://github.com/SmartThingsCommunity/app-examples) repository.
140+
142141
### Localization
143142

144143
Configuration page strings are specified in a separate `locales/en.json` file, which can be automatically created the first time you run the app. Here's a completed English localization file for the previous example:

examples/basic/README.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)