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 achieve Cloud synchronization with Qt

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

Share

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

This article focuses on "how to achieve cloud synchronization in Qt". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to achieve cloud synchronization in Qt".

I. Preface

The cloud synchronization function is prepared for later expansion. His purpose is to synchronize the records in the local database, such as real-time collected data and stored operation records, to the cloud database. By default, Aliyun's mysql database is used. Ariyun is still very fast. I have bought low-equipped and high-equipped Ali cloud servers, and the high-end ones are really awesome. The processing speed above is much faster than that of my local computer. In remotely connected desktops, it is not too cool to use. The disadvantage is that it costs money. What I buy is charged according to the use time, which is still very good. Turn on the server when you want to use it. Turn it off when you run out of it or when you don't use it.

As for the cloud synchronization function, it specially encapsulates a general class DbTcpClientThread, which is specially used for cloud data synchronization of various systems, and will automatically reconnect the database. In the software, you only need to set the server address, database and other information during initialization, and then insert the corresponding sql statement where needed, so that the remote end can execute the sql statement to achieve data synchronization. In fact, what is generally used is the data collected synchronously in real time. Then refresh the mobile app or web to view the corresponding data, which is the basic application scenario.

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 "dbtcpclientthread.h" QScopedPointer DbTcpClientThread::self;DbTcpClientThread * DbTcpClientThread::Instance () {if (self.isNull ()) {static QMutex mutex; QMutexLocker locker (& mutex); if (self.isNull ()) {self.reset (new DbTcpClientThread);}} return self.data () } DbTcpClientThread::DbTcpClientThread (QObject * parent): QThread (parent) {stopped = false; dbOkNet = false; errorCount = 0; lastCheckTimeNet = QDateTime::currentDateTime (); checkConn = false; checkInterval = 30; dbTypeNet = DbType_MySql; connNameNet = "dbClient"; hostNameNet = "127.0.0.1"; portNet = 3306; dbNameNet = "db_mysql"; userNameNet = "root"; userPwdNet = "root"; maxCount = 1000 ListSql.clear (); initSql.clear ();} DbTcpClientThread::~DbTcpClientThread () {this- > stop (); this- > wait ();} void DbTcpClientThread::run () {while (! stopped) {/ / perform a regular verification of the database connection QDateTime now = QDateTime::currentDateTime (); if (checkConn & & lastCheckTimeNet.secsTo (now) > = checkInterval) {checkDb () LastCheckTimeNet = now; continue;} / / process the data if the database connection is normal, and reconnect the database if (dbOkNet) {checkData (); msleep (500);} else if (errorCount > = 3) {closeDb (); msleep (3000); openDb () Emit debug (QString ("reconnect cloud real-time database% 1") .arg (dbOkNet? "success": "failure"); msleep (3000);}} stopped = false;} bool DbTcpClientThread::openDb () {/ / you can add support for other databases if (dbTypeNet = = DbType_MySql) {dbConnNet = QSqlDatabase::addDatabase ("QMYSQL", connNameNet); dbConnNet.setConnectOptions ("MYSQL_OPT_RECONNECT=1;MYSQL_OPT_CONNECT_TIMEOUT=1;MYSQL_OPT_READ_TIMEOUT=1") MYSQL_OPT_WRITE_TIMEOUT=1 "); dbConnNet.setHostName (hostNameNet); dbConnNet.setPort (portNet); dbConnNet.setDatabaseName (dbNameNet); dbConnNet.setUserName (userNameNet); dbConnNet.setPassword (userPwdNet);} errorCount = 0; dbOkNet = dbConnNet.open (); return dbOkNet;} bool DbTcpClientThread::checkDb () {QDateTime dtStart = QDateTime::currentDateTime (); QString sql =" select 1 "; QSqlQuery query (dbConnNet) DbOkNet = query.exec (sql); dbOkNet? (errorCount = 0): errorCount++; QDateTime dtEnd = QDateTime::currentDateTime (); double ms = dtStart.msecsTo (dtEnd); emit debug ("check cloud real-time database connections (% 1 / 2 seconds)". Arg (1) .arg (QString::number (ms / 1000, 'fags, 3)); return dbOkNet;} void DbTcpClientThread::closeDb () {dbConnNet.close (); QSqlDatabase::removeDatabase (connNameNet) DbOkNet = false; emit debug ("shut down the cloud real-time database");} void DbTcpClientThread::initDb () {/ / guarantee at least one piece of data int count = initSql.count (); if (dbOkNet & & count > 1) {QDateTime dtStart = QDateTime::currentDateTime (); / / strictly stipulate that the first sql statement is to clear all data QSqlQuery query (dbConnNet) of the table. Query.exec (initSql.first ()); / / create a new initial value data for (int I = 1; I

< count; i++) { query.clear(); query.exec(initSql.at(i)); } QDateTime dtEnd = QDateTime::currentDateTime(); double ms = dtStart.msecsTo(dtEnd); emit debug(QString("远程清空并新建数据(共 %1 条/用时 %2 秒)").arg(count).arg(QString::number(ms / 1000, 'f', 3))); }}void DbTcpClientThread::stop(){ stopped = true;}void DbTcpClientThread::append(const QString &sql){ mutex.lock(); //只有不一样的sql语句才需要处理 if (!listSql.contains(sql)) { //如果达到最大数量,则移除最后一条,追加到末尾 if (listSql.count() >

= maxCount) {listSql.removeLast ();} listSql.append (sql);} mutex.unlock ();} void DbTcpClientThread::clear () {mutex.lock (); listSql.clear (); mutex.unlock ();} bool DbTcpClientThread::checkData () {mutex.lock (); QStringList sqls = listSql; mutex.unlock (); int count = sqls.count () If (count = = 0) {return false;} / / fetch all sql statements in the queue, and commit one-time transactions to the remote database if (dbOkNet) {QDateTime dtStart = QDateTime::currentDateTime (); dbConnNet.transaction (); / / execute sql statements foreach (QString sql, sqls) {QSqlQuery query (dbConnNet); query.exec (sql) one by one } / / clear the sql statement queue after a successful commit / / if the commit fails, you need to roll back the transaction dbOkNet = dbConnNet.commit (); if (dbOkNet) {clear (); QDateTime dtEnd = QDateTime::currentDateTime (); double ms = dtStart.msecsTo (dtEnd) Emit debug (QString ("remote real-time update data (% 1 entries /% 2 seconds)") .arg (count) .arg (QString::number (ms / 1000, 'fallow, 3));} else {dbConnNet.rollback (); emit debug (QString ("commit transaction failed:% 1") .arg (dbConnNet.lastError () .text () }} return dbOkNet;} void DbTcpClientThread::setConnInfo (DbType dbType, const QString & connName, const QString & hostName, int port, const QString & dbName, const QString & userName, const QString & userPwd) {this- > dbTypeNet = dbType; this- > connNameNet = connName; this- > hostNameNet = hostName; this- > portNet = port; this- > dbNameNet = dbName; this- > userNameNet = userName; this- > userPwdNet = userPwd } void DbTcpClientThread::setCheckConn (bool checkConn) {this- > checkConn = checkConn;} void DbTcpClientThread::setCheckInterval (int checkInterval) {if (checkInterval > 5 & & this- > checkInterval! = checkInterval) {this- > checkInterval = checkInterval;}} void DbTcpClientThread::setInitSql (const QStringList & initSql) {this- > initSql = initSql;} so far, I believe you have a deeper understanding of "Qt how to achieve cloud synchronization", you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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