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.

FilterUltimate.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. module.exports = function(RED) {
  2. function FilterUltimate(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 sTopic = node.config.name;
  9. if (msg.hasOwnProperty("topic")){
  10. sTopic = (msg.topic === "" ? sTopic : msg.topic);
  11. }
  12. if (typeof msg.payload !== "undefined") {
  13. var bRes = ToBoolean(msg.payload);
  14. if (typeof bRes === "undefined") return;
  15. var msgTrue = {
  16. topic: sTopic,
  17. payload: true
  18. };
  19. var msgFalse = {
  20. topic: sTopic,
  21. payload: false
  22. };
  23. if (bRes === true) {
  24. node.status( {fill: "green" ,shape: "dot" ,text: "(Send) true,null"});
  25. node.send([msgTrue, null]);
  26. } else
  27. {
  28. node.status( {fill: "green" ,shape: "dot" ,text: "(Send) null,false"});
  29. node.send([null, msgFalse]);
  30. }
  31. return;
  32. }
  33. });
  34. function ToBoolean( value ) {
  35. var res = false;
  36. if (typeof value === 'boolean') {
  37. res = value;
  38. }
  39. else if( typeof value === 'number' || typeof value === 'string' ) {
  40. // Is it formated as a decimal number?
  41. if( decimal.test( value ) ) {
  42. var v = parseFloat( value );
  43. res = v != 0;
  44. }
  45. else {
  46. res = value.toLowerCase() === "true";
  47. }
  48. }
  49. return res;
  50. };
  51. }
  52. RED.nodes.registerType("FilterUltimate",FilterUltimate);
  53. }