Skip to content

Commit

Permalink
node-event
Browse files Browse the repository at this point in the history
startOutput option
  • Loading branch information
andreypopov committed Jun 16, 2019
1 parent 5476709 commit 7b82be6
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 57 deletions.
38 changes: 38 additions & 0 deletions deconz.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
$.getScript('deconz/static/js/node-red-contrib-deconz-in.js');
$.getScript('deconz/static/js/node-red-contrib-deconz-out.js');
$.getScript('deconz/static/js/node-red-contrib-deconz-get.js');
$.getScript('deconz/static/js/node-red-contrib-deconz-event.js');
$.getScript('deconz/static/js/node-red-contrib-deconz-server.js');
$.getScript('deconz/static/js/node-red-contrib-deconz-helpers.js');
</script>
Expand Down Expand Up @@ -53,6 +54,10 @@
<label for="force-refresh"><i class="fa fa-refresh"></i> Refresh</label>
<button type="button" style="width: 70%" class="btn btn-light" id="force-refresh" name="force-refresh">Refresh devices list</button>
</div>
<div class='form-row'>
<label for='node-input-outputAtStartup'><i class='fa fa-share-square'></i> Start output</label>
<input type='checkbox' id='node-input-outputAtStartup' style='width:auto; margin: 0;' checked="checked">
</div>
</script>

<script type="text/x-red" data-help-name="deconz-input">
Expand All @@ -63,6 +68,7 @@
<li><b>Server:</b> Choose the deconz server instance to use.</li>
<li><b>Device:</b> Select channel to listen to.</li>
<li><b>State:</b> The node indicates the connection status to deconz via a status indicator and affects payload.</li>
<li><b>Start output:</b> Output state at startup.</li>
</ul>

<h3>Outputs</h3>
Expand Down Expand Up @@ -184,6 +190,38 @@ <h5>Homekit output</h5>



<!-- *************** event Node *************** -->
<script type="text/x-red" data-template-name="deconz-event">
<link rel="stylesheet" href="deconz/static/css/bootstrap-multiselect.css" type="text/css" />
<link rel="stylesheet" href="deconz/static/css/multiselect.css" type="text/css" />
<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>
<div class="form-row">
<label for="node-input-server"><i class="fa fa-globe"></i> Server</label>
<input type="text" id="node-input-server">
</div>
</script>

<script type="text/x-red" data-help-name="deconz-event">
<p>This node listens for messages sent from your deconz.</p>
<b>Configuration</b><br>
<ul>
<li><b>Name:</b> Choose any name to identify your node.</li>
<li><b>Server:</b> Choose the deconz server instance to use.</li>
</ul>

<h3>Outputs</h3>
<h5>Raw output</h5>
<dl class="message-properties">
<dt>msg.payload <span class="property-type">object</span></dt>
<dd>full data of event</dd>
<dt>msg.device <span class="property-type">object</span></dt>
<dd>device info</dd>
</dl>
</script>



