var msgEntree = msg.payload;
var msg2 = { payload: null };
var finMills = (6 - msg.payload.temps) * 60000;
var tempsMills = msg.payload.temps * 60000;
context.T1 = null;
context.T2 = null;
if (msg.payload == "reset"){
msg.payload = {action : "eteint", pieceId : 0, lampeId : 0};
node.status({});
clearTimeout(context.T1);
clearTimeout(context.T2);
context.T1 = null;
context.T2 = null;
node.send(msg);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
if (msgEntree.debutFin == "0"){
//node.send(msg);
//visuel
node.status({fill:"red",shape:"dot",text:msgEntree.temps + " min ON"});
//passage du message pour allumage direct, on dans le cas d'un demarrage "debut" de periode
msg.payload = msgEntree;
node.send(msg);
// extinction au bout du temps
context.T1 = setTimeout(function(){
// visuel
node.status({fill:"green",shape:"dot",text:(360000 - tempsMills)/60000 + " min OFF"});
//extinction au bout du temps
msgEntree.action = "eteint";
msg.payload = msgEntree;
node.send(msg);
context.T1 = null;
// redemarrage du random au bout d'une heure
context.T2 = setTimeout(function(){
msg.payload = "absent";
node.send([null,msg]);
context.T2 = null;
clearTimeout(context.T1);
clearTimeout(context.T2);
node.status({});
},360000 - tempsMills);
}, tempsMills);
///////////////////////////////////////////////////////////////////////////////////////
} else if (msgEntree.debutFin == "1"){
//visuel
node.status({fill:"blue",shape:"dot",text:finMills / 60000 + " min OFF"});
context.T1 = setTimeout(function(){
//visuel
node.status({fill:"grey",shape:"dot",text:(360000 - finMills)/60000 + " min ON"});
msg.payload = msgEntree;
node.send(msg);
context.T1 = null;
context.T2 = setTimeout(function(){
msg.payload = {action : "eteint", pieceId : 0, lampeId : 0};
msg2.payload = "absent";
node.send([msg,msg2]);
context.T2 = null;
clearTimeout(context.T1);
clearTimeout(context.T2);
node.status({});
},360000 - finMills);
},finMills);
}
////////////////////////////////////////////////////////////////////////////////////////////0
else {node.status({});}