-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathintegrations.js
91 lines (80 loc) · 3.08 KB
/
integrations.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
'use strict';
const { mongoose } = require('../db'),
utils = require('../utils'),
isJSONParseableValidation = [utils.isJSONParseable, '{PATH} is not parseable as JSON'],
mqttClient = require('./mqttClient');
const mqttSchema = new mongoose.Schema({
enabled: { type: Boolean, default: false, required: true },
url: { type: String, trim: true, validate: [function validMqttUri (url) { return (
url === '' ||
url.startsWith('mqtt://') ||
url.startsWith('mqtts://') ||
url.startsWith('ws://') ||
url.startsWith('wss://')
); }, '{PATH} must be either empty or start with mqtt(s):// or ws(s)://'] },
topic: { type: String, trim: true },
messageFormat: { type: String, trim: true, enum: ['json', 'csv', 'application/json', 'text/csv', 'debug_plain', ''] },
decodeOptions: { type: String, trim: true, validate: isJSONParseableValidation },
connectionOptions: { type: String, trim: true, validate: isJSONParseableValidation }
}, { _id: false, usePushEach: true });
const ttnSchema = new mongoose.Schema({
dev_id: { type: String, trim: true, required: true },
app_id: { type: String, trim: true, required: true },
port: { type: Number, min: 0 },
profile: { type: String, trim: true, enum: ['json', 'debug', 'sensebox/home', 'lora-serialization', 'cayenne-lpp'], required: true },
decodeOptions: [{}]
}, { _id: false, usePushEach: true });
const tinggSchema = new mongoose.Schema({
imsi: { type: String, trim: true, required: true },
secret_code: { type: String, trim: true, required: true }
}, { _id: false, usePushEach: true });
const integrationSchema = new mongoose.Schema({
mqtt: {
type: mqttSchema,
required: false,
validate: [function validMqttConfig (mqtt) {
if (mqtt.enabled === true) {
if (!utils.isNonEmptyString(mqtt.url)) {
return false;
}
if (!utils.isNonEmptyString(mqtt.topic)) {
return false;
}
if (!utils.isNonEmptyString(mqtt.messageFormat)) {
return false;
}
}
return true;
}, 'if {PATH} is true, url, topic and messageFormat should\'t be empty or invalid']
},
ttn: {
type: ttnSchema,
required: false,
validate: [{
/* eslint-disable func-name-matching */
validator: function validTTNDecodeOptions (ttn) {
/* eslint-enable func-name-matching */
if (['debug', 'lora-serialization', 'cayenne-lpp'].indexOf(ttn.profile) !== -1) {
return (ttn.decodeOptions && ttn.decodeOptions.constructor === Array);
}
},
msg: 'this profile requires an array \'decodeOptions\''
}]
},
gsm: {
type: tinggSchema,
required: false,
validate: function validate () {
return true;
}, msg: 'Something went wrong with GSM creds'
}
}, { _id: false, usePushEach: true });
const addIntegrationsToSchema = function addIntegrationsToSchema (schema) {
schema.add({ integrations: { type: integrationSchema } });
mqttClient.addToSchema(schema);
};
module.exports = {
schema: integrationSchema,
addToSchema: addIntegrationsToSchema,
// no model, bc its used as subdocument in boxSchema!
};