-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgcapture.js
54 lines (45 loc) · 1.72 KB
/
imgcapture.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
//imgcapture.js
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 checksum = require('checksum');
var writeToDB = function(connection, dbtablename, timestamp, buffer) {
if(buffer.length < 10000){
console.log("Buffer length < 10,000 bytes - image corrupt, discarding.");
return;
}
connection.query("SELECT imagedatasize as imagedata_size, timestamp as timestamp FROM " + dbtablename + " WHERE timestamp=(select MAX(timestamp) from " + dbtablename + " )", function(er, results, fields){
console.log("Comparing this image size " + buffer.length + " to size " + results[0].imagedata_size + " from timestamp " + results[0].timestamp);
if(results != null && results[0].imagedata_size == buffer.length){
console.log("Image has same size as most recent image. Discarding.");
}
else{
console.log("Image has different size as most recent image. Writing image to db.");
var writeQuery = "INSERT INTO " + dbtablename + " SET ?",
values = {
timestamp: timestamp,
imagedatasize: buffer.length,
imagedata: new Buffer(buffer, 'binary')
};
//console.log("Connection = " + connection);
connection.query(writeQuery, values, function (er, da) {
if(er)throw er;
});
}
});
}
exports.captureimage = function (connection, dbtablename, options) {
var request = http.get(options, function(res){
var imagedata = '';
res.setEncoding('binary');
res.on('data', function(chunk){
imagedata += chunk;
})
res.on('end', function(){
writeToDB(connection, dbtablename, moment().valueOf(), imagedata)
})
})
}