-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.js
132 lines (100 loc) · 2.57 KB
/
json.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
const write_options = { spaces : '\t' };
const defaults = require('defaults-deep');
const jsonfile = require('jsonfile');
const file_config = './config.json';
const file_status = './status.json';
const config_default = require('./config-default');
const status_default = require('./status-default');
let saveInterval;
// Read config+status
async function read(skipInterval = false) {
// Read JSON config+status files
await readConfig();
await readStatus();
if (skipInterval === true) return;
log.lib('Set 5 minute data save interval');
saveInterval = setInterval(write, (5 * 60 * 1000));
}
// Write config+status
async function write(param = null) {
// Write JSON config+status files
await writeConfig();
await writeStatus();
if (param === 'term') clearInterval(saveInterval);
}
// Read config JSON
async function readConfig() {
let config_data;
try {
config_data = await jsonfile.readFileSync(file_config);
}
catch (error) {
log.lib('Failed reading config, applying default config');
// log.error(error);
config = config_default;
await writeConfig();
return false;
}
// Lay the default values on top of the read object, in case new values were added
config = await defaults(config_data, config_default);
log.lib('Read config');
}
// Read status JSON
async function readStatus() {
let status_data;
try {
status_data = await jsonfile.readFileSync(file_status);
}
catch (error) {
log.lib('Failed reading status, applying default status');
// log.error(error);
status = status_default;
await writeStatus();
return false;
}
// Lay the default values on top of the read object, in case new values were added
status = await defaults(status_data, status_default);
log.lib('Read status');
}
// Write config JSON
async function writeConfig() {
// Don't write if empty
if (typeof config.mqtt === 'undefined') {
log.lib('Failed writing config, config object empty');
return;
}
try {
await jsonfile.writeFileSync(file_config, config, write_options);
}
catch (error) {
log.lib('Failed writing config');
log.error(error);
return false;
}
log.lib('Wrote config');
}
// Write status JSON
async function writeStatus() {
// Don't write if empty
if (typeof status.xbee === 'undefined') {
log.lib('Failed writing status, status object empty');
return;
}
try {
await jsonfile.writeFileSync(file_status, status, write_options);
}
catch (error) {
log.lib('Failed writing status');
log.error(error);
return false;
}
log.lib('Wrote status');
}
module.exports = {
readConfig,
writeConfig,
readStatus,
writeStatus,
read,
write,
};