main.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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 <QQuickWindow>
  13. //uncomment this line to add the Live Client Module and use live reloading with your custom C++ code
  14. //#include <FelgoLiveClient>
  15. class param;
  16. QString param::table_mqtt = "mqtt"; //mqtt配置表
  17. QString param::table_mqtt_other = "mqtt_other"; //mqtt对接配置表
  18. QString param::table_ftp = "ftp"; //FTP配置表
  19. QString param::table_ftp_other = "ftp_other"; //FTP对接配置表
  20. QString param::table_camera = "camera"; //相机参数配置表
  21. QString param::table_location = "location"; //定位信息参数配置表
  22. QString param::table_config = "config"; //参数配置表
  23. void traverseQmlObject(QObject *qmlObject, int depth = 0) {
  24. QString indentation = QString(" ").repeated(depth);
  25. qDebug() << indentation << "Type:" << qmlObject->metaObject()->className()
  26. << "Name:" << qmlObject->objectName();
  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. //QJniObject activity = QJniObject::callStaticObjectMethod("com/yfkj/cb/Q6iot/MyQtActivity","getCurrentActivity","()Landroid/app/Activity;");
  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. QObject::connect(&serialThread,&SerialThread::dataUpload,&mqttHandler,&MqttHandler::publish_m); //串口到MQTT
  472. QObject::connect(&mqttHandler,&MqttHandler::dataReceived,&serialThread,&SerialThread::mqtttoserial);//MQTT到串口
  473. QObject::connect(&networkMonitor,&NetworkInfoHelper::reachabilityChanged,[&](QNetworkInformation::Reachability newReachability){
  474. if(newReachability==QNetworkInformation::Reachability::Online){
  475. WARN("网络连接!");
  476. param::net_alive = 1;
  477. }else{
  478. param::net_alive = 0;
  479. WARN("网络断开!");
  480. mqttHandler.disconnectFromBroker();
  481. }
  482. });
  483. QObject::connect(&uploader_ftp, &CurlFtpUploader::finished,
  484. &mqttHandler,&MqttHandler::storage_Picture);
  485. QObject::connect(&mqttHandler, &MqttHandler::signal_reupload_image,
  486. &uploader_ftp,&CurlFtpUploader::uploadFileAsync);
  487. QObject::connect(&mqttHandler, &MqttHandler::signal_upload_log,
  488. &uploader_ftp,&CurlFtpUploader::uploadFileAsync);
  489. QObject::connect(&mqttHandler,&MqttHandler::updateStm32,[&](){
  490. curlHandler.seturl("ftp://8.136.98.49/_UPDATE_FILE/A7/STM32/Application_BZYV2.rbl");
  491. curlHandler.setfilepath(appDataPath+"/rtthread.rbl");
  492. curlHandler.settype("主板升级文件更新");
  493. curlHandler.start();
  494. //sendBroadcastFromQt("install");
  495. });
  496. QObject::connect(&mqttHandler,&MqttHandler::appdownload,[&](){
  497. curlHandler.seturl("ftp://8.136.98.49/_UPDATE_FILE/Android/BZYV2/app.apk");
  498. //curlHandler.setfilepath(appDataPath+"/app.apk");
  499. curlHandler.setfilepath("/sdcard/app.apk");
  500. curlHandler.settype("App升级文件更新");
  501. curlHandler.start();
  502. //
  503. });
  504. QObject::connect(&mqttHandler,&MqttHandler::sysReboot,[&](){
  505. WARN("系统重启");
  506. sendBroadcastFromQt("rebootSystem");
  507. });
  508. QObject::connect(&mqttHandler,&MqttHandler::configChange,[&](){
  509. sqlConfig->OpenDb();
  510. if(sqlConfig->isTableExist(param::table_camera)){
  511. qDebug() << "UpdateData stitch_photonum !!!!!";
  512. sqlConfig->UpdateData(param::table_camera,"stitch_photonum",QString::number(param::stitch_photonum));
  513. }
  514. sqlConfig->CloseDb();
  515. });
  516. QObject::connect(&mqttHandler,&MqttHandler::signal_imei_change,[&](QString NEW_IMEI){
  517. sqlConfig->OpenDb();
  518. if(sqlConfig->isTableExist(param::table_mqtt)){
  519. LOG("Update Data imei:{}",NEW_IMEI.toStdString());
  520. sqlConfig->UpdateData(param::table_mqtt,"imei",NEW_IMEI);
  521. }
  522. sqlConfig->CloseDb();
  523. });
  524. QObject::connect(&curlHandler,&CurlHandler::downloadFinished,[&](bool result,QString mtype){
  525. LOG("downloadFinished:{}-{}",result,mtype.toStdString());
  526. if(result&&mtype.contains("App")){
  527. // QByteArray download_success("{\"cmd\":\"warn\",\"ext\":{\"status\":\"App DownLoad Success!\"}}");
  528. // mqttHandler.publish_m(download_success);
  529. emit mqttHandler.appInstall();
  530. }
  531. });
  532. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
  533. &app, [url](QObject *obj, const QUrl &objUrl) {
  534. if (!obj && url == objUrl)
  535. QCoreApplication::exit(-1);
  536. }, Qt::QueuedConnection);
  537. // 将位置信息源暴露给QML界面
  538. engine.load(QUrl(felgo.mainQmlFileName()));
  539. //FelgoLiveClient liveClient(&engine);
  540. //UserFunc myClass(engine.rootObjects()); // 获取QML对象
  541. #if WIN32
  542. app.installNativeEventFilter(myClass.mySerialEvent); // 安装window串口拔插事件
  543. #endif
  544. //engine.rootContext()->setContextProperty("myClass", &myClass); // 将C++类添加进QML使用
  545. //engine.rootContext()->setContextProperty("mySerial", myClass.serial_main); // 将C++类添加进QML使用
  546. QObject *rootItem = engine.rootObjects().value(0);
  547. //traverseQmlObject(rootItem);
  548. #if 1
  549. if(rootItem)
  550. {
  551. QObject::connect(&mqttHandler,&MqttHandler::autotakephoto,[&](bool stat){
  552. QObject *Navigation_main = rootItem->findChild<QObject*>("Navigation_main");
  553. if(Navigation_main){
  554. qDebug() << "Navigation_main is !!!!!"<<stat;
  555. QMetaObject::invokeMethod(Navigation_main, "setCurrentTab", Q_ARG(QVariant, 1));
  556. //打开相机
  557. QObject *autoFocus_Page = rootItem->findChild<QObject*>("autoFocus_Page");
  558. if(autoFocus_Page){
  559. QMetaObject::invokeMethod(autoFocus_Page, "autotake",Q_ARG(QVariant,stat));
  560. //将param::stitchphoto保存
  561. }
  562. }
  563. }); //mqtt到QML,控制qml跳转到自动对焦界面,开始对焦拍照
  564. QObject::connect(&serialThread,&SerialThread::auto_takephoto,[&](){
  565. QObject *Navigation_main = rootItem->findChild<QObject*>("Navigation_main");
  566. if(Navigation_main){
  567. qDebug() << "Navigation_main is !!!!!";
  568. QMetaObject::invokeMethod(Navigation_main, "setCurrentTab", Q_ARG(QVariant, 1));
  569. //打开相机
  570. QObject *autoFocus_Page = rootItem->findChild<QObject*>("autoFocus_Page");
  571. if(autoFocus_Page){
  572. QMetaObject::invokeMethod(autoFocus_Page, "autotake",Q_ARG(QVariant,true));
  573. }
  574. }
  575. }); //串口到QML,控制qml跳转到自动对焦界面,结束对焦拍照
  576. //获取按钮点击事件
  577. QObject *systemset_Item = rootItem->findChild<QObject*>("systemset_Item");
  578. if(systemset_Item){
  579. QObject::connect(systemset_Item, SIGNAL(selected()),&myCppClass,SLOT(systemPage()));
  580. QObject::connect(&myCppClass, &MyCppClass::tosystempage,[&](){
  581. QObject *openbar_btn = rootItem->findChild<QObject*>("openbar_btn"); //打开状态栏
  582. if(openbar_btn){
  583. QObject::connect(openbar_btn, SIGNAL(opernBar()),&myCppClass,SLOT(openbar_btnClicked()));
  584. QObject::connect(&myCppClass, &MyCppClass::opensystembar,[&](){
  585. sendBroadcastFromQt("openbar_btn");
  586. });
  587. QObject::connect(openbar_btn, SIGNAL(closeBar()),&myCppClass,SLOT(closebar_btnClicked()));
  588. QObject::connect(&myCppClass, &MyCppClass::closesystembar,[&](){
  589. sendBroadcastFromQt("closebar_btn");
  590. });
  591. }
  592. QObject *opennav_btn = rootItem->findChild<QObject*>("opennav_btn"); //控制导航栏
  593. if(opennav_btn){
  594. QObject::connect(opennav_btn, SIGNAL(opernNav()),&myCppClass,SLOT(opennav_btnClicked()));
  595. QObject::connect(&myCppClass, &MyCppClass::opensystemnav,[&](){
  596. sendBroadcastFromQt("opennav_btn");
  597. });
  598. QObject::connect(opennav_btn, SIGNAL(closeNav()),&myCppClass,SLOT(closenav_btnClicked()));
  599. QObject::connect(&myCppClass, &MyCppClass::closesystemnav,[&](){
  600. sendBroadcastFromQt("closenav_btn");
  601. });
  602. }
  603. });
  604. }
  605. // 获取根 QML 对象
  606. QObject *autofocus_item = rootItem->findChild<QObject*>("autofocus_Item");
  607. if(autofocus_item){
  608. bool isConnect_frame = true;
  609. //const QMetaObject *metaObj = autofocus_item->metaObject();
  610. LOG("Found autofocus_item!");
  611. QObject::connect(autofocus_item, SIGNAL(selected()),&myCppClass,SLOT(qmlSlot()));
  612. QObject::connect(&myCppClass, &MyCppClass::recvqmlslot,[&](){
  613. LOG("connect myCppClass");
  614. Qt6QCamera::Frames *m_frames = qobject_cast<Qt6QCamera::Frames*>(rootItem->findChild<QObject*>("Frames"));
  615. if(!m_frames) {ERROR("m_frames is null!!!");}
  616. if(m_frames&&isConnect_frame){
  617. LOG("m_frames connected!");
  618. //engine.addImageProvider("ucamera",m_frames->getImageProvider());
  619. QObject::connect(m_frames,&Qt6QCamera::Frames::serial_data,&serialThread,&SerialThread::sendData); //Frame到串口
  620. QObject::connect(&serialThread,&SerialThread::takephotodone,m_frames,&Qt6QCamera::Frames::saveImage); //串口到QML,控制qml跳转到自动对焦界面
  621. //QObject::connect(m_frames,&Qt6QCamera::Frames::signal_stitch,&imagestitcher,&ImageStitcher::stitch_thread);
  622. QObject::connect(&serialThread,&SerialThread::signal_movy,m_frames,&Qt6QCamera::Frames::onMovDone);
  623. QObject::connect(m_frames,&Qt6QCamera::Frames::dataUpload,&mqttHandler,&MqttHandler::publish_m); //frames到MQTT
  624. // 修改信号槽连接方式
  625. QObject::connect(m_frames, &Qt6QCamera::Frames::signal_wait_upload,
  626. &uploader_ftp, &CurlFtpUploader::uploadFileAsync);
  627. QObject::connect(&uploader_ftp, &CurlFtpUploader::finished,
  628. m_frames,&Qt6QCamera::Frames::handle_reult_upload);
  629. isConnect_frame = false;
  630. }else{
  631. ERROR("m_frames no !!!!!");
  632. }
  633. });
  634. }else{
  635. qDebug() << "autofocus_item no !!!!!";
  636. }
  637. }else{
  638. ERROR("rootItem is null !!!!!");
  639. }
  640. #endif
  641. LOG("app start!");
  642. // to start your project as Live Client, comment (remove) the lines "felgo.setMainQmlFileName ..." & "engine.load ...",
  643. // and uncomment the line below
  644. //FelgoLiveClient client (&engine);
  645. GPSInfo *gps_start = new GPSInfo(sqlConfig,&app);
  646. QObject::connect(gps_start,&GPSInfo::position_success,[&](){
  647. emit serialThread.gpsSuccess();
  648. });
  649. return app.exec();
  650. }
  651. #include "main.moc"