147 lines
3.5 KiB
Markdown
147 lines
3.5 KiB
Markdown
# Device SDK for C++
|
|
|
|
## 依赖
|
|
- libcurl
|
|
- nlohmann/json
|
|
- OpenSSL
|
|
|
|
### Windows
|
|
1. vcpkg 安装依赖:
|
|
```bash
|
|
vcpkg install curl:x64-windows
|
|
vcpkg install openssl:x64-windows
|
|
vcpkg install nlohmann-json:x64-windows
|
|
```
|
|
### Linux
|
|
1. Ubuntu/Debian:
|
|
```bash
|
|
sudo apt-get install libcurl4-openssl-dev libssl-dev nlohmann-json-dev
|
|
```
|
|
2. CentOS/RHEL:
|
|
```bash
|
|
sudo yum install libcurl-devel openssl-devel nlohmann-json-devel
|
|
```
|
|
|
|
## 编译
|
|
### Windows (Visual Studio)
|
|
1. 添加包含目录:
|
|
- $(vcpkg_root)/installed/x64-windows/include
|
|
2. 添加库目录:
|
|
- $(vcpkg_root)/installed/x64-windows/lib
|
|
3. 添加依赖库:
|
|
- libcurl
|
|
- libssl
|
|
- libcrypto
|
|
|
|
### Linux
|
|
|
|
```bash
|
|
g++ -o example example.cpp device_sdk.cpp -lcurl -lssl -lcrypto -I/usr/include/nlohmann
|
|
```
|
|
|
|
## 基本用法
|
|
### 初始化
|
|
|
|
```cpp
|
|
#include "device_sdk.h"
|
|
DeviceClient client("http://localhost:8080", "your-32-byte-encrypt-key-here123456");
|
|
```
|
|
### 设备注册
|
|
```cpp
|
|
// 不带授权码注册
|
|
DeviceInfo device;
|
|
device.uid = "device-001";
|
|
device.device_model = "test-model";
|
|
std::string error;
|
|
if (!client.registerDevice(device, error)) {
|
|
std::cerr << "注册失败: " << error << std::endl;
|
|
return;
|
|
}
|
|
// 带授权码注册
|
|
DeviceInfo deviceWithLicense;
|
|
deviceWithLicense.uid = "device-002";
|
|
deviceWithLicense.device_model = "test-model";
|
|
deviceWithLicense.license_code = "your-license-code";
|
|
if (!client.registerDevice(deviceWithLicense, error)) {
|
|
std::cerr << "注册失败: " << error << std::endl;
|
|
return;
|
|
}
|
|
```
|
|
### 设备验证
|
|
```cpp
|
|
ValidateResponse response;
|
|
std::string error;
|
|
if (!client.validateDevice("device-001", response, error)) {
|
|
std::cerr << "验证失败: " << error << std::endl;
|
|
return;
|
|
}
|
|
std::cout << "设备状态: " << response.status << std::endl;
|
|
std::cout << "授权类型: " << response.license_type << std::endl;
|
|
std::cout << "过期时间: " << response.expire_time << std::endl;
|
|
```
|
|
|
|
### 更新启动次数
|
|
|
|
```cpp
|
|
int count;
|
|
std::string error;
|
|
if (!client.updateStartCount("device-001", count, error)) {
|
|
std::cerr << "更新失败: " << error << std::endl;
|
|
return;
|
|
}
|
|
std::cout << "当前启动次数: " << count << std::endl;
|
|
```
|
|
### 绑定授权码
|
|
```cpp
|
|
std::string error;
|
|
if (!client.bindLicense("device-001", "license-code-123", error)) {
|
|
std::cerr << "绑定失败: " << error << std::endl;
|
|
return;
|
|
}
|
|
```
|
|
## 完整示例
|
|
```cpp
|
|
#include <iostream>
|
|
#include "device_sdk.h"
|
|
int main() {
|
|
try {
|
|
DeviceClient client("http://localhost:8080", "your-32-byte-encrypt-key-here123456");
|
|
std::string error;
|
|
// 注册设备
|
|
DeviceInfo device;
|
|
device.uid = "device-001";
|
|
device.device_model = "test-model";
|
|
if (!client.registerDevice(device, error)) {
|
|
std::cerr << "注册失败: " << error << std::endl;
|
|
return 1;
|
|
}
|
|
std::cout << "设备注册成功" << std::endl;
|
|
// 绑定授权码
|
|
if (!client.bindLicense("device-001", "license-code-123", error)) {
|
|
std::cerr << "绑定失败: " << error << std::endl;
|
|
return 1;
|
|
}
|
|
std::cout << "授权码绑定成功" << std::endl;
|
|
// 验证设备
|
|
ValidateResponse response;
|
|
if (!client.validateDevice("device-001", response, error)) {
|
|
std::cerr << "验证失败: " << error << std::endl;
|
|
return 1;
|
|
}
|
|
std::cout << "设备状态: " << response.status << std::endl;
|
|
std::cout << "授权类型: " << response.license_type << std::endl;
|
|
// 更新启动次数
|
|
int count;
|
|
if (!client.updateStartCount("device-001", count, error)) {
|
|
std::cerr << "更新失败: " << error << std::endl;
|
|
return 1;
|
|
}
|
|
std::cout << "当前启动次数: " << count << std::endl;
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "发生错误: " << e.what() << std::endl;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
```
|