您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

InvertUltimate.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. var dDate = new Date();
  37. node.status({fill: fill,shape: shape,text: text + " (" + dDate.getDate() + ", " + dDate.toLocaleTimeString() + ")"})
  38. }
  39. }
  40. RED.nodes.registerType("InvertUltimate",InvertUltimate);
  41. }