Last active
March 14, 2018 15:50
-
-
Save amostajo/46b133dc827ee84833b6fc159b8fa9d9 to your computer and use it in GitHub Desktop.
Plexus sample / firebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
const bigquery = require('@google-cloud/bigquery')(); | |
const cors = require('cors')({ origin: true }); | |
admin.initializeApp(functions.config().firebase); | |
const db = admin.database(); | |
/** | |
* @see https://github.com/alvarowolfx/weather-station-gcp-mongoose-os/blob/master/functions/index.js | |
*/ | |
exports.setDeviceCapturedData = functions.pubsub | |
.topic('iot-topic') | |
.onPublish(event => { | |
// Set device data | |
let data = event.data.json; | |
data.updatedAt = event.timestamp; | |
return Promise.all([ | |
updateRealtime( | |
event.data.attributes['clientId'], | |
event.data.attributes['deviceId'], | |
data | |
) | |
]); | |
}); | |
/** | |
* Updates firebase realtime data, with the most resent data captured by a device. | |
* | |
* @param {string} clientId ID of the client the device belongs to. | |
* @param {string} deviceId Device ID. | |
* @param {object} data Data captured by device. | |
*/ | |
function updateRealtime(clientId, deviceId, data) | |
{ | |
return db.ref(`${clientId}/devices/${deviceId}`).set(data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment