feat(M06-A): 建立MQTT连接与Broker基础

This commit is contained in:
Codex
2026-06-22 17:25:33 +08:00
parent a8c3e7d2fe
commit c140613718
15 changed files with 875 additions and 36 deletions
+6 -5
View File
@@ -13,9 +13,10 @@ port="${QIPAI_MQTT_PORT:-1883}"
protocol="${QIPAI_MQTT_PROTOCOL:-mqtt}"
mqtt_version="${QIPAI_MQTT_VERSION:-3.1.1}"
qos="${QIPAI_MQTT_QOS:-1}"
client_id="qipai-device-smoke-$(date +%s)"
allowed_publish_topic="${QIPAI_MQTT_ALLOWED_PUBLISH_TOPIC:-qipai/${client_id}/status}"
allowed_subscribe_topic="${QIPAI_MQTT_ALLOWED_SUBSCRIBE_TOPIC:-qipai/${client_id}/command/#}"
device_id="${QIPAI_MQTT_DEVICE_ID:-SMOKE_DEVICE}"
client_id="qipai-device-${device_id}"
allowed_publish_topic="${QIPAI_MQTT_ALLOWED_PUBLISH_TOPIC:-/devicesend/${device_id}}"
allowed_subscribe_topic="${QIPAI_MQTT_ALLOWED_SUBSCRIBE_TOPIC:-/deviceaccept/${device_id}}"
denied_subscribe_topic="${QIPAI_MQTT_DENIED_SUBSCRIBE_TOPIC:-\$SYS/#}"
enable_tls_smoke="${QIPAI_MQTT_ENABLE_TLS_SMOKE:-false}"
enable_will_smoke="${QIPAI_MQTT_ENABLE_WILL_SMOKE:-false}"
@@ -24,9 +25,9 @@ tls_host="${QIPAI_MQTT_TLS_HOST:-$host}"
tls_port="${QIPAI_MQTT_TLS_PORT:-8883}"
tls_protocol="${QIPAI_MQTT_TLS_PROTOCOL:-mqtts}"
tls_insecure="${QIPAI_MQTT_TLS_INSECURE:-false}"
will_topic="${QIPAI_MQTT_WILL_TOPIC:-qipai/${client_id}/will}"
will_topic="${QIPAI_MQTT_WILL_TOPIC:-/devicewill/${device_id}}"
will_message="${QIPAI_MQTT_WILL_MESSAGE:-{\"type\":\"will\",\"clientId\":\"${client_id}\"}}"
idempotency_topic="${QIPAI_MQTT_IDEMPOTENCY_TOPIC:-qipai/${client_id}/status}"
idempotency_topic="${QIPAI_MQTT_IDEMPOTENCY_TOPIC:-/devicesend/${device_id}}"
idempotency_key="${QIPAI_MQTT_IDEMPOTENCY_KEY:-smoke-${client_id}}"
echo "INFO: MQTT smoke target ${protocol}://${host}:${port}, version ${mqtt_version}, qos ${qos}"
+57
View File
@@ -5,6 +5,52 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib.sh
. "${SCRIPT_DIR}/lib.sh"
qipai_emqx_install_or_update() {
qipai_require_root_for_write
qipai_check_arch
if command -v mqttx >/dev/null 2>&1; then
qipai_fail "MQTTX must not be installed on the production server"
return 1
fi
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y curl ca-certificates gnupg lsb-release
if ! command -v emqx >/dev/null 2>&1; then
local installer
installer="/tmp/install-emqx-deb.sh"
curl -fsSL https://assets.emqx.com/scripts/install-emqx-deb.sh -o "$installer"
bash -n "$installer"
bash "$installer"
fi
apt-get update
apt-get install -y emqx
systemctl enable --now emqx
qipai_pass "EMQX package installed/updated and service enabled"
qipai_emqx_status
qipai_info "Authentication users and secrets must be created from /etc/qipai/qipai.secrets; they are never printed here."
qipai_info "Apply deploy/emqx/acl.conf.template only after creating qipai_backend_prod and device credentials."
}
qipai_emqx_manage() {
qipai_emqx_status
if command -v emqx >/dev/null 2>&1; then
printf "输入 U 更新 EMQX,直接回车仅查看状态: "
else
printf "输入 I 安装 EMQX,直接回车仅查看状态: "
fi
local choice
read -r choice || true
case "${choice}" in
I|i|U|u) qipai_emqx_install_or_update ;;
"") qipai_info "EMQX status check complete; no package changes requested" ;;
*) qipai_warn "unknown choice; no package changes made" ;;
esac
}
qipai_emqx_status() {
local acl_template authz_template
acl_template="${SCRIPT_DIR}/../../deploy/emqx/acl.conf.template"
@@ -20,6 +66,9 @@ qipai_emqx_status() {
if command -v emqx >/dev/null 2>&1; then
qipai_pass "emqx command exists: $(command -v emqx)"
emqx version 2>/dev/null || true
if command -v dpkg-query >/dev/null 2>&1; then
dpkg-query -W -f='${Package} ${Version} ${Architecture}\n' emqx 2>/dev/null || true
fi
else
qipai_warn "emqx command not found"
fi
@@ -45,6 +94,12 @@ qipai_emqx_status() {
fi
done
if command -v mqttx >/dev/null 2>&1; then
qipai_fail "MQTTX is installed on the server; production policy forbids it"
else
qipai_pass "MQTTX is not installed on the server"
fi
[ -f "$acl_template" ] && qipai_pass "ACL template exists" || qipai_warn "ACL template not found"
[ -f "$authz_template" ] && qipai_pass "authorization template exists" || qipai_warn "authorization template not found"
@@ -54,4 +109,6 @@ qipai_emqx_status() {
if [ "${1:-}" = "--run" ]; then
qipai_emqx_status
elif [ "${1:-}" = "--install" ]; then
qipai_emqx_install_or_update
fi