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

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