About this post
I’m building my first SmartApp to migrate to the SmartThings Edge platform an app developed using legacy Groovy technology. I have very little knowledge about the Unix platform, NodeJS, AWS and SmartThings Edge. When I started from scratch, I made a lot of mistakes, because many things that seem obvious to me now were not obvious to me at the beginning. I just want to share my experience and the information I have gathered from various sources. I hope this will save you time to develop your first SmartApp.
Troubleshooting
You are likely to receive error messages during development. Something like this: “An unexpected error occurred… email us at support@smartthing.com … Reference ID …” or “Network error… Please try again later”. Do not contact SmartThings support (you will confuse them and they will not be able to help you) and do not try again later. This is your fault and the only way to resolve it is to look through the logs and ask this community for help. Here I found many thoughtful and helpful people who helped me solve my problems.
Hosting SmartApp
Before registering a SmartApp at Developer Workspace , you need to create it. I use AWS because AWS can easily host WebHook and Lambda functions. The implementation is very similar. Here is the code I used for testing:
exports.handler = function (request, context, callback) {
try {
const webHood = request.lifecycle === undefined;
if (webHood) {
request = JSON.parse(request.body);
}
let response = ProcessRequest(request);
if (response.statusCode === undefined)
response.statusCode = 200;
if (webHood)
callback(null, { statusCode: 200, body: JSON.stringify(response) });
else
callback(null, response);
} catch (ex) {
callback({}, null);
}
}
I tested both, WebHook and Lambda, and decided to use Lambda because I don’t need to check WebHook digital signatures, etc.
After you create AWS Lambda, you need to set permissions for the function. The document says to run the following command in a terminal shell:
aws lambda add-permission --profile [my-profile-name] --function-name [my-function-name] --statement-id smartthings --principal [PRINCIPAL ID FROM SMARTTHINGS] --action lambda:InvokeFunction
You must use CloudShell on AWS. CloudShell can be launched from the [>] icon in the upper right corner of the AWS console.
Don’t ask me what the “my-profile-name - your named profile as stored in your AWS config file” means. I just ran the following command and it did the job:
aws lambda add-permission --function-name [my-function-name] --statement-id smartthings --principal 906037444270 --action lambda:InvokeFunction
You can see the results in the Lambda console under Configuration/Permissions section.
SmartApp Registration
You can now register your SmartApp at Developer Workspace. You can even submit it for testing. The SmartThings backend won’t call it, but it will check permissions etc.
NodeJS SDK
Finally, you are ready to write the code. I’m using the REST API directly because I’m developing a stateless application and I want it to be lightweight and fast. But you probably want to use the NodeJS SDK.
I tried running “npm i @smartthings/smartapp --save” and found that the SDK was installed in the “/home/cloudshell-user/node_modules/@smartthings/smartapp” folder. But when I tried to add “const SmartApp = require(‘@smartthings/smartapp’)” to my source code, I got an error that the package was not found.
I believe I should add the package path to my Lambda config settings. However, I am not currently using the SDK. Can anyone share their experience of installing the NodeJS SDK?
[To be continued]