This commit is contained in:
2025-10-26 13:18:30 +08:00
parent 08bdd1664e
commit f00e01fd9e
3 changed files with 67 additions and 110 deletions

Binary file not shown.

View File

@@ -33,9 +33,9 @@ bp::child video_proc; // 视频推流进程
bp::child net_proc; // 开启网络进程 bp::child net_proc; // 开启网络进程
// 文件设置路径 // 文件设置路径
string filepath = "/home/orangepi/InitAuth/conf/.env"; string filepath = "../../InitAuth/conf/.env";
string cameraPath = "/opt/rknn-yolov11/.env"; string cameraPath = "/opt/rknn-yolov11/.env";
string passwd = "/home/orangepi/InitAuth/pwd/.env"; // string passwd = "/home/orangepi/InitAuth/pwd/.env";
// 云端Web认证接口 // 云端Web认证接口
const string url = "http://116.147.36.110:8095/device/validateDevice"; const string url = "http://116.147.36.110:8095/device/validateDevice";
@@ -62,18 +62,9 @@ struct OutSignal
bool inPutMode; // 触发输入模式 true--高电平,false -- 低电平 bool inPutMode; // 触发输入模式 true--高电平,false -- 低电平
} algor{}; } algor{};
// 确认是否已经进行过认证
bool ConfirmInit();
// 获取cpu序列号和SIM卡号
string GetCardInfo();
// 获取SIM卡号 // 获取SIM卡号
string GetSimICCID(const string &tty = "/dev/ttyUSB2"); string GetSimICCID(const string &tty = "/dev/ttyUSB2");
// 发送cpu序列号和SIM卡号
void SendCardInfo(mqtt::async_client &client);
// 开始进行设备验证 // 开始进行设备验证
bool verification(); bool verification();
@@ -117,29 +108,23 @@ int main(int argc, char *argv[])
blockAllSignals(); blockAllSignals();
signal(SIGINT, Exit); // 捕获Ctrl+C信号 signal(SIGINT, Exit); // 捕获Ctrl+C信号
// 开启验证
if (verification() == false)
{
// 验证失败,退出程序
return 0;
}
// 验证成功,启动程序
// 初始化mqtt // 初始化mqtt
mqttInit(); mqttInit();
// // // 开启服务 // 开启系统服务
// // 如果没有进行过初始化,发送物联网卡信息进行认证
// if (ConfirmInit() == false)
// {
// SendCardInfo(client);
// }
// else
// {
// if (verification() == false)
// {
// cerr << "验证失败" << endl;
// return 0;
// }
// }
// 开启服务
thread(StartService).detach(); thread(StartService).detach();
// 开启推流服务器
thread Rtsp(OpenRTSP); thread Rtsp(OpenRTSP);
// OpenRTSP();
std::this_thread::sleep_for(chrono::seconds(2)); // 等待RTSP服务器启动 std::this_thread::sleep_for(chrono::seconds(2)); // 等待RTSP服务器启动
// 进行RTSP视频推流 // 进行RTSP视频推流
@@ -147,7 +132,7 @@ int main(int argc, char *argv[])
while (isRunning) while (isRunning)
{ {
this_thread::sleep_for(1s); this_thread::sleep_for(1s); // 防止mpu沾满
} }
// Processing.join(); // Processing.join();
@@ -184,23 +169,10 @@ void getMsgCallback(mqtt::const_message_ptr msg)
{ {
string buffer = payload; string buffer = payload;
if (buffer.empty()) if (buffer.empty())
return; return;
// Pass: 更新 Init 标志并写入密码文件
if (buffer.find("Pass:") != string::npos)
{
WriteFile wf(filepath);
string str = "yse\n";
wf.overwriteAtPos(str, wf.countBytesPattern("InitOrNot:", true), str.size());
WriteFile wf2(passwd);
wf2.overwriteText(buffer.substr(5));
return;
}
// SET_DISTANCES: 解析 JSON 并更新 cameraPath 文件 // SET_DISTANCES: 解析 JSON 并更新 cameraPath 文件
if (buffer.find("SET_DISTANCES") != string::npos) if (buffer.find("SET_DISTANCES") != string::npos)
{ { //设置报警距离
auto res = nlohmann::json::parse(buffer); auto res = nlohmann::json::parse(buffer);
auto &danger_json = res["params"]["danger_distance"]; auto &danger_json = res["params"]["danger_distance"];
auto &warning_json = res["params"]["warning_distance"]; auto &warning_json = res["params"]["warning_distance"];
@@ -235,6 +207,7 @@ void getMsgCallback(mqtt::const_message_ptr msg)
out += lines[i]; out += lines[i];
if (i + 1 < lines.size()) out += "\n"; if (i + 1 < lines.size()) out += "\n";
} }
rf.Close();
WriteFile wf(cameraPath); WriteFile wf(cameraPath);
wf.overwriteText(out); wf.overwriteText(out);
} }
@@ -243,7 +216,7 @@ void getMsgCallback(mqtt::const_message_ptr msg)
// media: 更新 cameraPath 的 MEDIA_* 设置 // media: 更新 cameraPath 的 MEDIA_* 设置
if (buffer.find("media") != string::npos) if (buffer.find("media") != string::npos)
{ {//设置摄像头参数
CalculateInfo(media, buffer); CalculateInfo(media, buffer);
ReadFile rf(cameraPath); ReadFile rf(cameraPath);
if (!rf.Open()) { if (!rf.Open()) {
@@ -261,15 +234,15 @@ void getMsgCallback(mqtt::const_message_ptr msg)
out += lines[i]; out += lines[i];
if (i + 1 < lines.size()) out += "\n"; if (i + 1 < lines.size()) out += "\n";
} }
rf.Close();
WriteFile wf(cameraPath); WriteFile wf(cameraPath);
wf.overwriteText(out); wf.overwriteText(out);
} }
return; return;
} }
// algorithm: 更新 filepath 中的 outPutMode/inPutMode // algorithm: 更新 filepath 中的 outPutMode/inPutMode
if (buffer.find("algorithm") != string::npos) if (buffer.find("algorithm") != string::npos)
{ {//设置报警输出模式
CalculateInfo(algor, buffer); CalculateInfo(algor, buffer);
ReadFile rf2(filepath); ReadFile rf2(filepath);
if (!rf2.Open()) { if (!rf2.Open()) {
@@ -286,6 +259,7 @@ void getMsgCallback(mqtt::const_message_ptr msg)
out += lines[i]; out += lines[i];
if (i + 1 < lines.size()) out += "\n"; if (i + 1 < lines.size()) out += "\n";
} }
rf2.Close();
WriteFile wf2(filepath); WriteFile wf2(filepath);
wf2.overwriteText(out); wf2.overwriteText(out);
} }
@@ -317,15 +291,56 @@ void StartNet()
net_proc = bp::child("/bin/bash", bp::args = {"-c", commd}); net_proc = bp::child("/bin/bash", bp::args = {"-c", commd});
} }
// 进行验证 // 进行设备验证
bool verification() bool verification()
{ {
bool flag = false; // 是否通过验证 bool flag = false; // 是否通过验证
// 获取ICCID
string ICCID = GetSimICCID(); string ICCID = GetSimICCID();
// string ICCID = "89860324045100015440";
// 实时加密
string pass = MD5(ICCID);
auto res = NetRequest::QuickPostJson(url,ICCID); // 获取ICCID
string request = format(R"({"cardNo":"{}"})", pass);
auto res = NetRequest::QuickPostJson(url, request);
if (res->status == 200)
{
// 线上认证
auto json = nlohmann::json::parse(res->body);
if (json["code"] == 200)
{ // 验证成功
flag = true;
}
}
else
{
// 线下验证
ReadFile rf(filepath);
if (rf.Open() == false)
return false;
auto lines = rf.ReadLines();
string str;
for (auto &ii : lines)
{
if (ii.find("ServerPwd:") != string::npos)
{
str = ii.substr(sizeof("ServerPwd"));
break;
}
}
pass = format(R"("{}")", pass);
if (str == pass)
{ // 验证成功
flag = true;
}
rf.Close();
}
return flag; return flag;
} }
@@ -365,53 +380,6 @@ void VideoStream()
video_proc = bp::child("/bin/bash", bp::args = {"-c", commnd}); video_proc = bp::child("/bin/bash", bp::args = {"-c", commnd});
} }
// 确认是否已经进行过认证
bool ConfirmInit()
{
ReadFile *rf = new ReadFile(filepath);
if (rf->Open() == false)
{
cerr << "文件打开失败\n";
return false;
}
auto vct = rf->ReadLines();
for (auto &ii : vct)
{
if (ii.find("InitOrNot:no") != string::npos)
return false;
}
return true;
}
// 获取cpu序列号和SIM卡号
string GetCardInfo()
{
ReadFile *rf = new ReadFile("/proc/cpuinfo");
if (rf->Open() == false)
{
cerr << "文件打开失败\n";
return "";
}
auto lines = rf->ReadLines();
string res = "";
for (auto &ii : lines)
{
if (ii.find("Serial") != string::npos)
{
auto pos = ii.find(":");
if (pos != string::npos)
{
res = ii.substr(pos + 1);
break;
}
}
}
return LRtrim(res);
}
// 获取SIM卡号 // 获取SIM卡号
// 通过串口发送 AT+CCID 指令读取并解析返回的ICCID号 // 通过串口发送 AT+CCID 指令读取并解析返回的ICCID号
string GetSimICCID(const string &tty) string GetSimICCID(const string &tty)
@@ -474,17 +442,6 @@ string GetSimICCID(const string &tty)
return ""; // 多次重试失败,返回空 return ""; // 多次重试失败,返回空
} }
// 发送cpu序列号和SIM卡号
void SendCardInfo(mqtt::async_client &client)
{
string CardID = GetCardInfo();
string SIMID = GetSimICCID();
string info = CardID + SIMID;
// 发送信息
client.publish(Topic, info, Qos, false);
}
// 解析接收的数据 // 解析接收的数据
template <class T> template <class T>
void CalculateInfo(T &conf, const string &json) void CalculateInfo(T &conf, const string &json)

View File

@@ -1,8 +1,8 @@
all:wifi all:wifi
wifi:main.cpp wifi:main.cpp
g++ -g -o wifi main.cpp /home/orangepi/RKApp/ApCreate/NetraLib/src/Netra.cpp /home/orangepi/RKApp/ApCreate/NetraLib/src/encrypt.cpp /home/orangepi/RKApp/ApCreate/NetraLib/src/NetRequest.cpp -I/home/orangepi/RKApp/ApCreate/NetraLib/include -lpaho-mqttpp3 -lpaho-mqtt3a -lpthread g++ -g -o wifi main.cpp /home/orangepi/RKApp/softWareInit/NetraLib/src/Netra.cpp /home/orangepi/RKApp/softWareInit/NetraLib/src/encrypt.cpp /home/orangepi/RKApp/softWareInit/NetraLib/src/NetRequest.cpp -I/home/orangepi/RKApp/softWareInit/NetraLib/include -lpaho-mqttpp3 -lpaho-mqtt3a -lpthread
mv ./wifi ../bin/wifi mv ./wifi ../bin/wifi
clean: clean:
rm -rf wifi rm -rf ../bin/wifi