From f25a02d40625b5d44433af126480ddcd9bf25581 Mon Sep 17 00:00:00 2001 From: iotcat Date: Mon, 29 Nov 2021 20:31:52 +0800 Subject: [PATCH] new_hall --- index.js | 18 +++++++++--------- lightCtl/core.js | 7 +++++++ lightCtl/isolat.js | 43 ++++++++++++++++++++++++++++++++++++++++++- mqtt/light.js | 15 +++++++++++++++ room/din.js | 9 ++++++--- room/hall.js | 8 +++++--- room/kit.js | 11 +++++++---- room/liv.js | 23 +++++++++++++---------- 8 files changed, 104 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index fc902bc..75adb20 100644 --- a/index.js +++ b/index.js @@ -105,24 +105,24 @@ var room = { /* lightCtl reg */ var lightCtl = { - hall: lightCtlObj(room.hall, light.hall), - din: lightCtlObj(room.din, light.din), - liv: lightCtlObj(room.liv, light.liv), - kit: lightCtlObj(room.kit, light.kit) + //hall: lightCtlObj(room.hall, light.hall), + //din: lightCtlObj(room.din, light.din), + //liv: lightCtlObj(room.liv, light.liv), + //kit: lightCtlObj(room.kit, light.kit) } /* peo mqtt */ var peo_mqtt = { - hall: peo(client, room.hall), - din: peo(client, room.din), - liv: peo(client, room.liv), - kit: peo(client, room.kit) + //hall: peo(client, room.hall), + //din: peo(client, room.din), + //liv: peo(client, room.liv), + //kit: peo(client, room.kit) } /* 走廊入口 */ var isolat = { - hall: isolatObj(room.hall, light.hall, [p.hall[0], p.hall[1]], lightCtl.hall), + hall: isolatObj(room.hall, light.hall, [p.hall[0], p.hall[1]], lightCtl.hall) //door: isolatObj(room.din, light.din, [p.din[0], p.livb[0]], lightCtl.din), } diff --git a/lightCtl/core.js b/lightCtl/core.js index 30ae44f..cda21fe 100644 --- a/lightCtl/core.js +++ b/lightCtl/core.js @@ -5,6 +5,13 @@ var core = (room, light) => { LastOffTime: 0, }; + setInterval(()=>{ + if(room.state != light.state){ + room.state = light.state; + } + room.LastSwiTime = light.LastSwiTime; + }, 50); + room.on('peopleIn', ()=>{ if(room.num == 1){ light.on(); diff --git a/lightCtl/isolat.js b/lightCtl/isolat.js index e1409e5..97b85cf 100644 --- a/lightCtl/isolat.js +++ b/lightCtl/isolat.js @@ -1,6 +1,46 @@ var isolat = (room, light, pir, lightCtl) => { - + + + const endDur = 120 * 1000; + + let pirStates = [false, false]; + let endT = 0; + + let getT = ()=>new Date().valueOf(); + + let LightSetter = function(ind, val){ + pirStates[ind] = val; +console.log(pirStates); + if(pirStates.every((t)=>t)) { + light.on(); + endT = getT(); + return; + } + + if(endDur + endT < getT()){ + light.off(); + return; + } + } + + + setInterval(()=>{ + if(endDur + endT < getT()){ + light.off(); + } + }, 1000); + + pir.forEach((item, ind)=>{ + item.on('peopleIn', ()=>{ + LightSetter(ind, true); + }); + item.on('peopleOut', ()=>{ + LightSetter(ind, false); + }); + }); + /* reg */ +/* pir.forEach((item)=>{ item.on('peopleIn', ()=>{ if(!room.num){ @@ -18,6 +58,7 @@ var isolat = (room, light, pir, lightCtl) => { } }); }); +*/ } exports.isolat = isolat; diff --git a/mqtt/light.js b/mqtt/light.js index 2a9f3a5..3031a17 100644 --- a/mqtt/light.js +++ b/mqtt/light.js @@ -4,6 +4,7 @@ var light = (client, clientId) => { state: false, LastOnTime: new Date().valueOf(), LastOffTime: new Date().valueOf(), + LastSwiTime: 0, on: ()=>{ client.publish('hass/autoLight/'+clientId, '1'); }, @@ -19,8 +20,22 @@ var light = (client, clientId) => { if(msg == 1) o.state = true; if(msg == 0) o.state = false; } + if(subject == 'hass/snsr/'+clientId+'/light'){ + if(msg == 1) o.state = true; + if(msg == 0) o.state = false; + } + if(subject == 'hass/snsr/'+clientId+'/swi'){ + o.LastSwiTime = new Date().valueOf(); + } + if(subject == 'hass/ctl/'+clientId+'/light'){ + o.LastSwiTime = new Date().valueOf(); + if(msg == 1) o.state = true; + if(msg == 0) o.state = false; + } }); + + return o; } diff --git a/room/din.js b/room/din.js index f847070..a15c983 100644 --- a/room/din.js +++ b/room/din.js @@ -5,6 +5,8 @@ var din = (zone, interface, p) => { id: 'din', num: 0, numAct: 0, + state: false, + LastSwiTime: 0, MaxNum: 5, LastActTime: new Date().valueOf(), MaxSensitTime: 1000 * 60 * 2, @@ -91,10 +93,11 @@ var din = (zone, interface, p) => { if( new Date().getHours() >= 18 && new Date().getHours() <= 20 ) o.isNight = true; else o.isNight = false; - if((o.numAct && o.act.rate > .2) || o.act.rate > .4){ + if( o.act.rate > .4){ if(!o.num) pIn(); - }else{ - if(o.num) pOut(); + } + if(o.act.rate < .02){ +// if((o.num || o.state) && o.LastSwiTime < new Date().valueOf() - 1000*60*20) pOut(); } o.extTime = ((o.num == 1) && o.isNight)*1000*60*1; diff --git a/room/hall.js b/room/hall.js index 3f0794d..5c540a7 100644 --- a/room/hall.js +++ b/room/hall.js @@ -5,7 +5,9 @@ var hall = (zone, interface) => { num: 0, MaxNum: 2, LastActTime: new Date().valueOf(), - MaxSensitTime: 1000 * 60 * 1, + state: false, + LastSwiTime: 0, + MaxSensitTime: 1000 * 60 * 3, func: { peopleIn: [], peopleOut: [], @@ -61,11 +63,11 @@ var hall = (zone, interface) => { }) interface.h.on('backward', ()=>{ - pOut(); + //pOut(); }) interface.hd.on('forward', ()=>{ - pOut(); + //pOut(); }); /* 超时衰减 */ diff --git a/room/kit.js b/room/kit.js index ef7a946..99479ef 100644 --- a/room/kit.js +++ b/room/kit.js @@ -7,6 +7,8 @@ var kit = (zone, interface) => { num: 0, numAct: 0, MaxNum: 3, + state: false, + LastSwiTime: 0, LastActTime: new Date().valueOf(), MaxSensitTime: 1000 * 60 * 2, func: { @@ -70,10 +72,11 @@ var kit = (zone, interface) => { /* 超时衰减 */ setInterval(()=>{ - if((o.numAct && o.act.rate > .2) || o.act.rate > .3){ - if(!o.num) pIn(); - }else{ - if(o.num) pOut(); + if(o.act.rate > .6){ + //if(!o.num) pIn(); + } + if(o.act.rate < .02){ + //if((o.num || o.state) && o.LastSwiTime < new Date().valueOf() - 1000*60*15) pOut(); } /*if(o.num && o.LastActTime + o.MaxSensitTime * o.act.rate < new Date().valueOf() diff --git a/room/liv.js b/room/liv.js index 1d2e1ce..8a43f2c 100644 --- a/room/liv.js +++ b/room/liv.js @@ -7,6 +7,8 @@ var liv = (zone, interface) => { num: 0, numAct: 0, MaxNum: 5, + state: false, + LastSwiTime: 0, LastActTime: new Date().valueOf(), MaxSensitTime: 1000 * 60 * 2, extTime: 0, @@ -25,7 +27,7 @@ var liv = (zone, interface) => { }, act: { rate: 0, - maxDetectTime: 1000 * 60 * 30, + maxDetectTime: 1000 * 60 * 25, actTimeArray: [] } }; @@ -76,27 +78,28 @@ var liv = (zone, interface) => { if( new Date().getHours() >= 19 && new Date().getHours() <= 21 ) o.isNight = true; else o.isNight = false; - +/* let r = 0.3; if(new Date().getHours() == 19) { r = 0.1; - o.act.maxDetectTime = 1000 * 60 * 15; + o.act.maxDetectTime = 1000 * 60 * 20; }else if(new Date().getHours() == 20) { r = 0.05; o.act.maxDetectTime = 1000 * 60 * 30; }else if(new Date().getHours() == 21) { r = 0.05; - o.act.maxDetectTime = 1000 * 60 * 15; + o.act.maxDetectTime = 1000 * 60 * 20; }else{ r = 0.3; - o.act.maxDetectTime = 1000 * 60 * 10; + o.act.maxDetectTime = 1000 * 60 * 20; } - - if(o.numAct || o.act.rate > r){ - if(!o.num) pIn(); - }else{ - if(o.num) pOut(); + */ + if(o.act.rate > .35){ + //if(!o.num) pIn(); + } + if(o.act.rate < .001){ + //if((o.num || o.state) && o.LastSwiTime < new Date().valueOf() - 1000*60*40) pOut(); }