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.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. module.exports = function(RED) {
  2. function FilterUltimate(config) {
  3. RED.nodes.createNode(this,config);
  4. this.config = config;
  5. var node = this;
  6. setNodeStatus( {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. // 24/01/2020 Clone input message and replace only relevant topics
  16. var msgTrue = RED.util.cloneMessage(msg);
  17. msgTrue.topic = sTopic;
  18. msgTrue.payload = true;
  19. var msgFalse = RED.util.cloneMessage(msg);
  20. msgFalse.topic = sTopic;
  21. msgFalse.payload = false;
  22. if (bRes === true) {
  23. setNodeStatus({ fill: "green", shape: "dot", text: "(Send) true,null" });
  24. node.send([msgTrue, null]);
  25. } else
  26. {
  27. setNodeStatus( {fill: "green" ,shape: "dot" ,text: "(Send) null,false"});
  28. node.send([null, msgFalse]);
  29. }
  30. return;
  31. }
  32. });
  33. function setNodeStatus({fill, shape, text})
  34. {
  35. var dDate = new Date();
  36. node.status({fill: fill,shape: shape,text: text + " (" + dDate.getDate() + ", " + dDate.toLocaleTimeString() + ")"})
  37. }
  38. function ToBoolean( value ) {
  39. var res = false;
  40. if (typeof value === 'boolean') {
  41. res = value;
  42. }
  43. else if( typeof value === 'number' || typeof value === 'string' ) {
  44. // Is it formated as a decimal number?
  45. if( decimal.test( value ) ) {
  46. var v = parseFloat( value );
  47. res = v != 0;
  48. }
  49. else {
  50. res = value.toLowerCase() === "true";
  51. }
  52. }
  53. return res;
  54. };
  55. }
  56. RED.nodes.registerType("FilterUltimate",FilterUltimate);
  57. }