-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from jryd/update-google-packages
Updated Google packages
- Loading branch information
Showing
3 changed files
with
73 additions
and
62 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,89 @@ | ||
var colors = require('colors'); | ||
var util = require('util'); | ||
let colors = require('colors') | ||
let util = require('util') | ||
let PubSub = require('@google-cloud/pubsub') | ||
let Datastore = require('@google-cloud/datastore') | ||
|
||
/* CONFIGURATION */ | ||
var config = { | ||
gcpProjectId: '', | ||
gcpPubSubSubscriptionName: '', | ||
gcpServiceAccountKeyFilePath: './gcp_private_key.json' | ||
let config = { | ||
gcpProjectId: '', | ||
gcpPubSubSubscriptionName: '', | ||
gcpServiceAccountKeyFilePath: './gcp_private_key.json' | ||
} | ||
_checkConfig(); | ||
/* END CONFIGURATION */ | ||
console.log(colors.magenta('Authenticating with Google Cloud...')) | ||
var gcloud = require('google-cloud')({ | ||
|
||
/* PUBSUB */ | ||
console.log(colors.magenta('Authenticating PubSub with Google Cloud...')) | ||
const pubsub = new PubSub({ | ||
projectId: config.gcpProjectId, | ||
keyFilename: config.gcpServiceAccountKeyFilePath, | ||
}); | ||
keyFilename: config.gcpServiceAccountKeyFilePath, | ||
}) | ||
console.log(colors.magenta('Authentication successful!')) | ||
|
||
const subscription = pubsub.subscription(config.gcpPubSubSubscriptionName); | ||
subscription.on('message', message => { | ||
console.log(colors.cyan('Particle event received from Pub/Sub!\r\n'), _createParticleEventObjectForStorage(message, true)); | ||
// Called every time a message is received. | ||
// message.id = ID used to acknowledge its receival. | ||
// message.data = Contents of the message. | ||
// message.attributes = Attributes of the message. | ||
storeEvent(message); | ||
message.ack(); | ||
}); | ||
/* END PUBSUB */ | ||
|
||
var datastore = gcloud.datastore(); | ||
var pubsub = gcloud.pubsub(); | ||
|
||
|
||
var subscription = pubsub.subscription(config.gcpPubSubSubscriptionName); | ||
|
||
/* DATASTORE */ | ||
console.log(colors.magenta('Authenticating Datastore with Google Cloud...')) | ||
const datastore = new Datastore({ | ||
projectId: config.gcpProjectId, | ||
keyFilename: config.gcpServiceAccountKeyFilePath, | ||
}) | ||
console.log(colors.magenta('Authentication successful!')) | ||
|
||
function storeEvent(message) { | ||
var key = datastore.key('ParticleEvent'); | ||
let key = datastore.key('ParticleEvent'); | ||
|
||
datastore.save({ | ||
key: key, | ||
data: _createEventObjectForStorage(message) | ||
}, function(err) { | ||
if(err) { | ||
console.log(colors.red('There was an error storing the event'), err); | ||
} | ||
console.log(colors.green('Particle event stored in Datastore!\r\n'), _createEventObjectForStorage(message, true)) | ||
}); | ||
datastore | ||
.save({ | ||
key: key, | ||
data: _createParticleEventObjectForStorage(message) | ||
}) | ||
.then(() => { | ||
console.log(colors.green('Particle event stored in Datastore!\r\n'), _createParticleEventObjectForStorage(message, true)) | ||
}) | ||
.catch(err => { | ||
console.log(colors.red('There was an error storing the event:'), err); | ||
}); | ||
|
||
}; | ||
/* END DATASTORE */ | ||
|
||
subscription.on('message', function(message) { | ||
console.log(colors.cyan('Particle event received from Pub/Sub!\r\n'), _createEventObjectForStorage(message, true)); | ||
// Called every time a message is received. | ||
// message.id = ID used to acknowledge its receival. | ||
// message.data = Contents of the message. | ||
// message.attributes = Attributes of the message. | ||
storeEvent(message); | ||
message.ack(); | ||
}); | ||
|
||
/* HELPERS */ | ||
function _checkConfig() { | ||
if(config.gcpProjectId === '' || !config.gcpProjectId) { | ||
console.log(colors.red('You must set your Google Cloud Platform project ID in tutorial.js')); | ||
process.exit(1); | ||
} | ||
if(config.gcpPubSubSubscriptionName === '' || !config.gcpPubSubSubscriptionName) { | ||
console.log(colors.red('You must set your Google Cloud Pub/Sub subscription name in tutorial.js')); | ||
process.exit(1); | ||
} | ||
if (config.gcpProjectId === '' || !config.gcpProjectId) { | ||
console.log(colors.red('You must set your Google Cloud Platform project ID in pubSubToDatastore.js')); | ||
process.exit(1); | ||
} | ||
if (config.gcpPubSubSubscriptionName === '' || !config.gcpPubSubSubscriptionName) { | ||
console.log(colors.red('You must set your Google Cloud Pub/Sub subscription name in pubSubToDatastore.js')); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
function _createEventObjectForStorage(message, log) { | ||
var obj = { | ||
gc_pub_sub_id: message.id, | ||
device_id: message.attributes.device_id, | ||
event: message.attributes.event, | ||
data: message.data, | ||
published_at: message.attributes.published_at | ||
} | ||
|
||
if(log) { | ||
return colors.grey(util.inspect(obj)); | ||
} else { | ||
return obj; | ||
} | ||
}; | ||
function _createParticleEventObjectForStorage(message, log) { | ||
|
||
let obj = { | ||
gc_pub_sub_id: message.id, | ||
device_id: message.attributes.device_id, | ||
event: message.attributes.event, | ||
data: data, | ||
published_at: message.attributes.published_at | ||
} | ||
|
||
if (log) { | ||
return colors.grey(util.inspect(obj)); | ||
} else { | ||
return obj; | ||
} | ||
}; | ||
/* END HELPERS */ |