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.

Invert.js 996B

5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. module.exports = function(RED) {
  2. function Invert(config) {
  3. RED.nodes.createNode(this,config);
  4. this.config = config;
  5. var node = this;
  6. this.on('input', function(msg) {
  7. var topic = msg.topic;
  8. var payload = msg.payload;
  9. if (topic !== undefined && payload !== undefined) {
  10. node.status( {fill: "green" ,shape: "dot" ,text: !ToBoolean(payload)});
  11. return({ topic: topic, payload: !ToBoolean(payload) });
  12. }
  13. });
  14. node.status( {fill: "grey" ,shape: "dot" ,text: payload});
  15. function ToBoolean( value ) {
  16. var res = false;
  17. if (typeof value === 'boolean') {
  18. res = value;
  19. }
  20. else if( typeof value === 'number' || typeof value === 'string' ) {
  21. // Is it formated as a decimal number?
  22. if( decimal.test( value ) ) {
  23. var v = parseFloat( value );
  24. res = v != 0;
  25. }
  26. else {
  27. res = value.toLowerCase() === "true";
  28. }
  29. }
  30. return res;
  31. };
  32. }
  33. RED.nodes.registerType("Invert",Invert);
  34. }