forked from empierre/domoticz_linky-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domoticz_linky.js
177 lines (162 loc) · 7.87 KB
/
domoticz_linky.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//##############################################################################
// This file is part of domoticz_linky - https://github.com/empierre/domotics_linky
// Copyright (C) 2014-2019 Emmanuel PIERRE ([email protected])
//
// domoticz_linky 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 2 of the License, or
// (at your option) any later version.
//
// MyDomoAtHome 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 MyDomoAtHome. If not, see <http://www.gnu.org/licenses/>.
//##############################################################################
var fs = require('fs');
var winston = require('winston');
global.logger = winston;
const path = require('path');
var devicerowid=process.argv[2];
var dateObj = new Date();
var q_year=dateObj.getUTCFullYear();
var q_month_s=dateObj.getUTCMonth();
var q_month_e=dateObj.getUTCMonth() + 1;
var q_day_s=dateObj.getUTCDate()-1;
var q_day_e=dateObj.getUTCDate();
var BASE_DIR = process.env.BASE_DIR || '/home/pi/domoticz/domoticz_linky';
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
function getTotal() {
try {
var fileExport = 'export_years_values.json';
var filePath = path.resolve(BASE_DIR, fileExport);
var obj = JSON.parse(fs.readFileSync(filePath, 'utf8'));
var conso_cumul=0.0;
for (var i = 0; i < Object.keys(obj).length; ++i) {
conso_cumul= conso_cumul+ (obj[i]["conso"]);
}
return(conso_cumul);
} catch (e) {
// It isn't accessible
console.log("Exception opening export_years_values.json : "+e);
}
}
function getYear(year) {
try {
var fileExport = 'export_years_values.json';
var filePath = path.resolve(BASE_DIR, fileExport);
var obj = JSON.parse(fs.readFileSync(filePath, 'utf8'));
for (var i = 0; i < Object.keys(obj).length; ++i) {
if (year == obj[i]["time"]) return (obj[i]["conso"]);
}
} catch (e) {
// It isn't accessible
console.log("Exception opening export_years_values.json : "+e);
}
}
function getMonth(month) {
var mth=[ 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
try {
var fileExport = 'export_months_values.json';
var filePath = path.resolve(BASE_DIR, fileExport);
var obj = JSON.parse(fs.readFileSync(filePath, 'utf8'));
for (var i = 0; i < Object.keys(obj).length; ++i) {
if (mth[month-1] == obj[i]["time"]) return (obj[i]["conso"]);
}
} catch (e) {
// It isn't accessible
console.log("Exception opening export_months_values.json : "+e);
}
}
function getDay(day,month) {
var mth=[ 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
try {
var fileExport = 'export_days_values.json';
var filePath = path.resolve(BASE_DIR, fileExport);
var obj = JSON.parse(fs.readFileSync(filePath, 'utf8'));
var dm=''+pad(day,2)+" "+mth[month-1];
for (var i = 0; i < Object.keys(obj).length; ++i) {
if (dm == obj[i]["time"]) return (obj[i]["conso"]);
}
} catch (e) {
// It isn't accessible
console.log("Exception opening export_days_values.json : "+e);
}
}
function generateDayHours() {
var cumul=getCumulBefore(q_year,q_month_s);
var mth=[ 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
try {
var fileExport = 'export_hours_values.json';
var filePath = path.resolve(BASE_DIR, fileExport);
var obj = JSON.parse(fs.readFileSync(filePath, 'utf8'));
for (var i = 0; i < Object.keys(obj).length; ++i) {
var req_date=''+q_year+'-'+pad(q_month_e,2)+'-'+pad(q_day_s,2)+' '+pad(obj[i]["time"].substr(0, 5),5)+':00';
if (obj[i]["conso"]>0) {
console.log('DELETE FROM \'Meter\' WHERE devicerowid='+devicerowid+' and date = \''+req_date+'\'; INSERT INTO \'Meter\' (DeviceRowID,Usage,Value,Date) VALUES ('+devicerowid+', \''+Math.round(obj[i]["conso"]*10000)+'\', \''+Math.round(cumul*1000)+'\', \''+req_date+'\');') ;
cumul=cumul+(obj[i]["conso"]);
}
}
} catch (e) {
// It isn't accessible
console.log("Exception opening export_hours_values.json : "+e);
}
}
function getCumulBefore(year,month) {
// Bring back the year-month previous total as domoticz expect it
try {
var fileExport = 'export_years_values.json';
var filePath = path.resolve(BASE_DIR, fileExport);
var obj = JSON.parse(fs.readFileSync(filePath, 'utf8'));
var conso_cumul=0.0;
for (var i = 0; i < Object.keys(obj).length; ++i) {
if (obj[i]["time"]<year) {
conso_cumul= conso_cumul+ (obj[i]["conso"]);
}
}
} catch (e) {
// It isn't accessible
console.log("Exception opening export_years_values.json : "+e);
}
var mth=[ 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
try {
var fileExport = 'export_months_values.json';
var filePath = path.resolve(BASE_DIR, fileExport);
var obj = JSON.parse(fs.readFileSync(filePath, 'utf8'));
for (var i = 0; i < month-1; ++i) {
conso_cumul= conso_cumul+ (obj[i]["conso"]);
}
return(conso_cumul);
} catch (e) {
// It isn't accessible
console.log("Exception opening export_months_values.json : "+e);
}
}
function generateMonthDays() {
var cumul=getCumulBefore(q_year,q_month_s);
var mth=[ 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
try {
var fileExport = 'export_days_values.json';
var filePath = path.resolve(BASE_DIR, fileExport);
var obj = JSON.parse(fs.readFileSync(filePath, 'utf8'));
for (var i = 0; i < Object.keys(obj).length; ++i) {
var req_date=''+q_year+'-'+pad((mth.indexOf(obj[i]["time"].substr(3, 3))+1),2)+'-'+pad(obj[i]["time"].substr(0, 2),2);
if (obj[i]["conso"]>0) {
console.log('DELETE FROM \'Meter_Calendar\' WHERE devicerowid='+devicerowid+' and date = \''+req_date+'\'; INSERT INTO \'Meter_Calendar\' (DeviceRowID,Value,Counter,Date) VALUES ('+devicerowid+', \''+Number((obj[i]["conso"]*1000).toFixed(2))+'\', \''+Math.round(cumul*1000)/1000+'\', \''+req_date+'\');') ;
cumul=cumul+(obj[i]["conso"]);
}
}
} catch (e) {
// It isn't accessible
console.log("Exception opening export_months_values.json : "+e);
}
}
logger.add(winston.transports.File, {filename: './lnk95.log'});
generateMonthDays();
generateDayHours();