Commit 60de9f9c authored by Neil Gershenfeld's avatar Neil Gershenfeld

free-run with event trigger

parent 4ebd8553
......@@ -2,7 +2,7 @@
// cams.js
// shopcam activity monitor
// Neil Gershenfeld
// 10/5/17
// 10/8/17
//
// before running: ./clean; node make_index.js
// to run: node cams.js
......@@ -20,7 +20,8 @@ var image_height = 1080
var thumbnail_size = 100
var compression = 50
var activity_threshold = 250
var milliseconds_between_images = 60000
var millisecond_image_delay = 15000
var days_to_save = 100
//
// requires
//
......@@ -39,48 +40,72 @@ function update() {
// name image by month, day, hour, minute
//
var date = new Date()
var year = date.getFullYear()
var month = ('0'+(1+parseInt(date.getMonth()))).slice(-2)
var day = ('0'+(1+parseInt(date.getDay()))).slice(-2)
var day = ('0'+date.getDate()).slice(-2)
var hour = ('0'+date.getHours()).slice(-2)
var minute = ('0'+date.getMinutes()).slice(-2)
var name = month+'-'+day+'-'+hour+'-'+minute+'.jpg'
//
// remove prior image if it exists
//
execSync('rm -f images/'+name)
var second = ('0'+date.getSeconds()).slice(-2)
var name = year+'-'+month+'-'+day+'-'+hour+'-'+minute+'-'+second+'.jpg'
//
// take new image
//
console.log('\ncapture '+name)
execSync('fswebcam --jpeg '+compression+' --resolution '+image_width+'x'+image_height+' --save image.jpg')
//
// if size has changed since last image beyond threshold, save it
// remove prior images
//
date.setDate(date.getDate()-days_to_save)
var oldyear = date.getFullYear()
var oldmonth = ('0'+(1+parseInt(date.getMonth()))).slice(-2)
var oldday = ('0'+date.getDate()).slice(-2)
var oldhour = ('0'+date.getHours()).slice(-2)
var oldminute = ('0'+date.getMinutes()).slice(-2)
var prefix = oldyear+'-'+oldmonth+'-'+oldday+'-'+oldhour+'-'+oldminute+'*'
console.log('remove '+prefix)
execSync('rm -f images/'+prefix+'*')
execSync('rm -f thumbnails/'+prefix+'*')
//
// check if image size has changed beyond threshold
//
var stats = fs.statSync('image.jpg')
var newsize = stats.size
console.log('old image size: '+oldsize+' new image size: '+newsize)
if (Math.abs(newsize-oldsize) > activity_threshold) {
console.log('activity detected, keep image')
//
// size has changed, keep image
//
console.log('activity detected, save image')
execSync('convert image.jpg -resize '+thumbnail_size+'x'+thumbnail_size+' thumbnails/'+name)
execSync('mv image.jpg images/'+name)
//
// rebuild day's index
//
console.log('reindex day')
execSync("echo '<html><body>' > index/newindex.html")
execSync('ls images | awk \'/^'+year+'-'+month+'-'+day+'/ { print "<a href=../images/"$1 "><img src=../thumbnails/" $1 "></a>" }\' >> index/newindex.html')
execSync("mv index/newindex.html index/"+month+"-"+day+".html")
//
// delay before next image
//
console.log('delay before next image')
oldsize = newsize
setTimeout(update,millisecond_image_delay)
}
else {
//
// size hasn't changed
//
console.log('no activity detected, don\'t save image')
//
// no delay before next image
//
oldsize = newsize
setTimeout(update,0)
}
else
console.log('no activity detected, delete image')
//
// rebuild day's index
//
console.log('index')
execSync("echo '<html><body>' > index/newindex.html")
execSync('ls images | awk \'/^'+month+'-'+day+'/ { print "<a href=../images/"$1 "><img src=../thumbnails/" $1 "></a>" }\' >> index/newindex.html')
execSync("mv index/newindex.html index/"+month+"-"+day+".html")
//
// wait for next image
//
console.log('wait for next image')
oldsize = newsize
}
//
// set update interval
//
update()
setInterval(update,milliseconds_between_images)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment