-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (102 loc) · 3.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
const mqtt = require('mqtt');
module.exports = class OrchestraTelemetry {
/**
* @param {Object} options
* @param {String} companyUUID Company UUID (obtainable from control.orchestra.it)
* @param {String} [options.environment=OrchestraTelemetry.ENVIRONMENTS.PROD] Which environment the data is sent to. It must be one of OrchestraTelemetry.ENVIRONMENTS
*/
constructor(options = {}) {
const {
companyUUID,
environment = OrchestraTelemetry.ENVIRONMENTS.PROD
} = options;
const validEnv = Object.values(OrchestraTelemetry.ENVIRONMENTS).includes(environment)
if (false === validEnv)
throw new Error(`Invalid Orchestra environment "${environment}"`);
this._config = OrchestraTelemetry._ENVIRONMENTS_CONFIG[environment];
// TODO: Check that companyUUID is a UUIDv4
if (typeof companyUUID !== 'string' && companyUUID instanceof String === false)
throw new Error(`companyUUID is not a string! "${companyUUID}"`);
this.companyUUID = companyUUID;
}
static get ENVIRONMENTS() {
return Object.freeze({
STAGE: 'STAGE',
PROD: 'PROD'
});
}
static get _ENVIRONMENTS_CONFIG() {
return Object.freeze({
STAGE: {
MQTT_HOST: 'mqtt://mqtt.stage.orchestra.it',
},
PROD: {
MQTT_HOST: 'mqtt://mqtt.orchestra.it',
}
})
}
static get SENSOR_TYPES() {
return Object.freeze({
PERCENTAGE: 'PERCENTAGE',
});
}
/**
*
* @param {Object} options
* @param {String} options.username MQTT username
* @param {String} options.password MQTT password
*/
async connect(options = {}) {
const { username, password } = options;
const url = this._config.MQTT_HOST;
this._client = mqtt.connect(url, { username, password });
return new Promise((resolve, reject) => {
this._client.on('connect', _ => {
console.log(`Connected to ${url}`);
resolve();
});
this._client.on('error', err => {
console.error(err);
reject(err);
});
});
}
/**
*
* @param {Object} options
* @param {Any} options.value It depends on the type of sensor
* @param {String} options.sensorType Must be one of OrchestraTelemetry.SENSOR_TYPES
* @param {String} options.sensorId Sensor id (obtained from control.orchestra.it)
* @param {Object} [options.extra] Additional data
*/
async sendData(options = {}) {
const { value, sensorType, sensorId, extra = {} } = options;
// Check that sensorId is a string
if (typeof sensorId !== 'string' && sensorId instanceof String === false)
throw new Error(`sensorId is not a string! "${sensorId}"`);
// Check valid sensorType
if (false === Object.values(OrchestraTelemetry.SENSOR_TYPES).includes(sensorType))
throw new Error(`Invalid sensorType "${sensorType}"`);
// Check "values"
if (value === undefined)
throw new Error(`Invalid value "undefined"`);
// Check that "extra" is a dictionary
if (!extra || extra.constructor !== Object)
throw new Error(`options.extra is not an Object "${JSON.stringify(extra)}"`);
const topic = `/Telemetry/v1/${this.companyUUID}/${sensorType}/${sensorId}/`;
const message = {
date: new Date(),
version: 'v1', // TODO: Handle support for different versions
value,
extra,
};
console.log(`sending`, topic, message);
return new Promise((resolve, reject) => {
this._client.publish(topic, JSON.stringify(message), { qos: 2 }, err => {
if (err) return reject(err);
resolve();
});
})
}
}