yanzheng
This commit is contained in:
Binary file not shown.
@@ -22,10 +22,12 @@ const string envPath = "/home/orangepi/RKApp/InitAuth/conf/.env";
|
||||
// MQTT相关配置
|
||||
const string mqtt_url = "tcp://192.168.12.1:1883";
|
||||
const string clientId = "RK3588_SubTest";
|
||||
const string Topic = "/bsd_camera/cmd";
|
||||
const string TopicRecv = "/bsd_camera/cmd"; // 接收手机传来的信息
|
||||
const string TopicSend = "/bsd_camera/init"; // 发送的话题
|
||||
const int Qos = 1;
|
||||
|
||||
atomic<bool> isRunning(true);
|
||||
atomic<bool> Deactivate(false); // 激活标志
|
||||
|
||||
mqtt::async_client client(mqtt_url, clientId);
|
||||
|
||||
@@ -45,12 +47,17 @@ void messageCallback(mqtt::const_message_ptr msg);
|
||||
// 检测设备是否已进行初始化
|
||||
bool checkUUID();
|
||||
|
||||
// 退出程序,开始进行设备验证
|
||||
void StartCheck();
|
||||
|
||||
int main()
|
||||
{
|
||||
if (checkUUID())
|
||||
Deactivate = checkUUID(); // 判断是否已被激活
|
||||
if (Deactivate)
|
||||
{
|
||||
// 设备已被激活,调用验证程序,退出本程序
|
||||
|
||||
cout << "设备已激活,开始验证" << endl;
|
||||
StartCheck();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -64,6 +71,14 @@ int main()
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 退出程序,开始进行设备验证
|
||||
void StartCheck()
|
||||
{
|
||||
isRunning = false; // 退出程序
|
||||
string cmd = "../../softWareInit/bin/wifi ";
|
||||
system(cmd.c_str());
|
||||
}
|
||||
|
||||
// 检测设备是否已经激活
|
||||
bool checkUUID()
|
||||
{
|
||||
@@ -98,19 +113,21 @@ void messageCallback(mqtt::const_message_ptr msg)
|
||||
{
|
||||
// 接受App传来的密文,并保存在配置文件中
|
||||
auto buffer = msg->to_string();
|
||||
cout << "msg: " << buffer << endl;
|
||||
// cout << "Topic:" << msg->get_topic() << ",msg:" << buffer << endl;
|
||||
if (buffer.find("Activate") != string::npos)
|
||||
{
|
||||
// 接受请求,发送SIM卡号
|
||||
string ICCID = GetSimICCID();
|
||||
client.publish(Topic, ICCID, Qos, false); // 不保存
|
||||
string deviceId = format("{\"deviceUid\":\"{}\"}", ICCID);
|
||||
// cout << "ICCID:" << deviceId << endl;
|
||||
client.publish(TopicSend, deviceId, Qos, false); // 不保存
|
||||
}
|
||||
else if (buffer.find("ServerPwd") != string::npos)
|
||||
{
|
||||
// 接受UUID,保存至配置文件中,退出程序,调用设备验证程序
|
||||
auto res = nlohmann::json::parse(buffer); // 准备解析接受到的秘钥
|
||||
auto pwd = res["Data"];
|
||||
cout << pwd << endl;
|
||||
auto pwd = res["Data"];
|
||||
// cout << pwd << endl;
|
||||
// 写入文件
|
||||
ReadFile rf(envPath);
|
||||
|
||||
@@ -127,16 +144,23 @@ void messageCallback(mqtt::const_message_ptr msg)
|
||||
{
|
||||
WriteFile wf(envPath);
|
||||
string out;
|
||||
out.resize(1024);
|
||||
out.reserve(1024);
|
||||
for (size_t i = 0; i < lines.size(); ++i)
|
||||
{
|
||||
out += lines[i];
|
||||
if (i + 1 < lines.size())
|
||||
out += "\n";
|
||||
}
|
||||
wf.overwriteText(out); })
|
||||
wf.overwriteText(out);
|
||||
|
||||
Deactivate =true; })
|
||||
.detach();
|
||||
}
|
||||
|
||||
if (Deactivate)
|
||||
{ // 已激活
|
||||
StartCheck();
|
||||
}
|
||||
}
|
||||
|
||||
// 连接成功的回调
|
||||
@@ -150,8 +174,8 @@ void mqttInit()
|
||||
client.set_message_callback(messageCallback);
|
||||
client.set_connected_handler(connectCallback);
|
||||
|
||||
client.connect()->wait(); // 进行连接
|
||||
client.subscribe(Topic, Qos)->wait(); // 订阅话题
|
||||
client.connect()->wait(); // 进行连接
|
||||
client.subscribe(TopicRecv, Qos)->wait(); // 订阅话题
|
||||
}
|
||||
|
||||
// 获取SIM卡号
|
||||
|
||||
Reference in New Issue
Block a user