Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize record cleaning by Qt

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to achieve record cleaning in Qt". In daily operation, I believe many people have doubts about how to achieve record cleaning in Qt. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve record cleaning in Qt". Next, please follow the editor to study!

I. Preface

The recording and cleaning function is not needed when the amount of data is very small. If the amount of data is large, it is extremely important to store it over the years, just like the video stored by NVR in video surveillance. Generally speaking, it is stored for 60 days. What should we do if it takes more than 60 days? just erase the early data to store the most recent data. In this gas safety management system, the amount of data is also very large over the years. Generally speaking, a node stores one data per minute by default. If 100 nodes (this should be a conservative number, it is said that there are about 500 nodes in many application sites. As for how to break the limit of modbus255 nodes, the following article will explain separately), there are 14.4 million pieces of data in 100,6024,144000,100 days. At the level of 10 million, if we do not clean up the early data, slowly various performance effects will become more and more prominent. Either increase the storage interval or set only the number of recent records to be saved. These parameters can be adjusted. The storage interval is set in the detector settings, and each detector node can set a different storage interval. The maximum number of records is set in the system setting, with a default of 100W, and the excess will regularly clean up the early data.

For this reason, a class DbCleanThread is specifically encapsulated to clean up data and folders. Why is there a function to clean up folders? this is also required in reality, for example, sometimes some pictures and video files are stored. If the memory capacity is not large enough, especially embedded linux, the memory on the board is generally not very large. At present, 8G is in the majority, and with the passage of time, the number of files will become more and more. The remaining capacity of its own storage will become smaller and smaller, which also requires regular cleaning of early files to ensure that the files are stored circularly when the existing capacity allows. This class is written as a general class as far as possible, taking into account many parameters. For example, you can set condition fields, sort fields, cleanup intervals, folder paths, maximum size, and so on.

II. Functional features

Collect data port, support serial port + network port, serial port support free setting of serial number + baud rate, network support free setting of IP address + communication port, each port supports collection cycle, default one address per second, support setting communication timeout times, default 3 times, support maximum reconnection time, used to re-read offline devices.

Controller information, you can add the controller name, select the controller address + controller model, and set the number of detectors under the controller.

Detector information, can add tag, free to choose detector type, gas type, gas symbol, high reported value, low reported value, buffer value, clear zero value, whether enabled, alarm sound, background map, storage cycle, numerical conversion decimal places, alarm delay time, alarm type (HH,LL,HL) and so on.

Controller model + detector type + gas type + gas symbol, all can be configured freely.

Maps can be imported and deleted, and all detectors can be dragged and saved freely.

Port information + controller information + detector information, support import and export + export to excel+ printing.

Run record + alarm record + user record, support multi-condition combination query, such as time period + controller + detector, etc., all records can be exported to excel+ printing.

Records exported to excel support all versions of table files such as excel+wps and do not rely on software such as excel.

You can delete data within a specified time range, support automatic cleaning of early data, and set the maximum number of saved records.

Support alarm short message forwarding, support multiple receiving mobile phone numbers, you can set the sending interval, such as instant sending or sending all alarm messages once every 6 hours, the text message content is too long, automatically split multiple text messages.

Support alarm email forwarding, support multiple receiving mailboxes, can set sending interval, such as immediate sending or sending all alarm messages once every 6 hours, support attachment sending.

Overreport color + underreport color + normal color + 0 value color + curve background + curve color, etc., are free to choose.

The Chinese title + English title + logo path + copyright of the software can be set freely.

Provide switch settings for boot operation + alarm sound + automatic login + remember password and so on.

Alarm sound can be set to play the number of times, the interface provides 17 kinds of skin file selection.

Cloud data synchronization is supported, and the information of cloud database can be set, such as database name, user name + password, etc.

Support network forwarding and network reception, after the network reception is turned on, the software receives data from udp for analysis. Network forwarding supports multiple target IP, so it realizes the software of local collection, and freely transfers the data to the client to view the detector data at any time.

Automatically remember the user's last stay in the interface + other information, restart and apply automatically.

The alarm automatically switches to the corresponding map, and the detector button flashes.

Double-click the detector icon to control it back.

Support user rights management, administrator + operator two categories, user login + user exit, can remember the password and automatic login, more than three error messages and close the program.

Support four monitoring modes, equipment panel monitoring + map monitoring + table data monitoring + curve data monitoring, free to switch, four synchronous applications.

Support alarm relay linkage, a tag can link multiple modules and relay numbers across serial ports, and support many-to-many.

The local data store supports sqlite+mysql and remote data synchronization to the cloud database. Automatically reconnect.

The data collected by the local device is uploaded to the cloud in real time so that it can be extracted by other methods such as APP or web.

Two kinds of data sources are supported, one is serial port and network to collect equipment data through protocol, the other is database collection. The database acquisition mode can be used as a general system.

Comes with a device simulation tool that supports 16 device data simulations, as well as database data simulations to test data when there is no device.

The default communication protocol uses modbus protocol, and later increases the support of mqtt and other Internet of things protocols to make a general system.

Supports all windows operating systems + linux operating systems and other operating systems.

Third, effect picture

