Skip to content

Commit

Permalink
Move from MQTT.js to paho-mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
mskvortsov committed Apr 18, 2024
1 parent fe9d3d7 commit 18c5e5a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"crypto-js": "^4.2.0",
"mqtt": "^5.5.2",
"paho-mqtt": "^1.1.0",
"protobufjs": "^7.2.6"
},
"devDependencies": {
Expand Down
43 changes: 21 additions & 22 deletions src/meshmon.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import 'crypto-js/mode-ctr';
import 'crypto-js/pad-nopadding';
import 'crypto-js/lib-typedarrays';

import mqtt from 'mqtt';
import Paho from 'paho-mqtt';
import protobuf from 'protobufjs/light';
import meshtasticJson from '/generated/meshtastic.json';

const defaultMqttUrl = 'wss://mqtt.eclipseprojects.io/mqtt';
const defaultMqttUrl = 'mqtt.eclipseprojects.io';
const defaultMqttTopic = 'msh';
const defaultKey = Base64.parse('1PG7OiApB1nwvP+rz05pAQ==');
const defaultMaxPackets = 2048;
Expand Down Expand Up @@ -241,35 +241,34 @@ function mqttOnDisconnect() {
resetToConnect();
}

function mqttOnError(error) {
mqttStatusHint.innerHTML = error.toString();
function mqttOnFailure(error) {
mqttStatusHint.innerHTML = error.errorMessage;
resetToConnect();
}

function onClickConnect() {
connectButton.disabled = true;
if (mqttClient && mqttClient.connected) {
mqttClient.on('close', mqttOnDisconnect);
mqttClient.end();
if (mqttClient && mqttClient.isConnected()) {
mqttClient.disconnect();
} else {
mqttUrlInput.disabled = true;
mqttTopicInput.disabled = true;
try {
mqttClient = mqtt.connect(mqttUrlInput.value, {
reconnectPeriod: 0,
connectTimeout: 5000,
manualConnect: true,
const clientId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
mqttClient = new Paho.Client(mqttUrlInput.value, 443, '/mqtt', 'meshmon-' + clientId);
mqttClient.onMessageArrived = mqttOnMessage;
mqttClient.onConnectionLost = mqttOnDisconnect;
mqttClient.connect({
useSSL: true,
onSuccess: mqttOnConnect,
onFailure: mqttOnFailure,
timeout: 5,
});
} catch (error) {
mqttStatusHint.innerHTML = error.toString();
mqttStatusHint.innerHTML = error.errorMessage;
resetToConnect();
return;
}
mqttClient.on('connect', mqttOnConnect);
mqttClient.on('message', mqttOnMessage);
mqttClient.on('error', mqttOnError);
mqttClient.connect();
mqttClient.stream.on('error', mqttOnError);
}
}

Expand Down Expand Up @@ -381,20 +380,20 @@ function render(se) {
};
}

function mqttOnMessage(topic, message) {
const topicLevels = topic.split('/');
function mqttOnMessage(message) {
const topicLevels = message.topic.split('/');
if (topicLevels.length == 0 || !topicLevels[topicLevels.length - 1].startsWith('!')) {
console.log(`unexpected topic ${topic}`);
return;
}

var se = null;
try {
se = meshtastic.ServiceEnvelope.decode(message);
se = meshtastic.ServiceEnvelope.decode(message.payloadBytes);
} catch (error) {
console.error(`Failed to decode ServiceEnvelope: ${error}`);
console.error(`Topic: ${topic}`);
console.error(`Message: x${arrayToString(message)}`);
console.error(`Topic: ${message.topic}`);
console.error(`Message: x${arrayToString(message.payloadBytes)}`);
return;
}

Expand Down

0 comments on commit 18c5e5a

Please sign in to comment.