-
Notifications
You must be signed in to change notification settings - Fork 0
/
log-job.js
131 lines (109 loc) · 3.65 KB
/
log-job.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
// Copyright (c) 2017 Sergii Popov
//
// GNU GENERAL PUBLIC LICENSE
// Version 3, 29 June 2007
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
'use strict';
let uuid = require('uuid');
let config = require('./config');
var deviceManager = require('./app/manager/device-manager');
var measurement = require('./app/model/measurement-model');
var logger = require('./app/logger');
var moment = require('moment');
var pubsub = require('./app/pubsub');
var shouldReportError = true;
var ConextModelConverter = function(conextModel, createdAt) {
let model = {};
model.id = uuid.v4();
model.inverter_id = conextModel.inverterId;
model.created_at = createdAt;
model.dc1_voltage = conextModel.dc[0].voltage;
model.dc1_current = conextModel.dc[0].current;
model.dc1_power = conextModel.dc[0].power;
model.dc1_energy = conextModel.dc[0].energy;
model.dc2_voltage = conextModel.dc[1].voltage;
model.dc2_current = conextModel.dc[1].current;
model.dc2_power = conextModel.dc[1].power;
model.dc2_energy = conextModel.dc[1].energy;
model.ac_voltage = conextModel.ac.voltage;
model.ac_current = conextModel.ac.current;
model.ac_power = conextModel.ac.power;
model.ac_energy = conextModel.ac.energy;
model.ac_freq = conextModel.ac.freq;
model.duration = conextModel.ac.duration;
model.total_energy = conextModel.ac.totalEnergy;
model.total_duration = conextModel.ac.totalDuration;
model.state = conextModel.ac.state;
return model;
};
var lastMeasurementIsMeaningful = {};
require('./app/telegram-bot');
function isUpdateNeeded(date, data) {
let isLastMeaningful = lastMeasurementIsMeaningful[date];
let isCurrentDataMeaningful = isDataMeaningful(data, date);
let isRecordPresent = date in lastMeasurementIsMeaningful;
if (isLastMeaningful && !isCurrentDataMeaningful) {
pubsub.emit('lastMeasurement', data);
}
if (!isLastMeaningful && isCurrentDataMeaningful) {
pubsub.emit('firstMeasurement', data);
}
lastMeasurementIsMeaningful[date] = isCurrentDataMeaningful;
if (isCurrentDataMeaningful || !isRecordPresent || isLastMeaningful) {
return true;
}
return false;
}
function isDataMeaningful(data, date) {
if (data.some((inverterData) => {
return inverterData.ac.power > 0 || inverterData.ac.online > 0;
})) {
lastMeasurementIsMeaningful[date] = data;
return true;
}
return false;
}
module.exports = function() {
var date = moment().format('YYYY-MM-DD');
deviceManager.readAll()
.then((data) => {
let createdAt;
logger.log('got data', data);
shouldReportError = true;
if (isUpdateNeeded(date, data)) {
createdAt = data[0].createdAt;
data.forEach((res) => {
measurement.create(ConextModelConverter(res, createdAt))
.then(() => {
logger.log('logged ok');
pubsub.emit('measurementRecorded');
});
});
} else {
logger.log('no logging needed');
}
})
.catch((err) => {
if (shouldReportError) {
pubsub.emit('readError', err);
}
logger.warn('Failed to read data from inverter', err);
shouldReportError = false;
});
};
if (require.main === module) {
// called from console
module.exports();
}