4. Core code # pragma execution_character_set ("utf-8") # include "dbcleanthread.h" QScopedPointer DbCleanThread::self;DbCleanThread * DbCleanThread::Instance () {if (self.isNull ()) {static QMutex mutex; QMutexLocker locker (& mutex); if (self.isNull ()) {self.reset (new DbCleanThread);}} return self.data () } DbCleanThread::DbCleanThread (QObject * parent): QThread (parent) {stopped = false; isBusy = false; lastTime = QDateTime::currentDateTime (); tableName = "DeviceLog"; countName = "*"; whereColumnName = "SaveTime"; orderSql = "SaveTime asc"; maxCount = 1000000; interval = 30 * 60; dirPath = ""; dirFileFilter = QStringList ("* .jpg"); dirMaxSize = 1024; dbType = DbType_MySql ConnName = "qt_sql_default_connection"; hostName = "127.0.0.1"; port = 3306; dbName = "db_mysql"; userName = "root"; userPwd = "root";} DbCleanThread::~DbCleanThread () {this- > stop (); this- > wait () } void DbCleanThread::run () {/ / Open the database here. Qt5.10 has added security later. The database connection QSqlDatabase dbConn; if (dbType = = DbType_Sqlite) that cannot be created by the main thread {dbConn = QSqlDatabase::addDatabase ("QSQLITE", connName); dbConn.setDatabaseName (dbName);} else if (dbType = = DbType_MySql) {dbConn = QSqlDatabase::addDatabase ("QMYSQL", connName) DbConn.setConnectOptions ("MYSQL_OPT_RECONNECT=1;MYSQL_OPT_CONNECT_TIMEOUT=1;MYSQL_OPT_READ_TIMEOUT=1;MYSQL_OPT_WRITE_TIMEOUT=1"); dbConn.setHostName (hostName); dbConn.setPort (port); dbConn.setDatabaseName (dbName); dbConn.setUserName (userName); dbConn.setPassword (userPwd) } / / if (! dbConn.open ()) {qDebug () objectName () = interval) {if (isBusy) {qDebug () objectName () tableName = tableName;} void DbCleanThread::setWhereColumnName (const QString & whereColumnName) {this- > whereColumnName = whereColumnName;} void DbCleanThread::setOrderSql (const QString & orderSql) {this- > orderSql = orderSql } void DbCleanThread::setMaxCount (int maxCount) {this- > maxCount = maxCount;} void DbCleanThread::setInterval (int interval) {this- > interval = interval;} void DbCleanThread::setPar (const QString & tableName, const QString & countName, const QString & whereColumnName, const QString & orderSql, int maxCount, int interval) {this- > tableName = tableName; this- > countName = countName; this- > whereColumnName = whereColumnName; this- > orderSql = orderSql This- > maxCount = maxCount; this- > interval = interval;} void DbCleanThread::setDirPath (const QString & dirPath) {this- > dirPath = dirPath;} void DbCleanThread::setDirFileFilter (const QStringList & dirFileFilter) {this- > dirFileFilter = dirFileFilter;} void DbCleanThread::setDirMaxSize (int dirMaxSize) {this- > dirMaxSize = dirMaxSize } void DbCleanThread::setConnInfo (DbCleanThread::DbType dbType, const QString & connName, const QString & hostName, int port, const QString & dbName, const QString & userName, const QString & userPwd) {this- > dbType = dbType; this- > connName = connName; this- > hostName = hostName; this- > port = port; this- > dbName = dbName; this- > userName = userName; this- > userPwd = userPwd } void DbCleanThread::deleteDirectory (const QString & path) {QDir dir (path); if (! dir.exists ()) {return;} dir.setFilter (QDir::AllEntries | QDir::NoDotAndDotDot); QFileInfoList fileList = dir.entryInfoList (); foreach (QFileInfo fi, fileList) {if (fi.isFile ()) {fi.dir (). Remove (fi.fileName ()) } else {deleteDirectory (fi.absoluteFilePath ()); dir.rmdir (fi.absoluteFilePath ());}} void DbCleanThread::stop () {stopped = true } void DbCleanThread::clean () {/ / first look for the total number of records. If the total number of records exceeds the limit, delete the excess in field order QString sql = QString ("select count (% 1) from% 2") .arg (countName) .arg (tableName); QSqlQuery query (QSqlDatabase::database (connName)); query.exec (sql); query.next (); int count = query.value (0). ToInt () Int cleanCount = (count-maxCount); if (cleanCount > = 100) {QDateTime dtStart = QDateTime::currentDateTime (); / / maximum clean up of 1000 pieces of data at a time cleanCount = cleanCount > 1000? 1000: cleanCount; / / query the collection of specified fields of data to be deleted query.clear () Sql = QString ("select 2 from 1 order by 3 limit 4") .Arg (tableName) .arg (whereColumnName) .arg (orderSql) .arg (cleanCount); query.exec (sql); QStringList list While (query.next ()) {list = dirMaxSize) {/ / Delete all files in this directory QString firstDir = dirPath + "/" + list.at (0); QDir dir (firstDir); dir.setFilter (QDir::AllEntries | QDir::NoDotAndDotDot); QStringList list = dir.entryList () Foreach (QString file, list) {dir.remove (file); qDebug ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report