浏览代码

优化

dev
test 3 个月前
父节点
当前提交
d8d3973641
共有 6 个文件被更改,包括 13 次插入147 次删除
  1. +4
    -4
      config.json
  2. +0
    -122
      install.sh
  3. +0
    -0
      server-cert.pem
  4. +0
    -0
      server-cert.pemopenssl
  5. +9
    -7
      src/main.rs
  6. +0
    -14
      weight-reader.service

+ 4
- 4
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"
}
}
}

+ 0
- 122
install.sh 查看文件

@@ -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

+ 0
- 0
server-cert.pem 查看文件


+ 0
- 0
server-cert.pemopenssl 查看文件


+ 9
- 7
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?;


+ 0
- 14
weight-reader.service 查看文件

@@ -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

正在加载...
取消
保存