-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagedisplayer.js
128 lines (111 loc) · 3.84 KB
/
imagedisplayer.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
var express = require('express');
var fs = require('fs');
var request = require('request');
var http = require('http');
var moment = require('moment');
var path = require('path');
var writingSignalCount = 0;
exports.renderImages = function(connection, dbtablename, req, res) {
console.log('Attempting to grab images from DB...');
countForWritingSignal(connection, dbtablename, function(){
var query = connection.query('SELECT timestamp, imagedata FROM ' + dbtablename);
query
.on('error', function(err) {
// Handle error, an 'end' event will be emitted after this as well
})
.on('fields', function(fields) {
// the field packets for the rows to follow
})
.on('result', function(row) {
// Pausing the connnection is useful if your processing involves I/O
console.log('Got a DB result!');
connection.pause();
writeRowToDiskCache(row, dbtablename, function() {
connection.resume();
});
})
.on('end', function() {
// all rows have been received
waitSignal(function(){
displayImagesFromDiskCache(dbtablename, req, res)
});
});
})
}
var countForWritingSignal = function(connection, dbtablename, callback){
connection.query('SELECT COUNT(*) AS imagesCount FROM ' + dbtablename, function (error, results, fields) {
// error will be an Error if one occurred during the query
// results will contain the results of the query
// fields will contain information about the returned results fields (if any)
writingSignalCount = results[0].imagesCount;
console.log('Initial writing signal count: ' + writingSignalCount);
callback();
});
}
var waitSignal = function(callback){
while(writingSignalCount > 0){
//wut
console.log('Awaiting write complete signal...');
}
callback();
}
var writeRowToDiskCache = function(row, dbtablename, callback) {
console.log('Write signal array size: ' + writingSignalCount);
var timestamp = row.timestamp;
var imagedata = row.imagedata;
var filename = 'public/images/' + dbtablename + '/' + timestamp + '.jpg';
console.log('Found image ' + timestamp + ' in DB. Checking exists...');
fs.exists(filename, function (exists) {
if(!exists){
console.log('Does not exist in cache! Writing...');
fs.writeFileSync(filename, imagedata, 'binary');
}else{
console.log('Already exists in cache, Skipping...');
}
});
//done writing!
writingSignalCount--;
callback();
}
var displayImagesFromDiskCache = function(dbtablename, req, res) {
var arrayitems = [];
var imageFiles = fs.readdirSync('public/images/' + dbtablename);
imageFiles.forEach(function (file) {
console.log('Pushing cache file ' + file);
if(path.extname(file) === '.jpg')
{
var timestampint = parseInt(path.basename(file, path.extname(file)));
var datetime = moment.utc(timestampint);
datetime.utcOffset(-240);
var date = datetime.format('YYYY-MM-DD');
//construct item
var item = {'filename':'/images/' + dbtablename + '/' + file, 'timestamp':timestampint, 'date':datetime.format('lll') + ' AST'};
//check for date in items to push to
//if no items with date, make new container and push to it
var found = false;
arrayitems.forEach(function (arrayitem){
if(arrayitem.date === date){
console.log("Date " + date + " found!");
arrayitem.items.push(item);
found = true;
return;
}
})
if(!found){
console.log("Date " + date + " not found! Adding.");
var arrayitem = {'date':date, 'items':[item]};
arrayitems.push(arrayitem);
console.log("Array now at " + arrayitems.length);
}
}
})
arrayitems.forEach(function(item){
item.items.sort(function(a,b){
return(moment(a.timestamp)-moment(b.timestamp));
});
});
arrayitems.sort(function(a,b){
return(moment(b.date)-moment(a.date));
});
res.render('home', { title: dbtablename + ' Camera Log', items: arrayitems });
}