Skip to content

Commit

Permalink
Node cmd refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
chameleonbr committed Aug 30, 2019
1 parent 70f6ccb commit 90aad61
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 31 deletions.
Binary file modified icons/redis.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 1 addition & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 32 additions & 9 deletions redis.html
Expand Up @@ -5,22 +5,22 @@
category: 'config',
defaults: {
name: {
value: "",
value: "Local",
required: true
},
options: {
value: "localhost",
value: "{}",
required: true,
validate: RED.validators.typedInput("optionsType"),

},
cluster: {
value: false
},
optionsType: { value: "str" }
optionsType: { value: "json" }
},
label: function () {
return this.name || "Host";
return this.name || "Redis host";
},
oneditprepare: function () {
$("#node-config-input-options").typedInput({
Expand Down Expand Up @@ -53,6 +53,7 @@
/*global RED*/
RED.nodes.registerType('redis-in', {
category: 'Redis',
inputs: 0,
outputs: 1,
color: "#ffffff",
icon: "redis.png",
Expand All @@ -69,15 +70,16 @@
value: ""
},
topic: {
value: ""
value: "",
required: true
},
timeout: {
value: 0,
validate: RED.validators.number()
}
},
label: function () {
return this.name || "Redis IN";
return this.name || this.topic || "redis in";
}
});
</script>
Expand Down Expand Up @@ -144,7 +146,7 @@
}
},
label: function () {
return this.name || "Redis OUT";
return this.name || this.topic || "redis out";
}
});
</script>
Expand Down Expand Up @@ -204,12 +206,28 @@
topic: {
value: ""
},
params: {
value: "{}",
validate: RED.validators.typedInput("paramsType"),
required: false
},
paramsType: { value: "json" },
payloadType: {
value: "json"
},
block: {
value: false
}
},
label: function () {
return this.name || "Redis " + this.command;
return this.name || "redis " + this.command + " " + (this.topic || "");
},
oneditprepare: function () {
$("#node-input-params").typedInput({
default: 'json',
typeField: $("#node-input-paramsType"),
types: ['json']
});
}
});
</script>
Expand All @@ -224,7 +242,7 @@
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Topic</label>
<label for="node-input-topic"><i class="icon-tasks"></i> Topic/Key</label>
<input type="text" id="node-input-topic" placeholder="Topic">
</div>
<div class="form-row">
Expand Down Expand Up @@ -402,6 +420,11 @@
<option value="zunionstore">ZUNIONSTORE</option>
</select>
</div>
<div class="form-row">
<label for="node-input-params"><i class="fa fa-server"></i> Params</label>
<input type="text" id="node-input-params">
<input type="hidden" id="node-input-paramsType">
</div>
<div class="form-row">
<label for="node-config-input-block">&nbsp;</label>
<input type="checkbox" id="node-config-input-block" style="display:inline-block; width:15px; vertical-align:baseline;"> Block Commands(Force use new connection)
Expand Down
58 changes: 47 additions & 11 deletions redis.js
Expand Up @@ -147,11 +147,15 @@ module.exports = function (RED) {
else {
topic = node.topic;
}
try {
client[node.command](topic, JSON.stringify(msg.payload));
}
catch (err) {
node.error(err);
if (topic === "") {
node.error('Missing topic, please send topic on msg or set Topic on node.', msg)
} else {
try {
client[node.command](topic, JSON.stringify(msg.payload));
}
catch (err) {
node.error(err, msg);
}
}
});

Expand All @@ -165,9 +169,10 @@ module.exports = function (RED) {
this.command = n.command;
this.name = n.name;
this.topic = n.topic;
this.params = n.params;
var node = this;
this.block = n.block || false;
let id = (this.block)?(n.id):(n.z);
let id = (this.block) ? (n.id) : (n.z);

let client = getConn(this.server, id);

Expand All @@ -179,19 +184,50 @@ module.exports = function (RED) {
});

node.on('input', function (msg) {
if (!Array.isArray(msg.payload)) {
throw Error('Payload is not Array');

let topic = undefined;

if (msg.topic) {
topic = msg.topic
} else if (node.topic && node.topic !== "") {
try {
topic = node.topic
} catch (e) {
topic = undefined
}
}
let payload = undefined;

if (msg.payload) {
payload = msg.payload
} else if (node.params && node.params !== "" && node.params !== "[]" && node.params !== "{}") {
try {
payload = JSON.parse(node.params)
} catch (e) {
payload = undefined
}
}

client[node.command](msg.payload, function (err, res) {
let response = function (err, res) {
if (err) {
node.error(err, msg);
}
else {
msg.payload = res;
node.send(msg);
}
});
};

if (!payload) {
payload = topic
topic = undefined
}

if (topic) {
client[node.command](topic, payload, response);
} else {
client[node.command](payload, response);
}
});

}
Expand All @@ -209,7 +245,7 @@ module.exports = function (RED) {
this.command = 'eval';
var node = this;
this.block = n.block || false;
let id = (this.block)?(n.id):(n.z);
let id = (this.block) ? (n.id) : (n.z);

let client = getConn(this.server, id);

Expand Down

0 comments on commit 90aad61

Please sign in to comment.