<!-- *************** Server Node *************** -->
Expand Down
112 changes: 55 additions & 57 deletions deconz.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ module.exports = function (RED) {
if (deviceMeta) {
devices[node.id] = deviceMeta.uniqueid;
node.meta = deviceMeta;
node.sendState(deviceMeta);
if (node.config.outputAtStartup) {
node.sendState(deviceMeta);
}
} else {
node.status({
fill: "red",
Expand All @@ -138,6 +140,7 @@ module.exports = function (RED) {
RED.nodes.registerType("deconz-input", deConzItemIn);



//*************** GET Node ***************
function deConzItemGet(config) {
RED.nodes.createNode(this, config);
Expand Down Expand Up @@ -395,6 +398,24 @@ module.exports = function (RED) {
RED.nodes.registerType("deconz-output", deConzOut);



//*************** Event Node ***************
function deConzItemEvent(config) {
RED.nodes.createNode(this, config);
var node = this;
node.config = config;
node.cleanTimer = null;
node.status({}); //clean

//get server node
node.server = RED.nodes.getNode(config.server);
if (!node.server) return status_no_server(node);


devices[node.id] = 'event';
}
RED.nodes.registerType("deconz-event", deConzItemEvent);

//*************** Server Node ***************
function deConzServerNode(n) {
RED.nodes.createNode(this, n);
Expand All @@ -409,7 +430,7 @@ module.exports = function (RED) {
node.pingTimeout = undefined;


this.discoverDevices = function (callback, forceRefresh = false) {
node.discoverDevices = function (callback, forceRefresh = false) {
if (forceRefresh || node.items === undefined) {
node.log('discoverDevices: Refreshing devices list');

Expand Down Expand Up @@ -458,7 +479,7 @@ module.exports = function (RED) {
}
}

this.getDeviceMeta = function (callback, uniqueid) {
node.getDeviceMeta = function (callback, uniqueid) {
var result = null;

node.discoverDevices(function(items){
Expand All @@ -477,7 +498,7 @@ module.exports = function (RED) {
}, false);
}

this.getItemsList = function (callback, forceRefresh = false) {
node.getItemsList = function (callback, forceRefresh = false) {
node.discoverDevices(function(items){
node.items_list = [];
for (var index in items) {
Expand Down Expand Up @@ -533,6 +554,22 @@ module.exports = function (RED) {
for (var nodeId in devices) {
var item = devices[nodeId];

if ("event" == item && "t" in dataParsed && dataParsed.t == "event") {
var node = RED.nodes.getNode(nodeId);
if (node && "type" in node && node.type === "deconz-event") {
var serverNode = RED.nodes.getNode(node.server.id);
node.send({'payload': dataParsed, 'device': serverNode.items[dataParsed.uniqueid]});
clearTimeout(node.cleanTimer);
node.status({
fill: "green",
shape: "dot",
text: 'event'
});
node.cleanTimer = setTimeout(function () {
node.status({}); //clean
}, 3000);
}
}

if (dataParsed.uniqueid === item) {
var node = RED.nodes.getNode(nodeId);
Expand Down Expand Up @@ -592,62 +629,23 @@ module.exports = function (RED) {
// case "ZHAHumidity":
// characteristic.CurrentRelativeHumidity = state.humidity/100;
// break;
// case "ZHALightLevel":
// // characteristic.CurrentRelativeHumidity = state.humidity;
// break;
// case "ZHAPresence":
// // characteristic.CurrentRelativeHumidity = state.humidity;
// break;
// case "ZHAOpenClose":
// // characteristic.ContactSensorState = state.humidity;
// break;
// case "ZHASwitch":
// // characteristic.ContactSensorState = state.humidity;
// break;
// case "CLIPLightlevel":
// // characteristic.ContactSensorState = state.humidity;
// break;
// case "CLIPHumidity":
// // characteristic.ContactSensorState = state.humidity;
// break;
// case "CLIPTemperature":
// // characteristic.ContactSensorState = state.humidity;
// break;
// case "CLIPPresence":
// // characteristic.ContactSensorState = state.humidity;
// break;
// case "CLIPOpenClose":
// // characteristic.ContactSensorState = state.humidity;
// break;
// case "CLIPSwitch":
// // characteristic.ContactSensorState = state.humidity;
// break;
// case "CLIPGenericStatus":
// // characteristic.ContactSensorState = state.humidity;
// break;
// case "CLIPGenericFlag":
// // characteristic.ContactSensorState = state.humidity;
// break;
// case "Daylight":
// // characteristic.ContactSensorState = state.humidity;
// break;
// }
// }

if (state['temperature'] !== undefined){
characteristic.CurrentTemperature = state.temperature/100;
characteristic.CurrentTemperature = state['temperature']/100;
}

if (state['humidity'] !== undefined){
characteristic.CurrentRelativeHumidity = state.humidity/100;
characteristic.CurrentRelativeHumidity = state['humidity']/100;
}

if (state['lux'] !== undefined){
characteristic.CurrentAmbientLightLevel = state['lux'];
}

if (state['fire'] !== undefined){
characteristic.SmokeDetected = state.fire;
characteristic.SmokeDetected = state['fire'];
}

if (state['buttonevent'] !== undefined){
Expand All @@ -658,37 +656,37 @@ module.exports = function (RED) {
}

if (state['presence'] !== undefined){
characteristic.MotionDetected = state.presence;
characteristic.MotionDetected = state['presence'];
}

if (state['open'] !== undefined){
characteristic.ContactSensorState = state.open;
characteristic.ContactSensorState = state['open'];
}

if (state['vibration'] !== undefined){
characteristic.ContactSensorState = state.vibration;
characteristic.ContactSensorState = state['vibration'];
}

if (state['on'] !== undefined){
characteristic.On = state.on;
characteristic.On = state['on'];
}

if (state['bri'] !== undefined){
characteristic.Brightness = state.bri/2.55
characteristic.Brightness = state['bri']/2.55
}

if (state['hue'] !== undefined){
characteristic.Hue = state.hue/182;
characteristic.Hue = state['hue']/182;
}

if (state['sat'] !== undefined){
characteristic.Saturation = state.sat/2.55
characteristic.Saturation = state['sat']/2.55
}

if (state['ct'] !== undefined){
characteristic.ColorTemperature = state.ct;
if (state.ct < 140) characteristic.ColorTemperature = 140;
else if (state.ct > 500) characteristic.ColorTemperature = 500;
characteristic.ColorTemperature = state['ct'];
if (state['ct'] < 140) characteristic.ColorTemperature = 140;
else if (state['ct'] > 500) characteristic.ColorTemperature = 500;
}
}

Expand Down
51 changes: 51 additions & 0 deletions static/js/node-red-contrib-deconz-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
RED.nodes.registerType('deconz-event', {
category: 'deCONZ',
color: '#f7aa3f',
defaults: {
name: {
value: ""
},
server: {
type: "deconz-server",
required: true
},
device_name: {
value: null
}
},
inputs: 0,
outputs: 1,
outputLabels: ["event"],
paletteLabel: 'event',
icon: "deconz.png",
label: function () {
var label = 'deconz-event';
if (this.name) {
label = this.name;
} else if (typeof(this.device_name) == 'string' && this.device_name.length) {
label = this.device_name;
} else if (typeof(this.device) == 'string' && this.device.length) {
label = this.device;
}

return label;
},
oneditprepare: function () {
var node = this;
setTimeout(function(){
deconz_getItemList(node.device, '#node-input-device', {allowEmpty:true});
}, 100); //we need small timeout, too fire change event for server select
},
oneditsave: function () {
var selectedOptions = $('#node-input-device option:selected');
if (selectedOptions) {
this.device = selectedOptions.map(function () {
return $(this).val();
});

this.device_name = selectedOptions.text();
} else {
this.device_name = this.device = null;
}
}
});
4 changes: 4 additions & 0 deletions static/js/node-red-contrib-deconz-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ RED.nodes.registerType('deconz-input', {
},
state: {
value: ""
},
outputAtStartup: {
value: true,
required: true,
}
},
inputs: 0,
Expand Down

0 comments on commit 7b82be6

Please sign in to comment.