-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata-logging.js
143 lines (116 loc) · 4.16 KB
/
data-logging.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
138
139
140
141
142
143
const fs = require('fs');
const path = require('path');
const sds011Sensor = require('./sensors/SDS011');
sds011Sensor.on('measure', sds011MeasureEvent);
const dht22Sensor = require('./sensors/DHT22');
dht22Sensor.on('measure', dht22MeasureEvent);
const mhz19bSensor = require('./sensors/MHZ19B');
mhz19bSensor.on('data', mhz19bMeasureEvent);
const latestReadings = {};
module.exports.latestReadings = latestReadings;
async function dht22MeasureEvent(data) {
try {
const timestamp = new Date();
timestamp.setUTCMilliseconds(0);
const temperature = data['temperature'];
const humidity = data['humidity'];
latestReadings.temperature = temperature;
latestReadings.humidity = humidity;
await LogReadingDaily('temperature', timestamp, temperature);
await LogReadingMonthly('temperature', timestamp, temperature);
await LogReadingDaily('humidity', timestamp, humidity);
await LogReadingMonthly('humidity', timestamp, humidity);
} catch (error) {
console.log('Error taking DHT22 reading', error);
}
}
async function sds011MeasureEvent(data) {
try {
const timestamp = new Date();
timestamp.setUTCMilliseconds(0);
const pm2p5 = data['PM2.5'];
const pm10 = data['PM10'];
latestReadings.pm2p5 = pm2p5;
latestReadings.pm10 = pm10;
await LogReadingDaily('pm2.5', timestamp, pm2p5);
await LogReadingMonthly('pm2.5', timestamp, pm2p5);
await LogReadingDaily('pm10', timestamp, pm10);
await LogReadingMonthly('pm10', timestamp, pm10);
} catch (error) {
console.log('Error taking SDS011 reading', error);
}
}
async function mhz19bMeasureEvent(data) {
try {
const timestamp = new Date();
timestamp.setUTCMilliseconds(0);
const co2 = data['co2'];
latestReadings.co2 = co2;
await LogReadingDaily('CO2', timestamp, co2);
await LogReadingMonthly('CO2', timestamp, co2);
} catch (error) {
console.log('Error taking MH-Z19B reading', error);
}
}
async function LogReadingDaily(name, timestamp, value) {
const readingDirectory = path.join(
__dirname,
'data',
name,
timestamp.getUTCFullYear().toString(),
zeroPad((timestamp.getUTCMonth() + 1), 2),
zeroPad((timestamp.getUTCDate()), 2)
);
await CreateDirectoryAsync(readingDirectory);
const filename = 'raw.csv';
const filepath = path.join(readingDirectory, filename);
const line = `${timestamp.toISOString()},${value}\r\n`;
await AppendFileAsync(filepath, line);
}
async function LogReadingMonthly(name, timestamp, value) {
const readingDirectory = path.join(
__dirname,
'data',
name,
timestamp.getUTCFullYear().toString(),
zeroPad((timestamp.getUTCMonth() + 1), 2)
);
await CreateDirectoryAsync(readingDirectory);
const filename = 'raw.csv';
const filepath = path.join(readingDirectory, filename);
const date = new Date()
const line = `${timestamp.toISOString()},${value}\r\n`;
await AppendFileAsync(filepath, line);
}
const zeroPad = (num, places) => String(num).padStart(places, '0');
async function AppendFileAsync(filepath, text) {
return new Promise((resolve, reject) => {
fs.appendFile(filepath, text, (err) => {
if (err) return reject(err);
return resolve();
})
})
}
async function CreateDirectoryAsync(directory) {
return new Promise((resolve, reject) => {
fs.mkdir(directory, { recursive: true }, (err) => {
if (err) return reject(err);
return resolve();
})
})
}
async function CreateAverages() {
const signalFolders = await GetFolders('./data');
// load averages files
// look for missing values
// try to load files with missing values
// calculate missing values
// save averages files
}
async function GetFolders(directory) {
return new Promise((resolve, reject) => {
fs.readdir(directory, { withFileTypes: true }, (err, files) => {
resolve(files.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name));
})
})
}