@@ -64,10 +64,12 @@ const PORT = 8080;
64
64
65
65
server .use (express .json ());
66
66
67
- // @smartthings_rsa.pub is your on-disk public key
68
- // If you do not have it yet, omit publicKey()
67
+ /* Define the SmartApp */
69
68
smartapp
69
+ // @smartthings_rsa.pub is your on-disk public key
70
+ // If you do not have it yet, omit publicKey()
70
71
.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
71
73
.configureI18n ()
72
74
.page (' mainPage' , (context , page , configData ) => {
73
75
page .section (' sensors' , section => {
@@ -77,36 +79,18 @@ smartapp
77
79
section .deviceSetting (' lights' ).capabilities ([' switch' ]).multiple (true ).permissions (' rx' );
78
80
});
79
81
})
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' );
82
86
})
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' ;
95
89
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
- */
105
90
});
106
91
107
92
/* Handle POST requests */
108
93
server .post (' /' , function (req , res , next ) {
109
- console .log (` ${ new Date ().toISOString ()} ${ req .method } ${ req .path } ${ req .body && req .body .lifecycle } ` );
110
94
smartapp .handleHttpCallback (req, res);
111
95
});
112
96
@@ -120,12 +104,14 @@ To run as a Lambda function instead of an HTTP server, ensure that your main ent
120
104
121
105
> ** Note:** This snippet is heavily truncated for brevity – see the web service example above a more detailed example of how to define a ` smartapp ` .
122
106
123
- ``` javascript
107
+ ```
124
108
const smartapp = require('@smartthings/smartapp')
125
109
smartapp
110
+ .app.enableEventLogging() // logs all lifecycle event requests and responses. Omit in production
126
111
.page( ... )
127
112
.updated(() => { ... })
128
113
.subscribedEventHandler( ... );
114
+
129
115
exports.handle = (event, context, callback) => {
130
116
smartapp.handleLambdaCallback(event, context, callback);
131
117
};
0 commit comments