-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
115 lines (107 loc) · 3.51 KB
/
parser.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
const util = require("util");
const fs = require("fs");
var CONFIGPATH;
var CONFIGNAME;
var ConfigFile;
var Config;
//console.log(util.inspect(student, { showHidden: false, depth: null }));
function checkENV(ENV, alt_var, secret = false) {
if (eval("process.env." + ENV)) {
if (secret) {
console.log("Set " + ENV + " from ENV to: ***");
} else {
console.log("Set " + ENV + " from ENV to: " + eval("process.env." + ENV));
}
return eval("process.env." + ENV);
} else {
if (secret) {
console.log("Set " + ENV + " from Default to: ***");
} else {
console.log("Set " + ENV + " from Default to: " + alt_var);
}
return alt_var;
}
}
function getSafe(fn, defaultVal) {
try {
return fn();
} catch (e) {
return defaultVal;
}
}
module.exports = {
init: function (settings = { configpath: "./media/", configfile: "config_files.json" }) {
return new Promise((resolve, reject) => {
console.log("--------INIT PLAYER--------");
CONFIGPATH = checkENV("CONFIGPATH", settings.configpath);
CONFIGNAME = checkENV("CONFIGNAME", settings.configfile);
let rawdata = fs.readFileSync(CONFIGPATH + CONFIGNAME);
ConfigFile = JSON.parse(rawdata);
resolve(true);
});
},
/*
files:[{
id: number,
file: string,
fileconfig:string,
}...],
trigger:[{
id: number,
customFunction: sring,
gpio: number,
}...]
*/
parseConfig: function () {
return new Promise((resolve, reject) => {
//console.log(configfile);
//console.log(util.inspect(ConfigFile, { showHidden: false, depth: null }));
var fileCount = getSafe(() => ConfigFile.deviceKind[0].files.length, 0);
var triggerCount = getSafe(() => ConfigFile.deviceKind[0].Trigger.length, 0);
Config = { files: [], trigger: [], mainfunction: null };
for (var i = 0; i < fileCount; i++) {
let newFile = {
id: getSafe(() => ConfigFile.deviceKind[0].files[i].id, null),
file: getSafe(() => ConfigFile.deviceKind[0].files[i].file.url, null),
fileconfig: getSafe(() => ConfigFile.deviceKind[0].files[i].fileconfig, null),
};
Config.files.push(newFile);
}
for (var i = 0; i < triggerCount; i++) {
Config.trigger.push(getSafe(() => ConfigFile.deviceKind[0].Trigger[i], null));
}
Config.mainfunction = getSafe(() => ConfigFile.deviceKind[0].mainFunction, null);
console.log("Parsed: Files: " + fileCount + " Trigger: " + triggerCount);
//console.log(util.inspect(Config, { showHidden: false, depth: null }));
resolve(Config);
});
},
getConfig: function () {
return Config;
},
getConfigPath: function () {
return CONFIGPATH + CONFIGNAME;
},
getBasePath: function () {
return CONFIGPATH;
},
checkENV: function (ENV, alt_var, secret = false) {
return checkENV(ENV, alt_var, (secret = false));
},
//Get File URL from Strapi Field ID
getFileById: function (id) {
for (var i = 0; i < getSafe(() => Config.files).length; i++) {
if (getSafe(() => Config.files[i].id) === id) return CONFIGPATH.substring(0, CONFIGPATH.length - 1) + getSafe(() => Config.files[i].file);
}
return null;
},
getIdByFile: function (file) {
var file = file.replace(CONFIGPATH, "/");
for (var i = 0; i < getSafe(() => Config.files).length; i++) {
//console.log(file);
//console.log(Config.files[i].file);
if (getSafe(() => Config.files[i].file) === file) return getSafe(() => Config.files[i].id);
}
return null;
},
};