-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
137 lines (116 loc) · 3.54 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
127
128
129
130
131
132
133
134
135
136
137
'use strict';
const transformers = require('./transformers'),
{ readTemplates } = require('./helpers');
process.env.SUPPRESS_NO_CONFIG_WARNING = 'y';
const config = require('config');
const templateFolderPath = `${__dirname}/../templates`;
const defaultConfig = {
ingress_domain: ''
};
const SketchTemplater = function SketchTemplater(cfg) {
// check type of ingressDomainConfig parameter
if (typeof cfg === 'string') {
cfg = { ingress_domain: cfg };
}
// Mixin configs that have been passed in, and make those my defaults
config.util.extendDeep(defaultConfig, cfg);
config.util.setModuleDefaults('sketch-templater', defaultConfig);
const ingress_domain = config.get('sketch-templater.ingress_domain');
if (ingress_domain === '' || typeof ingress_domain !== 'string') {
console.warn('Invalid or missing ingressDomain');
}
// pre load templates from templates folder
const templates = readTemplates(templateFolderPath);
if (Object.keys(templates).length === 0) {
console.warn(
`Sketch Templater Error: No valid templates found in path ${templateFolderPath}`
);
}
this._templates = templates;
};
SketchTemplater.prototype.generateSketch = function generateSketch(
box,
{ encoding } = {}
) {
if (this._templates[box.model]) {
// transform CO₂ to CO2 just for node sketch templater
box.sensors = box.sensors.map((s) => {
if (s.title === 'CO₂') {
s.title = 'CO2';
}
return s;
});
if (encoding && encoding === 'base64') {
return Buffer.from(this._executeTemplate(box)).toString('base64');
}
return this._executeTemplate(box);
}
return `Error: No sketch template availiable for model ${box.model}`;
};
SketchTemplater.prototype._cloneBox = function _cloneBox({
_id,
name,
sensors,
serialPort,
soilDigitalPort,
soundMeterPort,
windSpeedPort,
ssid,
password,
devEUI,
appEUI,
appKey,
access_token,
display_enabled
}) {
return Object.assign(
{},
{
SENSEBOX_ID: _id,
SENSEBOX_NAME: name,
SENSOR_IDS: sensors,
INGRESS_DOMAIN: config.get('sketch-templater.ingress_domain'),
NUM_SENSORS: sensors.length,
SERIAL_PORT: serialPort,
SOIL_DIGITAL_PORT: soilDigitalPort,
SOUND_METER_PORT: soundMeterPort,
WIND_DIGITAL_PORT: windSpeedPort,
SSID: ssid,
PASSWORD: password,
SENSORS: sensors,
DEV_EUI: devEUI,
APP_EUI: appEUI,
APP_KEY: appKey,
ACCESS_TOKEN: access_token,
DISPLAY_ENABLED: display_enabled
}
);
};
SketchTemplater.prototype._executeTemplate = function _executeTemplate(
sourceBox
) {
// clone box. We're going to change its properties.
// Also appends ingressDomain property
const box = this._cloneBox(sourceBox);
// return the template matching the box model with every occurrence of
// @@subTemplateKey@@ replaced with the properties of the box
return this._templates[sourceBox.model].replace(
/@@(.+)@@/g,
function (_, subTemplateKey) {
// check if there is a transformer defined
// eslint-disable-next-line prefer-const
let [key, transformer = 'as-is'] = subTemplateKey.split('|');
let params = [];
if (transformer.indexOf('~') >= -1) {
[transformer, params] = transformer.split('~');
}
if (params) {
const parameters = params.split(',');
return transformers[transformer](box[key], ...parameters);
} else if (transformers[transformer]) {
return transformers[transformer](box[key]);
}
}
);
};
module.exports = SketchTemplater;