main.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. #include <main.h>
  2. //#include "userfunc.h"
  3. #include <cameramanager.h>
  4. #include <mqtthandler.h>
  5. #include <mserial.h>
  6. #include <mysql.h>
  7. #include <EasyTableModel.h>
  8. #include <customitem.h>
  9. #include <m_loger.h>
  10. #include <TreeModel.h>
  11. #include <app_curlFunc.h>
  12. #include <wif_service.h>
  13. #include <QQuickWindow>
  14. //uncomment this line to add the Live Client Module and use live reloading with your custom C++ code
  15. //#include <FelgoLiveClient>
  16. class param;
  17. QString param::table_mqtt = "mqtt"; //mqtt配置表
  18. QString param::table_mqtt_other = "mqtt_other"; //mqtt对接配置表
  19. QString param::table_ftp = "ftp"; //FTP配置表
  20. QString param::table_ftp_other = "ftp_other"; //FTP对接配置表
  21. QString param::table_camera = "camera"; //相机参数配置表
  22. QString param::table_location = "location"; //定位信息参数配置表
  23. QString param::table_config = "config"; //参数配置表
  24. void traverseQmlObject(QObject *qmlObject, int depth = 0) {
  25. QString indentation = QString(" ").repeated(depth);
  26. DEBUG("{} Type:{} Name:{}", indentation.toStdString(), qmlObject->metaObject()->className(), qmlObject->objectName().toStdString());
  27. for (QObject *child : qmlObject->children()) {
  28. traverseQmlObject(child, depth + 1);
  29. }
  30. }
  31. class MyCppClass : public QObject {
  32. Q_OBJECT
  33. public:
  34. void setQmlRootObject(QObject *rootObject) {
  35. m_qmlRootObject = rootObject;
  36. }
  37. void JumpToCamera(){
  38. }
  39. public slots:
  40. void qmlSlot() {
  41. LOG("Received from QML");
  42. emit recvqmlslot();
  43. }
  44. void systemPage(){
  45. LOG("systemPage");
  46. emit tosystempage();
  47. }
  48. void openbar_btnClicked(){
  49. LOG("openbar_btnClicked");
  50. emit opensystembar();
  51. }
  52. void closebar_btnClicked(){
  53. LOG("closebar_btnClicked");
  54. emit closesystembar();
  55. }
  56. void opennav_btnClicked(){
  57. LOG("opennav_btnClicked");
  58. emit opensystemnav();
  59. }
  60. void closenav_btnClicked(){
  61. LOG("closenav_btnClicked");
  62. emit closesystemnav();
  63. }
  64. signals:
  65. void recvqmlslot();
  66. void tosystempage();
  67. void opensystembar();
  68. void closesystembar();
  69. void opensystemnav();
  70. void closesystemnav();
  71. private:
  72. QObject *m_qmlRootObject = nullptr;
  73. };
  74. class GPSInfo : public QObject
  75. {
  76. Q_OBJECT
  77. public:
  78. double nowGroundSpeed=0.0;
  79. double nowVerticalSpeed=0.0;
  80. double nowMagneticVariation=0.0;
  81. double nowHorizontalAccuracy=0.0;
  82. double nowVerticalAccuracy=0.0;
  83. double nowLongitude=0.0;
  84. double nowLatitude=0.0;
  85. double nowDirection=0.0;
  86. QString nowTimes="";
  87. int recv_cnt = 0;
  88. QGeoPositionInfoSource *positionSource;
  89. public:
  90. GPSInfo(SqliteOperator *db,QObject *parent = nullptr)
  91. : QObject(parent) // 注入外部数据库对象
  92. , gpsConfig(db)
  93. {
  94. param::gps_state = "0";
  95. positionSource = QGeoPositionInfoSource::createDefaultSource(this);
  96. //QGeoSatelliteInfoSource *satelliteSource = QGeoSatelliteInfoSource::createDefaultSource(this);
  97. if (positionSource) {
  98. connect(positionSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
  99. this, SLOT(positionUpdated(QGeoPositionInfo)));
  100. positionSource->setUpdateInterval(1000);//设置刷新时间 单位毫秒
  101. positionSource->startUpdates();//启动设备
  102. }
  103. }
  104. signals:
  105. void position_success();
  106. private:
  107. SqliteOperator *gpsConfig = nullptr; // 不再在内部创建
  108. private slots://槽函数中实现了GPS部分信息的读取
  109. void positionUpdated(const QGeoPositionInfo &info)
  110. {
  111. nowLongitude = info.coordinate().longitude();//经度
  112. nowLatitude = info.coordinate().latitude();//纬度
  113. nowDirection = info.attribute(QGeoPositionInfo::Direction);
  114. nowGroundSpeed = info.attribute(QGeoPositionInfo::GroundSpeed);
  115. nowVerticalSpeed = info.attribute(QGeoPositionInfo::VerticalSpeed);
  116. nowMagneticVariation = info.attribute(QGeoPositionInfo::MagneticVariation);
  117. nowHorizontalAccuracy = info.attribute(QGeoPositionInfo::HorizontalAccuracy);
  118. nowVerticalAccuracy = info.attribute(QGeoPositionInfo::VerticalAccuracy);
  119. nowTimes = info.timestamp().toString();
  120. if(param::gps_lat.contains(".")&&param::gps_lat.size()>3){
  121. param::gps_state = "1";
  122. recv_cnt++;
  123. if(recv_cnt==20){ //定位成功后120S把定位信息保存到数据库中
  124. emit position_success();
  125. gpsConfig->OpenDb();
  126. if(gpsConfig->isTableExist(param::table_location)){
  127. gpsConfig->UpdateData(param::table_location,"gps_lat",param::gps_lat);
  128. gpsConfig->UpdateData(param::table_location,"gps_lng",param::gps_lng);
  129. }
  130. gpsConfig->CloseDb();
  131. positionSource->stopUpdates();
  132. }
  133. }
  134. param::gps_lat = QString::number(nowLatitude);
  135. param::gps_lng = QString::number(nowLongitude);
  136. LOG("经度:{}",nowLongitude);
  137. LOG("纬度:{}",nowLatitude);
  138. LOG("角度:{}",nowDirection);
  139. LOG("地表速度:{}",nowGroundSpeed);
  140. LOG("垂直速度:{}",nowVerticalSpeed);
  141. LOG("磁偏角:{}",nowMagneticVariation);
  142. LOG("水平精度:{}",nowHorizontalAccuracy);
  143. LOG("垂直精度:{}",nowVerticalAccuracy);
  144. LOG("定位次数:{}",recv_cnt);
  145. }
  146. };
  147. void sendBroadcastFromQt(QString btn_type) {
  148. auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context()); //activity
  149. //QJniObject qtContext = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;");
  150. if (qtContext.isValid()) {
  151. if(btn_type=="sleep"){
  152. QJniObject jClass("com/yfkj/bzyv1/Backlight");
  153. //jboolean ret = jClass.callMethod<jboolean>("checkWriteSettingPermission","(Landroid/content/Context;I)Z",qtContext.object(),100);
  154. jClass.callMethod<void>("setScreenLight","(Landroid/content/Context;I)V",qtContext.object(),100);
  155. }
  156. //QJniObject jClass("com/yfkj/bzyv1/StatusBarCtr");
  157. QJniObject jClass("com/yfkj/bzyv1/Backlight");
  158. if(btn_type=="openbar_btn"){
  159. jClass.callMethod<void>("setStatusBar","(Landroid/content/Context;I)V",qtContext.object(),1);
  160. }else if(btn_type=="closebar_btn"){
  161. jClass.callMethod<void>("setStatusBar","(Landroid/content/Context;I)V",qtContext.object(),0);
  162. }else if(btn_type=="closenav_btn"){
  163. jClass.callMethod<void>("setNavigationBar","(Landroid/content/Context;I)V",qtContext.object(),0);
  164. }else if(btn_type=="opennav_btn"){
  165. jClass.callMethod<void>("setNavigationBar","(Landroid/content/Context;I)V",qtContext.object(),1);
  166. }else if(btn_type=="imei"){
  167. QJniObject jClass("com/yfkj/bzyv1/StatusBarCtr");
  168. QJniObject java_imei= jClass.callObjectMethod("getAndroidIMEI","(Landroid/content/Context;)Ljava/lang/String;",qtContext.object());
  169. if(java_imei.isValid()){
  170. QString imei = java_imei.toString();
  171. if(!imei.isEmpty()&&param::imei.isEmpty()){
  172. param::imei = imei;
  173. }
  174. LOG("IMEI:{}",param::imei.toStdString());
  175. }
  176. }else if(btn_type=="csq"){
  177. jint rsrp = QJniObject::callStaticMethod<jint>("com/yfkj/bzyv1/StatusBarCtr",
  178. "getRsrp",
  179. "(Landroid/content/Context;)I",
  180. qtContext.object());
  181. LOG("getRsrp:{}",rsrp);
  182. }else if(btn_type=="iccid"){
  183. QJniObject jClass("com/yfkj/bzyv1/StatusBarCtr");
  184. QJniObject iccid_java = jClass.callObjectMethod("getICCID","(Landroid/content/Context;)Ljava/lang/String;",qtContext.object());
  185. if(iccid_java.isValid()){
  186. QString iccid = iccid_java.toString();
  187. if(!iccid.isEmpty()){
  188. param::iccid = iccid;
  189. }
  190. LOG("ICCID:{}",param::iccid.toStdString());
  191. }
  192. }else if(btn_type=="install"){
  193. QJniObject jClass("com/yfkj/bzyv1/StatusBarCtr");
  194. DEBUG("install process start!");
  195. jClass.callMethod<void>("installApp","(Landroid/content/Context;)V",qtContext.object());
  196. }else if(btn_type=="rebootSystem"){
  197. jClass.callMethod<void>("rebootSystem","(Landroid/content/Context;)V",qtContext.object());
  198. }else if(btn_type=="service"){
  199. DEBUG("启动android服务");
  200. QAndroidIntent serviceIntent(qtContext.object(),
  201. "com/yfkj/bzyv1/QtAndroidService");
  202. QJniObject result = qtContext.callObjectMethod(
  203. "startService",
  204. "(Landroid/content/Intent;)Landroid/content/ComponentName;",
  205. serviceIntent.handle().object());
  206. }
  207. }
  208. }
  209. // 定义函数:设置每日8:00重启的定时器
  210. auto setDailyRebootTimer = []() {
  211. // 获取当前时间(本地时间)
  212. QDateTime currentTime = QDateTime::currentDateTime();
  213. // 构造当天8:00的目标时间(基于本地时间的0点+8小时)
  214. QDateTime targetTime = currentTime.date().startOfDay().addSecs(8 * 3600); // 8:00 = 8 * 3600秒
  215. // 如果当前时间已过今日8:00,目标时间调整为次日8:00
  216. if (currentTime >= targetTime) {
  217. targetTime = targetTime.addDays(1); // 加1天
  218. }
  219. // 计算当前时间到目标时间的毫秒间隔
  220. qint64 intervalMs = currentTime.msecsTo(targetTime);
  221. // 设置单次定时器,触发后执行重启逻辑,并重新设置下一日的定时器
  222. QTimer::singleShot(intervalMs, []() {
  223. LOG("触发每日8:00重启");
  224. sendBroadcastFromQt("rebootSystem"); // 发送重启广播
  225. // 注意:若需要严格每日执行,建议在重启前重新设置定时器(根据实际需求)
  226. // 若系统重启后会自动启动本程序,可省略此处(程序重启后会重新设置定时器)
  227. // setDailyRebootTimer();
  228. });
  229. };
  230. //adb shell input keyevent 26 // 224 关
  231. // 构建权限列表
  232. QStringList m_permissions = {
  233. "android.permission.WRITE_EXTERNAL_STORAGE",
  234. "android.permission.READ_EXTERNAL_STORAGE",
  235. "android.permission.MANAGE_EXTERNAL_STORAGE",
  236. "android.permission.ACCESS_MEDIA_LOCATION",
  237. "android.permission.CAMERA",
  238. "android.permission.ACCESS_FINE_LOCATION",
  239. "android.permission.ACCESS_COARSE_LOCATION",
  240. "android.permission.WRITE_SETTINGS",
  241. "android.permission.READ_PHONE_STATE",
  242. "android.permission.INTERNET",
  243. "android.permission.ACCESS_NETWORK_STATE",
  244. "android.permission.WAKE_LOCK",
  245. "android.permission.VIBRATE"
  246. };
  247. // ========== 全志A133P缓存清理函数(关键)==========
  248. void clearAllCaches() {
  249. #ifndef WIN32
  250. QDir dir;
  251. // ========== 1. 标准缓存位置 ==========
  252. QStringList cacheLocations = {
  253. QStandardPaths::writableLocation(QStandardPaths::CacheLocation),
  254. QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation),
  255. // QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation),
  256. // QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)
  257. };
  258. // ========== 2. Android特定缓存位置 ==========
  259. cacheLocations << QDir::homePath() + "/.cache";
  260. cacheLocations << QDir::homePath() + "/.local/share";
  261. // ========== 3. 所有需要清理的缓存目录 ==========
  262. QStringList cacheDirs = {
  263. "qmlsc", "qml", "rhi", "scenegraph", "shaders", "pipelines",
  264. "graphicsshadercache", "QtShaderCache", "QtQuick", "cache",
  265. "Qt", "shadercache", "textures", "graphics", "RenderCache",
  266. "ShaderCache", "TextureCache"
  267. };
  268. QStringList cacheFileFilters = {"*.qmlc", "*.jsc", "*.cache", "*.tmp"};
  269. // ========== 4. 遍历并清理 ==========
  270. for (const QString &cachePath : cacheLocations) {
  271. QDir cacheDir(cachePath);
  272. if (cacheDir.exists()) {
  273. DEBUG("Scanning cache location: {}", cachePath.toStdString());
  274. // 清理指定目录
  275. for (const QString &subDir : cacheDirs) {
  276. QDir sub(cacheDir.filePath(subDir));
  277. if (sub.exists()) {
  278. sub.removeRecursively();
  279. DEBUG("Cleared cache directory: {}", sub.absolutePath().toStdString());
  280. }
  281. }
  282. // 清理匹配的缓存文件
  283. cacheDir.setNameFilters(cacheFileFilters);
  284. QStringList files = cacheDir.entryList(QDir::Files);
  285. for (const QString &file : files) {
  286. cacheDir.remove(file);
  287. DEBUG("Removed cache file: {}", cacheDir.filePath(file).toStdString());
  288. }
  289. }
  290. }
  291. // ========== 5. 清理临时文件 ==========
  292. QDir tempDir(QDir::tempPath());
  293. if (tempDir.exists()) {
  294. QStringList filters;
  295. filters << "Qt*" << "qml*" << "shader*" << "rhi*";
  296. tempDir.setNameFilters(filters);
  297. QStringList tempFiles = tempDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  298. for (const QString &tempFile : tempFiles) {
  299. QFileInfo info(tempDir.filePath(tempFile));
  300. if (info.isDir()) {
  301. QDir(tempDir.filePath(tempFile)).removeRecursively();
  302. } else {
  303. tempDir.remove(tempFile);
  304. }
  305. DEBUG("Removed temp file/dir: {}", tempDir.filePath(tempFile).toStdString());
  306. }
  307. }
  308. DEBUG("All caches cleared successfully!");
  309. #endif
  310. }
  311. int main(int argc, char *argv[])
  312. {
  313. // ========== Android权限申请 ==========
  314. // 在应用启动时申请所有必要的权限
  315. // 使用正确的API逐个申请权限
  316. for (const QString &permission : m_permissions) {
  317. QtAndroidPrivate::checkPermission(permission);
  318. QtAndroidPrivate::requestPermission(permission);
  319. }
  320. INITLOG();
  321. sendBroadcastFromQt("sleep");
  322. QGuiApplication app(argc, argv);
  323. // ========== 第二阶段:QGuiApplication创建后再次清理缓存 ==========
  324. // 有些缓存只有在QGuiApplication创建后才能正确访问
  325. clearAllCaches();
  326. LOG("Second cache clear completed!");
  327. MyCppClass myCppClass;
  328. #if WIN32
  329. QString subDirPath = "logs/run_log.txt";
  330. QString appDataPath ="./";
  331. #else
  332. // 获取应用程序内部存储空间路径
  333. QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/Android/data/com.yfkj.bzyv1/files/";
  334. LOG("appDataPath:{}",appDataPath.toStdString());
  335. QString appPicPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/Android/data/com.yfkj.bzyv1/files/Pictures";
  336. LOG("appPicPath:{}",appPicPath.toStdString());
  337. #endif
  338. sendBroadcastFromQt("iccid");
  339. sendBroadcastFromQt("csq");
  340. //创建并打开SQLite数据库
  341. QString db_config = "config.db";
  342. // QString db_data = "data.db";
  343. SqliteOperator *sqlConfig;
  344. sqlConfig = new SqliteOperator(db_config);
  345. sqlConfig->OpenDb();
  346. //判断数据表是否存在
  347. config_db_type config_class;
  348. if(!sqlConfig->isTableExist(param::table_mqtt)){
  349. sqlConfig->CreateTable(param::table_mqtt);
  350. //插入数据
  351. config_class.mqtt_db = {1,"bzy","8.136.98.49",61883,"","user","f335bf402c655ee5fd2b5300905124e","",0,60,"/yfkj/bzyL/s2c/","/yfkj/bzyL/c2s/","/yfkj/bzyL/offline/",10};
  352. sqlConfig->InsertData(config_class,param::table_mqtt);
  353. //查询全部数据
  354. sqlConfig->QueryAllData(param::table_mqtt);
  355. }else{
  356. sqlConfig->QueryAllData(param::table_mqtt);
  357. }
  358. if(param::imei.isEmpty()){
  359. sendBroadcastFromQt("imei");
  360. QTimer::singleShot(1000, [&]() {
  361. sqlConfig->OpenDb();
  362. if(sqlConfig->isTableExist(param::table_mqtt)){
  363. LOG("Update imei:{}",param::imei.toStdString());
  364. sqlConfig->UpdateData(param::table_mqtt,"imei",param::imei);
  365. }
  366. sqlConfig->CloseDb();
  367. });
  368. }
  369. if(!sqlConfig->isTableExist(param::table_mqtt_other))
  370. {
  371. sqlConfig->CreateTable(param::table_mqtt_other);
  372. config_class.mqtt_db = {1,"bzy","0.0.0.0",1883,"","uid","pwd","clientid",0,60,"/yfkj/bzy/s2c/","/yfkj/bzy/c2s/","/yfkj/bzy/offline/",10};
  373. sqlConfig->InsertData(config_class,param::table_mqtt_other);
  374. //查询全部数据
  375. sqlConfig->QueryAllData(param::table_mqtt_other);
  376. }else{
  377. sqlConfig->QueryAllData(param::table_mqtt_other);
  378. }
  379. if(!sqlConfig->isTableExist(param::table_ftp))
  380. {
  381. sqlConfig->CreateTable(param::table_ftp);
  382. config_class.ftp_db = {1,"8.136.98.49",21,"","bzy_user","YFkj@6O2O",0};
  383. sqlConfig->InsertData(config_class,param::table_ftp);
  384. sqlConfig->QueryAllData(param::table_ftp);
  385. }else{
  386. sqlConfig->QueryAllData(param::table_ftp);
  387. }
  388. if(!sqlConfig->isTableExist(param::table_ftp_other))
  389. {
  390. sqlConfig->CreateTable(param::table_ftp_other);
  391. config_class.ftp_db = {1,"0.0.0.0",21,"","cbd_user","YFkj@6O2O",0};
  392. sqlConfig->InsertData(config_class,param::table_ftp_other);
  393. sqlConfig->QueryAllData(param::table_ftp_other);
  394. }else{
  395. sqlConfig->QueryAllData(param::table_ftp_other);
  396. }
  397. if(!sqlConfig->isTableExist(param::table_location))
  398. {
  399. sqlConfig->CreateTable(param::table_location);
  400. config_class.location_db = {1,"0","0","0","0","0"};
  401. sqlConfig->InsertData(config_class,param::table_location);
  402. sqlConfig->QueryAllData(param::table_location);
  403. }else{
  404. sqlConfig->QueryAllData(param::table_location);
  405. }
  406. //相机图像参数
  407. if(!sqlConfig->isTableExist(param::table_camera)){
  408. sqlConfig->CreateTable(param::table_camera);
  409. config_class.camera_db = {"hikrobot","static",2,1000,1000,"0","f80","b80","f80","b80"};
  410. sqlConfig->InsertData(config_class,param::table_camera);
  411. sqlConfig->QueryAllData(param::table_camera);
  412. }else{
  413. sqlConfig->QueryAllData(param::table_camera);
  414. }
  415. //配置
  416. if(!sqlConfig->isTableExist(param::table_config)){
  417. sqlConfig->CreateTable(param::table_config);
  418. config_class.config_db = {1,1,1000,500,0,0};
  419. sqlConfig->InsertData(config_class,param::table_config);
  420. sqlConfig->QueryAllData(param::table_config);
  421. }else{
  422. sqlConfig->QueryAllData(param::table_config);
  423. }
  424. sqlConfig->CloseDb();
  425. //InsertJson_mqtt();
  426. qRegisterMetaType<QByteArray>("QByteArray");
  427. FelgoApplication felgo;
  428. QQmlApplicationEngine engine;
  429. felgo.initialize(&engine);
  430. felgo.setLicenseKey("6BBDF824AF729C33ECAA6DC917319EDB8A7F76A9EB5217C380F81C28203CF84078DBCEEC5DB44C7DB60373C0FFE65240DA17EAC1C8376F5939F26782D2A3110CFCDB938C557DDAC8C63F942AF917CD7BCD4235973E76002E69BE740BD2FE99BB7F22AD12408FB5EE3D3A538E604803F0947C372A8486E63B5CF101A18B6CEDF7AEDC615273B15ADBCF1DCC54F123FA093609F650378D2F2BC11C74421663CAB7C178C4123AF6C8B6E44EE45A5A790448F85D4039D7EB1C23C3CA97B42E0724B2987C185114E732662A4EFD507EDC0E5DC2DA7A9FF7754678F48BFC8AAFD40B21926B5A1FD427E279FDE44EC0315229371F4D77B918CC4663CB71572051E1E84DCBF8E183C44981C9D921DB7C5A02879E2D9352ECEC03FF21D99F941B9E31E10D6D625D2603135C101658FEA17FB7B397");
  431. LOG("信息(Info)");
  432. TreeModel model;
  433. engine.setOfflineStoragePath(appDataPath); //设置数据库存储路径
  434. engine.rootContext()->setContextProperty("appdir", qApp->applicationDirPath()); //使用QApplication::applicationDirPath()获取,并注册到qml中
  435. engine.rootContext()->setContextProperty("treeModel",&model);
  436. qmlRegisterType<EasyTableModel>("EasyModel",1,0,"EasyTableModel"); //注册C++模块到QML中
  437. qmlRegisterType<DatabaseInterface>("com.example.database", 1, 0, "DatabaseInterface");
  438. qmlRegisterType<CustomItem>("ExampleCustomMaterial",1,0,"CustomItem");
  439. // Set an optional license key from project file
  440. // This does not work if using Felgo Live, only for Felgo Cloud Builds and local builds
  441. //felgo.setLicenseKey(PRODUCT_LICENSE_KEY);
  442. // use this during development
  443. // for PUBLISHING, use the entry point below
  444. felgo.setMainQmlFileName(QStringLiteral("qml/Main.qml"));
  445. // use this instead of the above call to avoid deployment of the qml files and compile them into the binary with qt's resource system qrc
  446. // this is the preferred deployment option for publishing games to the app stores, because then your qml files and js files are protected
  447. // to avoid deployment of your qml files and images, also comment the DEPLOYMENTFOLDERS command in the .pro file
  448. // also see the .pro file for more details
  449. //felgo.setMainQmlFileName(QStringLiteral("qrc:/qml/Main.qml"));
  450. const QUrl url(felgo.mainQmlFileName());
  451. Qt6QCamera::Frames::registerQmlType();
  452. SerialThread serialThread;
  453. serialThread.start();
  454. engine.rootContext()->setContextProperty("serial", &serialThread);
  455. CurlFtpUploader uploader_ftp; // 成员变量
  456. QString remotepath ="/photo_hard/" + param::imei;
  457. uploader_ftp.setFtpParams("bzy_user", "YFkj@6O2O", "8.136.98.49", 21, remotepath);
  458. MqttHandler mqttHandler;
  459. mqttHandler.storage()->setJsonCacheEnabled(param::jsonCacheEnable);
  460. mqttHandler.storage()->setImageCacheEnabled(param::imageCacheEnable);
  461. mqttHandler.storage()->setMaxJsonEntries(param::maxJsonEntries);
  462. mqttHandler.storage()->setMaxImageEntries(param::maxImageEntries);
  463. // 启动时从 SQLite 把景深融合开关同步到 param::fusion_enable
  464. // 这样未经 MQTT 指令也能在重启后保留上次设置
  465. param::fusion_enable = mqttHandler.storage()->fusionEnabled() ? 1 : 0;
  466. LOG("[main] 启动时加载 fusion_enable={}", param::fusion_enable);
  467. CurlHandler curlHandler("ftp://8.136.98.49/_UPDATE_FILE/Android/BZYV2/armv7a/app.apk",appDataPath+"/app.apk"); //app升级
  468. NetworkInfoHelper networkMonitor;
  469. engine.rootContext()->setContextProperty("mqtt", &mqttHandler);
  470. engine.rootContext()->setContextProperty("curl", &curlHandler);
  471. //engine.rootContext()->setContextProperty("ymodem",serialThread.getYmodem());
  472. engine.rootContext()->setContextProperty("curl_picture",&uploader_ftp);
  473. engine.rootContext()->setContextProperty("networkMonitor", &networkMonitor);
  474. // 注册OpenCV图像融合功能到QML
  475. OpencvFunc opencvFunc;
  476. engine.rootContext()->setContextProperty("opencvFunc", &opencvFunc);
  477. // 网飞系统基础服务 AIDL 集成: 绑定 service 并把 wrapper 暴露给 QML.
  478. WifService *wifService = new WifService(&app);
  479. QObject::connect(wifService, &WifService::connectedChanged, [&](bool connected) {
  480. if (connected) {
  481. // 服务已连接,可以安全调用
  482. wifService->setKioskMode(true);
  483. }
  484. });
  485. wifService->bind();
  486. engine.rootContext()->setContextProperty("wifService", wifService);
  487. QObject::connect(&serialThread,&SerialThread::dataUpload,&mqttHandler,&MqttHandler::publish_m); //串口到MQTT
  488. QObject::connect(&mqttHandler,&MqttHandler::dataReceived,&serialThread,&SerialThread::mqtttoserial);//MQTT到串口
  489. QObject::connect(&networkMonitor,&NetworkInfoHelper::reachabilityChanged,[&](QNetworkInformation::Reachability newReachability){
  490. if(newReachability==QNetworkInformation::Reachability::Online){
  491. WARN("网络连接!");
  492. param::net_alive = 1;
  493. }else{
  494. param::net_alive = 0;
  495. WARN("网络断开!");
  496. mqttHandler.disconnectFromBroker();
  497. }
  498. });
  499. QObject::connect(&uploader_ftp, &CurlFtpUploader::finished,
  500. &mqttHandler,&MqttHandler::storage_Picture);
  501. QObject::connect(&mqttHandler, &MqttHandler::signal_reupload_image,
  502. &uploader_ftp,&CurlFtpUploader::uploadFileAsync);
  503. QObject::connect(&mqttHandler, &MqttHandler::signal_upload_log,
  504. &uploader_ftp,&CurlFtpUploader::uploadFileAsync);
  505. QObject::connect(&mqttHandler,&MqttHandler::updateStm32,[&](){
  506. curlHandler.seturl("ftp://8.136.98.49/_UPDATE_FILE/Android/BZYV2/armv7a/Application_BZYV2.rbl");
  507. curlHandler.setfilepath("/sdcard/rtthread.rbl");
  508. curlHandler.settype("主板升级文件更新");
  509. curlHandler.start();
  510. //sendBroadcastFromQt("install");
  511. });
  512. QObject::connect(&mqttHandler,&MqttHandler::appdownload,[&](){
  513. curlHandler.seturl("ftp://8.136.98.49/_UPDATE_FILE/Android/BZYV2/armv7a/app.apk");
  514. //curlHandler.setfilepath(appDataPath+"/app.apk");
  515. curlHandler.setfilepath("/sdcard/app.apk");
  516. curlHandler.settype("App升级文件更新");
  517. curlHandler.start();
  518. //
  519. });
  520. QObject::connect(&mqttHandler,&MqttHandler::sysReboot,[&](){
  521. WARN("系统重启");
  522. sendBroadcastFromQt("rebootSystem");
  523. });
  524. QObject::connect(&mqttHandler,&MqttHandler::configChange,[&](){
  525. sqlConfig->OpenDb();
  526. if(sqlConfig->isTableExist(param::table_camera)){
  527. DEBUG("UpdateData stitch_photonum !!!!!");
  528. sqlConfig->UpdateData(param::table_camera,"stitch_photonum",QString::number(param::stitch_photonum));
  529. }
  530. sqlConfig->CloseDb();
  531. });
  532. QObject::connect(&mqttHandler,&MqttHandler::signal_imei_change,[&](QString NEW_IMEI){
  533. sqlConfig->OpenDb();
  534. if(sqlConfig->isTableExist(param::table_mqtt)){
  535. LOG("Update Data imei:{}",NEW_IMEI.toStdString());
  536. sqlConfig->UpdateData(param::table_mqtt,"imei",NEW_IMEI);
  537. }
  538. sqlConfig->CloseDb();
  539. });
  540. QObject::connect(&curlHandler,&CurlHandler::downloadFinished,[&](bool result,QString mtype){
  541. LOG("downloadFinished:{}-{}",result,mtype.toStdString());
  542. if(result&&mtype.contains("App")){
  543. // QByteArray download_success("{\"cmd\":\"warn\",\"ext\":{\"status\":\"App DownLoad Success!\"}}");
  544. // mqttHandler.publish_m(download_success);
  545. emit mqttHandler.appInstall();
  546. }
  547. if(result&&mtype.contains("主板升级文件更新")){
  548. QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
  549. // 在内部存储空间路径下创建子目录
  550. QString subDirName = "/rtthread.rbl";
  551. QString subDirPath = appDataPath + subDirName;
  552. LOG("升级文件路径:{}",subDirPath.toStdString());
  553. emit serialThread.updatestart(subDirPath);
  554. }
  555. });
  556. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
  557. &app, [url](QObject *obj, const QUrl &objUrl) {
  558. if (!obj && url == objUrl)
  559. QCoreApplication::exit(-1);
  560. }, Qt::QueuedConnection);
  561. // 将位置信息源暴露给QML界面
  562. engine.load(QUrl(felgo.mainQmlFileName()));
  563. //FelgoLiveClient liveClient(&engine);
  564. //UserFunc myClass(engine.rootObjects()); // 获取QML对象
  565. #if WIN32
  566. app.installNativeEventFilter(myClass.mySerialEvent); // 安装window串口拔插事件
  567. #endif
  568. //engine.rootContext()->setContextProperty("myClass", &myClass); // 将C++类添加进QML使用
  569. //engine.rootContext()->setContextProperty("mySerial", myClass.serial_main); // 将C++类添加进QML使用
  570. QObject *rootItem = engine.rootObjects().value(0);
  571. //traverseQmlObject(rootItem);
  572. #if 1
  573. if(rootItem)
  574. {
  575. QObject::connect(&mqttHandler,&MqttHandler::autotakephoto,[&](bool stat){
  576. QObject *Navigation_main = rootItem->findChild<QObject*>("Navigation_main");
  577. if(Navigation_main){
  578. DEBUG("Navigation_main is !!!!! {}", stat);
  579. QMetaObject::invokeMethod(Navigation_main, "setCurrentTab", Q_ARG(QVariant, 1));
  580. //打开相机
  581. QObject *autoFocus_Page = rootItem->findChild<QObject*>("autoFocus_Page");
  582. if(autoFocus_Page){
  583. QMetaObject::invokeMethod(autoFocus_Page, "autotake",Q_ARG(QVariant,stat));
  584. //将param::stitchphoto保存
  585. }
  586. }
  587. }); //mqtt到QML,控制qml跳转到自动对焦界面,开始对焦拍照
  588. QObject::connect(&serialThread,&SerialThread::auto_takephoto,[&](){
  589. QObject *Navigation_main = rootItem->findChild<QObject*>("Navigation_main");
  590. if(Navigation_main){
  591. DEBUG("Navigation_main is !!!!!");
  592. QMetaObject::invokeMethod(Navigation_main, "setCurrentTab", Q_ARG(QVariant, 1));
  593. //打开相机
  594. QObject *autoFocus_Page = rootItem->findChild<QObject*>("autoFocus_Page");
  595. if(autoFocus_Page){
  596. QMetaObject::invokeMethod(autoFocus_Page, "autotake",Q_ARG(QVariant,true));
  597. }
  598. }
  599. }); //串口到QML,控制qml跳转到自动对焦界面,结束对焦拍照
  600. //获取按钮点击事件
  601. QObject *systemset_Item = rootItem->findChild<QObject*>("systemset_Item");
  602. if(systemset_Item){
  603. QObject::connect(systemset_Item, SIGNAL(selected()),&myCppClass,SLOT(systemPage()));
  604. QObject::connect(&myCppClass, &MyCppClass::tosystempage,[&](){
  605. QObject *openbar_btn = rootItem->findChild<QObject*>("openbar_btn"); //打开状态栏
  606. if(openbar_btn){
  607. QObject::connect(openbar_btn, SIGNAL(opernBar()),&myCppClass,SLOT(openbar_btnClicked()));
  608. QObject::connect(&myCppClass, &MyCppClass::opensystembar,[&](){
  609. sendBroadcastFromQt("openbar_btn");
  610. });
  611. QObject::connect(openbar_btn, SIGNAL(closeBar()),&myCppClass,SLOT(closebar_btnClicked()));
  612. QObject::connect(&myCppClass, &MyCppClass::closesystembar,[&](){
  613. sendBroadcastFromQt("closebar_btn");
  614. });
  615. }
  616. QObject *opennav_btn = rootItem->findChild<QObject*>("opennav_btn"); //控制导航栏
  617. if(opennav_btn){
  618. QObject::connect(opennav_btn, SIGNAL(opernNav()),&myCppClass,SLOT(opennav_btnClicked()));
  619. QObject::connect(&myCppClass, &MyCppClass::opensystemnav,[&](){
  620. sendBroadcastFromQt("opennav_btn");
  621. });
  622. QObject::connect(opennav_btn, SIGNAL(closeNav()),&myCppClass,SLOT(closenav_btnClicked()));
  623. QObject::connect(&myCppClass, &MyCppClass::closesystemnav,[&](){
  624. sendBroadcastFromQt("closenav_btn");
  625. });
  626. }
  627. });
  628. }
  629. // 获取根 QML 对象
  630. QObject *autofocus_item = rootItem->findChild<QObject*>("autofocus_Item");
  631. if(autofocus_item){
  632. bool isConnect_frame = true;
  633. //const QMetaObject *metaObj = autofocus_item->metaObject();
  634. LOG("Found autofocus_item!");
  635. QObject::connect(autofocus_item, SIGNAL(selected()),&myCppClass,SLOT(qmlSlot()));
  636. QObject::connect(&myCppClass, &MyCppClass::recvqmlslot,[&](){
  637. LOG("connect myCppClass");
  638. Qt6QCamera::Frames *m_frames = qobject_cast<Qt6QCamera::Frames*>(rootItem->findChild<QObject*>("Frames"));
  639. if(!m_frames) {ERROR("m_frames is null!!!");}
  640. if(m_frames&&isConnect_frame){
  641. LOG("m_frames connected!");
  642. //engine.addImageProvider("ucamera",m_frames->getImageProvider());
  643. QObject::connect(m_frames,&Qt6QCamera::Frames::serial_data,&serialThread,&SerialThread::sendData); //Frame到串口
  644. QObject::connect(&serialThread,&SerialThread::takephotodone,m_frames,&Qt6QCamera::Frames::saveImage); //串口到QML,控制qml跳转到自动对焦界面
  645. //QObject::connect(m_frames,&Qt6QCamera::Frames::signal_stitch,&imagestitcher,&ImageStitcher::stitch_thread);
  646. QObject::connect(&serialThread,&SerialThread::signal_movy,m_frames,&Qt6QCamera::Frames::onMovDone);
  647. QObject::connect(&serialThread,&SerialThread::autoFocusDone,m_frames,&Qt6QCamera::Frames::finishDynamicFusion);
  648. QObject::connect(m_frames,&Qt6QCamera::Frames::dataUpload,&mqttHandler,&MqttHandler::publish_m); //frames到MQTT
  649. // 修改信号槽连接方式
  650. QObject::connect(m_frames, &Qt6QCamera::Frames::signal_wait_upload,
  651. &uploader_ftp, &CurlFtpUploader::uploadFileAsync);
  652. QObject::connect(&uploader_ftp, &CurlFtpUploader::finished,
  653. m_frames,&Qt6QCamera::Frames::handle_reult_upload);
  654. QObject::connect(m_frames, &Qt6QCamera::Frames::signal_fusion_progress,
  655. &opencvFunc, &OpencvFunc::fusionProgress);
  656. isConnect_frame = false;
  657. }else{
  658. ERROR("m_frames no !!!!!");
  659. }
  660. });
  661. }else{
  662. DEBUG("autofocus_item no !!!!!");
  663. }
  664. }else{
  665. ERROR("rootItem is null !!!!!");
  666. }
  667. #endif
  668. LOG("app start!");
  669. // to start your project as Live Client, comment (remove) the lines "felgo.setMainQmlFileName ..." & "engine.load ...",
  670. // and uncomment the line below
  671. //FelgoLiveClient client (&engine);
  672. GPSInfo *gps_start = new GPSInfo(sqlConfig,&app);
  673. QObject::connect(gps_start,&GPSInfo::position_success,[&](){
  674. emit serialThread.gpsSuccess();
  675. });
  676. serialThread.recoverMcuParam();
  677. return app.exec();
  678. }
  679. #include "main.moc"