Skip to content

Commit

Permalink
fix(runtime): runtime mqtt client id name undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
mwfarb committed Apr 26, 2022
1 parent 5f9341e commit ed5faff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
9 changes: 5 additions & 4 deletions src/runtime-mngr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class RuntimeMngr {

// instanciate runtime messages factory
this.rtMsgs = new RuntimeMsgs(this);

// create a last will message (delete runtime)
this.lastWillStringMsg = JSON.stringify(this.rtMsgs.deleteRuntime());

Expand Down Expand Up @@ -144,7 +144,7 @@ export class RuntimeMngr {
}

async init() {
// mqtt connect; setup delete runtime msg as last will
// mqtt connect; setup delete runtime msg as last will
let rtMngr = this;
this.mc = new MQTTClient({
mqtt_host: rtMngr.mqttHost,
Expand All @@ -153,6 +153,7 @@ export class RuntimeMngr {
onMessageCallback: rtMngr.onMqttMessage.bind(rtMngr),
willMessage: rtMngr.lastWillStringMsg,
willMessageTopic: rtMngr.regTopic,
name: rtMngr.name, // TODO(mwfarb): review
});

await this.mc.connect();
Expand Down Expand Up @@ -346,10 +347,10 @@ export class RuntimeMngr {

getRtDbgTopic() {
return `${this.dbgTopic}/${this.uuid}`;
}
}

/* pubsub topic where the runtime sends unregister messages (ctl_topic+module uuid) */
getRtCtlTopic() {
return `${this.ctlTopic}/${this.uuid}`;
}
}
}
32 changes: 16 additions & 16 deletions src/runtime-mngr/mqtt-client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @fileoverview Paho MQTT client wrapper;
* The runtime manager has its own mqtt client
* @fileoverview Paho MQTT client wrapper;
* The runtime manager has its own mqtt client
* TODO: reconcile with other mqtt clients (main arena client, chat ?)
*
* Open source software under the terms in /LICENSE
Expand All @@ -12,7 +12,7 @@ import * as Paho from 'paho-mqtt'; // https://www.npmjs.com/package/paho-mqtt

export default class MQTTClient {
constructor(st) {

// handle default this.settings
st = st || {};
this.settings = {
Expand All @@ -33,30 +33,31 @@ export default class MQTTClient {
willMessageTopic:
st.willMessageTopic !== undefined ? st.willMessageTopic : "lastwill",
dbg: st.dbg !== undefined ? st.dbg : false,
userid: st.name, // TODO(mwfarb): review
};

if (this.settings.willMessage !== undefined) {
const lw = new Paho.Message(this.settings.willMessage);
lw.destinationName =
st.willMessageTopic !== undefined ? st.willMessageTopic : "lwtopic";
lw.qos = 2;
lw.retained = false;

this.settings.willMessage = lw;
}
}

async connect(force = false) {
if (this.connected == true && force == false) return;
this.mqttc = new Paho.Client(
this.settings.mqtt_host,
"rtmngr-" + this.settings.userid,

);

this.mqttc.onConnectionLost = this.onConnectionLost.bind(this);
this.mqttc.onMessageArrived = this.onMessageArrived.bind(this);

let _this = this;
return new Promise(function (resolve, reject) {
_this.mqttc.connect({
Expand All @@ -78,25 +79,25 @@ export default class MQTTClient {
});
});
}

onConnectionLost(message) {
console.error("MQTT Client Disconnect.");
this.connected = false;
}

subscribe(topic, qos = 0) {
this.mqttc.subscribe(topic, qos);
}

unsubscribe(topic, qos = 0) {
this.mqttc.unsubscribe(topic);
}

async publish(topic, payload, qos = 0, retained = false) {
if (this.settings.dbg == true) console.log(`publish ${topic}: ${payload}`);
this.mqttc.send(topic, payload, qos, retained);
}

/**
* Callback; Called when a message arrives
*/
Expand All @@ -109,9 +110,8 @@ export default class MQTTClient {
message.payloadString +
"\n"
);

if (this.settings.onMessageCallback != undefined)
this.settings.onMessageCallback(message);
}
}

2 comments on commit ed5faff

@mwfarb
Copy link
Contributor Author

@mwfarb mwfarb commented on ed5faff Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nampereira Can you check my quick fix here? It's probably not the best, but I think it stops a MQTT client name conflict where all runtime connects used client id: rtmngr-undefined from userid being undefined. I think this made it to 1.6.0 arena-core, so arena0 may be stuck.

ed5faff#diff-4a740ba41db43c0d9f5bdf23de96378f8c8c392545adcc3961bf1d792f2c4e0aL53

@nampereira
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch! Made some minor edits so that the mqqt client defaults are safer: 4016646

Please sign in to comment.