diff --git a/CMakeLists.txt b/CMakeLists.txt index d5331bb2..31563492 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,8 +87,8 @@ set(CMAKE_AUTORCC ON) # set the module path set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules) -# qt sql Network -find_package(Qt6 REQUIRED COMPONENTS Widgets Qml Core Charts Quick SerialBus SerialPort) +# qt +find_package(Qt6 REQUIRED COMPONENTS Widgets Qml Core Network Sql Charts Quick SerialBus SerialPort) qt_standard_project_setup() @@ -678,7 +678,7 @@ else() target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Widgets Qt6::Qml -# Qt6::Sql + Qt6::Sql Qt6::Charts m dl diff --git a/src/cdlgknownguid.cpp b/src/cdlgknownguid.cpp index 3e4d280c..c95c2d06 100644 --- a/src/cdlgknownguid.cpp +++ b/src/cdlgknownguid.cpp @@ -128,7 +128,7 @@ CDlgKnownGuid::fillGuidFromDb(void) { vscpworks* pworks = (vscpworks*)QCoreApplication::instance(); - /*pworks->m_mutexGuidMap.lock(); + pworks->m_mutexGuidMap.lock(); QSqlQuery query("SELECT * FROM guid order by name", pworks->m_worksdb); @@ -139,7 +139,7 @@ CDlgKnownGuid::fillGuidFromDb(void) insertGuidItem(guid, name); } - pworks->m_mutexGuidMap.unlock();*/ + pworks->m_mutexGuidMap.unlock(); } /////////////////////////////////////////////////////////////////////////////// @@ -201,7 +201,7 @@ void CDlgKnownGuid::listItemClicked(QTableWidgetItem* item) { vscpworks* pworks = (vscpworks*)QCoreApplication::instance(); -/* + int currentRow = ui->listGuid->selectionModel()->currentIndex().row(); if (-1 == currentRow) { currentRow = 0; // First row @@ -235,7 +235,6 @@ CDlgKnownGuid::listItemClicked(QTableWidgetItem* item) } pworks->m_mutexGuidMap.unlock(); -*/ } /////////////////////////////////////////////////////////////////////////////// @@ -460,7 +459,7 @@ CDlgKnownGuid::btnAdd(void) } QString strQuery = tr("INSERT INTO guid (guid, name, description) VALUES ('%1', '%2', '%3');"); -/* + pworks->m_mutexGuidMap.lock(); QSqlQuery query(strQuery .arg(strguid) @@ -476,7 +475,7 @@ CDlgKnownGuid::btnAdd(void) query.lastError().text().toStdString()); goto again; } -*/ + // Add to the internal table pworks->m_mapGuidToSymbolicName[strguid] = dlg.getName(); pworks->m_mutexGuidMap.unlock(); @@ -532,7 +531,7 @@ CDlgKnownGuid::btnEdit(void) QString strQuery = tr("SELECT * FROM guid WHERE guid='%1';"); pworks->m_mutexGuidMap.lock(); - /*QSqlQuery query(strQuery.arg(strguid), pworks->m_worksdb); + QSqlQuery query(strQuery.arg(strguid), pworks->m_worksdb); if (QSqlError::NoError != query.lastError().type()) { pworks->m_mutexGuidMap.unlock(); @@ -552,7 +551,7 @@ CDlgKnownGuid::btnEdit(void) } pworks->m_mutexGuidMap.unlock(); -*/ + again: if (QDialog::Accepted == dlg.exec()) { @@ -561,22 +560,22 @@ CDlgKnownGuid::btnEdit(void) QString strQuery = tr("UPDATE guid SET name='%1', description='%2' WHERE guid='%3';"); pworks->m_mutexGuidMap.lock(); - // QSqlQuery query(strQuery - // .arg(strname) - // .arg(strdescription) - // .arg(strguid), - // pworks->m_worksdb); - // if (QSqlError::NoError != query.lastError().type()) { - // pworks->m_mutexGuidMap.unlock(); - // QMessageBox::information(this, - // tr(APPNAME), - // tr("Unable to save edited GUID into database.\n\n Error =") + query.lastError().text(), - // QMessageBox::Ok); - - // spdlog::error(std::string(tr("Unable to save edited GUID into database. Err =").toStdString()) + - // query.lastError().text().toStdString()); - // goto again; - // } + QSqlQuery query(strQuery + .arg(strname) + .arg(strdescription) + .arg(strguid), + pworks->m_worksdb); + if (QSqlError::NoError != query.lastError().type()) { + pworks->m_mutexGuidMap.unlock(); + QMessageBox::information(this, + tr(APPNAME), + tr("Unable to save edited GUID into database.\n\n Error =") + query.lastError().text(), + QMessageBox::Ok); + + spdlog::error(std::string(tr("Unable to save edited GUID into database. Err =").toStdString()) + + query.lastError().text().toStdString()); + goto again; + } // Add to the internal table pworks->m_mapGuidToSymbolicName[strguid] = dlg.getName(); @@ -617,26 +616,26 @@ CDlgKnownGuid::btnClone(void) QString strguid = itemGuid->text(); strguid = strguid.trimmed(); - // QString strQuery = tr("SELECT * FROM guid WHERE guid='%1';"); - // pworks->m_mutexGuidMap.lock(); - // QSqlQuery query(strQuery.arg(strguid), pworks->m_worksdb); - - // if (QSqlError::NoError != query.lastError().type()) { - // pworks->m_mutexGuidMap.unlock(); - // QMessageBox::information(this, - // tr(APPNAME), - // tr("Unable to find record in database.\n\n Error =") + query.lastError().text(), - // QMessageBox::Ok); - // spdlog::error(std::string(tr("Unable to find record in database. Err =").toStdString()) + - // query.lastError().text().toStdString()); - // return; - // } - - // if (query.next()) { - // dlg.setGuid(query.value(1).toString()); - // dlg.setName(query.value(2).toString()); - // dlg.setDescription(query.value(3).toString()); - // } + QString strQuery = tr("SELECT * FROM guid WHERE guid='%1';"); + pworks->m_mutexGuidMap.lock(); + QSqlQuery query(strQuery.arg(strguid), pworks->m_worksdb); + + if (QSqlError::NoError != query.lastError().type()) { + pworks->m_mutexGuidMap.unlock(); + QMessageBox::information(this, + tr(APPNAME), + tr("Unable to find record in database.\n\n Error =") + query.lastError().text(), + QMessageBox::Ok); + spdlog::error(std::string(tr("Unable to find record in database. Err =").toStdString()) + + query.lastError().text().toStdString()); + return; + } + + if (query.next()) { + dlg.setGuid(query.value(1).toString()); + dlg.setName(query.value(2).toString()); + dlg.setDescription(query.value(3).toString()); + } pworks->m_mutexGuidMap.unlock(); @@ -673,22 +672,22 @@ CDlgKnownGuid::btnClone(void) QString strQuery = tr("INSERT INTO guid (guid, name, description) VALUES ('%1', '%2', '%3');"); - // pworks->m_mutexGuidMap.lock(); - // QSqlQuery query(strQuery - // .arg(strguid) - // .arg(dlg.getName()) - // .arg(dlg.getDescription()), - // pworks->m_worksdb); - // if (QSqlError::NoError != query.lastError().type()) { - // pworks->m_mutexGuidMap.unlock(); - // QMessageBox::information(this, - // tr(APPNAME), - // tr("Unable to save GUID into database (duplicate?).\n\n Error =") + query.lastError().text(), - // QMessageBox::Ok); - // spdlog::error(std::string(tr("Unable to save GUID into database (duplicate?). Err =").toStdString()) + - // query.lastError().text().toStdString()); - // goto again; - // } + pworks->m_mutexGuidMap.lock(); + QSqlQuery query(strQuery + .arg(strguid) + .arg(dlg.getName()) + .arg(dlg.getDescription()), + pworks->m_worksdb); + if (QSqlError::NoError != query.lastError().type()) { + pworks->m_mutexGuidMap.unlock(); + QMessageBox::information(this, + tr(APPNAME), + tr("Unable to save GUID into database (duplicate?).\n\n Error =") + query.lastError().text(), + QMessageBox::Ok); + spdlog::error(std::string(tr("Unable to save GUID into database (duplicate?). Err =").toStdString()) + + query.lastError().text().toStdString()); + goto again; + } // Add to the internal table pworks->m_mapGuidToSymbolicName[strguid] = dlg.getName(); @@ -727,32 +726,32 @@ CDlgKnownGuid::btnDelete(void) QString strguid = item->text(); strguid = strguid.trimmed(); - // QString strQuery = tr("DELETE FROM guid WHERE guid='%1';"); - // pworks->m_mutexGuidMap.lock(); - // QSqlQuery query(strQuery.arg(strguid), pworks->m_worksdb); - - // if (QSqlError::NoError != query.lastError().type()) { - // pworks->m_mutexGuidMap.unlock(); - // QMessageBox::information(this, - // tr(APPNAME), - // tr("Unable to delete GUID.\n\n Error =") + query.lastError().text(), - // QMessageBox::Ok); - // spdlog::error(std::string(tr("Unable to delete GUID. Err =").toStdString()) + - // query.lastError().text().toStdString()); - // return; - // } - // else { - - // // Delete row - // ui->listGuid->removeRow(row); - - // // Delete from internal table - // pworks->m_mapGuidToSymbolicName.erase(strguid); - // // std::map::iterator it = pworks->m_mapGuidToSymbolicName.find(strguid); - // // if (std::map::end != it) { - - // // } - // } + QString strQuery = tr("DELETE FROM guid WHERE guid='%1';"); + pworks->m_mutexGuidMap.lock(); + QSqlQuery query(strQuery.arg(strguid), pworks->m_worksdb); + + if (QSqlError::NoError != query.lastError().type()) { + pworks->m_mutexGuidMap.unlock(); + QMessageBox::information(this, + tr(APPNAME), + tr("Unable to delete GUID.\n\n Error =") + query.lastError().text(), + QMessageBox::Ok); + spdlog::error(std::string(tr("Unable to delete GUID. Err =").toStdString()) + + query.lastError().text().toStdString()); + return; + } + else { + + // Delete row + ui->listGuid->removeRow(row); + + // Delete from internal table + pworks->m_mapGuidToSymbolicName.erase(strguid); + // std::map::iterator it = pworks->m_mapGuidToSymbolicName.find(strguid); + // if (std::map::end != it) { + + // } + } pworks->m_mutexGuidMap.unlock(); } diff --git a/src/cdlgsensorindex.cpp b/src/cdlgsensorindex.cpp index b87c9c5e..f82ce04a 100644 --- a/src/cdlgsensorindex.cpp +++ b/src/cdlgsensorindex.cpp @@ -104,17 +104,17 @@ CDlgSensorIndex::CDlgSensorIndex(QWidget *parent, int link_to_guid) : pworks->m_mutexSensorIndexMap.lock(); - // QString strQuery = "SELECT * FROM sensorindex WHERE link_to_guid = %1 ORDER BY sensor;"; - // QSqlQuery query(strQuery.arg(m_link_to_guid), pworks->m_worksdb); + QString strQuery = "SELECT * FROM sensorindex WHERE link_to_guid = %1 ORDER BY sensor;"; + QSqlQuery query(strQuery.arg(m_link_to_guid), pworks->m_worksdb); - // while (query.next()) { - // QString sensor = query.value(2).toString(); - // QString name = query.value(3).toString(); + while (query.next()) { + QString sensor = query.value(2).toString(); + QString name = query.value(3).toString(); - // insertSensorIndexItem(sensor, name); - // } + insertSensorIndexItem(sensor, name); + } - // pworks->m_mutexSensorIndexMap.unlock(); + pworks->m_mutexSensorIndexMap.unlock(); } /////////////////////////////////////////////////////////////////////////////// @@ -177,31 +177,31 @@ void CDlgSensorIndex::listItemClicked(QTableWidgetItem *item) QString strsensor = itemSensor->text(); // Search db record for description -// QString strQuery = tr("SELECT * FROM sensorindex WHERE sensor=%1 AND link_to_guid=%2;"); -// pworks->m_mutexSensorIndexMap.lock(); -// QSqlQuery query(strQuery.arg(strsensor.toInt()).arg(m_link_to_guid), pworks->m_worksdb); - -// // Check for database operation error -// if (QSqlError::NoError != query.lastError().type()) { -// spdlog::error(std::string(tr("Unable to find record in database. Err =").toStdString() -// + query.lastError().text().toStdString())); -// QMessageBox::information(this, -// tr(APPNAME), -// tr("Unable to find record in database.\n\n Error =") + query.lastError().text(), -// QMessageBox::Ok ); -// pworks->m_mutexSensorIndexMap.unlock(); -// return; -// } - -// if (query.next()) { -// #if QT_VERSION >= 0x050E00 -// ui->textDescription->setMarkdown(query.value(4).toString()); -// #else -// ui->textDescription->setText(query.value(3).toString()); -// #endif -// } - -// pworks->m_mutexSensorIndexMap.unlock(); + QString strQuery = tr("SELECT * FROM sensorindex WHERE sensor=%1 AND link_to_guid=%2;"); + pworks->m_mutexSensorIndexMap.lock(); + QSqlQuery query(strQuery.arg(strsensor.toInt()).arg(m_link_to_guid), pworks->m_worksdb); + + // Check for database operation error + if (QSqlError::NoError != query.lastError().type()) { + spdlog::error(std::string(tr("Unable to find record in database. Err =").toStdString() + + query.lastError().text().toStdString())); + QMessageBox::information(this, + tr(APPNAME), + tr("Unable to find record in database.\n\n Error =") + query.lastError().text(), + QMessageBox::Ok ); + pworks->m_mutexSensorIndexMap.unlock(); + return; + } + + if (query.next()) { +#if QT_VERSION >= 0x050E00 + ui->textDescription->setMarkdown(query.value(4).toString()); +#else + ui->textDescription->setText(query.value(3).toString()); +#endif + } + + pworks->m_mutexSensorIndexMap.unlock(); } /////////////////////////////////////////////////////////////////////////////// @@ -425,30 +425,30 @@ void CDlgSensorIndex::btnAdd(void) QString strQuery = tr("INSERT INTO sensorindex (link_to_guid, sensor, name, description) VALUES (%1, %2, '%3', '%4');"); - // pworks->m_mutexSensorIndexMap.lock(); - // QSqlQuery query(strQuery - // .arg(m_link_to_guid) - // .arg(dlg.getSensor()) - // .arg(dlg.getName()) - // .arg(dlg.getDescription()), - // pworks->m_worksdb); + pworks->m_mutexSensorIndexMap.lock(); + QSqlQuery query(strQuery + .arg(m_link_to_guid) + .arg(dlg.getSensor()) + .arg(dlg.getName()) + .arg(dlg.getDescription()), + pworks->m_worksdb); - // // Check for database operation error - // if (QSqlError::NoError != query.lastError().type()) { - // spdlog::error(std::string(tr("Unable to save GUID into database (duplicate?). Err =").toStdString() - // + query.lastError().text().toStdString())); - // QMessageBox::information(this, - // tr(APPNAME), - // tr("Unable to save sensor into database (duplicate?).\n\n Error =") + query.lastError().text(), - // QMessageBox::Ok ); - // pworks->m_mutexSensorIndexMap.unlock(); - // goto again; - // } - - // // Add to the internal table - // pworks->m_mapSensorIndexToSymbolicName[(m_link_to_guid << 8) + dlg.getSensor()] = dlg.getName(); - - // pworks->m_mutexSensorIndexMap.unlock(); + // Check for database operation error + if (QSqlError::NoError != query.lastError().type()) { + spdlog::error(std::string(tr("Unable to save GUID into database (duplicate?). Err =").toStdString() + + query.lastError().text().toStdString())); + QMessageBox::information(this, + tr(APPNAME), + tr("Unable to save sensor into database (duplicate?).\n\n Error =") + query.lastError().text(), + QMessageBox::Ok ); + pworks->m_mutexSensorIndexMap.unlock(); + goto again; + } + + // Add to the internal table + pworks->m_mapSensorIndexToSymbolicName[(m_link_to_guid << 8) + dlg.getSensor()] = dlg.getName(); + + pworks->m_mutexSensorIndexMap.unlock(); // Add to dialog List insertSensorIndexItem(QString::number(dlg.getSensor()), dlg.getName()); @@ -499,25 +499,25 @@ void CDlgSensorIndex::btnEdit(void) QString strQuery = tr("SELECT * FROM sensorindex WHERE sensor=%1 AND link_to_guid=%2;"); pworks->m_mutexSensorIndexMap.lock(); - // QSqlQuery query(strQuery.arg(strsensor).arg(m_link_to_guid), pworks->m_worksdb); - - // // Check for database operation error - // if (QSqlError::NoError != query.lastError().type()) { - // spdlog::error(std::string(tr("Unable to find record in database. Err =").toStdString() - // + query.lastError().text().toStdString())); - // QMessageBox::information(this, - // tr(APPNAME), - // tr("Unable to find record in database.\n\n Error =") + query.lastError().text(), - // QMessageBox::Ok ); - // pworks->m_mutexSensorIndexMap.unlock(); - // return; - // } + QSqlQuery query(strQuery.arg(strsensor).arg(m_link_to_guid), pworks->m_worksdb); + + // Check for database operation error + if (QSqlError::NoError != query.lastError().type()) { + spdlog::error(std::string(tr("Unable to find record in database. Err =").toStdString() + + query.lastError().text().toStdString())); + QMessageBox::information(this, + tr(APPNAME), + tr("Unable to find record in database.\n\n Error =") + query.lastError().text(), + QMessageBox::Ok ); + pworks->m_mutexSensorIndexMap.unlock(); + return; + } - // if (query.next()) { - // dlg.setSensor(query.value(2).toInt()); - // dlg.setName(query.value(3).toString()); - // dlg.setDescription(query.value(4).toString()); - // } + if (query.next()) { + dlg.setSensor(query.value(2).toInt()); + dlg.setName(query.value(3).toString()); + dlg.setDescription(query.value(4).toString()); + } pworks->m_mutexSensorIndexMap.unlock(); @@ -529,24 +529,24 @@ void CDlgSensorIndex::btnEdit(void) QString strQuery = tr("UPDATE sensorindex SET name='%1', description='%2' WHERE sensor='%3' AND link_to_guid=%4;"); pworks->m_mutexSensorIndexMap.lock(); - // QSqlQuery query(strQuery - // .arg(strname) - // .arg(strdescription) - // .arg(strsensor) - // .arg(m_link_to_guid), - // pworks->m_worksdb); - - // // Check for database operation error - // if (QSqlError::NoError != query.lastError().type()) { - // spdlog::error(std::string(tr("Unable to save edited GUID into database. Err =").toStdString() - // + query.lastError().text().toStdString())); - // QMessageBox::information(this, - // tr(APPNAME), - // tr("Unable to save edited GUID into database.\n\n Error =") + query.lastError().text(), - // QMessageBox::Ok ); - // pworks->m_mutexSensorIndexMap.unlock(); - // goto again; - // } + QSqlQuery query(strQuery + .arg(strname) + .arg(strdescription) + .arg(strsensor) + .arg(m_link_to_guid), + pworks->m_worksdb); + + // Check for database operation error + if (QSqlError::NoError != query.lastError().type()) { + spdlog::error(std::string(tr("Unable to save edited GUID into database. Err =").toStdString() + + query.lastError().text().toStdString())); + QMessageBox::information(this, + tr(APPNAME), + tr("Unable to save edited GUID into database.\n\n Error =") + query.lastError().text(), + QMessageBox::Ok ); + pworks->m_mutexSensorIndexMap.unlock(); + goto again; + } // Add to the in memory table pworks->m_mapSensorIndexToSymbolicName[(m_link_to_guid << 8) + dlg.getSensor()] = dlg.getName(); @@ -589,25 +589,25 @@ void CDlgSensorIndex::btnClone(void) QString strQuery = tr("SELECT * FROM sensorindex WHERE sensor=%1 AND link_to_guid=%2;"); pworks->m_mutexSensorIndexMap.lock(); - // QSqlQuery query(strQuery.arg(strsensor).arg(m_link_to_guid), pworks->m_worksdb); - - // // Check for database operation error - // if (QSqlError::NoError != query.lastError().type()) { - // spdlog::error(std::string(tr("Unable to find record in database Err =").toStdString() - // + query.lastError().text().toStdString())); - // QMessageBox::information(this, - // tr(APPNAME), - // tr("Unable to find record in database.\n\n Error =") + query.lastError().text(), - // QMessageBox::Ok ); - // pworks->m_mutexSensorIndexMap.unlock(); - // return; - // } + QSqlQuery query(strQuery.arg(strsensor).arg(m_link_to_guid), pworks->m_worksdb); + + // Check for database operation error + if (QSqlError::NoError != query.lastError().type()) { + spdlog::error(std::string(tr("Unable to find record in database Err =").toStdString() + + query.lastError().text().toStdString())); + QMessageBox::information(this, + tr(APPNAME), + tr("Unable to find record in database.\n\n Error =") + query.lastError().text(), + QMessageBox::Ok ); + pworks->m_mutexSensorIndexMap.unlock(); + return; + } - // if (query.next()) { - // //dlg.setSensor(query.value(2).toInt()); - // dlg.setName(query.value(3).toString()); - // dlg.setDescription(query.value(4).toString()); - // } + if (query.next()) { + //dlg.setSensor(query.value(2).toInt()); + dlg.setName(query.value(3).toString()); + dlg.setDescription(query.value(4).toString()); + } pworks->m_mutexSensorIndexMap.unlock(); @@ -616,30 +616,30 @@ void CDlgSensorIndex::btnClone(void) QString strQuery = tr("INSERT INTO sensorindex (link_to_guid, sensor, name, description) VALUES (%1, %2, '%3', '%4');"); - // pworks->m_mutexSensorIndexMap.lock(); - // QSqlQuery query(strQuery - // .arg(m_link_to_guid) - // .arg(dlg.getSensor()) - // .arg(dlg.getName()) - // .arg(dlg.getDescription()), - // pworks->m_worksdb); + pworks->m_mutexSensorIndexMap.lock(); + QSqlQuery query(strQuery + .arg(m_link_to_guid) + .arg(dlg.getSensor()) + .arg(dlg.getName()) + .arg(dlg.getDescription()), + pworks->m_worksdb); - // // Check for database operation error - // if (QSqlError::NoError != query.lastError().type()) { - // spdlog::error(std::string(tr("Unable to save GUID into database (duplicate?). Err =").toStdString() - // + query.lastError().text().toStdString())); - // QMessageBox::information(this, - // tr(APPNAME), - // tr("Unable to save sensor into database (duplicate?).\n\n Error =") + query.lastError().text(), - // QMessageBox::Ok ); - // pworks->m_mutexSensorIndexMap.unlock(); - // goto again; - // } - - // // Add to the in memory table - // pworks->m_mapSensorIndexToSymbolicName[(m_link_to_guid << 8) + dlg.getSensor()] = dlg.getName(); - - // pworks->m_mutexSensorIndexMap.unlock(); + // Check for database operation error + if (QSqlError::NoError != query.lastError().type()) { + spdlog::error(std::string(tr("Unable to save GUID into database (duplicate?). Err =").toStdString() + + query.lastError().text().toStdString())); + QMessageBox::information(this, + tr(APPNAME), + tr("Unable to save sensor into database (duplicate?).\n\n Error =") + query.lastError().text(), + QMessageBox::Ok ); + pworks->m_mutexSensorIndexMap.unlock(); + goto again; + } + + // Add to the in memory table + pworks->m_mapSensorIndexToSymbolicName[(m_link_to_guid << 8) + dlg.getSensor()] = dlg.getName(); + + pworks->m_mutexSensorIndexMap.unlock(); // Add to dialog List insertSensorIndexItem(QString::number(dlg.getSensor()), dlg.getName()); @@ -686,27 +686,27 @@ void CDlgSensorIndex::btnDelete(void) QString strQuery = tr("DELETE FROM sensorindex WHERE sensor='%1' AND link_to_guid=%2;"); pworks->m_mutexSensorIndexMap.lock(); - // QSqlQuery query(strQuery.arg(strsensor).arg(m_link_to_guid), pworks->m_worksdb); - - // // Check for database operation error - // if (QSqlError::NoError != query.lastError().type()) { - // spdlog::error(std::string(tr("Unable to delete sensor. Err =").toStdString() - // + query.lastError().text().toStdString())); - // QMessageBox::information(this, - // tr(APPNAME), - // tr("Unable to delete sensor.\n\n Error =") + query.lastError().text(), - // QMessageBox::Ok ); - // } - // else { + QSqlQuery query(strQuery.arg(strsensor).arg(m_link_to_guid), pworks->m_worksdb); - // // Delete row - // ui->listSensors->removeRow(row); + // Check for database operation error + if (QSqlError::NoError != query.lastError().type()) { + spdlog::error(std::string(tr("Unable to delete sensor. Err =").toStdString() + + query.lastError().text().toStdString())); + QMessageBox::information(this, + tr(APPNAME), + tr("Unable to delete sensor.\n\n Error =") + query.lastError().text(), + QMessageBox::Ok ); + } + else { - // // Delete from in memory table - // pworks->m_mapSensorIndexToSymbolicName.erase((m_link_to_guid << 8) + strsensor.toInt()); - // } + // Delete row + ui->listSensors->removeRow(row); + + // Delete from in memory table + pworks->m_mapSensorIndexToSymbolicName.erase((m_link_to_guid << 8) + strsensor.toInt()); + } - // pworks->m_mutexSensorIndexMap.unlock(); + pworks->m_mutexSensorIndexMap.unlock(); } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/cfrmmdf.cpp b/src/cfrmmdf.cpp index 64f0bd14..0d09d08a 100644 --- a/src/cfrmmdf.cpp +++ b/src/cfrmmdf.cpp @@ -78,12 +78,12 @@ #include #include #include -//#include +#include #include #include #include #include -//#include +#include #include // ---------------------------------------------------------------------------- diff --git a/src/cfrmnodeconfig.cpp b/src/cfrmnodeconfig.cpp index f231b0bf..0d3535c8 100644 --- a/src/cfrmnodeconfig.cpp +++ b/src/cfrmnodeconfig.cpp @@ -79,13 +79,13 @@ #include #include #include -//#include +#include #include #include #include #include #include -//#include +#include #include #include diff --git a/src/cfrmnodeconfig.h b/src/cfrmnodeconfig.h index 153d3b0f..3b398076 100644 --- a/src/cfrmnodeconfig.h +++ b/src/cfrmnodeconfig.h @@ -44,8 +44,7 @@ #include #include #include -#include -//#include +#include QT_BEGIN_NAMESPACE class QAction; diff --git a/src/cfrmnodescan.cpp b/src/cfrmnodescan.cpp index 6b14bc19..aaa6fe85 100644 --- a/src/cfrmnodescan.cpp +++ b/src/cfrmnodescan.cpp @@ -67,12 +67,12 @@ #include #include #include -//#include +#include #include #include #include #include -//#include +#include #include // ---------------------------------------------------------------------------- diff --git a/src/cfrmsession.cpp b/src/cfrmsession.cpp index a177a2bc..05440b4b 100644 --- a/src/cfrmsession.cpp +++ b/src/cfrmsession.cpp @@ -65,13 +65,13 @@ #include #include #include -//#include +#include #include #include #include #include #include -//#include +#include #include // ---------------------------------------------------------------------------- diff --git a/src/cfrmsession.h b/src/cfrmsession.h index 498dd82e..b3a88007 100644 --- a/src/cfrmsession.h +++ b/src/cfrmsession.h @@ -43,10 +43,7 @@ #include #include #include -#include -#include -#include -//#include +#include #include QT_BEGIN_NAMESPACE diff --git a/src/main.cpp b/src/main.cpp index cb93a3be..7f78d439 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,6 @@ #include "vscp.h" #include "vscphelper.h" -#include #include #include diff --git a/src/vscpworks.cpp b/src/vscpworks.cpp index 683fa043..acb5c577 100644 --- a/src/vscpworks.cpp +++ b/src/vscpworks.cpp @@ -47,15 +47,14 @@ #include #include #include -#include #include #include #include #include #include #include -// #include -// #include +#include +#include #include #include #include @@ -138,7 +137,7 @@ vscpworks::~vscpworks() break; } } - //m_worksdb.close(); + m_worksdb.close(); } /////////////////////////////////////////////////////////////////////////////// @@ -610,7 +609,6 @@ vscpworks::removeConnection(const QString& uuid, bool bSave) bool vscpworks::loadEventDb(void) { -/* m_mutexVscpEventsMaps.lock(); m_evdb = QSqlDatabase::addDatabase("QSQLITE", "vscpevents"); @@ -656,7 +654,6 @@ vscpworks::loadEventDb(void) } m_mutexVscpEventsMaps.unlock(); -*/ return true; } @@ -697,53 +694,53 @@ vscpworks::openVscpWorksDatabase(void) // Set up database QString eventdbname = m_shareFolder + "vscpworks.sqlite3"; - // QString dbName(eventdbname); - // m_worksdb = QSqlDatabase::addDatabase("QSQLITE", "vscpworks"); - // m_worksdb.setDatabaseName(dbName); - // m_worksdb.open(); + QString dbName(eventdbname); + m_worksdb = QSqlDatabase::addDatabase("QSQLITE", "vscpworks"); + m_worksdb.setDatabaseName(dbName); + m_worksdb.open(); // Create GUID table if it does not exist - // QSqlQuery query = QSqlQuery(m_worksdb); - // if (!query.exec("CREATE TABLE IF NOT EXISTS guid (" - // "idx INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE," - // "guid TEXT UNIQUE," - // "name TEXT," - // "description TEXT);")) { - // qDebug() << query.lastError(); - // return false; - // } + QSqlQuery query = QSqlQuery(m_worksdb); + if (!query.exec("CREATE TABLE IF NOT EXISTS guid (" + "idx INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE," + "guid TEXT UNIQUE," + "name TEXT," + "description TEXT);")) { + qDebug() << query.lastError(); + return false; + } // Create GUID name index - // if (!query.exec("CREATE INDEX IF NOT EXISTS \"idxGuidName\" ON \"guid\" (\"guid\" ASC)")) { - // qDebug() << query.lastError(); - // return false; - // } - - // if (!query.exec("CREATE TABLE IF NOT EXISTS \"sensorindex\" (" - // "\"idx\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE," - // "\"link_to_guid\" INTEGER, " - // "\"sensor\" INTEGER, " - // "\"name\" TEXT, " - // "\"description\" TEXT );")) { - // qDebug() << query.lastError(); - // return false; - // } - - // // Create sensor link + idx unique index - // if (!query.exec("CREATE UNIQUE INDEX IF NOT EXISTS \"idxSensors\" ON \"sensorindex\" (\"link_to_guid\" ASC, \"sensor\" ASC)")) { - // qDebug() << query.lastError(); - // return false; - // } - - // // Create log table if it does not exist - // if (!query.exec("CREATE TABLE IF NOT EXISTS log (" - // "idx INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE," - // "level INTEGER," - // "datetime TEXT," - // "message TEXT);")) { - // qDebug() << query.lastError(); - // return false; - // } + if (!query.exec("CREATE INDEX IF NOT EXISTS \"idxGuidName\" ON \"guid\" (\"guid\" ASC)")) { + qDebug() << query.lastError(); + return false; + } + + if (!query.exec("CREATE TABLE IF NOT EXISTS \"sensorindex\" (" + "\"idx\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE," + "\"link_to_guid\" INTEGER, " + "\"sensor\" INTEGER, " + "\"name\" TEXT, " + "\"description\" TEXT );")) { + qDebug() << query.lastError(); + return false; + } + + // Create sensor link + idx unique index + if (!query.exec("CREATE UNIQUE INDEX IF NOT EXISTS \"idxSensors\" ON \"sensorindex\" (\"link_to_guid\" ASC, \"sensor\" ASC)")) { + qDebug() << query.lastError(); + return false; + } + + // Create log table if it does not exist + if (!query.exec("CREATE TABLE IF NOT EXISTS log (" + "idx INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE," + "level INTEGER," + "datetime TEXT," + "message TEXT);")) { + qDebug() << query.lastError(); + return false; + } // Load known GUID's to memory loadGuidTable(); @@ -763,17 +760,17 @@ vscpworks::loadGuidTable(void) { m_mutexGuidMap.lock(); - // QSqlQuery query("SELECT * FROM guid order by name", m_worksdb); - // if (QSqlError::NoError != query.lastError().type()) { - // m_mutexGuidMap.unlock(); - // return false; - // } + QSqlQuery query("SELECT * FROM guid order by name", m_worksdb); + if (QSqlError::NoError != query.lastError().type()) { + m_mutexGuidMap.unlock(); + return false; + } - // while (query.next()) { - // QString guid = query.value(1).toString(); - // QString name = query.value(2).toString(); - // m_mapGuidToSymbolicName[guid] = name; - // } + while (query.next()) { + QString guid = query.value(1).toString(); + QString name = query.value(2).toString(); + m_mapGuidToSymbolicName[guid] = name; + } m_mutexGuidMap.unlock(); return true; @@ -788,18 +785,18 @@ vscpworks::loadSensorTable(void) { m_mutexSensorIndexMap.lock(); - // QSqlQuery query("SELECT * FROM sensorindex order by sensor", m_worksdb); - // if (QSqlError::NoError != query.lastError().type()) { - // m_mutexSensorIndexMap.unlock(); - // return false; - // } + QSqlQuery query("SELECT * FROM sensorindex order by sensor", m_worksdb); + if (QSqlError::NoError != query.lastError().type()) { + m_mutexSensorIndexMap.unlock(); + return false; + } - // while (query.next()) { - // int link_to_guid = query.value(1).toInt(); - // int sensor = query.value(2).toInt(); - // QString name = query.value(3).toString(); - // m_mapSensorIndexToSymbolicName[(link_to_guid << 8) + sensor] = name; - // } + while (query.next()) { + int link_to_guid = query.value(1).toInt(); + int sensor = query.value(2).toInt(); + QString name = query.value(3).toString(); + m_mapSensorIndexToSymbolicName[(link_to_guid << 8) + sensor] = name; + } m_mutexSensorIndexMap.unlock(); return true; @@ -821,15 +818,15 @@ vscpworks::addGuid(QString guid, QString name) return true; } - // QString strInsert = "INSERT INTO guid (guid, name) VALUES (%1,%2);"; - // QSqlQuery queryClass(strInsert.arg(guid).arg(name), m_worksdb); - // if (queryClass.lastError().isValid()) { - // spdlog::error(std::string(tr("Failed to insert GUID into database %s") - // .arg(queryClass.lastError().text()) - // .toStdString())); - // qDebug() << queryClass.lastError(); - // return false; - // } + QString strInsert = "INSERT INTO guid (guid, name) VALUES (%1,%2);"; + QSqlQuery queryClass(strInsert.arg(guid).arg(name), m_worksdb); + if (queryClass.lastError().isValid()) { + spdlog::error(std::string(tr("Failed to insert GUID into database %s") + .arg(queryClass.lastError().text()) + .toStdString())); + qDebug() << queryClass.lastError(); + return false; + } // Add to loaded m_mapGuidToSymbolicName[guid] = name; @@ -851,15 +848,15 @@ vscpworks::getIdxForGuidRecord(const QString& guid) QString strInsert = "SELECT * FROM guid WHERE guid='%1';"; - // QSqlQuery query(strInsert.arg(guid), m_worksdb); - // if (query.lastError().isValid()) { - // m_mutexGuidMap.unlock(); - // return -1; - // } + QSqlQuery query(strInsert.arg(guid), m_worksdb); + if (query.lastError().isValid()) { + m_mutexGuidMap.unlock(); + return -1; + } - // if (query.next()) { - // index = query.value(0).toInt(); - // } + if (query.next()) { + index = query.value(0).toInt(); + } m_mutexGuidMap.unlock(); return index; @@ -932,18 +929,18 @@ vscpworks::getUnitInfo(uint16_t vscpClass, uint16_t vscpType, uint8_t unit) QString strQuery = "SELECT * FROM vscp_unit WHERE nunit='%1' AND link_to_class=%2 AND link_to_type=%3;"; - // QSqlQuery query(m_evdb); - // query.exec(strQuery.arg(unit).arg(vscpClass).arg(vscpType)); - - // while (query.next()) { - // u.m_unit = query.value(3).toInt(); - // u.m_name = query.value(4).toString().toStdString(); - // u.m_description = query.value(5).toString().toStdString(); - // u.m_conversion0 = query.value(6).toString().toStdString(); - // u.m_conversion = query.value(7).toString().toStdString(); - // u.m_symbol_ascii = query.value(8).toString().toStdString(); - // u.m_symbol_utf8 = query.value(9).toString().toStdString(); - // } + QSqlQuery query(m_evdb); + query.exec(strQuery.arg(unit).arg(vscpClass).arg(vscpType)); + + while (query.next()) { + u.m_unit = query.value(3).toInt(); + u.m_name = query.value(4).toString().toStdString(); + u.m_description = query.value(5).toString().toStdString(); + u.m_conversion0 = query.value(6).toString().toStdString(); + u.m_conversion = query.value(7).toString().toStdString(); + u.m_symbol_ascii = query.value(8).toString().toStdString(); + u.m_symbol_utf8 = query.value(9).toString().toStdString(); + } return u; } @@ -1030,44 +1027,44 @@ vscpworks::getVscpRenderData(uint16_t vscpClass, uint16_t vscpType, QString type QString strQuery = "SELECT * FROM vscp_render WHERE type='%1' AND link_to_class=%2 AND link_to_type=%3;"; qDebug() << strQuery.arg(type).arg(vscpClass).arg(vscpType); - // QSqlQuery query(m_evdb); - // query.exec(strQuery.arg(type).arg(vscpClass).arg(vscpType)); - // // Try if there is a general render definition if none - // // is defined for the event - // qDebug() << query.size(); - // qDebug() << query.numRowsAffected(); - // if (query.next()) { - // query.first(); - // } - // else { - // // Definition for all events of class - // query.exec(strQuery.arg(type).arg(vscpClass).arg(-1)); - // query.first(); - // } - // do { - - // // * * * VARIABLES * * * - // QString vscpVariables = query.value(4).toString(); - // if (vscpVariables.startsWith("BASE64:", Qt::CaseInsensitive)) { - // vscpVariables = vscpVariables.right(vscpVariables.length() - 7); - // vscpVariables = QByteArray::fromBase64(vscpVariables.toLatin1(), QByteArray::Base64Encoding); - // } - - // qDebug() << vscpVariables; - // qDebug() << "---------------------------------------------"; - // vscpVariables.replace("\"", ""e;").replace(""e;", "'").replace("&", "&").replace(">", ">").replace("<", "<"); - // qDebug() << vscpVariables; - - // // * * * TEMPLATES * * * - // QString vscpTemplate = query.value(5).toString(); - // if (vscpTemplate.startsWith("BASE64:", Qt::CaseInsensitive)) { - // vscpTemplate = vscpTemplate.right(vscpTemplate.length() - 7); - // vscpTemplate = QByteArray::fromBase64(vscpVariables.toLatin1(), QByteArray::Base64Encoding); - // } - // vscpTemplate.replace("\"", ""e;").replace(""e;", "'").replace("&", "&").replace(">", ">").replace("<", "<"); - // qDebug() << vscpTemplate; - // strList << vscpVariables << vscpTemplate; - // } while (query.next()); + QSqlQuery query(m_evdb); + query.exec(strQuery.arg(type).arg(vscpClass).arg(vscpType)); + // Try if there is a general render definition if none + // is defined for the event + qDebug() << query.size(); + qDebug() << query.numRowsAffected(); + if (query.next()) { + query.first(); + } + else { + // Definition for all events of class + query.exec(strQuery.arg(type).arg(vscpClass).arg(-1)); + query.first(); + } + do { + + // * * * VARIABLES * * * + QString vscpVariables = query.value(4).toString(); + if (vscpVariables.startsWith("BASE64:", Qt::CaseInsensitive)) { + vscpVariables = vscpVariables.right(vscpVariables.length() - 7); + vscpVariables = QByteArray::fromBase64(vscpVariables.toLatin1(), QByteArray::Base64Encoding); + } + + qDebug() << vscpVariables; + qDebug() << "---------------------------------------------"; + vscpVariables.replace("\"", ""e;").replace(""e;", "'").replace("&", "&").replace(">", ">").replace("<", "<"); + qDebug() << vscpVariables; + + // * * * TEMPLATES * * * + QString vscpTemplate = query.value(5).toString(); + if (vscpTemplate.startsWith("BASE64:", Qt::CaseInsensitive)) { + vscpTemplate = vscpTemplate.right(vscpTemplate.length() - 7); + vscpTemplate = QByteArray::fromBase64(vscpVariables.toLatin1(), QByteArray::Base64Encoding); + } + vscpTemplate.replace("\"", ""e;").replace(""e;", "'").replace("&", "&").replace(">", ">").replace("<", "<"); + qDebug() << vscpTemplate; + strList << vscpVariables << vscpTemplate; + } while (query.next()); return strList; } diff --git a/src/vscpworks.h b/src/vscpworks.h index 197510cc..4a5b5a93 100644 --- a/src/vscpworks.h +++ b/src/vscpworks.h @@ -49,9 +49,8 @@ #include #include #include -#include -#include -//#include +#include + #include #include @@ -538,10 +537,10 @@ class vscpworks : public QApplication { std::map m_mapSensorIndexToSymbolicName; /// VSCP works database - //QSqlDatabase m_worksdb; + QSqlDatabase m_worksdb; /// Event database - //QSqlDatabase m_evdb; + QSqlDatabase m_evdb; // List with open childwindows std::list m_childWindows;