Skip to content

Commit 87a16f9

Browse files
committed
Drop support for multiple connections
1 parent 223e24b commit 87a16f9

File tree

3 files changed

+80
-85
lines changed

3 files changed

+80
-85
lines changed

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -24,48 +24,48 @@ import ArduinoCloud from 'arduino-iot-js';
2424
// apiUrl: 'AUTH SERVER URL', // Default is https://auth.arduino.cc
2525
// onDisconnect: message => { /* Disconnection callback */ }
2626
// }
27-
ArduinoCloud.connect(options).then(connectionId => {
27+
ArduinoCloud.connect(options).then(() => {
2828
// Connected
2929
});
3030

31-
ArduinoCloud.disconnect(connectionId).then(() => {
31+
ArduinoCloud.disconnect().then(() => {
3232
// Disconnected
3333
});
3434

35-
ArduinoCloud.subscribe(connectionId, topic, cb).then(topic => {
35+
ArduinoCloud.subscribe(topic, cb).then(topic => {
3636
// Subscribed to topic, messaged fired in the cb
3737
});
3838

39-
ArduinoCloud.unsubscribe(connectionId, topic).then(topic => {
39+
ArduinoCloud.unsubscribe(topic).then(topic => {
4040
// Unsubscribed to topic
4141
});
4242

43-
ArduinoCloud.sendMessage(connectionId, topic, message).then(() => {
43+
ArduinoCloud.sendMessage(topic, message).then(() => {
4444
// Message sent
4545
});
4646

47-
ArduinoCloud.openCloudMonitor(connectionId, deviceId, cb).then(topic => {
47+
ArduinoCloud.openCloudMonitor(deviceId, cb).then(topic => {
4848
// Cloud monitor messages fired to cb
4949
});
5050

51-
ArduinoCloud.writeCloudMonitor(connectionId, deviceId, message).then(() => {
51+
ArduinoCloud.writeCloudMonitor(deviceId, message).then(() => {
5252
// Message sent to cloud monitor
5353
});
5454

55-
ArduinoCloud.closeCloudMonitor(connectionId, deviceId).then(topic => {
55+
ArduinoCloud.closeCloudMonitor(deviceId).then(topic => {
5656
// Close cloud monitor
5757
});
5858

5959
// Send a property value to a device
6060
// - value can be a string, a boolean or a number
6161
// - timestamp is a unix timestamp, not required
62-
ArduinoCloud.sendProperty(connectionId, thingId, name, value, timestamp).then(() => {
62+
ArduinoCloud.sendProperty(thingId, name, value, timestamp).then(() => {
6363
// Property value sent
6464
});
6565

6666
// Register a callback on a property value change
6767
//
68-
ArduinoCloud.onPropertyValue(connectionId, thingId, propertyName, updateCb).then(() => {
68+
ArduinoCloud.onPropertyValue(thingId, propertyName, updateCb).then(() => {
6969
// updateCb(message) will be called every time a new value is available. Value can be string, number, or a boolean depending on the property type
7070
});
7171

src/index.js

+46-50
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import CBOR from 'cbor-js';
4848

4949
import ArduinoCloudError from './ArduinoCloudError';
5050

51-
const connections = {};
51+
let connection = null;
5252
const subscribedTopics = {};
5353
const propertyCallback = {};
5454
const arduinoCloudPort = 8443;
@@ -82,6 +82,10 @@ const connect = options => new Promise((resolve, reject) => {
8282
onConnected: options.onConnected,
8383
};
8484

85+
if (connection) {
86+
return reject(new Error('connection failed: connection already open'));
87+
}
88+
8589
if (!opts.host) {
8690
return reject(new Error('connection failed: you need to provide a valid host (broker)'));
8791
}
@@ -140,10 +144,8 @@ const connect = options => new Promise((resolve, reject) => {
140144
if (reconnect === true) {
141145
// This is a re-connection: re-subscribe to all topics subscribed before the
142146
// connection loss
143-
Object.getOwnPropertySymbols(subscribedTopics).forEach((connectionId) => {
144-
Object.values(subscribedTopics[connectionId]).forEach((subscribeParams) => {
145-
subscribe(connectionId, subscribeParams.topic, subscribeParams.cb);
146-
});
147+
Object.values(subscribedTopics).forEach((subscribeParams) => {
148+
subscribe(subscribeParams.topic, subscribeParams.cb);
147149
});
148150
}
149151

@@ -170,9 +172,8 @@ const connect = options => new Promise((resolve, reject) => {
170172
reconnect: true,
171173
keepAliveInterval: 30,
172174
onSuccess: () => {
173-
const id = Symbol(clientID);
174-
connections[id] = client;
175-
return resolve(id);
175+
connection = client;
176+
return resolve();
176177
},
177178
onFailure: ({ errorCode, errorMessage }) => reject(
178179
new ArduinoCloudError(errorCode, errorMessage),
@@ -192,20 +193,19 @@ const connect = options => new Promise((resolve, reject) => {
192193
}, reject);
193194
});
194195

195-
const disconnect = id => new Promise((resolve, reject) => {
196-
const client = connections[id];
197-
if (!client) {
198-
return reject(new Error('disconnection failed: client not found'));
196+
const disconnect = () => new Promise((resolve, reject) => {
197+
if (!connection) {
198+
return reject(new Error('disconnection failed: connection closed'));
199199
}
200200

201201
try {
202-
client.disconnect();
202+
connection.disconnect();
203203
} catch (error) {
204204
return reject(error);
205205
}
206206

207207
// Remove the connection
208-
delete connections[id];
208+
connection = null;
209209

210210
// Remove property callbacks to allow resubscribing in a later connect()
211211
Object.keys(propertyCallback).forEach((topic) => {
@@ -215,35 +215,36 @@ const disconnect = id => new Promise((resolve, reject) => {
215215
});
216216

217217
// Clean up subscribed topics - a new connection might not need the same topics
218-
delete subscribedTopics[id];
218+
Object.keys(subscribedTopics).forEach((topic) => {
219+
delete subscribedTopics[topic];
220+
});
221+
219222
return resolve();
220223
});
221224

222-
const subscribe = (id, topic, cb) => new Promise((resolve, reject) => {
223-
const client = connections[id];
224-
if (!client) {
225-
return reject(new Error('subscription failed: client not found'));
225+
const subscribe = (topic, cb) => new Promise((resolve, reject) => {
226+
if (!connection) {
227+
return reject(new Error('subscription failed: connection closed'));
226228
}
227229

228-
return client.subscribe(topic, {
230+
return connection.subscribe(topic, {
229231
onSuccess: () => {
230-
if (!client.topics[topic]) {
231-
client.topics[topic] = [];
232+
if (!connection.topics[topic]) {
233+
connection.topics[topic] = [];
232234
}
233-
client.topics[topic].push(cb);
235+
connection.topics[topic].push(cb);
234236
return resolve(topic);
235237
},
236238
onFailure: () => reject(),
237239
});
238240
});
239241

240-
const unsubscribe = (id, topic) => new Promise((resolve, reject) => {
241-
const client = connections[id];
242-
if (!client) {
243-
return reject(new Error('disconnection failed: client not found'));
242+
const unsubscribe = topic => new Promise((resolve, reject) => {
243+
if (!connection) {
244+
return reject(new Error('disconnection failed: connection closed'));
244245
}
245246

246-
return client.unsubscribe(topic, {
247+
return connection.unsubscribe(topic, {
247248
onSuccess: () => resolve(topic),
248249
onFailure: () => reject(),
249250
});
@@ -260,32 +261,31 @@ const arrayBufferToBase64 = (buffer) => {
260261
return window.btoa(binary);
261262
};
262263

263-
const sendMessage = (id, topic, message) => new Promise((resolve, reject) => {
264-
const client = connections[id];
265-
if (!client) {
266-
return reject(new Error('disconnection failed: client not found'));
264+
const sendMessage = (topic, message) => new Promise((resolve, reject) => {
265+
if (!connection) {
266+
return reject(new Error('disconnection failed: connection closed'));
267267
}
268268

269-
client.publish(topic, message, 1, false);
269+
connection.publish(topic, message, 1, false);
270270
return resolve();
271271
});
272272

273-
const openCloudMonitor = (id, deviceId, cb) => {
273+
const openCloudMonitor = (deviceId, cb) => {
274274
const cloudMonitorOutputTopic = `/a/d/${deviceId}/s/o`;
275-
return subscribe(id, cloudMonitorOutputTopic, cb);
275+
return subscribe(cloudMonitorOutputTopic, cb);
276276
};
277277

278-
const writeCloudMonitor = (id, deviceId, message) => {
278+
const writeCloudMonitor = (deviceId, message) => {
279279
const cloudMonitorInputTopic = `/a/d/${deviceId}/s/i`;
280-
return sendMessage(id, cloudMonitorInputTopic, message);
280+
return sendMessage(cloudMonitorInputTopic, message);
281281
};
282282

283-
const closeCloudMonitor = (id, deviceId) => {
283+
const closeCloudMonitor = (deviceId) => {
284284
const cloudMonitorOutputTopic = `/a/d/${deviceId}/s/o`;
285-
return unsubscribe(id, cloudMonitorOutputTopic);
285+
return unsubscribe(cloudMonitorOutputTopic);
286286
};
287287

288-
const sendProperty = (connectionId, thingId, name, value, timestamp) => {
288+
const sendProperty = (thingId, name, value, timestamp) => {
289289
const propertyInputTopic = `/a/t/${thingId}/e/i`;
290290

291291
if (timestamp && !Number.isInteger(timestamp)) {
@@ -315,7 +315,7 @@ const sendProperty = (connectionId, thingId, name, value, timestamp) => {
315315
break;
316316
}
317317

318-
return sendMessage(connectionId, propertyInputTopic, CBOR.encode([cborValue]));
318+
return sendMessage(propertyInputTopic, CBOR.encode([cborValue]));
319319
};
320320

321321
const getSenml = (deviceId, name, value, timestamp) => {
@@ -357,7 +357,7 @@ const getCborValue = (senMl) => {
357357
return arrayBufferToBase64(cborEncoded);
358358
};
359359

360-
const sendPropertyAsDevice = (connectionId, deviceId, thingId, name, value, timestamp) => {
360+
const sendPropertyAsDevice = (deviceId, thingId, name, value, timestamp) => {
361361
const propertyInputTopic = `/a/t/${thingId}/e/o`;
362362

363363
if (timestamp && !Number.isInteger(timestamp)) {
@@ -369,10 +369,10 @@ const sendPropertyAsDevice = (connectionId, deviceId, thingId, name, value, time
369369
}
370370

371371
const senMlValue = getSenml(deviceId, name, value, timestamp);
372-
return sendMessage(connectionId, propertyInputTopic, CBOR.encode([senMlValue]));
372+
return sendMessage(propertyInputTopic, CBOR.encode([senMlValue]));
373373
};
374374

375-
const onPropertyValue = (connectionId, thingId, name, cb) => {
375+
const onPropertyValue = (thingId, name, cb) => {
376376
if (!name) {
377377
throw new Error('Invalid property name');
378378
}
@@ -381,19 +381,15 @@ const onPropertyValue = (connectionId, thingId, name, cb) => {
381381
}
382382
const propOutputTopic = `/a/t/${thingId}/e/o`;
383383

384-
if (!subscribedTopics[connectionId]) {
385-
subscribedTopics[connectionId] = {};
386-
}
387-
388-
subscribedTopics[connectionId][thingId] = {
384+
subscribedTopics[thingId] = {
389385
topic: propOutputTopic,
390386
cb,
391387
};
392388

393389
if (!propertyCallback[propOutputTopic]) {
394390
propertyCallback[propOutputTopic] = {};
395391
propertyCallback[propOutputTopic][name] = cb;
396-
subscribe(connectionId, propOutputTopic, cb);
392+
subscribe(propOutputTopic, cb);
397393
} else if (propertyCallback[propOutputTopic] && !propertyCallback[propOutputTopic][name]) {
398394
propertyCallback[propOutputTopic][name] = cb;
399395
}

0 commit comments

Comments
 (0)