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.

43 satır
1.0KB

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