SAP Appgyver. Smart Home Automation powered with k...
Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
This brief, part of the SAP Community Focus: SAP BTP Kyma Runtime blog posts series, is to showcase the power of kyma serverless functions that come along with SAP BTP, Kyma runtime. and how they nicely blend into the Low-code/No-code SAP strategy.
In a nutshell, "serverless" means the server itself is provided by the underlying runtime infrastructure freeing you to focus on the business logic rather then on doing the plumbing...
This time I shall describe how the kyma serveless, together with SAP LCNC Productivity tools, can be leveraged in a consumer-grade scenario against a 3rd party Telldus Live API that gives access to a variety of home automation accessories (various sensors, 433MHz transmitters/receivers, etc).
A github repo with SSH access enabled for seamless and continuous integration/development against your kyma cluster. Furthermore, any development tool or IDE/WebIDE can be used to manage your business code logic. And the kyma cluster will do the rest for you.
Disclaimer:
The ideas presented in this blog are personal insights thus not necessarily endorsed by SAP.
I have no affiliation with any of the non-SAP brand names quoted in this brief.
Telldus is a Swedish brand owned and marketed by Proove AB.
Please note all the code snippets and gists are provided “as is”.
Images/data in this blog post is from SAP internal sandbox, sample data, or personal demo systems. Any resemblance to real data is purely coincidental.
Access to online resources referenced in this blog may be subject to a contractual relationship with these software vendors. Always refer to T&C.
Once upon a time.
I dipped into home automation quite some time ago. At that time there were plenty of relatively cheap RF (radio-frequency) accessories on the market. And I happened to put my hands on a Telldus device which is a wonderful RF dual controller (433 transmitter and receiver) backed with a cloud native service.
That's how it all started....Let me walk you through the main building blocks of the SAP Appgyver crafted home automation application.
Before we start...
OAuth 1.0a Request Authorization refresher.
Telldus Live APIs require consumer key based authentication with OAuth 1.0a protocol.
This protocol is used by a number of popular services like Twitter, Flickr, etc.
In a nutshell a consumer key can be used to request a short lived access token that, after verification, will be exchanged against permanent private keys.
I recommend the following OAuth 1.0a protocol primers:
You can get away with implementing all the steps of OAuth 1.0a based authentication flow by using the private keys generated by Telldus for your own user.
However, if you wanted to distribute your app [to other Telldus Live users], you will need to implement the full-fledged OAuth 1.0a authentication flow with a public consumer key (that you will have to request from Telldus side).
Step1. SAP BTP Kyma runtime (SKR). Telldus Live APIs and CORS policy.
I have chosen to implement the Telldus Live APIs using a single kyma nodejs serverless function as a business logic API provider.
At the same time, by leveraging the intrinsic SAP BTP, Kyma Runtime connectivity proxy, I was able to cater for both telldus stick net and znet devices that I own, at a time.
Worth mentioning API endpoints of any kyma function exposed with an API rule are already CORS-enabled.
The below depicts the view of the function code (handler.js and package.json) in a github repo:
Good to know:
any development tool or IDE/WebIDE can be used to manage your business code logic. And the kyma cluster will do the rest for you. The "rest" means the kyma cluster, at each gitibub repo function code commit, will take your code and create and/or update the necessary kubernetes resources on the fly. This way your business logic runs forever and there is never any downtime.
In my example all the business logic is implemented in one single nodejs function telldus_anywhere which is called from the main function of the kyma function handler.js module.
module.exports = {
main: async function (event, context) {
return telldus_anywhere(event);
}
}
Good to know:
If required, one can have multiple source code files using the CommonJS require mechanism for the index.js and then the ecmascript loading mechanism for the other source code files...
Connectivity proxy and Cloud Connector
TellStickZnetLiteV2 features can be also used offline without the internet connection or in case there is an outage of the cloud service.
In order to explore this feature I opted for having a Cloud Connector running in my private home network and used the connectivity proxy on the kyma cluster side.
The most important to understand is that the Cloud Connector must be hooked up with the BTP sub-account where the connectivity proxy service instance has been provisioned as depicted below:
The virtual mappings on SAP BTP sub-account coming from an on premise side Cloud Connector
On. a side note this is the same BTP sub-account where your SKR cluster has been provisioned. SKR will always use cross-consumable BTP services from the BTP sub-account where it has been provisioned from.
Good to know:
You may find useful the following blogs post of mine with further details on using a connectivity proxy in the context of a kyma/k8s cluster.
The below code snippet explains how to call into on on premise znet device HTTP REST APIs from the public internet using a serverless function running on SAP BTP, Kyma Runtime.
Step2. Designing myTelldus-on-Kyma web application (SAP Appgyver)
myTelldus-on-Kyma is a web application.
And as such it can be deployed to any HTML5/Fiori host, including the SAP BTP Launchpad Service or SAP Work Zone. Both provide a unified and federated user experience and the latter is an excellent digital asset management tool for a workplace.
At the same time, I made sure the web-app is still compatible with the mobile devices (e.g. it can be run on a mobile device or with the SAP Appgyver Preview app).
This goal can be achieved dynamically by testing the active runtime thus allowing for mobile app compatible widgets to be displayed only if the app is running on such a compatible device.
Whatever you do in Composer Pro it follows the Model-Viewer-Controller design paradigm.
Thus, let's start with the data model.
To start with I have defined a few of HTPP REST API endpoints in my kyma function. As afore-mentioned these endpoints are already CORS-enabled thus I can use them directly from SAP Appgyver Data Manager as follows:
Base definition
Get Collection - getting the list of client devices
When it comes to smart devices and home automation the geo-localisation is one of the most fundamental features and capabilities. For instance no geo-fencing without geo-localisation!
SAP Appgyver offers a map view component on its marketplace that accepts the geo-location coordinates but it can only be used with the native mobile runtimes. In other words if you want to have one single application built for both web and native environments you may need an alternative solution, for instance an HTML5 hosted map:
Good to know:
You may have wondered how to provide a zoom value for the map view. With React Native things may get a bit messy and confusing especially when it comes to understand a rather cumbersome concept of latitude and longitude delta.