Skip to main content

ai_cortex_sdk/
device.rs

1use serde::Serialize;
2
3/// 采集到的设备身份信息
4#[derive(Debug, Clone)]
5pub struct DeviceIdentity {
6    /// 设备指纹(直接取 machine_uid),作为设备唯一标识上送服务端
7    pub fingerprint: String,
8    /// 设备原始信息(JSON 字符串,随请求上送,便于后台展示)
9    pub info: String,
10}
11
12#[derive(Debug, Clone, Serialize)]
13struct DeviceInfo {
14    os: &'static str,
15    machine_uid: String,
16}
17
18/// 采集当前机器的设备指纹。
19///
20/// 优先调用 `machine_uid`(Linux: /etc/machine-id,Windows: MachineGuid,macOS: IOPlatformUUID),
21/// 直接以其返回值作为稳定指纹。采集失败时回退为固定占位串,保证链路不中断
22/// (此场景下指纹稳定性下降,但不会阻塞心跳/绑定)。
23pub fn collect() -> DeviceIdentity {
24    let uid = machine_uid::get().unwrap_or_else(|_| "unknown-device".to_string());
25
26    let info = DeviceInfo {
27        os: std::env::consts::OS,
28        machine_uid: uid.clone(),
29    };
30    let info_json = serde_json::to_string(&info).unwrap_or_else(|_| "{}".to_string());
31
32    DeviceIdentity {
33        fingerprint: uid,
34        info: info_json,
35    }
36}