当前位置:网站首页>QT writing IOT management platform 47 general database settings
QT writing IOT management platform 47 general database settings
2022-07-26 05:27:00 【feiyangqingyun】
One 、 Preface
In order to make this general database component , Virtual machines are specially installed to install various versions of different types of databases for testing , Including compiling the corresponding database plug-in , I have always believed that everything should proceed from reality + Have the actual right to speak , Including differences Qt Version compilation mysql、oracle Database plug-ins , And different databases are querying data 、 Display data in pages 、 Empty data 、 Bulk insert data 、 Batch delete data and other operations , Experienced at least half a month of crazy practice , light mysql It's installed 5.7 and 8.0 edition ,postgresql Installed 9.0、10.0、13.0 Three versions ,oracle Installed 11.0, Domestic databases are installed kingbaseV8, Repeatedly integrate the IOT management platform 、 Video monitoring system 、 Visual large screen electronic Kanban system , Test under various databases , Optimized hundreds of details, large and small , It also supports direct database connection and ODBC There are two forms of data source connection to the database , Such as through oci Library collocation qoci The plug-in connection oracle database 、 adopt odbc Data source matching qodbc The plug-in connection oracle database , To form the current version .
Basic functions of general database components :
- Detect whether the database connection is normal , It is usually detected without specifying the database name , Because the database does not exist at this time , You need to connect before you create a new .
- perform sql Script initializes the database , Including creating a new database 、 New database table 、 Initialization record, etc .
- General database connection and management class , Be responsible for opening the database 、 close 、 perform sql sentence 、 Batch execution 、 Automatic reconnection 、 Real time detection of connection status, etc .
- General database record cleaning class , Be responsible for clearing the records of the specified database table .
- General database record page turning class , Different database types correspond to different query page turning display record statements .
- Bind database tables to the data model QSqlTableModel.
- Get the number of query records , Different database types are different sql sentence .
- Query all table names in the database 、 All field names , Different database types are different sql sentence .
- General date range sql sentence .
Two 、 Functional characteristics
2.1 Software modules
- Equipment monitoring module , Including data monitoring ( Show... In tabular form )、 Device panel ( Display in panel form )、 Map monitoring ( Display in map form )、 Curve monitoring ( Show in curve form ).
- Data query module , Including alarm records 、 Operation record 、 Operation record .
- System setup module , Including basic settings 、 Port Management 、 Controller management 、 Detector Management 、 Alarm linkage 、 Type setting, etc .
- Other setting modules , Including user management 、 Map Management 、 Position adjustment 、 Configuration design 、 Equipment commissioning, etc .
2.2 Basic function
- Device data acquisition , Support serial port 、 The Internet , Serial port can set serial port number 、 Baud rate , The network can be set IP Address 、 Communication port .
- Each port supports acquisition cycle time , Default 1 One device per second .
- Support to set communication timeout times , Default 3 Time .
- Support maximum reconnection time , For rereading offline devices .
- Controller information , Can add controller name , Select the controller address 、 Controller model , Set the number of detectors under the controller .
- Detector information , Can add tag number 、 Detector model 、 Gas type 、 Gas symbol 、 High reported value 、 Under reported value 、 Buffer value 、 Zero value 、 Is it enabled? 、 Alarm sound 、 Background map 、 Storage cycle 、 The number of decimal places for numerical conversion 、 Alarm delay time 、 Type of alarm (HH,LL,HL) etc. .
- Type management configurable controller model 、 Detector model 、 Gas type 、 Gas symbols, etc .
- Map supports importing and deleting , The positions of all detectors on the map can be dragged and saved freely .
- Port information 、 Controller information 、 Detector information 、 Type information 、 User information, etc , Both support import 、 export 、 Export to excel、 Print .
- Operation record 、 Alarm records 、 Operation record , Both support multi condition combined query , For example, time period 、 controller 、 Detector, etc , All records support export to excel/pdf And print .
- Operation record 、 Alarm records 、 All operation records can delete data within a specified time range .
- The system can select the maximum number of records saved in the corresponding table , Automatically clean up early data , Leave enough space for important data .
- Alarm SMS forwarding , Support multiple receiving mobile numbers , The sending interval can be set , Like instant delivery or 6 Send all alarm messages once an hour , The message is too long , Automatically split multiple messages .
- Alarm mail forwarding , Support multiple receiving mailboxes , The sending interval can be set , Like instant delivery or 6 Send all alarm messages once an hour , Support attachment sending .
- Set the Chinese title of the software 、 English title 、logo route 、 All rights reserved .
- The switch is set for startup and operation 、 Alarm sound 、 automatic logon 、 Remember the password, etc .
- Alarm sound can be set to play times , Interface style provides 18 Set skin file selection .
- User management , Including user permission configuration , Different users can have different module permissions .
- User login and user logout , Can remember password and auto login , More than three error prompts and close the program .
- Four monitoring modes , Equipment panel monitoring 、 Map monitoring 、 Form data monitoring 、 Curve data monitoring , Free to switch , The collected data is displayed in real time in all four modes , Alarm flashing, etc .
- Alarm relay linkage , A tag number can link multiple modules and relay numbers across serial ports , Support many to many .
2.3 Special function
- Communication protocol supports modbus_com、modbus_tcp_rtu, Later expansion mqtt Such agreement .
- The data source is in addition to the real hardware device collection , Optional database acquisition , In this way, users can arrange other programmers, such as java The programmer puts the data collected by the front end into the database , The system can collect data directly from the database . Database collection mode can be used as a general system , More suitable for multi person and multi system cooperation .
- Smart skip timeout device , Speed up the acquisition of online devices , Especially useful when there are a large number of devices .
- For smart skip timeout devices , Automatically collect once at the set reconnection time , In order to detect whether the equipment is online again .
- Each detector can be controlled whether it is enabled , If it is not enabled, it will not collect , It will not be displayed on the interface , It is equivalent to temporary shutdown in the operation phase .
- The detector can set the buffer value and alarm delay time , An alarm generated by fluctuations near this value , Not included in the alarm , Only when it is continuously at the alarm value and exceeds the alarm delay time can it be considered as a real alarm , This can avoid many false positives caused by fluctuations .
- The detector can set the storage cycle , Store an operation record according to the set time , The storage cycle can be shorter for those with high importance according to the degree of importance , The unimportant setting is bigger , This can save a lot of storage space , It also ensures the timely storage of important data .
- The detector can set the zero clearing value , When some high-precision and sensitive equipment may leave the factory, the default value may not be 0, The reset value needs to be set to represent the initial value .
- The detector can set the decimal point , It is used to control the display of decimal places for the calculated real data , Equivalent to divided by 10、 Divide 100、 Divide 1000, In this way, most of the detector data directly control the real converted value through the decimal point setting , If special conversion is required, it can be agreed in the communication protocol .
- The type of detector alarm supports multiple types , Some devices are higher than a certain value and report high , Under reporting below a certain value , And some equipment is in the range of minimum and maximum value, which is high alarm , Under reporting below the minimum value , Above maximum normal . In this way, it can be handled according to the situation , Covers various alarm types .
- Original data import 、 export 、 Printer system , Cross platform does not rely on any components , Export data instantaneously .
- Export to excel The records support all excel、wps Wait for the form file version , Do not rely on excel Such as software .
- High color 、 Low color 、 Normal color 、 Default values: color, etc , Can be set freely .
- Support cloud data synchronization , Synchronize the data collected locally to the cloud in real time .
- Support network forwarding and network receiving , When network reception is turned on , Software from udp Receive data for parsing . Network forwarding supports multiple targets IP, In this way, the local acquisition software is realized , Free to transfer data to client , Check the collected data at any time .
- Automatically remember the last user interface and other configuration information , Automatic application after restart .
- The alarm automatically switches to the corresponding map , The detector button flashes , The table data is displayed in the corresponding color .
- Double click the probe Icon , Pop up the corresponding detector details , The back control operation can be customized as required .
- The database supports a variety of , Include sqlite、mysql、sqlserver、postgresql、oracle、 People's Congress, Jincang, etc .
- The data collected by the local device is uploaded to the cloud in real time , So that cell phones APP perhaps web And so on .
- Built in device simulation tools , Support data simulation of multiple devices of different models , At the same time, it also has database data simulation , In order to test the data when there is no equipment .
- standard modbus agreement , Various controller types 、 Detector type 、 species 、 All custom symbols, etc , Very flexible and powerful , The communication protocol sample data is very complete , General various modbus Protocol system , It is applicable to various application scenarios .
- At the same time, it integrates serial communication 、 Network communication 、 Database communication 、 Data import / export printing 、 Communication protocol analysis 、 Interface UI、 Many components and knowledge points such as global skin change , Very suitable for beginners and advanced .
- Support xp、win7、win10、、win11、linux、mac、 All kinds of domestic systems (UOS、 Winning Qilin 、 Galaxy unicorn, etc )、 The embedded linux Such as system .
- Note complete , The project structure is clear , Super detailed complete use of the development manual , Accurate to the function description of each code file , Keep iterating over versions .
3、 ... and 、 Experience address
- Domestic site :https://gitee.com/feiyangqingyun
- International sites :https://github.com/feiyangqingyun
- Personal home page :https://blog.csdn.net/feiyangqingyun
- Zhihu Homepage :https://www.zhihu.com/people/feiyangqingyun
- The product page :https://blog.csdn.net/feiyangqingyun/article/details/97565652
- Online document :https://feiyangqingyun.gitee.io/qwidgetdemo/iotsystem/
- Experience address :https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A Extraction code :o05q file name :bin_iotsystem.zip.
- Article navigation :https://qtchina.blog.csdn.net/article/details/121330922
Four 、 design sketch






