Przeglądaj źródła

添加mac地址

dev
OCEAN 3 miesięcy temu
rodzic
commit
9f4ed18fea
3 zmienionych plików z 2017 dodań i 308 usunięć
  1. +1954
    -304
      Cargo.lock
  2. +4
    -0
      Cargo.toml
  3. +59
    -4
      src/main.rs

+ 1954
- 304
Cargo.lock
Plik diff jest za duży
Wyświetl plik


+ 4
- 0
Cargo.toml Wyświetl plik

@@ -9,6 +9,10 @@ anyhow = "1.0"
rumqttc = "0.23"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
actix-web = "4.4"
actix-cors = "0.6"
tokio = { version = "1.0", features = ["full"] }
reqwest = { version = "0.11", features = ["blocking", "json"] }

[profile.release]
opt-level = 3


+ 59
- 4
src/main.rs Wyświetl plik

@@ -7,6 +7,8 @@ use std::time::Duration;
use std::thread;
use serialport;
use std::process::Command;
use actix_web::{web, App, HttpServer, HttpResponse};
use actix_cors::Cors;

#[derive(Serialize, Deserialize)]
struct WeightData {
@@ -14,6 +16,11 @@ struct WeightData {
timestamp: u64,
}

#[derive(Serialize)]
struct MacResponse {
mac_address: String,
}

fn get_mac_address() -> String {
let output = Command::new("getmac")
.arg("/fo")
@@ -28,10 +35,58 @@ fn get_mac_address() -> String {
mac.replace('-', ":").to_lowercase()
}

fn main() -> Result<()> {
// HTTP处理函数
async fn get_mac() -> HttpResponse {
let mac = get_mac_address();
println!("HTTP请求:获取MAC地址 = {}", mac); // 添加日志
let response = MacResponse {
mac_address: mac,
};
HttpResponse::Ok().json(response)
}

#[actix_web::main]
async fn main() -> Result<()> {
println!("程序启动...");
// 启动HTTP服务器
println!("正在启动HTTP服务器...");
// 创建HTTP服务器
let server = HttpServer::new(|| {
// 配置CORS
let cors = Cors::default()
.allow_any_origin() // 允许所有来源
.allow_any_method() // 允许所有HTTP方法
.allow_any_header() // 允许所有请求头
.max_age(3600); // 设置预检请求的缓存时间(秒)
App::new()
.wrap(cors) // 添加CORS中间件
.route("/mac", web::get().to(get_mac))
})
.bind(("0.0.0.0", 8080))?
.run();
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?;
Ok(())
}

// MQTT和串口处理函数
async fn run_mqtt_and_serial() -> Result<()> {
// 创建 MQTT 客户端
// let mut mqttopts = MqttOptions::new("weight_reader", "192.168.0.100", 1883);
let mut mqttopts = MqttOptions::new("weight_reader", "112.33.111.160", 1883);
let mut mqttopts = MqttOptions::new("weight_reader", "112.33.111.160", 1883);
mqttopts.set_keep_alive(Duration::from_secs(5));
mqttopts.set_credentials("auseft", "1q2w3E**");
let (mut client, mut connection) = Client::new(mqttopts, 10);
@@ -40,7 +95,7 @@ fn main() -> Result<()> {
thread::spawn(move || {
for notification in connection.iter() {
match notification {
Ok(notification) => {
Ok(_) => {
// if(!notification.contains("PingRe")) {
// println!("MQTT事件: {:?}", notification);
// }


Ładowanie…
Anuluj
Zapisz