Преглед на файлове

Fixed persistent state deletion upon node update/delete

master
Massimo преди 5 години
родител
ревизия
bc37f40b60
променени са 9 файла, в които са добавени 132 реда и са изтрити 153 реда
  1. +1
    -0
      .gitignore
  2. +4
    -0
      CHANGELOG.md
  3. +0
    -32
      boolean-logic-ultimate/BDebug.html
  4. +0
    -22
      boolean-logic-ultimate/BDebug.js
  5. +2
    -1
      boolean-logic-ultimate/BooleanLogicUltimate.html
  6. +98
    -17
      boolean-logic-ultimate/BooleanLogicUltimate.js
  7. +25
    -5
      boolean-logic-ultimate/Invert.js
  8. +0
    -73
      boolean-logic-ultimate/NodeHelper.js
  9. +2
    -3
      package.json

+ 1
- 0
.gitignore Целия файл

@@ -1,2 +1,3 @@
node_modules
.DS_Store
states

+ 4
- 0
CHANGELOG.md Целия файл

@@ -1,5 +1,9 @@
# node-red-contrib-boolean-logic-ultimate
<p>
<b>Version 0.0.2</b><br/>
- Fixed persistent state deletion upon node update/delete<br/>
</p>
<p>
<b>Version 0.0.1</b><br/>
- Initial release<br/>
</p>

+ 0
- 32
boolean-logic-ultimate/BDebug.html Целия файл

@@ -1,32 +0,0 @@
<script type="text/javascript">
RED.nodes.registerType('BDebug',{
category: 'boolean logic ultimate',
color: '#ff8080',
defaults: {
name: {
value: "Debug"
}
},
inputs:1,
outputs:0,
icon: "debug.png",
label:
function() {
return this.name||"Debug";
},
paletteLabel: function() {
return "Debug";
}
});
</script>

<script type="text/x-red" data-template-name="BDebug">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>

<script type="text/x-red" data-help-name="BDebug">
<p>A debug node specifically targeting boolean logic.</p>
</script>

+ 0
- 22
boolean-logic-ultimate/BDebug.js Целия файл

@@ -1,22 +0,0 @@
module.exports = function(RED) {
function BDebug(config) {
RED.nodes.createNode(this,config);
this.config = config;
var node = this;
var NodeHelper = require('./NodeHelper.js');
var h = new NodeHelper( node );
this.on('input', function(msg) {
var topic = msg.topic;
var payload = msg.payload;
if( topic !== undefined && payload !== undefined ) {
h.DisplayStatus( h.ToBoolean( payload ) );
}
});

h.DisplayUnkownStatus();
}
RED.nodes.registerType("BDebug",BDebug);
}

+ 2
- 1
boolean-logic-ultimate/BooleanLogicUltimate.html Целия файл

@@ -57,7 +57,8 @@
return "Boolean Logic Ultimate";
},
oneditsave: function () {
// Delete persistent state file
$.get( "stateoperation_delete?nodeid=" +this.id, function( data ) {});
}
});
</script>

+ 98
- 17
boolean-logic-ultimate/BooleanLogicUltimate.js Целия файл

