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.

51 satır
1.2KB

  1. module.exports = function(RED) {
  2. function InvertUltimate(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 topic = msg.topic || "";
  10. var payload = msg.payload;
  11. if (topic !== undefined && payload !== undefined) {
  12. setNodeStatus( {fill: "green" ,shape: "dot" ,text: "(Send) " + !ToBoolean(payload)});
  13. node.send({ topic: topic, payload: !ToBoolean(payload) });
  14. return;
  15. }
  16. });
  17. function ToBoolean( value ) {
  18. var res = false;
  19. if (typeof value === 'boolean') {
  20. res = value;
  21. }
  22. else if( typeof value === 'number' || typeof value === 'string' ) {
  23. // Is it formated as a decimal number?
  24. if( decimal.test( value ) ) {
  25. var v = parseFloat( value );
  26. res = v != 0;
  27. }
  28. else {
  29. res = value.toLowerCase() === "true";
  30. }
  31. }
  32. return res;
  33. };
  34. function setNodeStatus({fill, shape, text})
  35. {
  36. node.status({fill: fill,shape: shape,text: text + " (Last " + new Date().toLocaleString() + ")"})
  37. }
  38. }
  39. RED.nodes.registerType("InvertUltimate",InvertUltimate);
  40. }