From cb6ea13a04ea2da415ef8ad24afb2eebde87168e Mon Sep 17 00:00:00 2001
From: Neil Gershenfeld <gersh@cba.mit.edu>
Date: Sat, 6 Jan 2018 17:37:28 -0500
Subject: [PATCH] motion detection working?

---
 modules/image/motion detect | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/modules/image/motion detect b/modules/image/motion detect
index 5d9998c..fb161a6 100755
--- a/modules/image/motion detect	
+++ b/modules/image/motion detect	
@@ -27,6 +27,7 @@ var name = 'motion detect'
 var init = function() {
    mod.threshold.value = 0.1
    mod.time.value = 15
+   mod.dpi = 100
    timeout()
    }
 //
@@ -51,14 +52,11 @@ var inputs = {
 var outputs = {
    image:{type:'RGBA',
       event:function(){
+         var ctx = mod.img.getContext("2d")
+         var img = ctx.getImageData(0,0,mod.img.width,mod.img.height)
          mods.output(mod,'image',img)}},
    imageInfo:{type:'object',
-      event:function(){
-         var obj = {}
-         obj.name = mod.name.nodeValue
-         obj.dpi = parseFloat(mod.dpitext.value)
-         obj.width = mod.img.width
-         obj.height = mod.img.height
+      event:function(obj){
          mods.output(mod,'imageInfo',obj)}},
    trigger:{type:'event',
       event:function(){
@@ -154,9 +152,26 @@ function compare_images() {
    var webworker = new Worker(url)
    webworker.addEventListener('message',function(evt) {
       window.URL.revokeObjectURL(url)
+      mod.change.nodeValue = 'relative change: '+evt.data.change.toFixed(3)
+      if (evt.data.change > parseFloat(mod.threshold.value)) {
+         var obj = {}
+         var date = new Date()
+         var year = date.getFullYear()
+         var month = ('0'+(1+parseInt(date.getMonth()))).slice(-2)
+         var day = ('0'+date.getDate()).slice(-2)
+         var hour = ('0'+date.getHours()).slice(-2)
+         var minute = ('0'+date.getMinutes()).slice(-2)
+         var second = ('0'+date.getSeconds()).slice(-2)
+         var name = year+'-'+month+'-'+day+'-'+hour+'-'+minute+'-'+second+'.png'
+         obj.name = name
+         obj.dpi = mod.dpi
+         obj.width = mod.img.width
+         obj.height = mod.img.height
+         outputs.imageInfo.event(obj)
+         outputs.image.event()
+         }
       var h = mod.img.height
       var w = mod.img.width
-      mod.change.nodeValue = 'relative change: '+evt.data.change.toFixed(3)
       if (w > h) {
          var x0 = 0
          var y0 = mod.canvas.height*.5*(1-h/w)
-- 
GitLab