Working config node and xiaomi-ht node, added package.json
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# Created by .ignore support plugin (hsz.mobi)
|
||||
|
||||
*.iml
|
||||
@@ -3,12 +3,75 @@
|
||||
category: 'config',
|
||||
defaults: {
|
||||
name: {value: ""},
|
||||
group: {value: "224.0.0.50", required: true},
|
||||
port: {value: 9898, required: true, validate:RED.validators.number()},
|
||||
udpv: {value: "udp4"}
|
||||
deviceList: {value:[{ sid:"a", desc:"b", model:"plug"}]}
|
||||
},
|
||||
label: function () {
|
||||
return this.name || "xiaomi-gateway";
|
||||
},
|
||||
oneditprepare: function() {
|
||||
var node = this;
|
||||
|
||||
console.log("config window openend: " + node.deviceList);
|
||||
|
||||
$("#node-config-input-devices").css('min-height','250px').css('min-width','450px').editableList({
|
||||
addItem: function(container, i, device) {
|
||||
if (!device.hasOwnProperty('model')) {
|
||||
device.model = 'sensor_ht';
|
||||
}
|
||||
var row = $('<div/>').appendTo(container);
|
||||
|
||||
$('<label/>',{for:"node-config-input-sid-"+i, style:"margin-left: 3px; width: 30px;vertical-align:middle"}).text("sid").appendTo(row);
|
||||
var sid = $('<input/>',{id:"node-config-input-sid-"+i,type:"text", style:"width:auto;vertical-align:top"}).appendTo(row);
|
||||
|
||||
$('<label/>',{for:"node-config-input-desc-"+i, style:"margin-left: 7px; width: 35px;vertical-align:middle"}).text("desc.").appendTo(row);
|
||||
var desc = $('<input/>',{id:"node-config-input-desc-"+i, type:"text", style:"width:auto;vertical-align:top"}).appendTo(row);
|
||||
|
||||
var model = $('<select/>',{id:"node-config-input-model-"+i},{style:"width:120px; margin-left: 5px; text-align: center;"}).appendTo(row);
|
||||
model.append($("<option></option>").val("sensor_ht").text("sensor_ht"));
|
||||
model.append($("<option></option>").val("magnet").text("magnet"));
|
||||
model.append($("<option></option>").val("plug").text("plug"));
|
||||
model.append($("<option></option>").val("gateway").text("gateway"));
|
||||
model.append($("<option></option>").val("switch").text("switch"));
|
||||
|
||||
sid.val(device.sid);
|
||||
desc.val(device.desc);
|
||||
model.val(device.model);
|
||||
|
||||
},
|
||||
removeItem: function(opt) {
|
||||
},
|
||||
sortItems: function(rules) {
|
||||
},
|
||||
sortable: true,
|
||||
removable: true
|
||||
});
|
||||
|
||||
console.log("Devices: " + JSON.stringify(node.deviceList));
|
||||
for (var i=0;i<this.deviceList.length;i++) {
|
||||
var device = this.deviceList[i];
|
||||
$("#node-config-input-devices").editableList('addItem', device);
|
||||
}
|
||||
|
||||
},
|
||||
oneditsave: function() {
|
||||
var devices = $("#node-config-input-devices").editableList('items');
|
||||
var node = this;
|
||||
var devicesArray = [];
|
||||
devices.each(function(i) {
|
||||
var deviceElement = $(this);
|
||||
var sid = deviceElement.find("#node-config-input-sid-"+i).val();
|
||||
var desc = deviceElement.find("#node-config-input-desc-"+i).val();
|
||||
var model = deviceElement.find("#node-config-input-model-"+i).val();
|
||||
console.log(desc, model, sid);
|
||||
var d = {};
|
||||
d['sid']=sid;
|
||||
d['desc']=desc;
|
||||
d['model']=model;
|
||||
console.log(d);
|
||||
devicesArray.push(d);
|
||||
});
|
||||
node.deviceList = devicesArray;
|
||||
console.log("devices-list: " + JSON.stringify(node.deviceList));
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@@ -18,19 +81,8 @@
|
||||
<label for="node-config-input-name"><i class="icon-tag"></i> Name</label>
|
||||
<input type="text" id="node-config-input-name" placeholder="Name">
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-group"><i class="fa fa-list"></i> Group</label>
|
||||
<input type="text" id="node-config-input-group">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-port"><i class="fa fa-sign-in"></i> on Port</label>
|
||||
<input type="text" id="node-config-input-port" style="width:80px">
|
||||
<span for="node-config-input-udpv"> using</span>
|
||||
<select id="node-config-input-udpv" style="width:80px">
|
||||
<option value="udp4">udp4</option>
|
||||
<option value="udp6">udp6</option>
|
||||
</select>
|
||||
<div class="form-row node-config-input-devices">
|
||||
<ol id="node-config-input-devices"></ol>
|
||||
</div>
|
||||
</script>
|
||||
|
||||
|
||||
@@ -3,12 +3,9 @@ module.exports = function(RED) {
|
||||
function RemoteServerNode(n) {
|
||||
RED.nodes.createNode(this, n);
|
||||
this.name = n.name;
|
||||
this.group = n.group;
|
||||
this.port = n.port;
|
||||
this.udpv = n.udpv;
|
||||
this.deviceList = n.deviceList || [];
|
||||
|
||||
var node = this;
|
||||
|
||||
}
|
||||
|
||||
RED.nodes.registerType("xiaomi-gateway", RemoteServerNode);
|
||||
|
||||
@@ -5,12 +5,12 @@
|
||||
defaults: {
|
||||
gateway: {value:"", type:"xiaomi-gateway"},
|
||||
name: {value: ""},
|
||||
sid: {value: "", required: true, validate: RED.validators.regex(/^[a-f0-9]{14}$/)},
|
||||
sid: {value: "", required: true},
|
||||
temperature: {value: "{{temperature}}"},
|
||||
humidity: {value: "{{humidity}}"},
|
||||
output: {value: "0"}
|
||||
},
|
||||
inputs: 0,
|
||||
inputs: 1,
|
||||
outputs: 2,
|
||||
outputLabels: ["Temperature","Humidity"],
|
||||
icon: "thermometer-icon.png",
|
||||
@@ -18,6 +18,13 @@
|
||||
return this.name || "xiaomi-ht";
|
||||
},
|
||||
oneditprepare: function() {
|
||||
var node = this;
|
||||
|
||||
// Get the config node id from the select box:
|
||||
var configNodeID = $('#node-input-gateway').val();
|
||||
// Get the config node using the ID:
|
||||
var configNode = RED.nodes.node(configNodeID);
|
||||
|
||||
$("#node-input-output").change(function () {
|
||||
if ($(this).val() == "2") {
|
||||
$(".node-input-msg").show();
|
||||
@@ -29,6 +36,19 @@
|
||||
$(".node-input-msg").hide();
|
||||
$("#node-input-output").val(node.output);
|
||||
|
||||
$("#node-input-gateway").change(function () {
|
||||
|
||||
});
|
||||
|
||||
for (key in configNode.deviceList) {
|
||||
var device = configNode.deviceList[key];
|
||||
$('#node-input-sid').append('<option value="' + device.sid + '">' + device.desc + '</option>');
|
||||
}
|
||||
$('#node-input-sid').val(node.sid);
|
||||
},
|
||||
oneditsave: function() {
|
||||
var node = this;
|
||||
node.sid = $("#node-input-sid").val();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@@ -43,9 +63,13 @@
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-sid"><i class="fa fa-id-badge"></i> Device SID</label>
|
||||
<input type="text" id="node-input-sid">
|
||||
<label for="node-input-sid"><i class="icon-tag"></i> Device</label>
|
||||
<select id="node-input-sid" placeholder="xiaomi gateway"></select>
|
||||
</div>
|
||||
<!--<div class="form-row">-->
|
||||
<!--<label for="node-input-sid"><i class="fa fa-id-badge"></i> Device SID</label>-->
|
||||
<!--<input type="text" id="node-input-sid">-->
|
||||
<!--</div>-->
|
||||
<div class="form-row">
|
||||
<label for="node-input-output"><i class="icon-tag"></i> Output</label>
|
||||
<select id="node-input-output" style="width:70%;">
|
||||
|
||||
@@ -17,44 +17,12 @@ module.exports = function(RED) {
|
||||
node.status({fill:"grey",shape:"ring",text:"battery"});
|
||||
|
||||
if (this.gateway) {
|
||||
|
||||
var opts = {type: node.gateway.udpv, reuseAddr:true};
|
||||
var server;
|
||||
|
||||
if (!udpInputPortsInUse.hasOwnProperty(node.gateway.port)) {
|
||||
node.log("New UDP socket");
|
||||
server = dgram.createSocket(opts); // default to udp4
|
||||
udpInputPortsInUse[node.gateway.port] = server;
|
||||
}
|
||||
else {
|
||||
node.warn(RED._("udp.errors.alreadyused", node.gateway.port));
|
||||
server = udpInputPortsInUse[node.gateway.port]; // re-use existing
|
||||
}
|
||||
|
||||
server.on('listening', function () {
|
||||
server.setBroadcast(true);
|
||||
try {
|
||||
server.setMulticastTTL(128);
|
||||
server.addMembership(node.gateway.group, null);
|
||||
node.log(RED._("udp.status.mc-group",{group:node.gateway.group}));
|
||||
|
||||
} catch (e) {
|
||||
if (e.errno == "EINVAL") {
|
||||
node.error(RED._("udp.errors.bad-mcaddress"));
|
||||
} else if (e.errno == "ENODEV") {
|
||||
node.error(RED._("udp.errors.interface"));
|
||||
} else {
|
||||
node.error(RED._("udp.errors.error",{error:e.errno}));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
server.on('message', function(msg, rinfo) {
|
||||
var payload = JSON.parse(msg);
|
||||
|
||||
node.on('input', function(msg) {
|
||||
// var payload = JSON.parse(msg);
|
||||
var payload = msg.payload;
|
||||
node.log("Received message from: " + payload.model + " sid: " + payload.sid + " payload: " + payload.data);
|
||||
|
||||
if (payload.sid == node.sid) {
|
||||
if (payload.sid == node.sid && payload.model == "sensor_ht") {
|
||||
var data = JSON.parse(payload.data)
|
||||
|
||||
if (data.voltage) {
|
||||
@@ -65,8 +33,6 @@ module.exports = function(RED) {
|
||||
} else {
|
||||
node.status({fill:"green",shape:"dot",text:"battery"});
|
||||
}
|
||||
} else {
|
||||
node.status({fill:"grey",shape:"ring",text:"battery"});
|
||||
}
|
||||
|
||||
if (node.output == "0") {
|
||||
@@ -100,30 +66,15 @@ module.exports = function(RED) {
|
||||
}
|
||||
});
|
||||
|
||||
node.on("input", function(msg) {
|
||||
// console.log("got input");
|
||||
});
|
||||
|
||||
node.on("close", function() {
|
||||
if (udpInputPortsInUse.hasOwnProperty(node.gateway.port)) {
|
||||
delete udpInputPortsInUse[node.gateway.port];
|
||||
}
|
||||
server.close();
|
||||
});
|
||||
|
||||
try { server.bind(node.gateway.port, null); }
|
||||
catch(e) { } // Don't worry if already bound
|
||||
|
||||
} else {
|
||||
// no gateway configured
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RED.httpAdmin.get('/udp-ports/:id', RED.auth.needsPermission('udp-ports.read'), function(req,res) {
|
||||
res.json(Object.keys(udpInputPortsInUse));
|
||||
});
|
||||
|
||||
RED.nodes.registerType("xiaomi-ht", XiaomiHtNode);
|
||||
|
||||
}
|
||||
|
||||
20
package.json
Normal file
20
package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "node-red",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+ssh://git@gitlab.com/h-rietman/xiaomi-devices-node-red.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Harald Rietman <harald.rietman@portofrotterdam.com> (http://blog.example.com)",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/h-rietman/xiaomi-devices-node-red/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/h-rietman/xiaomi-devices-node-red#README"
|
||||
}
|
||||
Reference in New Issue
Block a user