From the example provided in the last section, it is very easy to implement an analytical function to check the signals and to apply a digital twin. The following function defines a simple excursion monitoring function and evaluates the value of data streaming:
const digitalTwin = {};
digitalTwin['signal1'] = {upperLimit: 40, lowerLimit: 10};
exports.helloPubSub = (event, callback) => {
const pubsubMessage = event.data;
const str = Buffer.from(pubsubMessage.data, 'base64').toString();
console.log(str);
const data= str.split(',');
console.log({
'device': data[0],
'tag': data[1],
'value': data[2],
'ts': data[3],
'quality': data[4]
});
const deviceId=data[0];
const tag=data[1];
const value=data[2];
const timestamp = data[3];
const quality = data[4];
var dt = digitalTwin[tag];
if ((value > dt.upperLimit || value <dt.lowerLimit) && (quality=='GOOD'))
{
console.log(`excursion on ${tag} : ${value}`);
}
callback();
};
Do not forget to call the callback to acknowledge the receipt.
To deploy the current function, we can follow the same procedure that we carried out previously:
- From the left-hand menu of the homepage, click on Cloud Functions, then Create function, and add the following information:
A simple excursion monitoring with cloud functions
- Finally, click on the Save button
To check whether our first basic analytics have executed successfully, run the device emulator again and check the function log.