Skip to content

Commit

Permalink
Refactored device select
Browse files Browse the repository at this point in the history
added device options
  • Loading branch information
andreypopov committed Feb 21, 2022
1 parent 08713a1 commit 51e9836
Show file tree
Hide file tree
Showing 12 changed files with 440 additions and 474 deletions.
43 changes: 1 addition & 42 deletions api.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
var request = require('request');
var NODE_PATH = '/zigbee2mqtt/';

module.exports = function(RED) {



RED.httpAdmin.get(NODE_PATH + 'getDevices', function (req, res) {
var config = req.query;
var controller = RED.nodes.getNode(config.controllerID);
Expand All @@ -19,41 +15,7 @@ module.exports = function(RED) {
}
}, forceRefresh, true);
} else {
res.status(404).end();
}
});

RED.httpAdmin.get(NODE_PATH + 'getLastStateById', function (req, res) {
var config = req.query;
var controller = RED.nodes.getNode(config.controllerID);
if (controller && controller.constructor.name === "ServerNode") {
var item = controller.getDeviceOrGroupByKey(config.device_id);
if (item) {
let opts = [];
if ('definition' in item && 'exposes' in item.definition) {
opts = item.definition.exposes;
}
res.json([opts, item.homekit]);
} else {
res.status(404).end();
}
} else {
res.status(404).end();
}
});

RED.httpAdmin.get(NODE_PATH + 'getStatesByDevice', function (req, res) {
var config = req.query;
var controller = RED.nodes.getNode(config.controllerID);
if (controller && controller.constructor.name === "ServerNode") {
var item = controller.getDeviceByKey(config.device_id);
if (item) {
res.json([item.current_values, item.homekit]);
} else {
res.status(404).end();
}
} else {
res.status(404).end();
res.json([{},{}]);
}
});

