Skip to content

Commit cf3ae59

Browse files
committed
2 parents 63220d9 + 67cf0f7 commit cf3ae59

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

javascript.js

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,8 +1216,15 @@
12161216
if (objects[_adapter] && objects[_adapter].common && objects[_adapter].common.subscribable) {
12171217
var alive = 'system.adapter.' + a + '.alive';
12181218
adapterSubs[alive] = adapterSubs[alive] || [];
1219-
adapterSubs[alive].push(pattern.id);
1220-
adapter.sendTo(a, 'subscribe', pattern.id);
1219+
1220+
var subExists = adapterSubs[alive].filter(function(sub){
1221+
return sub === pattern.id;
1222+
}).length > 0;
1223+
1224+
if (!subExists) {
1225+
adapterSubs[alive].push(pattern.id);
1226+
adapter.sendTo(a, 'subscribe', pattern.id);
1227+
}
12211228
}
12221229
}
12231230

@@ -1249,22 +1256,23 @@
12491256
}
12501257
},
12511258
adapterUnsubscribe: function (id) {
1252-
if (typeof id !== 'string') {
1253-
adapter.log.error('adapterSubscribe: invalid type of id' + typeof id);
1254-
return;
1255-
}
1256-
var parts = id.split('.');
1257-
var _adapter = 'system.adapter.' + parts[0] + '.' + parts[1];
1258-
if (objects[_adapter] && objects[_adapter].common && objects[_adapter].common.subscribable) {
1259-
var a = parts[0] + '.' + parts[1];
1260-
var alive = 'system.adapter.' + a + '.alive';
1261-
if (adapterSubs[alive]) {
1262-
var pos = adapterSubs[alive].indexOf(id);
1263-
if (pos != -1) adapterSubs[alive].splice(pos, 1);
1264-
if (!adapterSubs[alive].length) delete adapterSubs[alive];
1265-
}
1266-
adapter.sendTo(a, 'unsubscribe', id);
1267-
}
1259+
unsubscribe(id);
1260+
// if (typeof id !== 'string') {
1261+
// adapter.log.error('adapterSubscribe: invalid type of id' + typeof id);
1262+
// return;
1263+
// }
1264+
// var parts = id.split('.');
1265+
// var _adapter = 'system.adapter.' + parts[0] + '.' + parts[1];
1266+
// if (objects[_adapter] && objects[_adapter].common && objects[_adapter].common.subscribable) {
1267+
// var a = parts[0] + '.' + parts[1];
1268+
// var alive = 'system.adapter.' + a + '.alive';
1269+
// if (adapterSubs[alive]) {
1270+
// var pos = adapterSubs[alive].indexOf(id);
1271+
// if (pos != -1) adapterSubs[alive].splice(pos, 1);
1272+
// if (!adapterSubs[alive].length) delete adapterSubs[alive];
1273+
// }
1274+
// adapter.sendTo(a, 'unsubscribe', id);
1275+
// }
12681276
},
12691277
unsubscribe: function (idOrObject) {
12701278
var i;
@@ -1738,26 +1746,29 @@
17381746
}
17391747
};
17401748
},
1749+
17411750
formatValue: function (value, decimals, format) {
17421751
if (!format && objects['system.config']) {
17431752
format = objects['system.config'].common.isFloatComma ? '.,' : ',.';
17441753
}
17451754
return adapter.formatValue(value, decimals, format);
17461755
},
1756+
17471757
formatDate: function (date, format) {
17481758
if (!format) {
17491759
format = objects['system.config'] ? (objects['system.config'].common.dateFormat || 'DD.MM.YYYY') : 'DD.MM.YYYY';
17501760
}
17511761

17521762
return adapter.formatDate(date, format);
17531763
},
1764+
17541765
writeFile: function (fileName, data, callback) {
17551766
adapter.writeFile(null, fileName, data, callback);
17561767
},
17571768
readFile: function (fileName, callback) {
17581769
adapter.readFile(null, fileName, callback);
17591770
},
1760-
getHistory: function (instance, options, callback) {
1771+
getHistory: function (instance, options, callback) {
17611772
if (typeof instance === 'object') {
17621773
callback = options;
17631774
options = instance;
@@ -1881,6 +1892,26 @@
18811892
}
18821893
}
18831894

1895+
function unsubscribe(id) {
1896+
if (typeof id !== 'string') {
1897+
adapter.log.error('adapterSubscribe: invalid type of id' + typeof id);
1898+
return;
1899+
}
1900+
var parts = id.split('.');
1901+
var _adapter = 'system.adapter.' + parts[0] + '.' + parts[1];
1902+
if (objects[_adapter] && objects[_adapter].common && objects[_adapter].common.subscribable) {
1903+
var a = parts[0] + '.' + parts[1];
1904+
var alive = 'system.adapter.' + a + '.alive';
1905+
if (adapterSubs[alive]) {
1906+
var pos = adapterSubs[alive].indexOf(id);
1907+
if (pos != -1) adapterSubs[alive].splice(pos, 1);
1908+
if (!adapterSubs[alive].length) delete adapterSubs[alive];
1909+
}
1910+
adapter.sendTo(a, 'unsubscribe', id);
1911+
}
1912+
}
1913+
1914+
18841915
function stop(name, callback) {
18851916
adapter.log.info('Stop script ' + name);
18861917

@@ -1891,7 +1922,10 @@
18911922
isEnums = false;
18921923
for (var i = subscriptions.length - 1; i >= 0 ; i--) {
18931924
if (subscriptions[i].name == name) {
1894-
subscriptions.splice(i, 1);
1925+
var sub = subscriptions.splice(i, 1)[0];
1926+
if (sub) {
1927+
unsubscribe(sub.pattern.id);
1928+
}
18951929
} else {
18961930
if (!isEnums && subscriptions[i].pattern.enumName || subscriptions[i].pattern.enumId) isEnums = true;
18971931
}

0 commit comments

Comments
 (0)