You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InvertUltimate.js 1.4KB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. }