Hi all,
I am moving an express API to a lambda gatway API and I want to continue to use the SmartThings SDK.
On the documentation it does suggest there is support for running lambda functions using the handleLambdaCallback
method.
I am transforming something like this (Express)
/// handleIndexPost ///
// This is the route that smarthings will post to
const handleIndexPost = async function (req, res, next) {
try{
// HAndle the http callback
let result = await smartapp.handleHttpCallback(req, res);
}catch(error){
logger.error(`Error in handling post request to \ ${error}`)
}
}
To something like this (Lambda)
var smartapp = require('./lib/smartapp')
exports.handler = async (event,context,callback) => {
if (event){
console.log(`EVENT BODY: ${event.body}`)
}
// Handle the Http callback
smartapp.handleLambdaCallback(event, context, callback);
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify('Success!'),
};
return response;
}
Lambda is receiving the data from SmartThings from subscriptions however it is not calling the subscription handlers I have defined in lib/smartapp
. Instead looking at the lambda logs I get:
2021-03-09T18:00:55.732Z warn: Lifecycle undefined not supported
I’m not sure why this is as it was working with express. The file ./lib/smatapp
is shown below. The environment variables are being read in correctly.
/// Imports ///
// SmartApp - SDK for Smarthings
// subscriptionHandler - Implements logic when subscription events occur
const SmartApp = require('@smartthings/smartapp');
const subscriptionHandler = require('./subscriptionHandler')
/* Define the SmartApp */
const smartapp = new SmartApp()
.appId(process.env.APP_ID)
.clientId(process.env.CLIENT_ID)
.clientSecret(process.env.CLIENT_SECRET)
.redirectUri(process.env.REDIRECT_URI)
/// Meter One Event Handler ///
.subscribedEventHandler('VoltageMeter1', subscriptionHandler.voltageHandler)
.subscribedEventHandler('EnergyMeter1', subscriptionHandler.energyHandler)
.subscribedEventHandler('PowerMeter1', subscriptionHandler.powerHandler)
/// Meter Two Event Handler ///
.subscribedEventHandler('VoltageMeter2', subscriptionHandler.voltageHandler)
.subscribedEventHandler('EnergyMeter2', subscriptionHandler.energyHandler)
.subscribedEventHandler('PowerMeter2', subscriptionHandler.powerHandler)
module.exports = smartapp
The file ./subscriptionHandler
is also shown below which holds the functions for subscription events.
/// powerHandler ///
// Event handler for when a power capabality changes state
async function powerHandler(ctx,event,eventTime){
console.log(`POWER ${event.deviceId} ${event.componentId}.${event.capability}.${event.attribute}: ${event.value}`)
}
/// voltageHandler ///
async function voltageHandler(ctx,event,eventTime){
console.log(`VOLTAGE ${event.deviceId} ${event.componentId}.${event.capability}.${event.attribute}: ${event.value}`)
}
/// energyHandler ///
async function energyHandler(ctx,event,eventTime){
console.log(`ENERGY ${event.deviceId} ${event.componentId}.${event.capability}.${event.attribute}: ${event.value}`)
}
module.exports = {
powerHandler,
voltageHandler,
energyHandler
}