5、 ... and 、 Related codes
void frmConfigDb::on_btnConnect_clicked()
{
{
// Initialize database connection information structure data
DbInfo dbInfo;
initDbInfo(dbInfo, connName);
QString dbType = AppConfig::LocalDbType.toUpper();
if (dbType == "SQLITE") {
dbInfo.dbName = DbHelper::getDbDefaultFile(connFlag);
if (QFile(dbInfo.dbName).size() <= 4) {
QUIHelper::showMessageBoxError(" Database file does not exist !", 5);
return;
}
}
QSqlDatabase database;
if (DbHelper::initDatabase(true, dbType, database, dbInfo)) {
if (database.open()) {
database.close();
QUIHelper::showMessageBoxInfo(" Open database successfully !", 3);
} else {
QString error = database.lastError().text();
QUIHelper::showMessageBoxError(" Failed to open database !\n" + error, 3);
}
} else {
QString error = database.lastError().text();
QUIHelper::showMessageBoxError(" Failed to connect to database !\n" + error, 3);
}
}
QSqlDatabase::removeDatabase(connName);
}
void frmConfigDb::on_btnInit_clicked()
{
if (QUIHelper::showMessageBoxQuestion(" Are you sure you want to initialize the database ? All data will be cleared and cannot be restored !") != QMessageBox::Yes) {
return;
}
QString sqlName = QString("%1/db/%2.sql").arg(QUIHelper::appPath()).arg(connFlag);
QFile file(sqlName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QUIHelper::showMessageBoxError(" Database script file open failed !", 3);
return;
}
QElapsedTimer time;
time.start();
{
// Initialize database connection information structure data
DbInfo dbInfo;
initDbInfo(dbInfo, connName);
QString dbType = AppConfig::LocalDbType.toUpper();
if (dbType == "SQLITE") {
dbInfo.dbName = DbHelper::getDbDefaultFile(connFlag);
// If the file exists, delete the original database file first , Looks like win Not on
QFile file(dbInfo.dbName);
if (file.exists()) {
bool ok = file.remove();
if (!ok) {
qDebug() << TIMEMS << "remove error" << dbInfo.dbName;
}
// Empty all tables
QStringList tables = QSqlDatabase::database().tables();
foreach (QString table, tables) {
DbHelper::clearTable(table, dbType);
qDebug() << TIMEMS << "clearTable" << table;
}
// Close the default database connection
QSqlDatabase::database().close();
}
}
// Initialize the database connection and open the database
QSqlDatabase database;
if (!DbHelper::initDatabase(true, dbType, database, dbInfo)) {
QString error = database.lastError().text();
QUIHelper::showMessageBoxError(" Failed to connect to database !\n" + error, 3);
return;
}
if (!database.open()) {
QString error = database.lastError().text();
QUIHelper::showMessageBoxError(" Failed to open database !\n" + error, 3);
return;
}
QSqlQuery query(QSqlDatabase::database(connName));
// First step : Delete the original database
QString sql = QString("DROP DATABASE %1;").arg(dbInfo.dbName);
qDebug() << TIMEMS << "sql:" << sql << "result:" << query.exec(sql);
// The second step : New database
sql = QString("CREATE DATABASE %1;").arg(dbInfo.dbName);
qDebug() << TIMEMS << "sql:" << sql << "result:" << query.exec(sql);
// The third step : Switch to the newly created database and execute the CREATE TABLE statement
database.close();
if (!DbHelper::initDatabase(false, dbType, database, dbInfo)) {
QString error = database.lastError().text();
QUIHelper::showMessageBoxError(" Failed to connect to database !\n" + error, 3);
return;
}
if (!database.open()) {
QString error = database.lastError().text();
QUIHelper::showMessageBoxError(" Failed to open database !\n" + error, 3);
return;
}
// An error will be executed sql Statement is output to a file for easy viewing
QString fileName2 = QString("%1/db/error.sql").arg(QUIHelper::appPath());
QFile file2(fileName2);
QSqlQuery query2(QSqlDatabase::database(connName));
sql = "BEGIN;";
qDebug() << TIMEMS << "sql:" << sql << "result:" << query2.exec(sql);
while (!file.atEnd()) {
sql = QString::fromUtf8(file.readLine());
sql.replace("\n", "");
// Some statements that are not supported by the database jump over
if (DbHelper::existNoSupportSql(sql)) {
continue;
}
// Re correct sql sentence
DbHelper::checkSql(dbType, sql);
if (!query2.exec(sql)) {
// Print and output error messages
QString error = query2.lastError().text();
qDebug() << TIMEMS << "sql:" << sql << error;
// If you don't open it, open it first
if (!file2.isOpen()) {
file2.open(QFile::WriteOnly | QFile::Append);
}
QString msg = QString(" Time [%1] sentence : %2 error : %3\n").arg(DATETIME).arg(sql).arg(error);
file2.write(msg.toUtf8());
}
}
sql = "COMMIT;";
qDebug() << TIMEMS << "sql:" << sql << "result:" << query2.exec(sql);
database.close();
//sqlite If the database is compressed, reduce the volume
if (dbType == "SQLITE") {
DbHelper::execSql("VACUUM;");
}
}
QSqlDatabase::removeDatabase(connName);
double ms = time.elapsed();
QString info = QString(" Database script executed successfully , Total time %1 second !\n Remember to restart the program !").arg(QString::number(ms / 1000, 'f', 1));
QUIHelper::showMessageBoxInfo(info, 3);
}
边栏推荐
猜你喜欢

