fix(mqtt): persist topics and support advanced options

This commit is contained in:
jinmo authored and GitHub committed 2026-08-06 23:15:17 +08:00
1 parent 670ddcfcb3
commit ab586d121a
19 files changed
+347 -51

No files matched your search

@@ -710,14 +710,18 @@ const mqttClientId = ref("");
const mqttProtocolVersion = ref<"v3" | "v4" | "v5">("v5");
const mqttTransportMode = ref<"tcp" | "websocket">("tcp");
const mqttWsPath = ref("/mqtt");
const mqttAuthKind = ref<"none" | "password">("none");
const mqttAuthKind = ref<"none" | "password" | "certificate">("none");
const mqttUsername = ref("");
const mqttPassword = ref("");
const mqttCaCertPath = ref("");
const mqttClientCertPath = ref("");
const mqttClientKeyPath = ref("");
const mqttTls = ref(false);
const mqttTlsSkipVerify = ref(false);
const mqttKeepAliveSecs = ref(60);
const mqttConnectTimeoutSecs = ref(30);
const mqttMaxPacketSizeBytes = ref(16 * 1024 * 1024);
const mqttSavedTopics = ref<MqttConnectionConfig["savedTopics"]>([]);
const nacosPrimaryAddressPlaceholder = computed(() => {
return "http://127.0.0.1:8848/nacos";
});
@@ -1231,15 +1235,29 @@ function resetMqttFields(config?: Partial<MqttConnectionConfig>) {
mqttKeepAliveSecs.value = Math.max(1, config?.keepAliveSecs || 60);
mqttConnectTimeoutSecs.value = Math.max(1, config?.connectTimeoutSecs || 30);
mqttMaxPacketSizeBytes.value = Math.min(268435455, Math.max(1024, config?.maxPacketSizeBytes || 16 * 1024 * 1024));
mqttSavedTopics.value = config?.savedTopics ?? [];
const auth = config?.auth;
if (auth && auth.kind === "password") {
mqttAuthKind.value = "password";
mqttUsername.value = auth.username || "";
mqttPassword.value = auth.password || "";
mqttCaCertPath.value = "";
mqttClientCertPath.value = "";
mqttClientKeyPath.value = "";
} else if (auth && auth.kind === "certificate") {
mqttAuthKind.value = "certificate";
mqttUsername.value = "";
mqttPassword.value = "";
mqttCaCertPath.value = auth.caCertPath || "";
mqttClientCertPath.value = auth.clientCertPath || "";
mqttClientKeyPath.value = auth.clientKeyPath || "";
} else {
mqttAuthKind.value = "none";
mqttUsername.value = "";
mqttPassword.value = "";
mqttCaCertPath.value = "";
mqttClientCertPath.value = "";
mqttClientKeyPath.value = "";
}
}
@@ -1252,7 +1270,12 @@ function hydrateMqttFields(value: unknown) {
}
function buildMqttExternalConfig(): MqttConnectionConfig {
const auth: MqttConnectionConfig["auth"] = mqttAuthKind.value === "password" ? { kind: "password", username: mqttUsername.value, password: mqttPassword.value } : { kind: "none" };
const auth: MqttConnectionConfig["auth"] =
mqttAuthKind.value === "password"
? { kind: "password", username: mqttUsername.value, password: mqttPassword.value }
: mqttAuthKind.value === "certificate"
? { kind: "certificate", caCertPath: mqttCaCertPath.value || undefined, clientCertPath: mqttClientCertPath.value || undefined, clientKeyPath: mqttClientKeyPath.value || undefined }
: { kind: "none" };
return {
host: mqttHost.value.trim(),
@@ -1266,6 +1289,7 @@ function buildMqttExternalConfig(): MqttConnectionConfig {
keepAliveSecs: Math.max(1, mqttKeepAliveSecs.value),
connectTimeoutSecs: Math.max(1, mqttConnectTimeoutSecs.value),
maxPacketSizeBytes: Math.min(268435455, Math.max(1024, mqttMaxPacketSizeBytes.value)),
savedTopics: mqttSavedTopics.value,
wsPath: mqttTransportMode.value === "websocket" ? mqttWsPath.value || "/mqtt" : undefined,
};
}
@@ -6050,6 +6074,7 @@ function openExternalUrl(url: string) {
<div class="col-span-3 flex gap-2">
<Button size="sm" :variant="mqttAuthKind === 'none' ? 'default' : 'outline'" @click="mqttAuthKind = 'none'">{{ t("connection.mqAuthNone") }}</Button>
<Button size="sm" :variant="mqttAuthKind === 'password' ? 'default' : 'outline'" @click="mqttAuthKind = 'password'">{{ t("connection.mqAuthBasic") }}</Button>
<Button size="sm" :variant="mqttAuthKind === 'certificate' ? 'default' : 'outline'" @click="mqttAuthKind = 'certificate'">{{ t("connection.mqttAuthCertificate") }}</Button>
</div>
</div>
<template v-if="mqttAuthKind === 'password'">
@@ -6062,6 +6087,20 @@ function openExternalUrl(url: string) {
<Input v-model="mqttPassword" type="password" class="col-span-3" :placeholder="t('connection.mqttPasswordPlaceholder')" />
</div>
</template>
<template v-else-if="mqttAuthKind === 'certificate'">
<div class="grid grid-cols-4 items-center gap-4">
<Label :class="connectionLabelClass">{{ t("connection.mqttCaCertPath") }}</Label>
<Input v-model="mqttCaCertPath" class="col-span-3" placeholder="/path/to/ca.pem" />
</div>
<div class="grid grid-cols-4 items-center gap-4">
<Label :class="connectionLabelClass">{{ t("connection.mqttClientCertPath") }}</Label>
<Input v-model="mqttClientCertPath" class="col-span-3" placeholder="/path/to/client.crt" />
</div>
<div class="grid grid-cols-4 items-center gap-4">
<Label :class="connectionLabelClass">{{ t("connection.mqttClientKeyPath") }}</Label>
<Input v-model="mqttClientKeyPath" class="col-span-3" placeholder="/path/to/client.key" />
</div>
</template>
<div class="grid grid-cols-4 items-center gap-4">
<Label :class="connectionLabelClass">{{ t("connection.mqttTls") }}</Label>
<div class="col-span-3 flex items-center gap-2">
@@ -20,6 +20,7 @@ const brokerInfo = ref<MqttBrokerInfo | null>(null);
const topicTree = ref<MqttTopicNode | null>(null);
const messages = ref<MqttMessage[]>([]);
const subscribedTopics = ref<[string, string][]>([]);
const noLocalSubscribe = ref(false);
const selectedTopic = ref<string>(props.initialTopic ?? "");
const loading = ref(true);
const error = ref<string | null>(null);
@@ -52,9 +53,9 @@ async function refreshData() {
}
}
async function handleSubscribe(topic: string) {
async function handleSubscribe(topic: string, noLocal = noLocalSubscribe.value) {
try {
await mqttSubscribe(props.connectionId, topic, "atmostonce");
await mqttSubscribe(props.connectionId, topic, "atmostonce", noLocal);
await refreshData();
} catch (e) {
error.value = String(e);
@@ -194,6 +195,7 @@ onUnmounted(() => {
"
>
<input class="flex-1 h-7 px-2 text-xs rounded border bg-transparent" :placeholder="t('connection.mqttSubscribePlaceholder')" />
<label class="flex items-center gap-1 text-[10px] whitespace-nowrap"><input v-model="noLocalSubscribe" type="checkbox" />{{ t("connection.mqttNoLocal") }}</label>
<Button size="sm" variant="ghost" class="h-7 text-xs" type="submit">{{ t("connection.mqttSubscribe") }}</Button>
</form>
</div>
@@ -216,13 +218,13 @@ onUnmounted(() => {
</select>
</div>
</div>
<div class="flex-1 overflow-auto">
<div class="flex-1 overflow-auto flex flex-col">
<div v-if="messages.length === 0" class="text-xs text-muted-foreground p-4 text-center">{{ t("connection.mqttNoMessages") }}</div>
<div
v-for="(msg, i) in messages"
:key="i"
class="px-3 py-2 border-b last:border-b-0 cursor-pointer text-xs"
:class="msg.direction === 'sent' ? 'bg-emerald-50/60 dark:bg-emerald-950/20 hover:bg-emerald-100/60 dark:hover:bg-emerald-950/40' : 'hover:bg-muted/50'"
class="px-3 py-2 border-b last:border-b-0 cursor-pointer text-xs self-start w-full"
:class="msg.direction === 'sent' ? 'bg-emerald-50/60 dark:bg-emerald-950/20 hover:bg-emerald-100/60 dark:hover:bg-emerald-950/40 ml-auto max-w-[85%] rounded-l-md' : 'hover:bg-muted/50 mr-auto max-w-[85%] rounded-r-md'"
@click="handleTopicClick(msg.topic)"
>
<div class="flex items-center gap-2 mb-0.5">
@@ -32,7 +32,7 @@ const qosHints = computed(() => [t("connection.mqttQosAtMostOnce"), t("connectio
const payloadPlaceholder = computed(() => {
switch (encoding.value) {
case "json":
return t("connection.mqttPayloadPlaceholderJson");
return t("connection.mqttPayloadPlaceholderJson", { example: '{"key": "value"}' });
case "base64":
return t("connection.mqttPayloadPlaceholderBase64");
case "hex":