-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.sqlite.js
198 lines (187 loc) · 6.78 KB
/
db.sqlite.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var fs = require('fs')
,sqlite3 = require('sqlite3').verbose()
,extend = require('extend')
;
/////
// sqlite init
/////
var db = new sqlite3.Database('maverickTemps.db'); //new sqlite3.Database(':memory:');
(function () {
var exists = fs.existsSync('maverickTemps.db');
if (!exists) {
db.serialize(function () {
db.run("CREATE TABLE schemaVersion (version INTEGER)");
db.run("CREATE TABLE batteryStatus (time TEXT, percent INTEGER)");
db.run("CREATE TABLE temps (probe INTEGER, time TEXT, temp INTEGER)");
db.run("CREATE TABLE events (id INTEGER PRIMARY KEY AUTOINCREMENT, startTime TEXT, stopTime TEXT, title TEXT)");
db.run("CREATE TABLE comments (id INTEGER PRIMARY KEY AUTOINCREMENT, eventId INTEGER, time TEXT, comment TEXT)");
var stmt = db.prepare("INSERT INTO schemaVersion VALUES (?)");
stmt.run(1);
stmt.finalize();
});
} else {
// check schema version
db.get('SELECT version FROM schemaVersion', function (err, row) {
if (undefined != err) {
console.log("*** Error checking schema - delete maverickTemps.db and try again?");
console.log(err);
process.exit();
}
if (row.version != 1) {
console.log('bad schema version');
process.exit();
}
});
}
})();
module.exports = {
close: function () {
db.close();
},
getBatteryStatus: function () {
return new Promise(function (resolve) {
db.serialize(function () {
db.each("SELECT time, percent FROM batteryStatus ORDER BY time LIMIT 1", function (err, row) {
resolve({
time: row.time,
percent: row.percent
});
});
});
});
},
setBatteryStatus: function (batteryPercent) {
db.serialize(function () {
var ts = new Date().toISOString();
var stmt = db.prepare("INSERT INTO batteryStatus VALUES (?, ?)");
stmt.run(ts, batteryPercent);
stmt.finalize();
});
},
getLastHourOfTemps: function() {
return new Promise(function (resolve) {
var temps = [];
db.serialize(function () {
var lastHour = new Date();
lastHour.setHours(lastHour.getHours() - 1);
db.each("SELECT probe, time, temp FROM temps WHERE datetime(time) > datetime(?) ORDER BY time", lastHour.toISOString(), function (err, row) {
temps.push({
probe: row.probe,
time: row.time,
temp: row.temp
});
}, function () {
// All done fetching records, render response
resolve({
temps: temps
});
});
});
});
},
setProbeTemp: function(probeId, ts, probeTemp) {
db.serialize(function () {
var stmt = db.prepare("INSERT INTO temps VALUES (?, ?, ?)");
stmt.run(probeId, ts, probeTemp);
stmt.finalize();
});
},
getAllEvents: function() {
return new Promise(function (resolve) {
var events = [];
db.serialize(function () {
db.each("SELECT id, title, startTime, stopTime FROM events ORDER BY id", function (err, row) {
events.push({
id: row.id,
title: row.title,
startTime: row.startTime,
stopTime: row.stopTime
});
}, function () {
resolve({
events: events
});
});
});
});
},
isEventExisting: function(id) {
return new Promise(function(resolve) {
db.get('select id from events where id=?', id, function (err, row) {
if (err || !row){
resolve(false);
}
resolve(true);
});
});
},
getEvent: function(id) {
return new Promise(function (resolve) {
db.get('select id, title, startTime, stopTime from events where id=?', id, function (err, row) {
resolve(row);
});
});
},
getEventTemps: function(event) {
return new Promise(function (resolve) {
var tempArr = [];
db.each('select probe, time, temp from temps where time between ? and ?', event.startTime, event.stopTime, function (err, row) {
tempArr.push(row);
}, function () {
resolve(extend(event, {
temps: tempArr
}));
})
});
},
getEventComments: function(event) {
return new Promise(function (resolve) {
var tempArr = [];
db.each('select id, time, comment from comments where eventId=?', event.id, function (err, row) {
tempArr.push(row);
}, function () {
resolve(extend(event, {
comments: tempArr
}));
})
});
},
setEventComment: function(eventId, comment) {
return new Promise(function (resolve) {
db.serialize(function () {
var stmt = db.prepare("INSERT INTO comments (eventId, time, comment) VALUES (?, ?, ?)");
stmt.run(eventId, (new Date()).toISOString(), comment);
stmt.finalize();
db.get('select seq from sqlite_sequence where name="comments"', function (err, row) {
resolve({
id: row.seq
});
});
});
});
},
startEvent: function(name) {
return new Promise(function (resolve) {
db.serialize(function () {
var stmt = db.prepare("INSERT INTO events (startTime, title) VALUES (?, ?)");
stmt.run((new Date()).toISOString(), name);
stmt.finalize();
db.get('select seq from sqlite_sequence where name="events"', function (err, row) {
resolve({
id: row.seq
});
});
});
});
},
stopEvent: function(id) {
return new Promise(function (resolve) {
var stmt = db.prepare("UPDATE events SET stopTime = ? WHERE id = ?");
stmt.run((new Date()).toISOString(), id);
stmt.finalize();
resolve({
success: true
});
});
}
}