LAMP架构

嵌入式开发小记,实用小知识分享

Hack the box - Web requests module detailed Chinese tutorial

10. 正则表达式匹配

Hack The Box - Web Requests Module详细讲解中文教程

Getaverse, a distant bridge to Web3

测试必备工具之Fiddler,你真的了解吗?

10. Regular expression matching
C语言详解系列——函数的认识(4)函数的声明与定义,简单练习题

Shell process control (emphasis), if judgment, case statement, let usage, for ((initial value; loop control condition; variable change)) and for variable in value 1 value 2 value 3..., while loop
随机推荐
MODFLOW flex, GMS, FEFLOW, hydraus practical application
SSH远程管理
Thread三种实现方式 和 Handler的用法
SIP账号注册的SIP软电话的使用和常见问题
High frequency electronic circuit review examination questions and answers
高分子物理知识点
The first positive number missing in question 41 of C language. Two methods, preprocessing, fast sorting and in situ hashing
【个人总结】2022.7.24周结
Getaverse,走向Web3的远方桥梁
Leetcode linked list problem - 206. reverse linked list (learn linked list by one question and one article)
循环结构 practice
TZC 1283: simple sort Bubble Sort
提升命令行效率的 Bash 快捷键 [完整版]
FPGA刷题——序列检测
Use flutter to adjust a color filter for the picture of my little sister
测试必备工具之Fiddler,你真的了解吗?
项目版本号怎么命名?看起来牛B
【STM32系列汇总】博主的STM32实战快速进阶之路(持续更新)
Shell process control (emphasis), if judgment, case statement, let usage, for ((initial value; loop control condition; variable change)) and for variable in value 1 value 2 value 3..., while loop
Hack the box - Web requests module detailed Chinese tutorial