Skip to content

Commit

Permalink
Merge pull request #7 from ioBroker/dev
Browse files Browse the repository at this point in the history
 Groups allowed for routers only #153
  • Loading branch information
modmax authored Jan 22, 2019
2 parents fabdc43 + 9696bc8 commit 351ede1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
46 changes: 25 additions & 21 deletions admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,18 @@ function deleteConfirmation(id, name) {
}

function editName(id, name) {
$('#modaledit').find("input[id='name']").val(name);
list2select('#groups', groups, devGroups[id] || []);
const dev = devices.find((d) => d._id == id);
$('#modaledit').find("input[id='d_name']").val(name);
if (dev.info.type == "Router") {
list2select('#d_groups', groups, devGroups[id] || []);
$("#d_groups").parent().parent().removeClass('hide');
} else {
$("#d_groups").parent().parent().addClass('hide');
}
$("#modaledit a.btn[name='save']").unbind("click");
$("#modaledit a.btn[name='save']").click(function(e) {
var newName = $('#modaledit').find("input[id='name']").val(),
newGroups = $('#groups').val();
var newName = $('#modaledit').find("input[id='d_name']").val(),
newGroups = $('#d_groups').val();
updateDev(id, newName, newGroups);
});
$('#modaledit').modal('open');
Expand Down Expand Up @@ -220,7 +226,7 @@ function showDevices() {
for (var i=0;i < devices.length; i++) {
var d = devices[i];
if (d.info && d.info.type == "Coordinator") continue;
if (d.groups) {
if (d.groups && d.info && d.info.type == "Router") {
devGroups[d._id] = d.groups;
d.groupNames = d.groups.map(item=>{
return groups[item] || '';
Expand All @@ -246,7 +252,6 @@ function showDevices() {
id = getDevId(dev_block),
name = getDevName(dev_block);
editName(id, name);
//openReval(e, id, name);
});
$("a.btn-floating[name='join']").click(function() {
var dev_block = $(this).parents("div.device");
Expand Down Expand Up @@ -403,9 +408,6 @@ function load(settings, onChange) {

$('#add_group').click(function() {
const maxind = parseInt(Object.getOwnPropertyNames(groups).reduce((a,b) => a>b ? a : b, 0));
// const newGroup = {};
// groups[maxind+1] = '';
// showGroups(onChange);
editGroupName(maxind+1, "");
});

Expand Down Expand Up @@ -1045,18 +1047,20 @@ function updateDev(id, newName, newGroups) {
if (dev && dev.common.name != newName) {
renameDevice(id, newName);
}
const oldGroups = devGroups[id] || [];
if (oldGroups.toString() != newGroups.toString()) {
devGroups[id] = newGroups;
dev.groups = newGroups;
// save dev-groups
sendTo(null, 'groupDevices', devGroups, function (msg) {
if (msg) {
if (msg.error) {
showMessage(msg.error, _('Error'), 'alert');
if (dev.info.type == "Router") {
const oldGroups = devGroups[id] || [];
if (oldGroups.toString() != newGroups.toString()) {
devGroups[id] = newGroups;
dev.groups = newGroups;
// save dev-groups
sendTo(null, 'groupDevices', devGroups, function (msg) {
if (msg) {
if (msg.error) {
showMessage(msg.error, _('Error'), 'alert');
}
}
}
});
showDevices();
});
showDevices();
}
}
}
8 changes: 4 additions & 4 deletions admin/index_m.html
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,12 @@ <h5 id="devActResult" class="card-title"></h5>
<!-- <h3 class="translate">Config device</h3> -->
<div class="row">
<div class="input-field">
<input id="name" type="text" class="value validate">
<label for="name" class="translate">Name</label>
<input id="d_name" type="text" class="value validate">
<label for="d_name" class="translate">Name</label>
</div>
<div class="input-field">
<select id="groups" class="materialSelect" multiple><option value="1">Значение</option></select>
<label for="groups" class="translate">Groups</label>
<select id="d_groups" class="materialSelect" multiple><option value="1">Значение</option></select>
<label for="d_groups" class="translate">Groups</label>
</div>
</div>
</div>
Expand Down
15 changes: 13 additions & 2 deletions lib/devstates.js
Original file line number Diff line number Diff line change
Expand Up @@ -1874,14 +1874,25 @@ const devices = [{

const commonStates = [
states.link_quality,
states.groups,
];

const groupStates = [].concat(lightStatesWithColor);

const byZigbeeModel = new Map();
for (const device of devices) {
for (const zigbeeModel of device.models) {
const stripModel = zigbeeModel.replace(/\0.*$/g, '').trim();
byZigbeeModel.set(stripModel, device);
}
}

module.exports = {
devices: devices,
commonStates: commonStates,
groupStates: groupStates,
findModel: (model) => devices.find((d) => d.models.includes(model)),
groupsState: states.groups,
findModel: (model) => {
const stripModel = model.replace(/\0.*$/g, '').trim();
return byZigbeeModel.get(stripModel);
}
};
14 changes: 10 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ function newDevice(id, msg) {
adapter.log.info('new dev ' + dev.ieeeAddr + ' ' + dev.nwkAddr + ' ' + dev.modelId);
logToPairing('New device joined ' + dev.ieeeAddr + ' model ' + dev.modelId, true);
updateDev(dev.ieeeAddr.substr(2), dev.modelId, dev.modelId, () =>
syncDevStates(dev.ieeeAddr.substr(2), dev.modelId));
syncDevStates(dev)
);
}
}

Expand Down Expand Up @@ -734,7 +735,7 @@ function onReady() {
// update dev and states
chain.push(new Promise((resolve, reject) => {
updateDev(device.ieeeAddr.substr(2), device.modelId, device.modelId, () => {
syncDevStates(device.ieeeAddr.substr(2), device.modelId);
syncDevStates(device);
resolve();
});
}));
Expand Down Expand Up @@ -1067,14 +1068,19 @@ function publishToState(devId, modelID, model, payload) {
}
}

function syncDevStates(devId, modelId) {
function syncDevStates(dev) {
const devId = dev.ieeeAddr.substr(2),
modelId = dev.modelId,
hasGroups = dev.type === 'Router';
// devId - iobroker device id
const stateModel = statesMapping.findModel(modelId);
if (!stateModel) {
adapter.log.debug('Device ' + devId + ' "' + modelId + '" not described in statesMapping.');
return;
}
const states = statesMapping.commonStates.concat(stateModel.states);
const states = statesMapping.commonStates.concat(stateModel.states)
.concat((hasGroups) ? [statesMapping.groupsState] : []);

for (const stateInd in states) {
if (!states.hasOwnProperty(stateInd)) continue;

Expand Down

0 comments on commit 351ede1

Please sign in to comment.