-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.js
76 lines (60 loc) · 2.28 KB
/
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
var dateFormat = require('dateformat');
var mysql = require('mysql');
var request = require("request");
var con = mysql.createConnection({
host: "",
user: "",
password: "",
database: ""
});
function drawOutput(count, date, con) {
var hours = Math.round(count/3600 *10) / 10;
var dateInsert = date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate();
con.query("INSERT INTO daily_hours (hours, date_time) VALUES ('"+hours+"', '" + dateInsert + "')", function(err, result) {
if(err) throw err;
console.log("On the "+ (date.getMonth() + 1) + "/"+ date.getDate() + "/" + date.getFullYear() + " the heat was on for " + hours + " hours");
});
}
con.connect(function(err) {
if (err) throw err;
var todayDateTime = new Date();
todayDateTime.setDate(todayDateTime.getDate()-1);
var min = todayDateTime.getFullYear() + "-" + (todayDateTime.getMonth()+1) + "-" + todayDateTime.getDate() + " 00:00:00";
var max = todayDateTime.getFullYear() + "-" + (todayDateTime.getMonth()+1) + "-" + todayDateTime.getDate() + " 23:59:59";
var heatCountInSeconds = 0;
var countOn = false;
var heatOnAt = null;
var outputDay = null;
var outputDate = null;
con.query("SELECT * FROM nest_log WHERE date_time BETWEEN '"+min+"' AND '"+max+"' ORDER BY date_time ASC", function (err, result, fields) {
if (err) throw err;
for(var i = 0; i < result.length; i++) {
var currentDate = new Date(result[i].date_time);
var currentDay = currentDate.getDate();
//Heat just switched on
if(result[i].heating_status == "heating" && countOn === false) {
countOn = true;
var dateTime = new Date(result[i].date_time);
heatOnAt = dateTime.getTime()/1000;
//console.log("heat on, now counting");
}
//Heat just switched off
if(result[i].heating_status == "off" && countOn === true) {
if(currentDay != outputDay) {
if(outputDay !== null) {
drawOutput(heatCountInSeconds, outputDate, con);
heatCountInSeconds = 0;
}
outputDay = currentDay;
outputDate = currentDate;
}
countOn = false;
var dateTime = new Date(result[i].date_time);
var heatOffAt = dateTime.getTime()/1000;
heatCountInSeconds += heatOffAt - heatOnAt;
}
}
drawOutput(heatCountInSeconds, currentDate, con);
con.end();
});
});