main.cpp 33 KB

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