Expand Down Expand Up @@ -181,9 +143,6 @@ module.exports = function(RED) {
var config = req.query;
var controller = RED.nodes.getNode(config.controllerID);
if (controller && controller.constructor.name === "ServerNode") {

// res.json( controller.refreshMap(true));

controller.refreshMap(true, config.engine).then(function(response){
res.json(response);
}).catch(error => {
Expand Down
80 changes: 39 additions & 41 deletions nodes/get.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
<label for="node-input-device_id" class="l-width"><i class="fa fa-crosshairs"></i> <span data-i18n="label.topic"></span></label>
<select id="node-input-device_id" class="s-width" data-single="true"></select>
</div>
<div class="form-row">
<label for="force-refresh" class="l-width"><i class="fa fa-refresh"></i> <span data-i18n="label.refresh"></span></label>
<a class="red-ui-button s-width" id="force-refresh" name="force-refresh"><span data-i18n="label.refresh_devices_list"></span></a>
</div>
<div class="form-row">
<label for="node-input-state" class="l-width"><i class="fa fa-tag"></i> <span data-i18n="label.state"></span></label>
<select id="node-input-state" class="s-width" data-i18n="[placeholder]multiselect.complete_payload"></select>
</div>
<div class="form-row">
<label for="force-refresh" class="l-width"><i class="fa fa-refresh"></i> <span data-i18n="label.refresh"></span></label>
<a class="red-ui-button s-width" id="force-refresh" name="force-refresh"><span data-i18n="label.refresh_devices_list"></span></a>
</div>
</script>

<script type='text/javascript'>
Expand Down Expand Up @@ -71,46 +71,44 @@
return label;
},
oneditprepare: function () {
var node = this;

var updateOutput = function () {
var selectedState = $('#node-input-state option:selected').val();
var selectedDevice = $('#node-input-device_id option:selected').val();

// if ( (selectedState != 0 && selectedState != null && selectedDevice.match(/^\d/)) || (node.state != 0 && node.state != null)) {
// $outputSelect.closest('.form-row').show();
// } else {
// $outputSelect.closest('.form-row').hide();
// }
// $outputSelect.multipleSelect('refresh');
}

$('#node-input-state').on("change", function() {
var state = $('#node-input-state option:selected').val();
if (state) { node.state = state; }
// if (parseInt(state) === 0) {$outputSelect.val('always').multipleSelect('refresh');}
updateOutput();
});


setTimeout(function(){
z2m_getItemList(node.device_id, '#node-input-device_id', {allowEmpty:true});

var $deviceInput = $('#node-input-device_id');
$deviceInput.on("change", function(){
z2m_getItemStateList(0, '#node-input-state');
// var device = $('#node-input-device_id option:selected').val();
updateOutput();
$('#node-input-friendly_name').val($('#node-input-device_id option:selected').attr('data-friendly_name'));
});
let node = this;

setTimeout(function () {
z2m_getItemStateList(node.state, '#node-input-state');
},100);
// setTimeout(()=> {
new Zigbee2MqttEditor(node, {
'allow_empty': true
}).build();
// }, 100);
// var updateOutput = function () {
// var selectedState = $('#node-input-state option:selected').val();
// var selectedDevice = $('#node-input-device_id option:selected').val();
// }

// if (!node.output) { $outputSelect.val('always').multipleSelect('refresh'); }
// $('#node-input-state').on("change", function() {
// var state = $('#node-input-state option:selected').val();
// if (state) { node.state = state; }
// // if (parseInt(state) === 0) {$outputSelect.val('always').multipleSelect('refresh');}
// updateOutput();
// });

}, 100); //we need small timeout, too fire change event for server select
//
// setTimeout(function(){
// z2m_getItemList(node.device_id, '#node-input-device_id', {allowEmpty:true});
//
// var $deviceInput = $('#node-input-device_id');
// $deviceInput.on("change", function(){
// z2m_getItemStateList(0, '#node-input-state');
// // var device = $('#node-input-device_id option:selected').val();
// updateOutput();
// $('#node-input-friendly_name').val($('#node-input-device_id option:selected').attr('data-friendly_name'));
// });
//
// setTimeout(function () {
// z2m_getItemStateList(node.state, '#node-input-state');
// },100);
//
// // if (!node.output) { $outputSelect.val('always').multipleSelect('refresh'); }
//
// }, 100); //we need small timeout, too fire change event for server select
},
oneditsave: function () {

Expand Down
1 change: 1 addition & 0 deletions nodes/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = function(RED) {
node.cleanTimer = null;
node.last_successful_status = {};
node.server = RED.nodes.getNode(node.config.server);
node.status({});
if (node.server) {
node.on('input', function(message_in) {

Expand Down
29 changes: 7 additions & 22 deletions nodes/in.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
<label for="node-input-device_id" class="l-width"><i class="fa fa-crosshairs"></i> <span data-i18n="label.topic"></span></label>
<select id="node-input-device_id" class="s-width" data-single="true"></select>
</div>
<div class="form-row">
<label for="force-refresh" class="l-width"><i class="fa fa-refresh"></i> <span data-i18n="label.refresh"></span></label>
<a class="red-ui-button s-width" id="force-refresh" name="force-refresh"><span data-i18n="label.refresh_devices_list"></span></a>
</div>
<div class="form-row">
<label for="node-input-state" class="l-width"><i class="fa fa-tag"></i> <span data-i18n="label.state"></span></label>
<select id="node-input-state" class="s-width" data-i18n="[placeholder]multiselect.complete_payload"></select>
</div>
<div class="form-row">
<label for="force-refresh" class="l-width"><i class="fa fa-refresh"></i> <span data-i18n="label.refresh"></span></label>
<a class="red-ui-button s-width" id="force-refresh" name="force-refresh"><span data-i18n="label.refresh_devices_list"></span></a>
</div>
<div class="form-row">
<label for='node-input-outputAtStartup' class="l-width"><i class='fa fa-share-square'></i> <span data-i18n="label.start_output"></span></label>
<input type="checkbox" id="node-input-outputAtStartup" checked="checked" style="display: inline-block; width: auto; vertical-align: top;"> <span data-i18n="label.start_output_help"></span></input>
</div>
<div class="form-row">
<div class="form-row">
<label for='node-input-filterChanges' class="l-width"><i class='fa fa-share-square'></i> <span data-i18n="label.filter_changes"></span></label>
<input type="checkbox" id="node-input-filterChanges" checked="checked" style="display: inline-block; width: auto; vertical-align: top;"> <span data-i18n="label.filter_changes_help"></span></input>
</div>
Expand Down Expand Up @@ -87,23 +87,8 @@
return label;
},
oneditprepare: function () {
var node = this;
setTimeout(function(){
var $deviceInput = $('#node-input-device_id');

z2m_getItemList(node.device_id, '#node-input-device_id', {allowEmpty:false});

$deviceInput.on("change", function(){
z2m_getItemStateList(0, '#node-input-state');

$('#node-input-friendly_name').val($('#node-input-device_id option:selected').attr('data-friendly_name'));
});

setTimeout(function () {
z2m_getItemStateList(node.state, '#node-input-state');
},100);

}, 100); //we need small timeout, too fire change event for server select
let node = this;
new Zigbee2MqttEditor(node).build();
},
oneditsave: function () {
var selectedOptions = $('#node-input-device_id option:selected');
Expand Down
3 changes: 2 additions & 1 deletion nodes/locales/en-US/out.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"topic": "Device",
"command": "Command",
"payload": "Payload",
"options": "Options",
"transition": "Transition, sec",
"refresh": "Refresh",
"refresh_devices_list": "Refresh Devices List"
Expand All @@ -27,4 +28,4 @@
"command_brightness_step": "Instead of setting a brightness by value, you can also change current value up or down.",
"option_transition": "Specifies the number of seconds the transition to this state takes. Doesn't work for brightness=0, state=toggle."
}
}
}
8 changes: 8 additions & 0 deletions nodes/locales/en-US/server.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@
},
"tip": {
"deploy": "<b>Important:</b> deploy server node to get devices list"
},
"editor": {
"groups": "Groups",
"devices": "Devices",
"nothing": "Nothing",
"complete_payload": "Complete payload",
"zigbee2mqtt": "zigbee2mqtt",
"homekit": "Apple Homekit"
}
}
Loading

0 comments on commit 51e9836

Please sign in to comment.