Commit 802f60fe authored by Spencer Williams's avatar Spencer Williams
Browse files

setup cloud provider strategy pattern

parent f3f70388
node_modules/
config.json
{
"cloudProvider": {
"aws": {
"accessKeyId": "",
"secretAccessKey": "",
"region": "",
"deliveryStream": ""
}
}
}
......@@ -4,6 +4,8 @@ const noble = require('noble');
const Device = require('./device');
const rigadoSensorBeacon = require('./rigado-sensor-beacon');
const CloudProvider = require('./providers/provider');
const config = require('./config.json');
/**
* Connected devices
......@@ -11,6 +13,12 @@ const rigadoSensorBeacon = require('./rigado-sensor-beacon');
*/
const devices = {};
/**
* Cloud Provider access
* @global
*/
const provider = new CloudProvider(config);
function startScanning() {
// Restart scanning
// https://github.com/sandeepmistry/noble/issues/223
......@@ -39,7 +47,11 @@ function onDiscover(beaconInst) {
device.setUp()
.then(() => {
device.onLuxometerChange((d, lux) => {
console.log(`Device ${d.id} lux: ${lux.toFixed(1)}`);
const telemetry = {
deviceID: d.id,
light: lux.toFixed(1),
};
provider.sendTelemetry(telemetry);
});
// Wait for as long as possible before scanning again to avoid race
......
'use strict';
const ProviderStrategy = require('./providerStrategy.js');
class AWSProvider extends ProviderStrategy {
constructor(config) {
super(config);
this.name = 'AWS';
}
connectGateway() {
console.log('[AWS] connectGateway');
}
sendTelemetry(telemetry) {
console.log(`[AWS] sendTelemetry ${JSON.stringify(telemetry)}`);
}
}
module.exports = AWSProvider;
'use strict';
const AWSProvider = require('./aws.js');
class Provider {
constructor(config) {
if (typeof config.cloudProvider === 'undefined' ||
Object.keys(config.cloudProvider).length === 0) {
throw new Error('No cloudProvider in config');
}
const provider = Object.keys(config.cloudProvider)[0];
const providerConfig = JSON.stringify(config.cloudProvider[provider], null, 4);
console.log(`Provider: ${provider}`);
console.log(`Config: ${providerConfig}`);
switch (provider) {
case 'aws':
this.strategy = new AWSProvider(config.cloudProvider[provider]);
break;
default:
throw new Error(`Unrecognized cloud provider '${provider}' in config.`);
}
}
connectGateway() {
return this.strategy.connectGateway();
}
sendHeartbeat() {
return this.strategy.sendHeartbeat();
}
sendTelemetry(telemetry) {
return this.strategy.sendTelemetry(telemetry);
}
connectEndDevice(device) {
return this.strategy.connectEndDevice(device);
}
}
module.exports = Provider;
'use strict';
const os = require('os');
/**
* Defines the public interface for all Cloud Provider strategies
*/
class ProviderStrategy {
constructor(config) {
this.config = config;
this.hostname = os.hostname();
this.osName = os.type();
this.NotYetImplementedError = new Error('Not yet implemented');
}
/**
* Sends information about this gateway to the Cloud Provider
* to get it connected. Should return the Gateway's authentication
* credentials.
*/
connectGateway() {
return Promise.reject(this.NotYetImplementedError);
}
/**
* Inform the Cloud Provider that the Gateway is alive and well
*/
sendHeartbeat() {
return Promise.reject(this.NotYetImplementedError);
}
/**
* Send sensor telemetry data to the Cloud Provider
*/
sendTelemetry(json) {
return Promise.reject(this.NotYetImplementedError);
}
/**
* Inform the Cloud Provider about a new sensor device that
* this Gateway is connected to
*/
connectEndDevice(device) {
return Promise.reject(this.NotYetImplementedError);
}
}
module.exports = ProviderStrategy;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment