@@ -44,32 +44,84 @@ import smartapp from '@smartthings/smartapp'
4444
4545## Example
4646
47- ### Run as an AWS Lambda function
47+ 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.
4848
49- Here's 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, set up to run as a Lambda.
49+ ### Running it as a web service
50+
51+ To run the app with an HTTP server, like Express.js:
5052
5153``` javascript
52- const smartapp = require (' @smartthings/smartapp' )
53- smartapp .configureI18n ()
54- .page (' mainPage' , (page ) => {
55- page .section (' sensors' , (section ) => {
56- section .deviceSetting (' contactSensor' ).capabilities ([' contactSensor' ]);
54+ const express = require (' express' );
55+ const smartapp = require (' @smartthings/smartapp' );
56+ const server = module .exports = express ();
57+ const PORT = 8080 ;
58+
59+ server .use (express .json ());
60+
61+ // @smartthings_rsa.pub is your on-disk public key
62+ // If you do not have it yet, omit publicKey()
63+ smartapp
64+ .publicKey (' @smartthings_rsa.pub' ) // optional until app verified
65+ .configureI18n ()
66+ .page (' mainPage' , (context , page , configData ) => {
67+ page .section (' sensors' , section => {
68+ section .deviceSetting (' contactSensor' ).capabilities ([' contactSensor' ]).required (false );
5769 });
58- page .section (' lights' , ( section ) => {
70+ page .section (' lights' , section => {
5971 section .deviceSetting (' lights' ).capabilities ([' switch' ]).multiple (true ).permissions (' rx' );
6072 });
6173 })
62- .updated (() => {
63- smartapp .api .devices .unsubscribeAll ().then (() => {
64- smartapp .api .devices .subscribe (smartapp .config .contactSensor , ' contactSensor' , ' contact' , ' openCloseHandler' );
74+ .installed ((context , installData ) => {
75+ console .log (' installed' , JSON .stringify (installData));
76+ })
77+ .uninstalled ((context , uninstallData ) => {
78+ console .log (' uninstalled' , JSON .stringify (uninstallData));
79+ })
80+ .updated ((context , updateData ) => {
81+ console .log (' updated' , JSON .stringify (updateData));
82+ context .api .subscriptions .unsubscribeAll ().then (() => {
83+ console .log (' unsubscribeAll() executed' );
84+ context .api .subscriptions .subscribeToDevices (context .config .contactSensor , ' contactSensor' , ' contact' , ' myDeviceEventHandler' );
6585 });
6686 })
67- .subscribedEventHandler (' openCloseHandler' , (event ) => {
68- const value = event .value === ' open' ? ' on' : ' off' ;
69- smartapp .api .devices .sendCommands (smartapp .config .lights , ' switch' , value);
87+ .subscribedEventHandler (' myDeviceEventHandler' , (context , deviceEvent ) => {
88+ const value = deviceEvent .value === ' open' ? ' on' : ' off' ;
89+ context .api .devices .sendCommands (context .config .lights , ' switch' , value);
90+ console .log (` sendCommands(${ JSON .stringify (context .config .lights )} , 'switch', '${ value} ')` );
91+
92+ /* All subscription event handler types:
93+ * - DEVICE_EVENT (context, deviceEvent)
94+ * - TIMER_EVENT (context, timerEvent)
95+ * - DEVICE_COMMANDS_EVENT (context, deviceId, command, deviceCommandsEvent)
96+ * - MODE_EVENT (context, modeEvent)
97+ * - SECURITY_ARM_STATE_EVENT (context, securityArmStateEvent)
98+ */
7099 });
71- exports .handle = (evt , context , callback ) => {
72- smartapp .handleLambdaCallback (evt, context, callback);
100+
101+ /* Handle POST requests */
102+ server .post (' /' , function (req , res , next ) {
103+ console .log (` ${ new Date ().toISOString ()} ${ req .method } ${ req .path } ${ req .body && req .body .lifecycle } ` );
104+ smartapp .handleHttpCallback (req, res);
105+ });
106+
107+ /* Start listening at your defined PORT */
108+ server .listen (PORT , () => console .log (` Server is up and running on port ${ PORT } ` ));
109+ ```
110+
111+ ### Running as an AWS Lambda function
112+
113+ To run as a Lambda function instead of an HTTP server, ensure that your main entry file exports ` smartapp.handleLambdaCallback(...) ` .
114+
115+ > ** Note:** This snippet is heavily truncated for brevity – see the web service example above a more detailed example of how to define a ` smartapp ` .
116+
117+ ``` javascript
118+ const smartapp = require (' @smartthings/smartapp' )
119+ smartapp
120+ .page ( ... )
121+ .updated (() => { ... })
122+ .subscribedEventHandler ( ... );
123+ exports .handle = (event , context , callback ) => {
124+ smartapp .handleLambdaCallback (event , context, callback);
73125};
74126```
75127
@@ -88,41 +140,6 @@ Configuration page strings are specified in a separate `locales/en.json` file, w
88140}
89141```
90142
91- ### Run as a web service
92-
93- To run the app in a webserver rather than a lambda replace the ` exports.handle = ... ` function with an HTTP server
94- with the public key file specified:
95-
96- ``` javascript
97- const express = require (' express' );
98- const bodyParser = require (' body-parser' );
99- const server = module .exports = express ();
100- const smartapp = require (' @smartthings/smartapp' );
101- smartapp .publicKey (` @${ process .env .HOME } /smartthings_rsa.pub` )
102- .configureI18n ()
103- .page (' mainPage' , (page ) => {
104- page .section (' sensors' , (section ) => {
105- section .deviceSetting (' contactSensor' ).capabilities ([' contactSensor' ]);
106- });
107- page .section (' lights' , (section ) => {
108- section .deviceSetting (' lights' ).capabilities ([' switch' ]).multiple (true ).permissions (' rx' );
109- });
110- })
111- .updated (() => {
112- smartapp .api .devices .unsubscribeAll ().then (() => {
113- smartapp .api .devices .subscribe (smartapp .config .contactSensor , ' contactSensor' , ' contact' , ' openCloseHandler' );
114- });
115- })
116- .subscribedEventHandler (' openCloseHandler' , (event ) => {
117- const value = event .value === ' open' ? ' on' : ' off' ;
118- smartapp .api .devices .sendCommands (smartapp .config .lights , ' switch' , value);
119- });
120- server .use (bodyParser .json ());
121- server .post (' /' , function (req , response ) {
122- smartapp .handleHttpCallback (req, response);
123- });
124- ```
125-
126143---
127144
128145## More about SmartThings
0 commit comments