@@ -4,20 +4,24 @@ module.exports = function(RED) {
this.config = config;
this.state = {};
var node = this;
var NodeHelper = require('./NodeHelper.js');
var fs = require('fs');
var h = new NodeHelper(node);
var decimal = /^\s*[+-]{0,1}\s*([\d]+(\.[\d]*)*)\s*$/
// Delete persistent states on change/deploy
DeletePersistFile();
// Helper for the config html, to be able to delete the peristent states file
RED.httpAdmin.get("/stateoperation_delete", RED.auth.needsPermission('BooleanLogicUltimate.read'), function (req, res) {
//node.send({ req: req });
DeletePersistFile(req.query.nodeid);
res.json({ status: 220 });
});

// Populate the state array with the persisten file
if (this.config.persist == true) {
if (node.config.persist == true) {
try {
var contents = fs.readFileSync(node.id.toString()).toString();
var contents = fs.readFileSync("states/" + node.id.toString()).toString();
if (typeof contents !== 'undefined') {
node.state = JSON.parse(contents);
node.status({fill: "blue",shape: "ring",text: "Loaded persistent states (" + Object.keys(this.state).length + " total)."});
node.status({fill: "blue",shape: "ring",text: "Loaded persistent states (" + Object.keys(node.state).length + " total)."});
}
} catch (error) {
node.status({fill: "grey",shape: "ring",text: "No persistent states"});
@@ -34,14 +38,15 @@ module.exports = function(RED) {
if (topic !== undefined && payload !== undefined) {
var value = h.ToBoolean( payload );
var value = ToBoolean( payload );
var state = node.state;
state[topic] = value;

// Sabe the state array to a perisistent file
if (this.config.persist == true) {
fs.writeFileSync(this.id.toString(),JSON.stringify(state));
if (!fs.existsSync("states")) fs.mkdirSync("states");
fs.writeFileSync("states/" + node.id.toString(),JSON.stringify(state));
}
// Do we have as many inputs as we expect?
@@ -53,12 +58,12 @@ module.exports = function(RED) {
var resOR = CalculateResult("OR");
var resXOR = CalculateResult("XOR");

if (this.config.filtertrue == "onlytrue") {
if (node.config.filtertrue == "onlytrue") {
if (!resAND) { resAND = null };
if (!resOR) { resOR = null };
if (!resXOR) { resXOR = null };
}
h.SetResult(resAND,resOR,resXOR, node.config.topic);
SetResult(resAND,resOR,resXOR, node.config.topic);
}
else if(keyCount > node.config.inputCount ) {
node.warn(
@@ -67,18 +72,30 @@ module.exports = function(RED) {
+ " [Logic]: More than the specified "
+ node.config.inputCount + " topics received, resetting. Will not output new value until " + node.config.inputCount + " new topics have been received.");
node.state = {};
h.DisplayUnkownStatus();
DisplayUnkownStatus();
}
}
});
});
function DeletePersistFile (){
this.on('close', function(removed, done) {
if (removed) {
// This node has been deleted
// Delete persistent states on change/deploy
DeletePersistFile(node.id);
} else {
// This node is being restarted
}
done();
});

function DeletePersistFile (_nodeid){
// Detele the persist file
var _node = RED.nodes.getNode(_nodeid); // Gets node object from nodeit, because when called from the config html, the node object is not defined
try {
fs.unlinkSync(node.id.toString());
node.status({fill: "red",shape: "ring",text: "Persistent states deleted."});
fs.unlinkSync("states/" + _nodeid.toString());
_node.status({fill: "red",shape: "ring",text: "Persistent states deleted ("+_nodeid.toString()+")."});
} catch (error) {
node.status({fill: "red",shape: "ring",text: "Error deleting persistent file: " + error.toString()});
_node.status({fill: "red",shape: "ring",text: "Error deleting persistent file: " + error.toString()});
}
}

@@ -132,7 +149,71 @@ module.exports = function(RED) {
return res;
}
function ToBoolean( value ) {
var res = false;
if (typeof value === 'boolean') {
res = value;
}
else if( typeof value === 'number' || typeof value === 'string' ) {
// Is it formated as a decimal number?
if( decimal.test( value ) ) {
var v = parseFloat( value );
res = v != 0;
}
else {
res = value.toLowerCase() === "true";
}
}
return res;
};
function DisplayStatus ( value ) {
node.status(
{
fill: value ? "green" : "red",
shape: value ? "dot" : "ring",
text: value ? "true" : "false"
}
);
};

function DisplayUnkownStatus () {
node.status(
{
fill: "gray",
shape: "dot",
text: "Unknown"
});
};

function SetResult( _valueAND, _valueOR, _valueXOR, optionalTopic ) {
DisplayStatus( "AND:" + _valueAND + " OR:" +_valueOR + " XOR:" +_valueXOR);
if (_valueAND!=null){
var msgAND = {
topic: optionalTopic === undefined ? "result" : optionalTopic,
operation:"AND",
payload: _valueAND
};
}
if (_valueOR!=null){
var msgOR = {
topic: optionalTopic === undefined ? "result" : optionalTopic,
operation:"OR",
payload: _valueOR
};
}
if (_valueXOR!=null){
var msgXOR = {
topic: optionalTopic === undefined ? "result" : optionalTopic,
operation:"XOR",
payload: _valueXOR
};
}
node.send([msgAND,msgOR,msgXOR]);
};
}
RED.nodes.registerType("BooleanLogicUltimate",BooleanLogicUltimate);

+ 25
- 5
boolean-logic-ultimate/Invert.js Целия файл

@@ -3,19 +3,39 @@ module.exports = function(RED) {
RED.nodes.createNode(this,config);
this.config = config;
var node = this;
var NodeHelper = require('./NodeHelper.js');
var h = new NodeHelper( node );

this.on('input', function(msg) {
var topic = msg.topic;
var payload = msg.payload;
if( topic !== undefined && payload !== undefined ) {
h.SetResult( !h.ToBoolean( payload ), topic );
if (topic !== undefined && payload !== undefined) {
node.status( {fill: "green" ,shape: "dot" ,text: !ToBoolean(payload)});
return({ topic: topic, payload: !ToBoolean(payload) });
}
});
h.DisplayUnkownStatus();
node.status( {fill: "grey" ,shape: "dot" ,text: payload});

function ToBoolean( value ) {
var res = false;
if (typeof value === 'boolean') {
res = value;
}
else if( typeof value === 'number' || typeof value === 'string' ) {
// Is it formated as a decimal number?
if( decimal.test( value ) ) {
var v = parseFloat( value );
res = v != 0;
}
else {
res = value.toLowerCase() === "true";
}
}
return res;
};
}

+ 0
- 73
boolean-logic-ultimate/NodeHelper.js Целия файл

@@ -1,73 +0,0 @@
var NodeHelper = function( node ) {
var myNode = node;
var self = this;
var decimal = /^\s*[+-]{0,1}\s*([\d]+(\.[\d]*)*)\s*$/
this.ToBoolean = function( value ) {
var res = false;

if (typeof value === 'boolean') {
res = value;
}
else if( typeof value === 'number' || typeof value === 'string' ) {
// Is it formated as a decimal number?
if( decimal.test( value ) ) {
var v = parseFloat( value );
res = v != 0;
}
else {
res = value.toLowerCase() === "true";
}
}
return res;
};

this.DisplayStatus = function( value ) {
myNode.status(
{
fill: value ? "green" : "red",
shape: value ? "dot" : "ring",
text: value ? "true" : "false"
}
);
};
this.DisplayUnkownStatus = function() {
myNode.status(
{
fill: "gray",
shape: "dot",
text: "Unknown"
});
};
this.SetResult = function( _valueAND, _valueOR, _valueXOR, optionalTopic ) {
self.DisplayStatus( "AND:" + _valueAND + " OR:" +_valueOR + " XOR:" +_valueXOR);
if (_valueAND!=null){
var msgAND = {
topic: optionalTopic === undefined ? "result" : optionalTopic,
operation:"AND",
payload: _valueAND
};
}
if (_valueOR!=null){
var msgOR = {
topic: optionalTopic === undefined ? "result" : optionalTopic,
operation:"OR",
payload: _valueOR
};
}
if (_valueXOR!=null){
var msgXOR = {
topic: optionalTopic === undefined ? "result" : optionalTopic,
operation:"XOR",
payload: _valueXOR
};
}
myNode.send([msgAND,msgOR,msgXOR]);
};
};
module.exports = NodeHelper;

+ 2
- 3
package.json Целия файл

@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-boolean-logic-ultimate",
"version": "0.0.1",
"version": "0.0.2",
"description": "A set of Node-RED enhanced boolean logic, with persisten values after reboot and more",
"author": "Supergiovane (https://github.com/Supergiovane)",
"dependencies": {
@@ -19,8 +19,7 @@
"node-red": {
"nodes": {
"BooleanLogicUltimate": "boolean-logic-ultimate/BooleanLogicUltimate.js",
"Invert": "boolean-logic-ultimate/Invert.js",
"BDebug": "boolean-logic-ultimate/BDebug.js"
"Invert": "boolean-logic-ultimate/Invert.js"
}
}
}

Loading…
Отказ
Запис