diff --git a/config.json b/config.json index 4cf2c92..60fcd65 100644 --- a/config.json +++ b/config.json @@ -4,11 +4,11 @@ "baud_rate": 9600, "mqtt": { "client_id": "weight_reader", - "host": "112.33.111.160", + "host": "10.180.4.100", "port": 1883, - "username": "auseft", - "password": "1q2w3E**", + "username": "admin", + "password": "Auseft@2025", "keep_alive_secs": 5, "topic_prefix": "weight/data" } -} +} \ No newline at end of file diff --git a/install.sh b/install.sh deleted file mode 100644 index 3376127..0000000 --- a/install.sh +++ /dev/null @@ -1,122 +0,0 @@ -#!/bin/bash - -# 确保脚本以root权限运行 -if [ "$EUID" -ne 0 ]; then - echo "请使用root权限运行此脚本" - exit 1 -fi - -SERVICE_NAME="weight-reader" -INSTALL_DIR="/opt/weight-reader" -SERVICE_FILE="/etc/systemd/system/weight-reader.service" - -# 创建安装目录 -create_install_dir() { - echo "创建安装目录..." - mkdir -p "$INSTALL_DIR" - chmod 755 "$INSTALL_DIR" -} - -# 复制文件 -copy_files() { - echo "复制文件..." - cp weight_reader "$INSTALL_DIR/" - cp config.json "$INSTALL_DIR/" - cp weight-reader.service "$SERVICE_FILE" - chmod 755 "$INSTALL_DIR/weight_reader" - chmod 644 "$INSTALL_DIR/config.json" - chmod 644 "$SERVICE_FILE" -} - -# 安装服务 -install_service() { - echo "安装服务..." - systemctl daemon-reload - systemctl enable $SERVICE_NAME - systemctl start $SERVICE_NAME - echo "服务已安装并启动" -} - -# 卸载服务 -uninstall_service() { - echo "停止并卸载服务..." - systemctl stop $SERVICE_NAME - systemctl disable $SERVICE_NAME - rm -f "$SERVICE_FILE" - rm -rf "$INSTALL_DIR" - systemctl daemon-reload - echo "服务已卸载" -} - -# 启动服务 -start_service() { - echo "启动服务..." - systemctl start $SERVICE_NAME - echo "服务已启动" -} - -# 停止服务 -stop_service() { - echo "停止服务..." - systemctl stop $SERVICE_NAME - echo "服务已停止" -} - -# 重启服务 -restart_service() { - echo "重启服务..." - systemctl restart $SERVICE_NAME - echo "服务已重启" -} - -# 查看服务状态 -status_service() { - systemctl status $SERVICE_NAME -} - -# 显示使用帮助 -show_help() { - echo "使用方法: $0 [命令]" - echo "命令:" - echo " install - 安装并启动服务" - echo " uninstall - 停止并卸载服务" - echo " start - 启动服务" - echo " stop - 停止服务" - echo " restart - 重启服务" - echo " status - 查看服务状态" - echo " help - 显示此帮助信息" -} - -# 主程序 -case "$1" in - "install") - create_install_dir - copy_files - install_service - ;; - "uninstall") - uninstall_service - ;; - "start") - start_service - ;; - "stop") - stop_service - ;; - "restart") - restart_service - ;; - "status") - status_service - ;; - "help"|"") - show_help - ;; - *) - echo "未知命令: $1" - show_help - exit 1 - ;; -esac - -exit 0 diff --git a/server-cert.pem b/server-cert.pem new file mode 100644 index 0000000..e69de29 diff --git a/server-cert.pemopenssl b/server-cert.pemopenssl new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs index 66a496c..4652a9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,6 +75,14 @@ async fn get_mac() -> HttpResponse { let response = MacResponse { mac_address: mac, }; + // 在新线程中运行MQTT和串口服务 + let mqtt_handle = tokio::spawn(async { + if let Err(e) = run_mqtt_and_serial().await { + eprintln!("MQTT/串口服务错误: {}", e); + } + }); + + HttpResponse::Ok().json(response) } @@ -114,13 +122,7 @@ async fn main() -> Result<()> { println!("HTTP服务器已启动,监听在 http://127.0.0.1:8080"); - // 在新线程中运行MQTT和串口服务 - let mqtt_handle = tokio::spawn(async { - if let Err(e) = run_mqtt_and_serial().await { - eprintln!("MQTT/串口服务错误: {}", e); - } - }); - + // 等待HTTP服务器结束 server.await?; diff --git a/weight-reader.service b/weight-reader.service deleted file mode 100644 index 9a8f0c0..0000000 --- a/weight-reader.service +++ /dev/null @@ -1,14 +0,0 @@ -[Unit] -Description=Weight Reader Service -After=network.target - -[Service] -Type=simple -User=root -WorkingDirectory=/opt/weight-reader -ExecStart=/opt/weight-reader/weight_reader -Restart=always -RestartSec=3 - -[Install] -WantedBy=multi-user.target