diff --git a/src/components/nrp-core-dashboard/nrp-core-dashboard.js b/src/components/nrp-core-dashboard/nrp-core-dashboard.js index 6a524bf503e550948f361b14cc9ef3f3f7894332..133c0b3d824e8a770da261e148df2a79badae5e3 100644 --- a/src/components/nrp-core-dashboard/nrp-core-dashboard.js +++ b/src/components/nrp-core-dashboard/nrp-core-dashboard.js @@ -21,9 +21,12 @@ export default class NrpCoreDashboard extends React.Component { console.error(err); } }); - // As a test to make sure MqttClientService can subscribe to multiple topics at once we use these two for testing - let token = MqttClientService.instance.subscribeToTopic('test_topic', (param1) => (console.info(param1))); - token = MqttClientService.instance.subscribeToTopic('test_topic_proto', (param1) => (console.info(param1))); + // As a test to make sure MqttClientService can subscribe to multiple topics (and the same topic) at once + let token1 = MqttClientService.instance.subscribeToTopic('test_topic', (param1) => (console.info(param1))); + let token2 = MqttClientService.instance.subscribeToTopic('test_topic', (param1) => (console.info(param1))); + let token3 = MqttClientService.instance.subscribeToTopic('test_topic', (param1) => (console.info(param1))); + let token4 = MqttClientService.instance.subscribeToTopic('test_topic_proto', (param1) => (console.info(param1))); + //TODO: test unsubscribe once implemented } render() { diff --git a/src/services/mqtt-client-service.js b/src/services/mqtt-client-service.js index 5b5de09c559dd41315a73436ddeada5db6e4a617..2813fa801451f9df95e6bd4230f3cb3c84b7baa6 100644 --- a/src/services/mqtt-client-service.js +++ b/src/services/mqtt-client-service.js @@ -4,7 +4,6 @@ import { EventEmitter } from 'events'; //import * as proto from 'nrp-jsproto/nrp-engine_msgs-protobuf.js'; //import { DataPackMessage } from 'nrp-jsproto/nrp-engine_msgs-protobuf.js'; import jspb from '../../node_modules/google-protobuf/google-protobuf'; -import { hasSubscribers } from 'diagnostics_channel'; let _instance = null; const SINGLETON_ENFORCER = Symbol(); @@ -20,8 +19,6 @@ export default class MqttClientService extends EventEmitter { } this.subTokensMap = new Map(); - - //console.info(DataPackMessage); } static get instance() { @@ -53,11 +50,11 @@ export default class MqttClientService extends EventEmitter { onMessage(topic, payload, packet) { //console.info('MQTT message: [topic, payload, packet]'); //console.info([topic, payload, packet]); - //Deserializatin of Data must happen here //Now we see which callbacks have been assigned for a topic if (typeof this.subTokensMap.get(topic) !== 'undefined') { for (var token in this.subTokensMap.get(topic)){ - if (typeof token.callback === 'function' && payload !== 'undefined'){ + if (typeof token.callback === 'function' && payload !== 'undefined') { + //Deserializatin of Data must happen here token.callback(payload); } }; @@ -80,21 +77,23 @@ export default class MqttClientService extends EventEmitter { } //callback should have args topic, payload - subscribeToTopic(topic, callback=Function()){ + subscribeToTopic(topic, callback) { + if (typeof callback !== 'function') { + console.error('trying to subscribe to topic "' + topic + '", but no callback function given!'); + return; + } + const token = { topic: topic, callback: callback }; if (this.subTokensMap.has(token.topic)){ - this.subTokensMap.set( - token.topic, - [this.subTokensMap.get(token.topic), token] - ); + this.subTokensMap.get(token.topic).push(token); } else{ this.subTokensMap.set( token.topic, - token + [token] ); } console.info('You have been subscribed to topic ' + topic);