Cloud Functions

Google Cloud Functions are serverless services that respond to events, such as data coming from our device. We are now going to set up a Cloud Function to extract the data sent by the device, process it immediately, and store it in the central data storage:

  1. From the Google Cloud Console's left-hand menu, click on Cloud Functions and then Enable API:

Enabling the Google Cloud Functions API
  1. Once it is enabled, we can click on the Create function section.
  1. We can then define the name of our function as iiot-book-function-1 and the memory allocated as 128 MB. The trigger is Cloud Pub/Sub and the topic is signals, which we created a few paragraphs prior on the Building the devices registry section:

The definition of iiot-book-function to process the incoming message
  1. Under the source code section of the inline editor on the index.js tab, we can copy and paste the following code:
// needs npm install --save @google-cloud/bigtable
// Imports the Google Cloud client library
const Bigtable = require('@google-cloud/bigtable');

// The name of the Cloud Bigtable instance
const INSTANCE_NAME = 'iiot-book-data';
const PROJECT_NAME = 'iiot-book'; //replace with iiot-book-local for simulation
// The name of the Cloud Bigtable table
const TABLE_NAME = 'iiot-book-signals';
const COLUMN_FAMILY_ID='signal1';
const COLUMN_QUALIFIER='value';

var bigtableOptions = {
projectId: PROJECT_NAME,
};

// Create a Bigtable client
const bigtable = new Bigtable(bigtableOptions);

// Connect to an existing instance:my-bigtable-instance
const instance = bigtable.instance(INSTANCE_NAME);

// Connect to an existing table:my-table
const table = instance.table(TABLE_NAME);

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];

try {

const tsarray = [deviceId +"#"+ timestamp];
const rowsToInsert = tsarray.map((ts, index) => ({
key: `${ts}`,
data: {
[tag]: {
[COLUMN_QUALIFIER]: {
value: value
},
},
},
}));
await table.insert(rowsToInsert);
} catch (err) {
console.error(`Error inserting data:`, err);
callback(); //DONE WITH ERROR
}

};
  1. Under the source code section on the package.json tab, we can copy and paste the following code:
{
"name": "sample-pubsub",
"version": "0.0.1",
"dependencies": {
"@google-cloud/bigtable" : "^1.0.0"
}
}

The purpose of this code is to parse our MQTT message and to store it in Google Bigtable. We can now run our example.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset