Skip to content

Commit a0855d8

Browse files
committed
add sharing topic
1 parent 3a78434 commit a0855d8

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

arduino-cloud.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = function (RED) {
1717
this.propertyVariableName = config.variableName;
1818
this.arduinoClient = await connectionManager.getClientMqtt(connectionConfig, RED);
1919
if (this.arduinoClient && this.arduinoClient.connection.connected) {
20-
await this.arduinoClient.onPropertyValue(this.thing, this.propertyVariableName, (msg) => {
20+
await this.arduinoClient.onPropertyValue(this.thing, this.propertyVariableName, config.id,(msg) => {
2121
this.send(
2222
{
2323
topic: this.propertyName,
@@ -35,7 +35,7 @@ module.exports = function (RED) {
3535
this.status({ fill: "red", shape: "ring", text: "Connection Error" });
3636
}
3737
this.on('close', function (done) {
38-
connectionManager.deleteClientMqtt(connectionConfig.credentials.clientid, this.thing, this.propertyName).then(() => { done(); });
38+
connectionManager.deleteClientMqtt(connectionConfig.credentials.clientid, this.thing, this.propertyName,config.id).then(() => { done(); });
3939
});
4040

4141
//this.poll(connectionConfig);

arduino-iot-client-mqtt/arduino-iot-client-mqtt.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ class ArduinoClientMqtt {
165165
}
166166
if (propertyNameKeyPrevious !== propertyNameKey) {
167167
if (this.propertyCallback[topic][propertyNameKeyPrevious]) {
168-
this.propertyCallback[topic][propertyNameKeyPrevious](valueToSend);
168+
for(var i=0; i<this.propertyCallback[topic][propertyNameKeyPrevious].length; i++){
169+
this.propertyCallback[topic][propertyNameKeyPrevious][i].callback(valueToSend);
170+
}
169171
}
170172
propertyNameKeyPrevious = propertyNameKey;
171173
valueToSend = {};
@@ -178,9 +180,11 @@ class ArduinoClientMqtt {
178180
}
179181
});
180182
if (valueToSend !== {} && this.propertyCallback[topic][propertyNameKey]) {
181-
this.propertyCallback[topic][propertyNameKey](valueToSend);
183+
for(var i=0; i<this.propertyCallback[topic][propertyNameKey].length; i++){
184+
this.propertyCallback[topic][propertyNameKey][i].callback(valueToSend);
185+
}
182186
}
183-
}
187+
}
184188
});
185189

186190
if (typeof this.opts.onDisconnect === 'function') {
@@ -560,7 +564,7 @@ class ArduinoClientMqtt {
560564
return arrayBufferToBase64(cborEncoded);
561565
};
562566

563-
onPropertyValue(thingId, name, cb) {
567+
onPropertyValue(thingId, name,nodeId, cb) {
564568
if (!name) {
565569
throw new Error('Invalid property name');
566570
}
@@ -576,23 +580,50 @@ class ArduinoClientMqtt {
576580
this.numSubscriptions++;
577581
if (!this.propertyCallback[propOutputTopic]) {
578582
this.propertyCallback[propOutputTopic] = {};
579-
this.propertyCallback[propOutputTopic][name] = cb;
583+
this.propertyCallback[propOutputTopic][name] = [];
584+
this.propertyCallback[propOutputTopic][name].push({
585+
nodeId: nodeId,
586+
callback:cb
587+
});
588+
580589
return this.subscribe(propOutputTopic, cb);
581590
}
582591

583592
if (this.propertyCallback[propOutputTopic] && !this.propertyCallback[propOutputTopic][name]) {
584-
this.propertyCallback[propOutputTopic][name] = cb;
593+
this.propertyCallback[propOutputTopic][name] = [];
594+
this.propertyCallback[propOutputTopic][name].push({
595+
nodeId: nodeId,
596+
callback:cb
597+
});
598+
}else if(this.propertyCallback[propOutputTopic] && this.propertyCallback[propOutputTopic][name]){
599+
this.propertyCallback[propOutputTopic][name].push({
600+
nodeId: nodeId,
601+
callback:cb
602+
});
585603
}
586604
return Promise.resolve(propOutputTopic);
587605
};
588606

589607

590-
removePropertyValueCallback(thingId, name) {
608+
removePropertyValueCallback(thingId, name, nodeId) {
591609
if (!name) {
592610
throw new Error('Invalid property name');
593611
}
594612
const propOutputTopic = `/a/t/${thingId}/e/o`;
595-
delete this.propertyCallback[propOutputTopic][name];
613+
var pos=-1;
614+
for(var i=0; i<this.propertyCallback[propOutputTopic][name].length; i++){
615+
var cbObject=this.propertyCallback[propOutputTopic][name][i];
616+
if(cbObject.nodeId===nodeId){
617+
pos=i;
618+
break;
619+
}
620+
}
621+
if(pos!=-1){
622+
this.propertyCallback[propOutputTopic][name].splice(pos,1);
623+
}
624+
if(this.propertyCallback[propOutputTopic][name].length===0){
625+
delete this.propertyCallback[propOutputTopic][name];
626+
}
596627
this.numSubscriptions--;
597628

598629
return Promise.resolve(this.numSubscriptions);

utils/arduino-connection-manager.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ async function updateToken(connectionConfig) {
229229
}
230230
}
231231

232-
async function deleteClientMqtt(clientId, thing, propertyName) {
232+
async function deleteClientMqtt(clientId, thing, propertyName, nodeId) {
233233
const releaseMutex = await getClientMutex.acquire();
234234
var user = findUser(clientId);
235235
if (user !== -1) {
236236
if (connections[user].clientMqtt !== null) {
237-
var ret = await connections[user].clientMqtt.removePropertyValueCallback(thing, propertyName);
237+
var ret = await connections[user].clientMqtt.removePropertyValueCallback(thing, propertyName,nodeId);
238238

239239
if (ret === 0) {
240240
await connections[user].clientMqtt.disconnect();

0 commit comments

Comments
 (0)