#include #include #include #include #include #include #include #include #include #include // Uncomment this line to add Felgo Hot Reload and use hot reloading with your custom C++ code //#include class param; QString param::table_mqtt = "mqtt"; //mqtt配置表 QString param::table_mqtt_other = "mqtt_other"; //mqtt对接配置表 QString param::table_ftp = "ftp"; //FTP配置表 QString param::table_ftp_other = "ftp_other"; //FTP对接配置表 QString param::table_camera = "camera"; //相机参数配置表 QString param::table_location = "location"; //定位信息参数配置表 QString param::table_config = "config"; //参数配置表 void traverseQmlObject(QObject *qmlObject, int depth = 0) { QString indentation = QString(" ").repeated(depth); qDebug() << indentation << "Type:" << qmlObject->metaObject()->className() << "Name:" << qmlObject->objectName(); for (QObject *child : qmlObject->children()) { traverseQmlObject(child, depth + 1); } } class MyCppClass : public QObject { Q_OBJECT public: void setQmlRootObject(QObject *rootObject) { m_qmlRootObject = rootObject; } void JumpToCamera(){ } public slots: void qmlSlot() { LOG("Received from QML"); emit recvqmlslot(); } void systemPage(){ LOG("systemPage"); emit tosystempage(); } void openbar_btnClicked(){ LOG("openbar_btnClicked"); emit opensystembar(); } void closebar_btnClicked(){ LOG("closebar_btnClicked"); emit closesystembar(); } void opennav_btnClicked(){ LOG("opennav_btnClicked"); emit opensystemnav(); } void closenav_btnClicked(){ LOG("closenav_btnClicked"); emit closesystemnav(); } signals: void recvqmlslot(); void tosystempage(); void opensystembar(); void closesystembar(); void opensystemnav(); void closesystemnav(); private: QObject *m_qmlRootObject = nullptr; }; class GPSInfo : public QObject { Q_OBJECT public: double nowGroundSpeed=0.0; double nowVerticalSpeed=0.0; double nowMagneticVariation=0.0; double nowHorizontalAccuracy=0.0; double nowVerticalAccuracy=0.0; double nowLongitude=0.0; double nowLatitude=0.0; double nowDirection=0.0; QString nowTimes=""; int recv_cnt = 0; QGeoPositionInfoSource *positionSource; public: GPSInfo(SqliteOperator *db,QObject *parent = nullptr) : QObject(parent) // 注入外部数据库对象 , gpsConfig(db) { param::gps_state = "0"; positionSource = QGeoPositionInfoSource::createDefaultSource(this); //QGeoSatelliteInfoSource *satelliteSource = QGeoSatelliteInfoSource::createDefaultSource(this); if (positionSource) { connect(positionSource, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo))); positionSource->setUpdateInterval(1000);//设置刷新时间 单位毫秒 positionSource->startUpdates();//启动设备 } } signals: void position_success(); private: SqliteOperator *gpsConfig = nullptr; // 不再在内部创建 private slots://槽函数中实现了GPS部分信息的读取 void positionUpdated(const QGeoPositionInfo &info) { nowLongitude = info.coordinate().longitude();//经度 nowLatitude = info.coordinate().latitude();//纬度 nowDirection = info.attribute(QGeoPositionInfo::Direction); nowGroundSpeed = info.attribute(QGeoPositionInfo::GroundSpeed); nowVerticalSpeed = info.attribute(QGeoPositionInfo::VerticalSpeed); nowMagneticVariation = info.attribute(QGeoPositionInfo::MagneticVariation); nowHorizontalAccuracy = info.attribute(QGeoPositionInfo::HorizontalAccuracy); nowVerticalAccuracy = info.attribute(QGeoPositionInfo::VerticalAccuracy); nowTimes = info.timestamp().toString(); if(param::gps_lat.contains(".")&¶m::gps_lat.size()>3){ param::gps_state = "1"; recv_cnt++; if(recv_cnt==20){ //定位成功后120S把定位信息保存到数据库中 emit position_success(); gpsConfig->OpenDb(); if(gpsConfig->isTableExist(param::table_location)){ gpsConfig->UpdateData(param::table_location,"gps_lat",param::gps_lat); gpsConfig->UpdateData(param::table_location,"gps_lng",param::gps_lng); } gpsConfig->CloseDb(); positionSource->stopUpdates(); } } param::gps_lat = QString::number(nowLatitude); param::gps_lng = QString::number(nowLongitude); LOG("经度:{}",nowLongitude); LOG("纬度:{}",nowLatitude); LOG("角度:{}",nowDirection); LOG("地表速度:{}",nowGroundSpeed); LOG("垂直速度:{}",nowVerticalSpeed); LOG("磁偏角:{}",nowMagneticVariation); LOG("水平精度:{}",nowHorizontalAccuracy); LOG("垂直精度:{}",nowVerticalAccuracy); LOG("定位次数:{}",recv_cnt); } }; void sendBroadcastFromQt(QString btn_type) { //QJniObject activity = QJniObject::callStaticObjectMethod("com/yfkj/bzyv1/MyQtActivity","getCurrentActivity","()Landroid/app/Activity;"); auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context()); //activity //QJniObject qtContext = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;"); if (qtContext.isValid()) { if(btn_type=="sleep"){ QJniObject jClass("com/yfkj/bzyv1/Backlight"); //jboolean ret = jClass.callMethod("checkWriteSettingPermission","(Landroid/content/Context;I)Z",qtContext.object(),100); jClass.callMethod("setScreenLight","(Landroid/content/Context;I)V",qtContext.object(),100); } //QJniObject jClass("com/yfkj/bzyv1/StatusBarCtr"); QJniObject jClass("com/yfkj/bzyv1/Backlight"); if(btn_type=="openbar_btn"){ jClass.callMethod("setStatusBar","(Landroid/content/Context;I)V",qtContext.object(),1); }else if(btn_type=="closebar_btn"){ jClass.callMethod("setStatusBar","(Landroid/content/Context;I)V",qtContext.object(),0); }else if(btn_type=="closenav_btn"){ jClass.callMethod("setNavigationBar","(Landroid/content/Context;I)V",qtContext.object(),0); }else if(btn_type=="opennav_btn"){ jClass.callMethod("setNavigationBar","(Landroid/content/Context;I)V",qtContext.object(),1); }else if(btn_type=="imei"){ QJniObject jClass("com/yfkj/bzyv1/StatusBarCtr"); QJniObject java_imei= jClass.callObjectMethod("getAndroidIMEI","(Landroid/content/Context;)Ljava/lang/String;",qtContext.object()); if(java_imei.isValid()){ QString imei = java_imei.toString(); if(!imei.isEmpty()&¶m::imei.isEmpty()){ param::imei = imei; } LOG("IMEI:{}",param::imei.toStdString()); } }else if(btn_type=="csq"){ jClass.callMethod("getMobileNetworkSignal","(Landroid/content/Context;)V",qtContext.object()); }else if(btn_type=="iccid"){ QJniObject jClass("com/yfkj/bzyv1/StatusBarCtr"); QJniObject iccid_java = jClass.callObjectMethod("getICCID","(Landroid/content/Context;)Ljava/lang/String;",qtContext.object()); if(iccid_java.isValid()){ QString iccid = iccid_java.toString(); if(!iccid.isEmpty()){ param::iccid = iccid; } LOG("ICCID:{}",param::iccid.toStdString()); } }else if(btn_type=="install"){ QJniObject jClass("com/yfkj/bzyv1/StatusBarCtr"); qDebug() << "install process start!"; jClass.callMethod("installApp","(Landroid/content/Context;)V",qtContext.object()); }else if(btn_type=="rebootSystem"){ jClass.callMethod("rebootSystem","(Landroid/content/Context;)V",qtContext.object()); }else if(btn_type=="service"){ qDebug() <<"启动android服务"; QAndroidIntent serviceIntent(qtContext.object(), "com/yfkj/bzyv1/QtAndroidService"); QJniObject result = qtContext.callObjectMethod( "startService", "(Landroid/content/Intent;)Landroid/content/ComponentName;", serviceIntent.handle().object()); } } } int main(int argc, char *argv[]) { INITLOG(); if (QtAndroidPrivate::checkPermission(QtAndroidPrivate::Storage).result() == QtAndroidPrivate::Denied) { QtAndroidPrivate::requestPermission(QtAndroidPrivate::Storage).waitForFinished(); } qInfo() << "anroid storage permission status: " << QtAndroidPrivate::checkPermission(QtAndroidPrivate::Storage).result(); sendBroadcastFromQt("sleep"); MyCppClass myCppClass; QApplication app(argc, argv); FelgoApplication felgo; QQmlApplicationEngine engine; felgo.initialize(&engine); #if WIN32 QString subDirPath = "logs/run_log.txt"; QString appDataPath ="./"; #else // 获取应用程序内部存储空间路径 QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); LOG("appDataPath:{}",appDataPath.toStdString()); QString appPicPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); LOG("appPicPath:{}",appPicPath.toStdString()); #endif sendBroadcastFromQt("iccid"); sendBroadcastFromQt("csq"); QString db_config = "config.db"; SqliteOperator *sqlConfig; sqlConfig = new SqliteOperator(db_config); sqlConfig->OpenDb(); //判断数据表是否存在 config_db_type config_class; if(!sqlConfig->isTableExist(param::table_mqtt)){ sqlConfig->CreateTable(param::table_mqtt); //插入数据 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}; sqlConfig->InsertData(config_class,param::table_mqtt); //查询全部数据 sqlConfig->QueryAllData(param::table_mqtt); }else{ sqlConfig->QueryAllData(param::table_mqtt); } if(param::imei.isEmpty()){ sendBroadcastFromQt("imei"); QTimer::singleShot(1000, [&]() { sqlConfig->UpdateData(param::table_mqtt,"imei",param::imei); }); } if(!sqlConfig->isTableExist(param::table_mqtt_other)) { sqlConfig->CreateTable(param::table_mqtt_other); 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}; sqlConfig->InsertData(config_class,param::table_mqtt_other); //查询全部数据 sqlConfig->QueryAllData(param::table_mqtt_other); }else{ sqlConfig->QueryAllData(param::table_mqtt_other); } if(!sqlConfig->isTableExist(param::table_ftp)) { sqlConfig->CreateTable(param::table_ftp); config_class.ftp_db = {1,"8.136.98.49",21,"","bzy_user","YFkj@6O2O",0}; sqlConfig->InsertData(config_class,param::table_ftp); sqlConfig->QueryAllData(param::table_ftp); }else{ sqlConfig->QueryAllData(param::table_ftp); } if(!sqlConfig->isTableExist(param::table_ftp_other)) { sqlConfig->CreateTable(param::table_ftp_other); config_class.ftp_db = {1,"0.0.0.0",21,"","cbd_user","YFkj@6O2O",0}; sqlConfig->InsertData(config_class,param::table_ftp_other); sqlConfig->QueryAllData(param::table_ftp_other); }else{ sqlConfig->QueryAllData(param::table_ftp_other); } if(!sqlConfig->isTableExist(param::table_location)) { sqlConfig->CreateTable(param::table_location); config_class.location_db = {1,"0","0","0","0","0"}; sqlConfig->InsertData(config_class,param::table_location); sqlConfig->QueryAllData(param::table_location); }else{ sqlConfig->QueryAllData(param::table_location); } //相机图像参数 if(!sqlConfig->isTableExist(param::table_camera)){ sqlConfig->CreateTable(param::table_camera); config_class.camera_db = {"hikrobot","static",2,1000,1000,"0","f80","b80","f80","b80"}; sqlConfig->InsertData(config_class,param::table_camera); sqlConfig->QueryAllData(param::table_camera); }else{ sqlConfig->QueryAllData(param::table_camera); } //配置 if(!sqlConfig->isTableExist(param::table_config)){ sqlConfig->CreateTable(param::table_config); config_class.config_db = {1,1,1000,500,0,0}; sqlConfig->InsertData(config_class,param::table_config); sqlConfig->QueryAllData(param::table_config); }else{ sqlConfig->QueryAllData(param::table_config); } sqlConfig->CloseDb(); // Set an optional license key from project file // This does not work if using Felgo Developer App, only for Felgo Cloud Builds and local builds //felgo.setLicenseKey(PRODUCT_LICENSE_KEY); TreeModel model; engine.setOfflineStoragePath(appDataPath); //设置数据库存储路径 engine.rootContext()->setContextProperty("appdir", qApp->applicationDirPath()); //使用QApplication::applicationDirPath()获取,并注册到qml中 engine.rootContext()->setContextProperty("treeModel",&model); qmlRegisterType("EasyModel",1,0,"EasyTableModel"); //注册C++模块到QML中 qmlRegisterType("com.example.database", 1, 0, "DatabaseInterface"); // use this during development // for PUBLISHING, use the entry point below felgo.setMainQmlFileName(QStringLiteral("qml/Main.qml")); const QUrl url(felgo.mainQmlFileName()); Qt6QCamera::Frames::registerQmlType(); SerialThread serialThread; engine.rootContext()->setContextProperty("serial", &serialThread); CurlFtpUploader uploader_ftp; // 成员变量 QString remotepath ="/photo_hard/" + param::imei; uploader_ftp.setFtpParams("bzy_user", "YFkj@6O2O", "8.136.98.49", 21, remotepath); YMODEM m_ymodem(&serialThread); serialThread.setSerialPortSettings("/dev/ttyS3", QSerialPort::Baud115200, QSerialPort::Data8, QSerialPort::NoParity, QSerialPort::OneStop, QSerialPort::NoFlowControl); MqttHandler mqttHandler; mqttHandler.storage()->setJsonCacheEnabled(param::jsonCacheEnable); mqttHandler.storage()->setImageCacheEnabled(param::imageCacheEnable); mqttHandler.storage()->setMaxJsonEntries(param::maxJsonEntries); mqttHandler.storage()->setMaxImageEntries(param::maxImageEntries); CurlHandler curlHandler("ftp://8.136.98.49/_UPDATE_FILE/Android/BZYV2/app.apk",appDataPath+"/app.apk"); //app升级 NetworkInfoHelper networkMonitor; engine.rootContext()->setContextProperty("mqtt", &mqttHandler); engine.rootContext()->setContextProperty("curl", &curlHandler); engine.rootContext()->setContextProperty("ymodem",&m_ymodem); engine.rootContext()->setContextProperty("curl_picture",&uploader_ftp); engine.rootContext()->setContextProperty("networkMonitor", &networkMonitor); serialThread.start(); QObject::connect(&serialThread,&SerialThread::dataUpload,&mqttHandler,&MqttHandler::publish_m); //串口到MQTT QObject::connect(&mqttHandler,&MqttHandler::dataReceived,&serialThread,&SerialThread::mqtttoserial);//MQTT到串口 QObject::connect(&serialThread,&SerialThread::updatestart,&m_ymodem,&YMODEM::start_ymodem); //串口到Ymodem QObject::connect(&m_ymodem,&YMODEM::modem_result,&serialThread,&SerialThread::getmodemresult); //Ymodem到串口 QObject::connect(&networkMonitor,&NetworkInfoHelper::reachabilityChanged,[&](QNetworkInformation::Reachability newReachability){ if(newReachability==QNetworkInformation::Reachability::Online){ WARN("网络连接!"); param::net_alive = 1; }else{ param::net_alive = 0; WARN("网络断开!"); mqttHandler.disconnectFromBroker(); } }); QObject::connect(&uploader_ftp, &CurlFtpUploader::finished, &mqttHandler,&MqttHandler::storage_Picture); QObject::connect(&mqttHandler, &MqttHandler::signal_reupload_image, &uploader_ftp,&CurlFtpUploader::uploadFileAsync); QObject::connect(&mqttHandler,&MqttHandler::updateStm32,[&](){ curlHandler.seturl("ftp://8.136.98.49/_UPDATE_FILE/A7/STM32/Application_BZYV2.rbl"); curlHandler.setfilepath(appDataPath+"/rtthread.rbl"); curlHandler.settype("主板升级文件更新"); curlHandler.start(); //sendBroadcastFromQt("install"); }); QObject::connect(&mqttHandler,&MqttHandler::appdownload,[&](){ curlHandler.seturl("ftp://8.136.98.49/_UPDATE_FILE/Android/BZYV2/app.apk"); //curlHandler.setfilepath(appDataPath+"/app.apk"); curlHandler.setfilepath("/sdcard/app.apk"); curlHandler.settype("App升级文件更新"); curlHandler.start(); // }); QObject::connect(&mqttHandler,&MqttHandler::sysReboot,[&](){ WARN("系统重启"); sendBroadcastFromQt("rebootSystem"); }); QObject::connect(&mqttHandler,&MqttHandler::configChange,[&](){ sqlConfig->OpenDb(); if(sqlConfig->isTableExist(param::table_camera)){ qDebug() << "UpdateData stitch_photonum !!!!!"; sqlConfig->UpdateData(param::table_camera,"stitch_photonum",QString::number(param::stitch_photonum)); } sqlConfig->CloseDb(); }); QObject::connect(&mqttHandler,&MqttHandler::signal_imei_change,[&](QString NEW_IMEI){ sqlConfig->OpenDb(); if(sqlConfig->isTableExist(param::table_mqtt)){ qDebug() << "UpdateData imei !!!!!"; sqlConfig->UpdateData(param::table_mqtt,"imei",NEW_IMEI); } sqlConfig->CloseDb(); }); QObject::connect(&curlHandler,&CurlHandler::downloadFinished,[&](bool result,QString mtype){ LOG("downloadFinished:{}-{}",result,mtype.toStdString()); if(result&&mtype.contains("App")){ // QByteArray download_success("{\"cmd\":\"warn\",\"ext\":{\"status\":\"App DownLoad Success!\"}}"); // mqttHandler.publish_m(download_success); emit mqttHandler.appInstall(); } }); // QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, // &app, [url](QObject *obj, const QUrl &objUrl) { // if (!obj && url == objUrl) // QCoreApplication::exit(-1); // }, Qt::QueuedConnection); // 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 // this is the preferred deployment option for publishing apps to the app stores, because then your qml files and js files are protected // to avoid deployment of your qml files and images, also comment the deploy_resources command in the CMakeLists file // also see the CMakeLists.txt file for more details //felgo.setMainQmlFileName(QStringLiteral("qrc:/qml/Main.qml")); engine.load(QUrl(felgo.mainQmlFileName())); QObject *rootItem = engine.rootObjects().value(0); if(rootItem) { QObject::connect(&mqttHandler,&MqttHandler::autotakephoto,[&](bool stat){ QObject *Navigation_main = rootItem->findChild("Navigation_main"); if(Navigation_main){ qDebug() << "Navigation_main is !!!!!"<findChild("autoFocus_Page"); if(autoFocus_Page){ QMetaObject::invokeMethod(autoFocus_Page, "autotake",Q_ARG(QVariant,stat)); //将param::stitchphoto保存 } } }); //mqtt到QML,控制qml跳转到自动对焦界面,开始对焦拍照 QObject::connect(&serialThread,&SerialThread::auto_takephoto,[&](){ QObject *Navigation_main = rootItem->findChild("Navigation_main"); if(Navigation_main){ qDebug() << "Navigation_main is !!!!!"; QMetaObject::invokeMethod(Navigation_main, "setCurrentTab", Q_ARG(QVariant, 1)); //打开相机 QObject *autoFocus_Page = rootItem->findChild("autoFocus_Page"); if(autoFocus_Page){ QMetaObject::invokeMethod(autoFocus_Page, "autotake",Q_ARG(QVariant,true)); } } }); //串口到QML,控制qml跳转到自动对焦界面,结束对焦拍照 //获取按钮点击事件 QObject *systemset_Item = rootItem->findChild("systemset_Item"); if(systemset_Item){ QObject::connect(systemset_Item, SIGNAL(selected()),&myCppClass,SLOT(systemPage())); QObject::connect(&myCppClass, &MyCppClass::tosystempage,[&](){ QObject *openbar_btn = rootItem->findChild("openbar_btn"); //打开状态栏 if(openbar_btn){ QObject::connect(openbar_btn, SIGNAL(opernBar()),&myCppClass,SLOT(openbar_btnClicked())); QObject::connect(&myCppClass, &MyCppClass::opensystembar,[&](){ sendBroadcastFromQt("openbar_btn"); }); QObject::connect(openbar_btn, SIGNAL(closeBar()),&myCppClass,SLOT(closebar_btnClicked())); QObject::connect(&myCppClass, &MyCppClass::closesystembar,[&](){ sendBroadcastFromQt("closebar_btn"); }); } QObject *opennav_btn = rootItem->findChild("opennav_btn"); //控制导航栏 if(opennav_btn){ QObject::connect(opennav_btn, SIGNAL(opernNav()),&myCppClass,SLOT(opennav_btnClicked())); QObject::connect(&myCppClass, &MyCppClass::opensystemnav,[&](){ sendBroadcastFromQt("opennav_btn"); }); QObject::connect(opennav_btn, SIGNAL(closeNav()),&myCppClass,SLOT(closenav_btnClicked())); QObject::connect(&myCppClass, &MyCppClass::closesystemnav,[&](){ sendBroadcastFromQt("closenav_btn"); }); } }); } // 获取根 QML 对象 QObject *autofocus_item = rootItem->findChild("autofocus_Item"); if(autofocus_item){ bool isConnect_frame = true; //const QMetaObject *metaObj = autofocus_item->metaObject(); LOG("Found autofocus_item!"); QObject::connect(autofocus_item, SIGNAL(selected()),&myCppClass,SLOT(qmlSlot())); QObject::connect(&myCppClass, &MyCppClass::recvqmlslot,[&](){ LOG("connect myCppClass"); Qt6QCamera::Frames *m_frames = qobject_cast(rootItem->findChild("Frames")); if(!m_frames) {ERROR("m_frames is null!!!");} if(m_frames&&isConnect_frame){ LOG("m_frames connected!"); //engine.addImageProvider("ucamera",m_frames->getImageProvider()); QObject::connect(m_frames,&Qt6QCamera::Frames::serial_data,&serialThread,&SerialThread::sendData); //Frame到串口 QObject::connect(&serialThread,&SerialThread::takephotodone,m_frames,&Qt6QCamera::Frames::saveImage); //串口到QML,控制qml跳转到自动对焦界面 //QObject::connect(m_frames,&Qt6QCamera::Frames::signal_stitch,&imagestitcher,&ImageStitcher::stitch_thread); QObject::connect(&serialThread,&SerialThread::signal_movy,m_frames,&Qt6QCamera::Frames::onMovDone); QObject::connect(m_frames,&Qt6QCamera::Frames::dataUpload,&mqttHandler,&MqttHandler::publish_m); //frames到MQTT // 修改信号槽连接方式 QObject::connect(m_frames, &Qt6QCamera::Frames::signal_wait_upload, &uploader_ftp, &CurlFtpUploader::uploadFileAsync); QObject::connect(&uploader_ftp, &CurlFtpUploader::finished, m_frames,&Qt6QCamera::Frames::handle_reult_upload); isConnect_frame = false; }else{ ERROR("m_frames no !!!!!"); } }); }else{ qDebug() << "autofocus_item no !!!!!"; } }else{ ERROR("rootItem is null !!!!!"); } LOG("app start!"); GPSInfo *gps_start = new GPSInfo(sqlConfig,&app); QObject::connect(gps_start,&GPSInfo::position_success,[&](){ emit serialThread.gpsSuccess(); }); // to start your project with Felgo Hot Reload, comment (remove) the lines "felgo.setMainQmlFileName ..." & "engine.load ...", // and uncomment the line below //FelgoHotReload felgoHotReload(&engine); return app.exec(); } #include "main.moc"