Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

220 lines
5.9KB

  1. module.exports = function(RED) {
  2. function BooleanLogicUltimate(config) {
  3. RED.nodes.createNode(this,config);
  4. this.config = config;
  5. this.state = {};
  6. var node = this;
  7. var fs = require('fs');
  8. var decimal = /^\s*[+-]{0,1}\s*([\d]+(\.[\d]*)*)\s*$/
  9. // Helper for the config html, to be able to delete the peristent states file
  10. RED.httpAdmin.get("/stateoperation_delete", RED.auth.needsPermission('BooleanLogicUltimate.read'), function (req, res) {
  11. //node.send({ req: req });
  12. DeletePersistFile(req.query.nodeid);
  13. res.json({ status: 220 });
  14. });
  15. // Populate the state array with the persisten file
  16. if (node.config.persist == true) {
  17. try {
  18. var contents = fs.readFileSync("states/" + node.id.toString()).toString();
  19. if (typeof contents !== 'undefined') {
  20. node.state = JSON.parse(contents);
  21. node.status({fill: "blue",shape: "ring",text: "Loaded persistent states (" + Object.keys(node.state).length + " total)."});
  22. }
  23. } catch (error) {
  24. node.status({fill: "grey",shape: "ring",text: "No persistent states"});
  25. }
  26. } else {
  27. node.status({fill: "yellow",shape: "dot",text: "Waiting for input states"});
  28. }
  29. this.on('input', function (msg) {
  30. var topic = msg.topic;
  31. var payload = msg.payload;
  32. if (topic !== undefined && payload !== undefined) {
  33. var value = ToBoolean( payload );
  34. var state = node.state;
  35. state[topic] = value;
  36. // Sabe the state array to a perisistent file
  37. if (this.config.persist == true) {
  38. if (!fs.existsSync("states")) fs.mkdirSync("states");
  39. fs.writeFileSync("states/" + node.id.toString(),JSON.stringify(state));
  40. }
  41. // Do we have as many inputs as we expect?
  42. var keyCount = Object.keys(state).length;
  43. if( keyCount == node.config.inputCount ) {
  44. var resAND = CalculateResult("AND");
  45. var resOR = CalculateResult("OR");
  46. var resXOR = CalculateResult("XOR");
  47. if (node.config.filtertrue == "onlytrue") {
  48. if (!resAND) { resAND = null };
  49. if (!resOR) { resOR = null };
  50. if (!resXOR) { resXOR = null };
  51. }
  52. SetResult(resAND,resOR,resXOR, node.config.topic);
  53. }
  54. else if(keyCount > node.config.inputCount ) {
  55. node.warn(
  56. (node.config.name !== undefined && node.config.name.length > 0
  57. ? node.config.name : "BooleanLogicUltimate")
  58. + " [Logic]: More than the specified "
  59. + node.config.inputCount + " topics received, resetting. Will not output new value until " + node.config.inputCount + " new topics have been received.");
  60. node.state = {};
  61. DisplayUnkownStatus();
  62. }
  63. }
  64. });
  65. this.on('close', function(removed, done) {
  66. if (removed) {
  67. // This node has been deleted
  68. // Delete persistent states on change/deploy
  69. DeletePersistFile(node.id);
  70. } else {
  71. // This node is being restarted
  72. }
  73. done();
  74. });
  75. function DeletePersistFile (_nodeid){
  76. // Detele the persist file
  77. var _node = RED.nodes.getNode(_nodeid); // Gets node object from nodeit, because when called from the config html, the node object is not defined
  78. try {
  79. fs.unlinkSync("states/" + _nodeid.toString());
  80. _node.status({fill: "red",shape: "ring",text: "Persistent states deleted ("+_nodeid.toString()+")."});
  81. } catch (error) {
  82. _node.status({fill: "red",shape: "ring",text: "Error deleting persistent file: " + error.toString()});
  83. }
  84. }
  85. function CalculateResult(_operation) {
  86. var res;
  87. if( _operation == "XOR") {
  88. res = PerformXOR();
  89. }
  90. else {
  91. // We need a starting value to perform AND and OR operations.
  92. var keys = Object.keys(node.state);
  93. res = node.state[keys[0]];
  94. for( var i = 1; i < keys.length; ++i ) {
  95. var key = keys[i];
  96. res = PerformSimpleOperation( _operation, res, node.state[key] );
  97. }
  98. }
  99. return res;
  100. }
  101. function PerformXOR()
  102. {
  103. // XOR = exclusively one input is true. As such, we just count the number of true values and compare to 1.
  104. var trueCount = 0;
  105. for( var key in node.state ) {
  106. if( node.state[key] ) {
  107. trueCount++;
  108. }
  109. }
  110. return trueCount == 1;
  111. }
  112. function PerformSimpleOperation( operation, val1, val2 ) {
  113. var res;
  114. if( operation === "AND" ) {
  115. res = val1 && val2;
  116. }
  117. else if( operation === "OR" ) {
  118. res = val1 || val2;
  119. }
  120. else {
  121. node.error( "Unknown operation: " + operation );
  122. }
  123. return res;
  124. }
  125. function ToBoolean( value ) {
  126. var res = false;
  127. if (typeof value === 'boolean') {
  128. res = value;
  129. }
  130. else if( typeof value === 'number' || typeof value === 'string' ) {
  131. // Is it formated as a decimal number?
  132. if( decimal.test( value ) ) {
  133. var v = parseFloat( value );
  134. res = v != 0;
  135. }
  136. else {
  137. res = value.toLowerCase() === "true";
  138. }
  139. }
  140. return res;
  141. };
  142. function DisplayStatus ( value ) {
  143. node.status(
  144. {
  145. fill: value ? "green" : "red",
  146. shape: value ? "dot" : "ring",
  147. text: value ? "true" : "false"
  148. }
  149. );
  150. };
  151. function DisplayUnkownStatus () {
  152. node.status(
  153. {
  154. fill: "gray",
  155. shape: "dot",
  156. text: "Unknown"
  157. });
  158. };
  159. function SetResult( _valueAND, _valueOR, _valueXOR, optionalTopic ) {
  160. DisplayStatus( "AND:" + _valueAND + " OR:" +_valueOR + " XOR:" +_valueXOR);
  161. if (_valueAND!=null){
  162. var msgAND = {
  163. topic: optionalTopic === undefined ? "result" : optionalTopic,
  164. operation:"AND",
  165. payload: _valueAND
  166. };
  167. }
  168. if (_valueOR!=null){
  169. var msgOR = {
  170. topic: optionalTopic === undefined ? "result" : optionalTopic,
  171. operation:"OR",
  172. payload: _valueOR
  173. };
  174. }
  175. if (_valueXOR!=null){
  176. var msgXOR = {
  177. topic: optionalTopic === undefined ? "result" : optionalTopic,
  178. operation:"XOR",
  179. payload: _valueXOR
  180. };
  181. }
  182. node.send([msgAND,msgOR,msgXOR]);
  183. };
  184. }
  185. RED.nodes.registerType("BooleanLogicUltimate",BooleanLogicUltimate);
  186. }