@@ -64,10 +64,12 @@ const PORT = 8080;
6464
6565server .use (express .json ());
6666
67- // @smartthings_rsa.pub is your on-disk public key
68- // If you do not have it yet, omit publicKey()
67+ /* Define the SmartApp */
6968smartapp
69+ // @smartthings_rsa.pub is your on-disk public key
70+ // If you do not have it yet, omit publicKey()
7071 .publicKey (' @smartthings_rsa.pub' ) // optional until app verified
72+ .app .enableEventLogging (2 ) // logs all lifecycle event requests and responses as pretty-printed JSON. Omit in production
7173 .configureI18n ()
7274 .page (' mainPage' , (context , page , configData ) => {
7375 page .section (' sensors' , section => {
@@ -77,36 +79,18 @@ smartapp
7779 section .deviceSetting (' lights' ).capabilities ([' switch' ]).multiple (true ).permissions (' rx' );
7880 });
7981 })
80- .installed ((context , installData ) => {
81- console .log (' installed' , JSON .stringify (installData));
82+ .updated (async (context , updateData ) => {
83+ // Called for both INSTALLED and UPDATED lifecycle events if there is no separate installed() handler
84+ await context .api .subscriptions .unsubscribeAll ()
85+ return context .api .subscriptions .subscribeToDevices (context .config .contactSensor , ' contactSensor' , ' contact' , ' myDeviceEventHandler' );
8286 })
83- .uninstalled ((context , uninstallData ) => {
84- console .log (' uninstalled' , JSON .stringify (uninstallData));
85- })
86- .updated ((context , updateData ) => {
87- console .log (' updated' , JSON .stringify (updateData));
88- context .api .subscriptions .unsubscribeAll ().then (() => {
89- console .log (' unsubscribeAll() executed' );
90- context .api .subscriptions .subscribeToDevices (context .config .contactSensor , ' contactSensor' , ' contact' , ' myDeviceEventHandler' );
91- });
92- })
93- .subscribedEventHandler (' myDeviceEventHandler' , (context , deviceEvent ) => {
94- const value = deviceEvent .value === ' open' ? ' on' : ' off' ;
87+ .subscribedEventHandler (' myDeviceEventHandler' , (context , event ) => {
88+ const value = event .value === ' open' ? ' on' : ' off' ;
9589 context .api .devices .sendCommands (context .config .lights , ' switch' , value);
96- console .log (` sendCommands(${ JSON .stringify (context .config .lights )} , 'switch', '${ value} ')` );
97-
98- /* All subscription event handler types:
99- * - DEVICE_EVENT (context, deviceEvent)
100- * - TIMER_EVENT (context, timerEvent)
101- * - DEVICE_COMMANDS_EVENT (context, deviceId, command, deviceCommandsEvent)
102- * - MODE_EVENT (context, modeEvent)
103- * - SECURITY_ARM_STATE_EVENT (context, securityArmStateEvent)
104- */
10590 });
10691
10792/* Handle POST requests */
10893server .post (' /' , function (req , res , next ) {
109- console .log (` ${ new Date ().toISOString ()} ${ req .method } ${ req .path } ${ req .body && req .body .lifecycle } ` );
11094 smartapp .handleHttpCallback (req, res);
11195});
11296
@@ -120,12 +104,14 @@ To run as a Lambda function instead of an HTTP server, ensure that your main ent
120104
121105> ** Note:** This snippet is heavily truncated for brevity – see the web service example above a more detailed example of how to define a ` smartapp ` .
122106
123- ``` javascript
107+ ```
124108const smartapp = require('@smartthings/smartapp')
125109smartapp
110+ .app.enableEventLogging() // logs all lifecycle event requests and responses. Omit in production
126111 .page( ... )
127112 .updated(() => { ... })
128113 .subscribedEventHandler( ... );
114+
129115exports.handle = (event, context, callback) => {
130116 smartapp.handleLambdaCallback(event, context, callback);
131117};
0 commit comments