Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

58 lines
1.4KB

  1. module.exports = function(RED) {
  2. function InvertUltimate(config) {
  3. RED.nodes.createNode(this,config);
  4. this.config = config;
  5. var node = this;
  6. var decimal = /^\s*[+-]{0,1}\s*([\d]+(\.[\d]*)*)\s*$/
  7. setNodeStatus( {fill: "grey" ,shape: "dot" ,text: "Waiting"});
  8. this.on('input', function(msg) {
  9. var topic = msg.topic || "";
  10. var payload = msg.payload;
  11. if (topic !== undefined && payload !== undefined) {
  12. setNodeStatus({ fill: "green", shape: "dot", text: "(Send) " + !ToBoolean(payload) });
  13. // 24/01/2020 Clone input message and replace only relevant topics
  14. var msgOUt = RED.util.cloneMessage(msg);
  15. msgOUt.topic = topic;
  16. msgOUt.payload = !ToBoolean(payload);
  17. node.send(msgOUt);
  18. return;
  19. }
  20. });
  21. function ToBoolean( value ) {
  22. var res = false;
  23. if (typeof value === 'boolean') {
  24. res = value;
  25. }
  26. else if( typeof value === 'number' || typeof value === 'string' ) {
  27. // Is it formated as a decimal number?
  28. if( decimal.test( value ) ) {
  29. var v = parseFloat( value );
  30. res = v != 0;
  31. }
  32. else {
  33. res = value.toLowerCase() === "true";
  34. }
  35. }
  36. return res;
  37. };
  38. function setNodeStatus({fill, shape, text})
  39. {
  40. var dDate = new Date();
  41. node.status({fill: fill,shape: shape,text: text + " (" + dDate.getDate() + ", " + dDate.toLocaleTimeString() + ")"})
  42. }
  43. }
  44. RED.nodes.registerType("InvertUltimate",InvertUltimate);
  45. }