Skip to content
Snippets Groups Projects
Commit b393f8af authored by Sandro Weber's avatar Sandro Weber
Browse files

small fix to subTokensMap entries (no arrays of arrays)

parent 82d33791
No related branches found
No related tags found
No related merge requests found
...@@ -21,9 +21,12 @@ export default class NrpCoreDashboard extends React.Component { ...@@ -21,9 +21,12 @@ export default class NrpCoreDashboard extends React.Component {
console.error(err); console.error(err);
} }
}); });
// As a test to make sure MqttClientService can subscribe to multiple topics at once we use these two for testing // As a test to make sure MqttClientService can subscribe to multiple topics (and the same topic) at once
let token = MqttClientService.instance.subscribeToTopic('test_topic', (param1) => (console.info(param1))); let token1 = MqttClientService.instance.subscribeToTopic('test_topic', (param1) => (console.info(param1)));
token = MqttClientService.instance.subscribeToTopic('test_topic_proto', (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() { render() {
......
...@@ -4,7 +4,6 @@ import { EventEmitter } from 'events'; ...@@ -4,7 +4,6 @@ import { EventEmitter } from 'events';
//import * as proto from 'nrp-jsproto/nrp-engine_msgs-protobuf.js'; //import * as proto from 'nrp-jsproto/nrp-engine_msgs-protobuf.js';
//import { DataPackMessage } 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 jspb from '../../node_modules/google-protobuf/google-protobuf';
import { hasSubscribers } from 'diagnostics_channel';
let _instance = null; let _instance = null;
const SINGLETON_ENFORCER = Symbol(); const SINGLETON_ENFORCER = Symbol();
...@@ -20,8 +19,6 @@ export default class MqttClientService extends EventEmitter { ...@@ -20,8 +19,6 @@ export default class MqttClientService extends EventEmitter {
} }
this.subTokensMap = new Map(); this.subTokensMap = new Map();
//console.info(DataPackMessage);
} }
static get instance() { static get instance() {
...@@ -53,11 +50,11 @@ export default class MqttClientService extends EventEmitter { ...@@ -53,11 +50,11 @@ export default class MqttClientService extends EventEmitter {
onMessage(topic, payload, packet) { onMessage(topic, payload, packet) {
//console.info('MQTT message: [topic, payload, packet]'); //console.info('MQTT message: [topic, payload, packet]');
//console.info([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 //Now we see which callbacks have been assigned for a topic
if (typeof this.subTokensMap.get(topic) !== 'undefined') { if (typeof this.subTokensMap.get(topic) !== 'undefined') {
for (var token in this.subTokensMap.get(topic)){ 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); token.callback(payload);
} }
}; };
...@@ -80,21 +77,23 @@ export default class MqttClientService extends EventEmitter { ...@@ -80,21 +77,23 @@ export default class MqttClientService extends EventEmitter {
} }
//callback should have args topic, payload //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 = { const token = {
topic: topic, topic: topic,
callback: callback callback: callback
}; };
if (this.subTokensMap.has(token.topic)){ if (this.subTokensMap.has(token.topic)){
this.subTokensMap.set( this.subTokensMap.get(token.topic).push(token);
token.topic,
[this.subTokensMap.get(token.topic), token]
);
} }
else{ else{
this.subTokensMap.set( this.subTokensMap.set(
token.topic, token.topic,
token [token]
); );
} }
console.info('You have been subscribed to topic ' + topic); console.info('You have been subscribed to topic ' + topic);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment