| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457 |
- #include "stdafx.h"
- #include "AppService.h"
- #include <ace/Init_ACE.h>
- #include <Simplelog.h>
- #include <ODBC/DBConnectPool.h>
- #include "MonitorObject.h"
- #include "ResistAlarm.h"
- #include "httplib.h"
- #include <Simplelog.h>
- #include <gbk2utf8.h>
- #include <HjFunc.h>
- #include "TimeSyncServerDlg.h"
- #include "315ClientManager.h"
- #ifdef _DEBUG
- #pragma comment(lib, "HJ_ACE650d.lib")
- #pragma message("Automatically linking with aced.dll")
- #else
- #pragma comment(lib, "HJ_ACE650.lib")
- #pragma message("Automatically linking with ace.dll")
- #endif
- #ifdef _DEBUG
- bool g_bLog = true;
- #else
- bool g_bLog;
- #endif // _DEBUG
- time_t g_stStart; //程序启动的时间
- string g_strExepath; //程序目录
- string g_strVersion; //程序版本
- uint32_t g_tReminderInterval = 21600;
- string g_strMoMp; //监控日志
- CMyRWLock g_lockSync; //同步读写锁
- bool g_bDataCompression; //历史数据是否压缩
- bool g_bExternalData; //是否启用外部数据
- C315ClientManager *g_p315ClientManager;
- void tagFileData::Join()
- {
- delete this;
- }
- CAppService::CAppService()
- {
- CreateDirectory("E:\\DB", NULL);
- CSimpleLog::CreateLogDir("E:\\DB\\Log\\");
- CSimpleLog::SetFileName("DataServices");
- #ifdef DEBUG_202
- CSimpleLog::SetDaysNum(7);
- #else
- CSimpleLog::SetDaysNum(15);
- #endif // DEBUG
- #ifdef _DEBUG
- CSimpleLog::SetLevel(CSimpleLog::debug);
- #else
- CSimpleLog::SetLevel(CSimpleLog::info);
- #endif // _DEBUG
- AfxSocketInit();
- CSimpleLog::Info("AfxSocketInit");
- ACE::init();
- CSimpleLog::Info(" ACE::init();");
- CString strLog;
- strLog.Format(_T("进程ID:%d"), getpid());
- CSimpleLog::Info(strLog);
- }
- CAppService::~CAppService()
- {
- for (auto& it :m_mapTempTypeData)
- {
- it.second->Join();
- it.second = nullptr;
- }
- for (auto& it : m_mapTypeData)
- {
- it.second->Join();
- it.second = nullptr;
- }
- m_mapTempTypeData.clear();
- m_mapTypeData.clear();
- ACE::fini();
- AfxSocketTerm();
- }
- bool CAppService::Start()
- {
- SYSTEM_INFO sysinfo;
- GetSystemInfo(&sysinfo);
- g_strExepath = hjfunc_GetAppDir();
- g_strVersion = hjfunc_GetVersion();
- auto strPath = CSimpleLog::GetAppDir() + "svg";
- if (CSimpleLog::PathFileExistsA(strPath) == FALSE)
- CreateDirectory(strPath, nullptr);
- strPath = CSimpleLog::GetAppDir() + "refer";
- if (CSimpleLog::PathFileExistsA(strPath) == FALSE)
- CreateDirectory(strPath, nullptr);
- m_strIniPath = CSimpleLog::GetAppDir() + "DataServices.ini";
- char szTemp[100] = { 0 };
- CString m_strDriver = "ODBC Driver 11 for SQL Server";
- CString m_strDataSource = "127.0.0.1,21009";
- CString m_strCataLog = "RMDB";
- CString m_strUserID = "zljc";
- CString m_strPassWD = "Zljc123!@#";
- int port = 10086;
- ::GetPrivateProfileStringA("ODBC", "DRIVER", CStringA(m_strDriver), szTemp, sizeof(szTemp), m_strIniPath);
- m_strDriver = CString(szTemp);
- ::GetPrivateProfileStringA("ODBC", "SERVER", CStringA(m_strDataSource), szTemp, sizeof(szTemp), m_strIniPath);
- m_strDataSource = CString(szTemp);
- ::GetPrivateProfileStringA("ODBC", "UID", CStringA(m_strUserID), szTemp, sizeof(szTemp), m_strIniPath);
- m_strUserID = CString(szTemp);
- ::GetPrivateProfileStringA("ODBC", "PWD", CStringA(m_strPassWD), szTemp, sizeof(szTemp), m_strIniPath);
- m_strPassWD = CString(szTemp);
- ::GetPrivateProfileStringA("ODBC", "DATABASE", CStringA(m_strCataLog), szTemp, sizeof(szTemp), m_strIniPath);
- m_strCataLog = CString(szTemp);
- port = ::GetPrivateProfileInt("SET", "port", port, m_strIniPath);
- g_tReminderInterval = ::GetPrivateProfileInt("SET", "reminder_interval", g_tReminderInterval, m_strIniPath);
- ::GetPrivateProfileString("SET", "momp", "", szTemp, sizeof(szTemp), m_strIniPath);
- g_strMoMp = szTemp;
- g_bDataCompression = ::GetPrivateProfileInt("SET", "DataCompression", g_bDataCompression, m_strIniPath);
- g_bExternalData = ::GetPrivateProfileInt("SET", "ExternalData", g_bExternalData, m_strIniPath);
- ::WritePrivateProfileStringA("ODBC", "DRIVER", CStringA(m_strDriver), m_strIniPath);
- ::WritePrivateProfileStringA("ODBC", "SERVER", CStringA(m_strDataSource), m_strIniPath);
- ::WritePrivateProfileStringA("ODBC", "DATABASE", CStringA(m_strCataLog), m_strIniPath);
- ::WritePrivateProfileString("ODBC", "UID", m_strUserID, m_strIniPath);
- ::WritePrivateProfileString("ODBC", "PWD", m_strPassWD, m_strIniPath);
- ::WritePrivateProfileStringA("SET", "port", (to_string(port) + " #Websocket 端口").c_str(), m_strIniPath);
- ::WritePrivateProfileString("SET", "reminder_interval", (to_string(g_tReminderInterval) + " #报警提醒间隔").c_str(), m_strIniPath);
- ::WritePrivateProfileString("SET", "momp", g_strMoMp.c_str(), m_strIniPath);
- ::WritePrivateProfileString("SET", "DataCompression", to_string(g_bDataCompression).c_str(), m_strIniPath);
- ::WritePrivateProfileString("SET", "ExternalData", fmt::format("{} #是否采用外部程序分析数据进行智能判断 默认为0 9.6以上", (int)g_bExternalData).c_str(), m_strIniPath);
- //ODBC
- if (CDBConnectPool::Instance()->Init(m_strDriver, m_strDataSource, m_strUserID, m_strPassWD, m_strCataLog, 5) == FALSE)
- {
- ASSERT(0);
- CString strLog;
- strLog.Format("数据库连接池初始化失败");
- TRACE("%s\r\n", strLog);
- CSimpleLog::Save(strLog, true, CSimpleLog::error);
- return false;
- }
- //升级
- {
- CString sql;
- BOOL ret;
- sql = "SELECT top 1 name1 FROM rm_map";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- if (TRUE != ret)
- {
- sql = "alter table rm_map add name1 nvarchar(50) default '';";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "update rm_map set name1 = '' where name1 is null;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "alter table rm_map add name2 nvarchar(50) default '';";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "update rm_map set name2 = '' where name2 is null;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "alter table rm_map add name3 nvarchar(50) default '';";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "update rm_map set name3 = '' where name3 is null;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- sql = "DELETE FROM rm_user WHERE username IN (SELECT username FROM ( SELECT username, COUNT(*) AS TotalCount FROM rm_user GROUP BY username) AS Counts WHERE TotalCount >= 2);";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "DELETE FROM rm_map WHERE zl_1_loca is null;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
-
- sql = "if not exists (select 1 from [rm_mo] where [id] = '100000' )\
- BEGIN\
- INSERT INTO [rm_mo] ([id],[type],[up],[name],[updatetime]) VALUES ('100000' ,'group' ,'100000', '国铁集团',GETDATE());\
- END";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "if not exists (select 1 from [rm_user] where [username] = 'system' )\
- BEGIN\
- INSERT INTO [dbo].[rm_user]([username] ,[password] ,[name] ,[admin] ,[token] ,[node] ,[enable] ,[mobile] ,[email] ,[mark]) VALUES ('system' ,'Zljc123!@#' ,'系统管理员' ,1 ,'AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA','100000',1,'15305712458','','');\
- END";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "UPDATE rm_alarm SET ack_result = 1,ack_name='system',ack_time=GETDATE() WHERE DATEDIFF(d,occur_time,GETDATE()) > 7 AND ack_result = 0;";
- CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "UPDATE rm_alarm SET handle_result = 1,handle_name='system',handle_time=GETDATE() WHERE DATEDIFF(d,occur_time,GETDATE()) > 30 AND handle_result = 0";
- CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "SELECT top 1 module FROM rm_record";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- if (TRUE != ret)
- {
- sql = "create table rm_record (\
- module tinyint not null,\
- dura int not null,\
- analyze_type tinyint null,\
- station nvarchar(50) not null,\
- station_name nvarchar(50) not null,\
- mo varchar(20) null,\
- mo_name varchar(20) null,\
- mp varchar(20) null,\
- mp_name varchar(20) null,\
- start_time bigint not null,\
- end_time bigint null,\
- time bigint not null,\
- name nvarchar(50) not null,\
- username varchar(50) null default '',\
- opt smallint not null,\
- mark nvarchar(200) null default ''\
- );\
- create clustered index IDX_STATION on rm_record(\
- station_name ASC,\
- time DESC\
- );\
- create index IDX_TIME on rm_record(\
- time DESC\
- );";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- if (ret != TRUE)
- CSimpleLog::Error(sql);
- }
- sql = "select top 1 event_id from rm_alarm";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- if (true != ret)
- {
- sql = "alter table rm_alarm add event_id char(36) null default newid();\
- alter table rm_alarm add rel_id char(36) null default ''; ";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "UPDATE [rm_alarm] SET [event_id]=NEWID() WHERE [event_id] is null;\
- UPDATE [rm_alarm] SET [rel_id]='' WHERE [rel_id] is null;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- sql = " IF NOT EXISTS ( SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID('rm_alarm') AND name = 'IDX_GUID' )\
- BEGIN CREATE INDEX IDX_GUID ON rm_alarm(event_id ASC) END";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = " IF NOT EXISTS ( SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID('rm_alarm') AND name = 'IDX_REL_GUID' )\
- BEGIN CREATE INDEX IDX_REL_GUID ON rm_alarm(rel_id ASC) END";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "select top 1 posi from rm_alarm";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- if (true != ret)
- {
- sql = "alter table rm_alarm add posi tinyint null default 0;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "alter table rm_alarm add loworhigh tinyint null default 0;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "alter table rm_alarm add referval int null default 0;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "alter table rm_alarm add recovery_time datetime null default 0;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "update rm_alarm set posi = 0, loworhigh = 0, referval = 0, recovery_time=0 where posi is null;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- sql = "select top 1 sunroof from rm_alarm";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- if (true != ret)
- {
- sql = "alter table rm_alarm add sunroof tinyint null default 0;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "update rm_alarm set sunroof= CASE WHEN (DATEPART(HOUR, occur_time) > 4) THEN 0 ELSE 1 END where sunroof is null;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
-
- sql = " select top 1 zzjno from rm_map";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- if (true != ret)
- {
- sql = "ALTER TABLE [rm_map] ADD [zzjno] int default 0 ;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "WITH CTE AS ( SELECT [zzjno], ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNum FROM [rm_map] ) UPDATE CTE SET [zzjno] = RowNum;\
- CREATE UNIQUE INDEX idx_unique_zzjno ON rm_map(zzjno);";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "ALTER TABLE rm_map add epos INT default 0;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "UPDATE rm_map SET epos=0 WHERE epos is null;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'rm_map' AND COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1;";
- COdbcStatement stmt;
- ret = CDBConnectPool::Instance()->DBQuery(stmt, sql);
- if (true == ret)
- {
- if (stmt.FetchNext() == 0)
- {
- sql = "ALTER TABLE [rm_map] DROP COLUMN [zzjno];";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "ALTER TABLE [rm_map] ADD [zzjno] int;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "WITH CTE AS ( SELECT [zzjno], ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNum FROM [rm_map] ) UPDATE CTE SET [zzjno] = RowNum;\
- CREATE UNIQUE INDEX idx_unique_zzjno ON rm_map(zzjno);";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- }
- sql = " select top 1 install_1 from rm_map";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- if (true != ret)
- {
- sql = "ALTER TABLE rm_map add install_1 tinyint default 1;ALTER TABLE rm_map add install_2 tinyint default 1;ALTER TABLE rm_map add install_3 tinyint default 1; ";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "UPDATE rm_map SET install_1=1 WHERE install_1 is null;UPDATE rm_map SET install_2=1 WHERE install_2 is null;UPDATE rm_map SET install_3=1 WHERE install_3 is null;";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- //修改rm_deviceinfo表
- sql = " select top 1 st0 from rm_deviceinfo";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- if (true != ret)
- {
- sql = "ALTER TABLE rm_deviceinfo add st0 tinyint default 1;"
- "ALTER TABLE rm_deviceinfo add st1 tinyint default 1;"
- "ALTER TABLE rm_deviceinfo add st2 tinyint default 1;"
- "ALTER TABLE rm_deviceinfo add st3 tinyint default 1;"
- "ALTER TABLE rm_deviceinfo add st4 tinyint default 1;"
- "ALTER TABLE rm_deviceinfo add st5 tinyint default 1;"
- "ALTER TABLE rm_deviceinfo add st6 tinyint default 1;"
- "ALTER TABLE rm_deviceinfo add st7 tinyint default 1;"
- "ALTER TABLE rm_deviceinfo add st8 tinyint default 1;"
- "ALTER TABLE rm_deviceinfo add dt0 tinyint default 0;"
- "ALTER TABLE rm_deviceinfo add dt1 tinyint default 0;"
- "ALTER TABLE rm_deviceinfo add dt2 tinyint default 0;"
- "ALTER TABLE rm_deviceinfo add dt3 tinyint default 0;"
- "ALTER TABLE rm_deviceinfo add dt4 tinyint default 0;"
- "ALTER TABLE rm_deviceinfo add dt5 tinyint default 0;"
- "ALTER TABLE rm_deviceinfo add dt6 tinyint default 0;"
- "ALTER TABLE rm_deviceinfo add dt7 tinyint default 0;"
- "ALTER TABLE rm_deviceinfo add dt8 tinyint default 0;"
- ;
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- //传感器离线暂时不作为报警 作为设备状态
- sql = "UPDATE rm_alarm set recovery_time=GETDATE() WHERE type = 0x21 and recovery_time < '2000'";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- sql = "UPDATE rm_alarm set ack_result=1, ack_name='system',ack_time=GETDATE() WHERE type = 0x21 and ack_result = 0";
- ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- //end 升级
- m_bWork = true;
- m_pCreateTableThread = new std::thread(CAppService::ThreadProcCreateTable, (DWORD_PTR)this);
- Sleep(10);
- if (!CMonitorObjectMng::Instance()->LoadMonitorTree())
- {
- ASSERT(0);
- CSimpleLog::Error("LoadMonitorTree Fail. ");
- }
- else
- {
- CMonitorObjectMng::Instance()->LoadHistoryData();
- //开启新的websocket
- m_mgServer = new CMGWSServer;
- if (!m_mgServer->Start(port))
- {
- ASSERT(0);
- CSimpleLog::Error("start mg websocket server fail.");
- return false;
- }
- //m_lwsServer = new CLWSServer;
- //if (!m_lwsServer->Start(port + 1))
- //{
- // assert(0);
- // CSimpleLog::Error("start lws server fail.");
- // return false;
- //}
- }
- if (!CResistAlarmMng::Instance()->Start())
- {
- CSimpleLog::Error("Alarm Start Fail.");
- }
- //启动315协议
- if (g_p315ClientManager == nullptr) g_p315ClientManager = new C315ClientManager;
- if (g_p315ClientManager->Run(m_strIniPath)) CSimpleLog::Info("[315]启动成功!");
- else CSimpleLog::Error("[315]启动失败!");
- if (!CSuperManager::Instance()->Start(m_strIniPath))
- {
- ASSERT(0);
- CSimpleLog::Error("start Super Manager fail.");
- return false;
- }
- m_pHandle = new CLNHandle;
- m_pProactorTask = new CProactorTask;
- m_pProactorTask->start(2);
- m_pAcceptor = new CLNAcceptor(m_pHandle);
- if (m_pAcceptor == nullptr)
- {
- ASSERT(0);
- CSimpleLog::Error("CZHGSAcceptor 创建失败!");
- return false;
- }
- if (m_pAcceptor->listen(51609) == -1)
- {
- ASSERT(0);
- CSimpleLog::Error("CZHGSAcceptor 51609 监听失败!");
- return false;
- }
- m_pUdpSocket = new CUDPSocket;
- m_pUdpSocket->SetProtocolHandler(m_pHandle);
- if (!m_pUdpSocket->Open(51609, NULL, 0, NULL))
- {
- ASSERT(0);
- CSimpleLog::Error("CUDPSocket 监听失败!");
- return false;
- }
- m_bRun = true;
- m_pThreadClearConn = new std::thread(CAppService::ThreadProc, (DWORD_PTR)this);
- m_pHttpThread = new std::thread(CAppService::ThreadProcForHTTP10088, (DWORD_PTR)this);
- m_pHttpThread2 = new std::thread(CAppService::ThreadProcForHTTP10087, (DWORD_PTR)this);
- m_pThreadSpace = new std::thread(CAppService::ThreadProcForSpace, (DWORD_PTR)this);
- m_pThreadTime = new std::thread(CAppService::ThreadProcForTime, (DWORD_PTR)this);
- m_pThreadSync = new std::thread(CAppService::ThreadProcSync, (DWORD_PTR)this);
- time(&g_stStart);
- PostMessage(AfxGetApp()->m_pMainWnd->GetSafeHwnd(), WM_START, 0, 0);
- CSimpleLog::Info(("start succ.Version:" + g_strVersion).c_str());
- SPDLOG_INFO("start succ.Version:{}", g_strVersion);
- TRACE("start succ.\r\n");
- return true;
- }
- void CAppService::Stop()
- {
- CSimpleLog::Info("停止服务");
- m_bRun = false;
- m_bWork = false;
- g_p315ClientManager->Stop();
- Sleep(100);
- TerminateProcess((HANDLE)-1, 0);
- if (m_pThreadSpace)
- {
- m_pThreadSpace->join();
- delete m_pThreadSpace;
- m_pThreadSpace = nullptr;
- }
- if (m_pThreadTime)
- {
- m_pThreadTime->join();
- delete m_pThreadTime;
- m_pThreadTime = nullptr;
- }
- if (m_pThreadClearConn)
- {
- m_pThreadClearConn->join();
- delete m_pThreadClearConn;
- m_pThreadClearConn = nullptr;
- }
- if (m_pCreateTableThread)
- {
- m_pCreateTableThread->join();
- delete m_pCreateTableThread;
- m_pCreateTableThread = nullptr;
- }
- if (m_pHttpThread)
- {
- m_svr10088.stop();
- m_pHttpThread->join();
- delete m_pHttpThread;
- m_pHttpThread = nullptr;
- }
- if (m_pHttpThread2)
- {
- m_svr10087.stop();
- m_pHttpThread2->join();
- delete m_pHttpThread2;
- m_pHttpThread2 = nullptr;
- }
- if (m_pAcceptor)
- {
- m_pAcceptor->ClearAllContexts();
- delete m_pAcceptor;
- m_pAcceptor = nullptr;
- }
- if (m_pUdpSocket)
- {
- m_pUdpSocket->Close();
- delete m_pUdpSocket;
- m_pUdpSocket = nullptr;
- }
- CResistAlarmMng::Instance()->Stop();
- //if (m_lwsServer)
- //{
- // m_lwsServer->Stop();
- // delete m_lwsServer;
- // m_lwsServer = nullptr;
- //}
- if (m_mgServer)
- {
- m_mgServer->Stop();
- delete m_mgServer;
- m_mgServer = nullptr;
- }
- if (m_pHandle)
- {
- m_pHandle->Close();
- delete m_pHandle;
- m_pHandle = nullptr;
- }
- CSuperManager::Instance()->Stop();
- CDBConnectPool::Instance()->Clear();
- }
- BOOL CAppService::FileDataConvertCharData(const uint8_t* filedata, const int filelen, char* strData)
- {
- if (strData == nullptr) return FALSE;
- for (auto i = 0; i < filelen; i++)
- sprintf_s(strData + i * 2, 3, "%02X", filedata[i]);
- return TRUE;
- }
- void CAppService::ThreadProc(DWORD_PTR wparam)
- {
- CAppService* pThis = (CAppService*)wparam;
- std::this_thread::sleep_for(std::chrono::seconds(10));
- while (pThis->m_bWork)
- {
- for (size_t i = 0; i < 10; i++)
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
- if (pThis->m_bWork == false) break;
- }
- pThis->m_pAcceptor->OnClearContext();
- for (size_t i = 0; i < 10; i++)
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
- if (pThis->m_bWork == false) break;
- }
- }
- }
- #include "HttpPrcess.h"
- void CAppService::ThreadProcForHTTP10088(DWORD_PTR pThis)
- {
- using namespace httplib;
- m_svr10088.Post("/data/.*", DealHttpPost);
- m_svr10088.Get("/data/.*", DealHttpGet);
- m_svr10088.Put("/data/.*", DealHttpPut);
- //string strPath = CSimpleLog::GetAppDir();
- //auto ret = m_svr.set_mount_point("/", strPath);
- //if (!ret) {
- // // The specified base directory doesn't exist...
- //}
- m_svr10088.set_keep_alive_max_count(20);// Default is 5
- m_svr10088.set_keep_alive_timeout(300);// Default is 5
- auto ret = m_svr10088.listen("0.0.0.0", 10088);
- return ;
- }
- void CAppService::ThreadProcForHTTP10087(DWORD_PTR)
- {
- using namespace httplib;
- m_svr10087.Post("/hist/.*", DealHttpPost);
- m_svr10087.Get("/hist/.*", DealHttpGet);
- m_svr10087.Put("/hist/.*", DealHttpPut);
- //string strPath = CSimpleLog::GetAppDir();
- //auto ret = m_svr.set_mount_point("/", strPath);
- //if (!ret) {
- // // The specified base directory doesn't exist...
- //}
- m_svr10087.set_keep_alive_max_count(20);// Default is 5
- m_svr10087.set_keep_alive_timeout(300);// Default is 5
- auto ret = m_svr10087.listen("0.0.0.0", 10087);
- return;
- }
- void CAppService::ThreadProcCreateTable(DWORD_PTR pThis)
- {
- auto pService = (CAppService*)pThis;
- CSimpleLog::Info("ThreadProcCreateTable 线程开始!");
- do
- {
- SYSTEMTIME stNow, stTom, stYest;
- GetLocalTime(&stNow);
- COleDateTime odt_now(stNow);
- COleDateTime odt_tom = odt_now + COleDateTimeSpan(1, 0, 0, 0);
- COleDateTime odt_yest = odt_now - COleDateTimeSpan(1, 0, 0, 0);
- odt_tom.GetAsSystemTime(stTom);
- odt_yest.GetAsSystemTime(stYest);
- CString tableName;
- CString tableNameTom;
- CString tableNameYest;
- tableName.Format("rm_resistance_%04d%02d%02d", stNow.wYear, stNow.wMonth, stNow.wDay, stNow.wHour, stNow.wMinute, stNow.wSecond);
- tableNameTom.Format("rm_resistance_%04d%02d%02d", stTom.wYear, stTom.wMonth, stTom.wDay, stTom.wHour, stTom.wMinute, stTom.wSecond);
- tableNameYest.Format("rm_resistance_%04d%02d%02d", stYest.wYear, stYest.wMonth, stYest.wDay, stYest.wHour, stYest.wMinute, stYest.wSecond);
- CString existsql = CString("select top 1 id from sysobjects where name = '") + tableName + "' and type = 'U';";
- CString existsqlTom = CString("select top 1 id from sysobjects where name = '") + tableNameTom + "' and type = 'U';";
- CString existsqlYest = CString("select top 1 id from sysobjects where name = '") + tableNameYest + "' and type = 'U';";
- CString tablesqltoday = CString("create table ") + tableName + " (\n" +
- " IMEI varchar(50) not null,\n" +
- " acquisitiontime datetime not null,\n" +
- " idx tinyint not null,\n" +
- " data0 int null,\n" +
- " data1 int null,\n" +
- " data2 int null\n" +
- ");" +
- "create unique clustered index IDX_TIME on " + tableName + " (\n" +
- "IMEI ASC,\n" +
- "acquisitiontime ASC,\n" +
- "idx ASC\n" +
- ")";
- CString tablesqlTom = "create table " + tableNameTom + " (\n" +
- " IMEI varchar(50) not null,\n" +
- " acquisitiontime datetime not null,\n" +
- " idx tinyint not null,\n" +
- " data0 int null,\n" +
- " data1 int null,\n" +
- " data2 int null\n" +
- ");" +
- "create unique clustered index IDX_TIME on " + tableNameTom + " (\n" +
- "IMEI ASC,\n" +
- "acquisitiontime ASC,\n" +
- "idx ASC\n" +
- ")";
- CString tableYest = "create table " + tableNameYest + " (\n" +
- " IMEI varchar(50) not null,\n" +
- " acquisitiontime datetime not null,\n" +
- " idx tinyint not null,\n" +
- " data0 int null,\n" +
- " data1 int null,\n" +
- " data2 int null\n" +
- ");" +
- "create unique clustered index IDX_TIME on " + tableNameYest + " (\n" +
- "IMEI ASC,\n" +
- "acquisitiontime ASC,\n" +
- "idx ASC\n" +
- ")";
- COdbcStatement stmt;
- CDBConnectPool::Instance()->DBQuery(stmt, existsql);
- int id = 0;
- stmt.BindIntCol(1, &id);
- if (stmt.FetchNext() != 0)
- {
- //不存在
- if (!CDBConnectPool::Instance()->DBExecuteSQL(tablesqltoday))
- {
- CSimpleLog::Error("数据库执行错误:" + tablesqltoday);
- }
- }
- stmt.Close();
- COdbcStatement stmtTom;
- CDBConnectPool::Instance()->DBQuery(stmtTom, existsqlTom);
- stmtTom.BindIntCol(1, &id);
- if (stmtTom.FetchNext() != 0)
- {
- //不存在
- if (!CDBConnectPool::Instance()->DBExecuteSQL(tablesqlTom))
- {
- CSimpleLog::Error("数据库执行错误:" + tablesqlTom);
- }
- }
- stmtTom.Close();
- COdbcStatement stmtYest;
- CDBConnectPool::Instance()->DBQuery(stmtYest, existsqlYest);
- stmtYest.BindIntCol(1, &id);
- if (stmtYest.FetchNext() != 0)
- {
- //不存在
- if (!CDBConnectPool::Instance()->DBExecuteSQL(tableYest))
- {
- CSimpleLog::Error("数据库执行错误:" + tableYest);
- }
- }
- stmtYest.Close();
- //temperature
- tableName.Format("rm_temphumidity_%04d%02d", stNow.wYear, stNow.wMonth, stNow.wDay, stNow.wHour, stNow.wMinute);
- tableNameTom.Format("rm_temphumidity_%04d%02d", stTom.wYear, stTom.wMonth, stTom.wDay, stTom.wHour, stTom.wMinute);
- existsql = CString("select top 1 id from sysobjects where name = '") + tableName + "' and type = 'U';";
- existsqlTom = CString("select top 1 id from sysobjects where name = '") + tableNameTom + "' and type = 'U';";
- tablesqltoday = CString("create table ") + tableName + " (\n" +
- " IMEI varchar(50) not null,\n" +
- " acquisitiontime datetime not null,\n" +
- " temperature int not null,\n" +
- " humidity int not null\n" +
- ");" +
- "create unique clustered index IDX_IMEI on " + tableName + " (\n" +
- "IMEI ASC,\n" +
- "acquisitiontime ASC\n" +
- ")";
- CDBConnectPool::Instance()->DBQuery(stmt, existsql);
- stmt.BindIntCol(1, &id);
- if (stmt.FetchNext() != 0)
- {
- //不存在
- if (!CDBConnectPool::Instance()->DBExecuteSQL(tablesqltoday))
- {
- CSimpleLog::Error("数据库执行错误:" + tablesqltoday);
- }
- }
- stmt.Close();
- if (stNow.wMonth != stTom.wMonth || stNow.wYear != stTom.wYear)
- {
- CDBConnectPool::Instance()->DBQuery(stmt, existsqlTom);
- stmt.BindIntCol(1, &id);
- if (stmt.FetchNext() != 0)
- {
- //不存在
- tablesqlTom = CString("create table ") + tableNameTom + " (\n" +
- " IMEI varchar(50) not null,\n" +
- " acquisitiontime datetime not null,\n" +
- " temperature int not null,\n" +
- " humidity int not null\n" +
- ");" +
- "create unique clustered index IDX_IMEI on " + tableNameTom + " (\n" +
- "IMEI ASC,\n" +
- "acquisitiontime ASC\n" +
- ")";
- if (!CDBConnectPool::Instance()->DBExecuteSQL(tablesqlTom))
- {
- CSimpleLog::Error("数据库执行错误:" + tablesqlTom);
- }
- }
- stmt.Close();
- }
- //扳动记录表
- {
- SYSTEMTIME lastSt;
- memcpy(&lastSt, &stNow, sizeof(SYSTEMTIME));
- move_today:
- string table_today = fmt::format("rm_move_{:0>4}{:0>2}", lastSt.wYear, lastSt.wMonth);
- COdbcStatement stmt;
- CDBConnectPool::Instance()->DBQuery(stmt, CString(fmt::format("select top 1 id from sysobjects where name = '{}' and type = 'U'; ", table_today).c_str()));
- int id = 0;
- stmt.BindIntCol(1, &id);
- if (stmt.FetchNext() != 0)
- {
- CString strSql = fmt::format("create table {0} (\
- mo varchar(20) not null,\
- mp varchar(20) not null,\
- show_time datetime not null,\
- start_time datetime null,\
- end_time datetime null,\
- curr_val int null,\
- show_val int not null,\
- idx tinyint null,\
- posi tinyint not null default(0),\
- mark nvarchar(255) null,\
- type tinyint not null default(0),\
- sunroof tinyint not null default(0),\
- );create unique clustered index IDX_MO_MP on {0}(mo ASC,mp ASC,show_time ASC,type ASC);\
- create index IDX_TIME on {0} (show_time DESC)", table_today).c_str();
- //不存在
- if (!CDBConnectPool::Instance()->DBExecuteSQL(strSql))
- {
- CSimpleLog::Error("数据库执行错误:" + strSql);
- }
- }
- stmt.Close();
- if (lastSt.wYear != stTom.wYear || lastSt.wMonth != stTom.wMonth)
- {
- memcpy(&lastSt, &stTom, sizeof(SYSTEMTIME));
- goto move_today;
- }
- auto move_reserve = ::GetPrivateProfileInt("SET", "move_reserve", 730, pService->m_strIniPath);
- if (move_reserve < 90) move_reserve = 90;
- ::WritePrivateProfileStringA("SET", "move_reserve", fmt::format("{} 天 #转换过程记录保留天数, 默认730天, 20230701起", move_reserve).c_str(), pService->m_strIniPath);
- static bool bExec_rm_move = true;
- COleDateTimeSpan dts_delete(move_reserve, 0, 0, 0);
- CString sql = "select name from sys.tables where name like 'rm_move_%' ORDER BY name;";
- if (CDBConnectPool::Instance()->DBQuery(stmt, sql)) {
- char tablename[50];
- stmt.BindCharCol(1, tablename, sizeof(tablename));
- while (true)
- {
- memset(tablename, 0, sizeof(tablename));
- if (stmt.FetchNext() != 0) {
- break;
- }
- if (strlen(tablename) < 14)
- {
- assert(0);
- continue;
- }
- COleDateTime dd;
- try
- {
- int year, month;
- sscanf_s(tablename, "rm_move_%04d%02d", &year, &month);
- dd = COleDateTime(year, month, 1, 0, 0, 0);
- }
- catch (const std::exception&)
- {
- assert(0);
- continue;
- }
- if (odt_now - dd > dts_delete)
- {
- sql.Format(" DROP TABLE %s ", tablename);
- CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- else
- {
- int ret = 0;
- if (bExec_rm_move == false) break;
- CString qq = CString("SELECT top 1 end_time FROM ") + tablename;
- if (CDBConnectPool::Instance()->DBExecuteSQL(qq) == FALSE)
- {
- qq.Format("ALTER TABLE %s ADD end_time datetime null;", tablename);
- CDBConnectPool::Instance()->DBExecuteSQL(qq);
- qq.Format("UPDATE %s SET end_time = 0 where end_time is null;", tablename);
- CDBConnectPool::Instance()->DBExecuteSQL(qq);
- }
- qq = CString("SELECT top 1 type FROM ") + tablename;
- if (CDBConnectPool::Instance()->DBExecuteSQL(qq) == FALSE)
- {
- qq.Format("ALTER TABLE %s ADD type tinyint null default(0);", tablename);
- ret = CDBConnectPool::Instance()->DBExecuteSQL(qq);
- qq.Format("UPDATE %s SET type = 0 where type is null;", tablename);
- ret = CDBConnectPool::Instance()->DBExecuteSQL(qq);
- }
- qq = CString("SELECT top 1 sunroof FROM ") + tablename;
- if (CDBConnectPool::Instance()->DBExecuteSQL(qq) == FALSE)
- {
- qq.Format("ALTER TABLE %s ADD sunroof tinyint null default(0);", tablename);
- ret = CDBConnectPool::Instance()->DBExecuteSQL(qq);
- }
- qq.Format("UPDATE %s SET sunroof = CASE WHEN (DATEPART(HOUR, show_time) > 4) THEN 0 ELSE 1 END where sunroof is null;", tablename);
- ret = CDBConnectPool::Instance()->DBExecuteSQL(qq);
- qq.Format("UPDATE %s set show_val = curr_val where show_val = 0 and type = 2 and curr_val !=0;", tablename); //临时
- ret = CDBConnectPool::Instance()->DBExecuteSQL(qq);
- }
- }
- stmt.Close();
- bExec_rm_move = false;
- }
- }
- //过车记录表
- {
- SYSTEMTIME lastSt;
- memcpy(&lastSt, &stNow, sizeof(SYSTEMTIME));
- pass_today:
- string table_today = fmt::format("rm_pass_{:0>4}{:0>2}", lastSt.wYear, lastSt.wMonth);
- COdbcStatement stmt;
- CDBConnectPool::Instance()->DBQuery(stmt, CString(fmt::format("select top 1 id from sysobjects where name = '{}' and type = 'U'; ", table_today).c_str()));
- int id = 0;
- stmt.BindIntCol(1, &id);
- if (stmt.FetchNext() != 0)
- {
- CString strSql = fmt::format("create table {0} (\
- mo varchar(20) not null,\
- mp varchar(20) not null,\
- show_time datetime null,\
- show_val int not null default(0),\
- curr_val int not null,\
- start_time datetime not null,\
- end_time datetime not null,\
- idx tinyint not null,\
- shaking int null default 0\
- );create unique clustered index IDX_MO_MP on {0}(mo ASC,mp ASC,start_time ASC);", table_today).c_str();
- //不存在
- if (!CDBConnectPool::Instance()->DBExecuteSQL(strSql))
- {
- CSimpleLog::Error("数据库执行错误:" + strSql);
- }
- }
- stmt.Close();
- if (lastSt.wYear != stTom.wYear || lastSt.wMonth != stTom.wMonth)
- {
- memcpy(&lastSt, &stTom, sizeof(SYSTEMTIME));
- goto pass_today;
- }
- auto move_reserve = ::GetPrivateProfileInt("SET", "pass_reserve", 730, pService->m_strIniPath);
- if (move_reserve < 90) move_reserve = 90;
- ::WritePrivateProfileStringA("SET", "pass_reserve", fmt::format("{} 天 #过车过程记录保留天数, 默认730天, 20240601起", move_reserve).c_str(), pService->m_strIniPath);
- static bool bExec_rm_pass = true;
- COleDateTimeSpan dts_delete(move_reserve, 0, 0, 0);
- CString sql = "select name from sys.tables where name like 'rm_pass_%' ORDER BY name;";
- if (CDBConnectPool::Instance()->DBQuery(stmt, sql)) {
- char tablename[50];
- stmt.BindCharCol(1, tablename, sizeof(tablename));
- while (true)
- {
- memset(tablename, 0, sizeof(tablename));
- if (stmt.FetchNext() != 0) {
- break;
- }
- if (strlen(tablename) < 14)
- {
- assert(0);
- continue;
- }
- COleDateTime dd;
- try
- {
- int year, month;
- sscanf_s(tablename, "rm_pass_%04d%02d", &year, &month);
- dd = COleDateTime(year, month, 1, 0, 0, 0);
- }
- catch (const std::exception&)
- {
- assert(0);
- continue;
- }
- if (odt_now - dd > dts_delete)
- {
- sql.Format(" DROP TABLE %s ", tablename);
- CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- else
- {
- if (bExec_rm_pass == false) break;
- //CString qq = CString("SELECT end_time FROM ") + tablename;
- //if (CDBConnectPool::Instance()->DBExecuteSQL(qq) == FALSE)
- //{
- // qq.Format("ALTER TABLE %s ADD end_time datetime null;", tablename);
- // CDBConnectPool::Instance()->DBExecuteSQL(qq);
- // qq.Format("UPDATE %s SET end_time = 0 where end_time is null;", tablename);
- // CDBConnectPool::Instance()->DBExecuteSQL(qq);
- //}
- //qq = CString("SELECT type FROM ") + tablename;
- //if (CDBConnectPool::Instance()->DBExecuteSQL(qq) == FALSE)
- //{
- // qq.Format("ALTER TABLE %s ADD type tinyint not null default(0);", tablename);
- // CDBConnectPool::Instance()->DBExecuteSQL(qq);
- // qq.Format("UPDATE %s SET type = 0 where type is null;", tablename);
- // CDBConnectPool::Instance()->DBExecuteSQL(qq);
- //}
- }
- }
- stmt.Close();
- bExec_rm_pass = false;
- }
- }
- ULARGE_INTEGER FreeBytesAvailableToCaller, TotalNumberOfBytes, TotalNumberOfFreeBytes;
- if (!GetDiskFreeSpaceEx("E:", &FreeBytesAvailableToCaller,
- &TotalNumberOfBytes, &TotalNumberOfFreeBytes))
- continue;
- auto dwCurLeftDisk = DWORD(FreeBytesAvailableToCaller.QuadPart >> 20);
- auto idle_reserve = ::GetPrivateProfileInt("SET", "idle_reserve", 20, pService->m_strIniPath);
- if (idle_reserve < 5) idle_reserve = 5;
- ::WritePrivateProfileStringA("SET", "idle_reserve", fmt::format("{} GB #E盘闲置空间, 默认20GB, 20230701起", idle_reserve).c_str(), pService->m_strIniPath);
- BOOL bClear = (dwCurLeftDisk < idle_reserve * 1024); BOOL bForceClear = (dwCurLeftDisk < 3 * 1024);
- auto resist_reserve = ::GetPrivateProfileInt("SET", "resist_reserve", 365, pService->m_strIniPath);
- if (resist_reserve < 30) resist_reserve = 30;
- ::WritePrivateProfileStringA("SET", "resist_reserve", fmt::format("{} 天 #阻力数据保留天数, 默认365天, 20230701起", resist_reserve).c_str(), pService->m_strIniPath);
- auto temp_reserve = ::GetPrivateProfileInt("SET", "temp_reserve", 365, pService->m_strIniPath);
- if (temp_reserve < 30) temp_reserve = 30;
- ::WritePrivateProfileStringA("SET", "temp_reserve", fmt::format("{} 天 #温湿度数据保留天数, 默认365天, 20230701起", temp_reserve).c_str(), pService->m_strIniPath);
- //删除365天前的数据
- if (bClear)
- {
- if (bForceClear) resist_reserve = resist_reserve / 2;
- COleDateTimeSpan dts_delete(resist_reserve, 0, 0, 0);
- COdbcStatement stmt;
- CString sql = "select name from sys.tables where name like 'rm_resistance_%' ORDER BY name;";
- if (CDBConnectPool::Instance()->DBQuery(stmt, sql)) {
- char tablename[50];
- stmt.BindCharCol(1, tablename, sizeof(tablename));
- while (true)
- {
- memset(tablename, 0, sizeof(tablename));
- if (stmt.FetchNext() != 0) {
- break;
- }
- if (strlen(tablename) < 22)
- {
- assert(0);
- continue;
- }
- COleDateTime dd;
- try
- {
- dd = COleDateTime(atoi(CString(tablename + 14, 4)), atoi(CString(tablename + 18, 2)), atoi(CString(tablename + 20, 2)), 0, 0, 0);
- }
- catch (const std::exception&)
- {
- assert(0);
- continue;
- }
- if (odt_now - dd > dts_delete)
- {
- sql.Format(" DROP TABLE [%s] ", tablename);
- CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- else
- {
- break;
- }
- }
- }
- }
- //删除200天的数据
- if (bClear)
- {
- if (bForceClear) temp_reserve = temp_reserve / 2;
- COleDateTimeSpan dts_delete(temp_reserve, 0, 0, 0);
- COdbcStatement stmt;
- CString sql = "select name from sys.tables where name like 'rm_temphumidity_%';";
- if (CDBConnectPool::Instance()->DBQuery(stmt, sql)) {
- char tablename[50];
- stmt.BindCharCol(1, tablename, sizeof(tablename));
- while (true)
- {
- memset(tablename, 0, sizeof(tablename));
- if (stmt.FetchNext() != 0) {
- break;
- }
- if (strlen(tablename) < 22)
- {
- assert(0);
- continue;
- }
- COleDateTime dd;
- try
- {
- auto a = atoi(CString(tablename + 16, 4));
- auto b = atoi(CString(tablename + 20, 2));
- dd = COleDateTime(atoi(CString(tablename + 16, 4)), atoi(CString(tablename + 20, 2)), 28, 0, 0, 0);
- }
- catch (const std::exception&)
- {
- assert(0);
- continue;
- }
- if (odt_now - dd > dts_delete)
- {
- sql.Format(" DROP TABLE %s ", tablename);
- CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- else
- {
- break;
- }
- }
- }
- }
- {
- CTime ctTime(stNow);
- CString sql;
- sql.Format("DELETE rm_record WHERE time < %I64d", (ctTime - CTimeSpan(366, 0, 0, 0)).GetTime());
- auto ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
- }
- //5小时检测一次
- #ifdef _DEBUG
- for (auto i = 0; i < 60; i++)
- #else
- for (auto i = 0; i < 18000; i++)
- #endif // _DEBUG
- if (pService->m_bWork)
- Sleep(1);
- else
- goto l;
- } while (pService->m_bWork);
- l:
- CSimpleLog::Info("ThreadProcCreateTable 线程结束!");
- }
- //获取E盘剩余容量,返回值以GB为单位
- double Get_E_DiskRemainingSpace()
- {
- DWORD64 qwFreeBytes, qwFreeBytesTocaller, qwTotalBytes;
- GetDiskFreeSpaceEx(TEXT("e:"), (PULARGE_INTEGER)&qwFreeBytesTocaller, (PULARGE_INTEGER)&qwTotalBytes, (PULARGE_INTEGER)&qwFreeBytes);
- double RemainingSpace = qwFreeBytes / 1024 / 1024 / 1024;
- return RemainingSpace;
- }
- void CAppService::ThreadProcForSpace(DWORD_PTR param)
- {
- CAppService* pThis = (CAppService*)param;
- //刚开始运行,停止一分钟
- std::this_thread::sleep_for(std::chrono::seconds(20));
- do
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(950));
- auto ttNow = chrono::system_clock::to_time_t(chrono::system_clock::now());
- if (ttNow % pThis->m_nInterval) continue;
- if (ttNow == pThis->m_tmLastSpaceTime) continue;
- pThis->m_tmLastSpaceTime = ttNow;
- auto e_disk_space = Get_E_DiskRemainingSpace();
- if (e_disk_space < 100 && pThis->m_bWork)
- {
- auto json = R"({"cmd":"disk_alarm","code":200,"msg":"磁盘余量告警,还剩 )" + fmt::format("{:.1f}", e_disk_space) + " GB.\"}";
- auto utf = ANSItoUTF8(json);
- pThis->GetMgServer()->SendToAllClient(utf.c_str(), utf.length());
- }
- } while (pThis->m_bWork);
- }
- void CAppService::ThreadProcForTime(DWORD_PTR param)
- {
- CAppService* pThis = (CAppService*)param;
- //刚开始运行,停止一分钟
- std::this_thread::sleep_for(std::chrono::seconds(20));
- do
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(950));
- auto ttNow = chrono::system_clock::to_time_t(chrono::system_clock::now());
- if (ttNow == pThis->m_tmLastSyncTime) continue;
- pThis->m_tmLastSyncTime = ttNow;
- SYSTEMTIME st;
- GetLocalTime(&st);
- if (st.wHour == 0 && st.wMinute == 5 && st.wSecond == 0 && pThis->m_bWork) //每天0点5分, 下发时钟同步
- pThis->GetHandle()->SendTimeAckToAllDevice();
- } while (pThis->m_bWork);
- }
- //半小时同步一次
- void CAppService::ThreadProcSync(DWORD_PTR param)
- {
- CAppService* pThis = (CAppService*)param;
- //刚开始运行,停止一分钟
- Sleep(3000);
- do
- {
- Sleep(950);
- auto ttNow = chrono::system_clock::to_time_t(chrono::system_clock::now());
- if (ttNow - pThis->m_tmLastSyncSuper < pThis->m_nIntervalSync) continue;
- pThis->m_tmLastSyncSuper = ttNow;
- if (CSuperManager::Instance()->GetSuperNum() == 0)
- break;
- uint8_t* pack = nullptr; int len = 0;
- //同步机构数据
- auto packno = CMonitorObjectMng::Instance()->GeneralMoPack(&pack, &len);
- if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_MO, false);
- delete[] pack;
- if (pThis->m_bWork == false)break;
- Sleep(1000);
- //同步设备数据
- packno = CMonitorObjectMng::Instance()->GeneralMpPack(&pack, &len);
- if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_MP, false);
- delete[] pack;
- if (pThis->m_bWork == false)break;
- Sleep(1000);
- //同步svg数据
- if (pThis->m_index % 10 == 0)
- {
- CFileFind filefind;
- string filePath = g_strExepath + "svg\\*.svg";
- bool bFind = filefind.FindFile(filePath.c_str());
- while (bFind) {
- bFind = filefind.FindNextFile();
- CString strFilenName = filefind.GetFileName();//含后缀
- auto strTitle = filefind.GetFileTitle(); //不含后缀
- auto filelen = filefind.GetLength(); //文件长度
- auto strFilePath = filefind.GetFilePath();//全部路径
- auto new_len = sizeof(HJDATAHEAD2) + 2 + 64 + filelen;
- pack = new uint8_t[new_len];
- memset(pack, 0, new_len);
- auto pData = pack + sizeof(HJDATAHEAD2);
- strcpy_s((char*)pData, 50, (char*)(LPCSTR)strTitle);
- CFile file;
- if (file.Open(strFilePath, CFile::modeRead | CFile::typeBinary) == FALSE)
- {
- SPDLOG_ERROR("打开文件{}失败", strFilePath);
- }
- else
- {
- file.Read(pData + 64, filelen);
- file.Close();
- packno = pThis->GetPackNo();
- auto send_len = CHjDataConver::conver_sendpack(pack, pData, 64 + filelen, 0, packno, E_ZL_PROTOCAL::ZL_SVG,
- OR_DATA_INFO(0, 0, 1, 2, OPT_TYPE::OPT_SYNC));
- if (packno) CSuperManager::Instance()->SendPack(pack, send_len, packno, E_ZL_PROTOCAL::ZL_SVG, FALSE);
- SPDLOG_INFO("发送SVG:{} packno:{}) len:{}", strFilePath, packno, send_len);
- }
- delete[] pack;
- pack = nullptr;
- Sleep(5000);
- if (pThis->m_bWork == false)break;
- }
- filefind.Close();
- Sleep(100);
- }
- //同步refer数据
- if (pThis->m_index % 10 == 0)
- {
- CFileFind filefind;
- string filePath = g_strExepath + "refer\\*.json";
- bool bFind = filefind.FindFile(filePath.c_str());
- while (bFind) {
- bFind = filefind.FindNextFile();
- CString strFilenName = filefind.GetFileName();//含后缀
- auto strTitle = filefind.GetFileTitle(); //不含后缀
- auto filelen = filefind.GetLength(); //文件长度
- auto strFilePath = filefind.GetFilePath();//全部路径
- auto new_len = sizeof(HJDATAHEAD2) + 2 + 64 + filelen;
- pack = new uint8_t[new_len];
- memset(pack, 0, new_len);
- auto pData = pack + sizeof(HJDATAHEAD2);
- //CTime lastWriteTime;
- //filefind.GetLastWriteTime(lastWriteTime);
- //*(uint64_t*)(pData) = lastWriteTime.GetTime();
- strcpy_s((char*)pData, 50, (char*)(LPCSTR)strTitle);
- CFile file;
- if (file.Open(strFilePath, CFile::modeRead | CFile::typeBinary) == FALSE)
- {
- SPDLOG_ERROR("打开文件{}失败", strFilePath);
- }
- else
- {
- file.Read(pData + 64, filelen);
- file.Close();
- packno = pThis->GetPackNo();
- auto send_len = CHjDataConver::conver_sendpack(pack, pData, 64 + filelen, 0, packno, E_ZL_PROTOCAL::ZL_REFER,
- OR_DATA_INFO(0, 0, 1, 2, OPT_TYPE::OPT_SYNC));
- if (packno) CSuperManager::Instance()->SendPack(pack, send_len, packno, E_ZL_PROTOCAL::ZL_REFER, false);
- }
- delete[] pack;
- pack = nullptr;
- Sleep(1000);
- if (pThis->m_bWork == false)break;
- }
- filefind.Close();
- Sleep(100);
- }
- //同步扳动数据
- packno = CAppService::GeneralMovePack(&pack, &len);
- if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_MOVE, false);
- delete[] pack;
- pack = nullptr;
- if (pThis->m_bWork == false)break;
- Sleep(1000);
- //同步报警设置
- packno = CResistAlarmMng::Instance()->GeneralAlarmSet(&pack, &len);
- if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_SET, false);
- delete[] pack;
- if (pThis->m_bWork == false)break;
- Sleep(1000);
- //同步未受理报警数据
- packno = CResistAlarmMng::Instance()->GeneralUnAck(&pack, &len);
- if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_UNACK, false);
- delete[] pack;
- if (pThis->m_bWork == false)break;
- Sleep(1000);
- pThis->m_index++;
- } while (pThis->m_bWork);
- }
- CAppService CAppService::obj;
- httplib::Server CAppService::m_svr10088;
- httplib::Server CAppService::m_svr10087;
- uint32_t CAppService::GeneralMovePack(uint8_t** pack, int* len)
- {
- #ifdef _DEBUG
- string table_name = "rm_move_202405";
- string strWhere = "";
- #else
- SYSTEMTIME st;
- GetLocalTime(&st);
- string table_name = fmt::format("rm_move_{:0>4}{:0>2}", st.wYear, st.wMonth);
- string strWhere = " WHERE [show_time] > DATEADD(mi, -35, GETDATE())";
- #endif // _DEBUG
- CString sql = fmt::format("SELECT [mo],[mp],[show_time],[start_time],[end_time],[curr_val],[show_val],[idx],[posi],[mark],[type] FROM {} {}",
- table_name, strWhere).c_str();
- COdbcStatement stmt;
- if (CDBConnectPool::Instance()->DBQuery(stmt, sql))
- {
- int nCol = 1;
- int show_val, curr_val;
- BYTE posi, idx, type;
- char mo[20], mp[10], show_time[25], start_time[25], end_time[25], mark[50];
- stmt.BindCharCol(nCol++, mo, sizeof(mo));
- stmt.BindCharCol(nCol++, mp, sizeof(mp));
- stmt.BindCharCol(nCol++, show_time, sizeof(show_time));
- stmt.BindCharCol(nCol++, start_time, sizeof(start_time));
- stmt.BindCharCol(nCol++, end_time, sizeof(end_time));
- stmt.BindIntCol(nCol++, &curr_val);
- stmt.BindIntCol(nCol++, &show_val);
- stmt.BindTinyIntCol(nCol++, &idx);
- stmt.BindTinyIntCol(nCol++, &posi);
- stmt.BindCharCol(nCol++, mark, sizeof(mark));
- stmt.BindTinyIntCol(nCol++, &type);
- //组装数据
- auto doc = yyjson_mut_doc_new(nullptr);
- auto root = yyjson_mut_arr(doc);
- yyjson_mut_doc_set_root(doc, root);
- int num = 0;
- while (true)
- {
- memset(mo, 0, sizeof(mo));
- memset(mp, 0, sizeof(mp));
- memset(show_time, 0, sizeof(show_time));
- memset(start_time, 0, sizeof(start_time));
- memset(end_time, 0, sizeof(end_time));
- memset(mark, 0, sizeof(mark));
- if (stmt.FetchNext() != 0)
- break;
- //添加数据
- auto obj = yyjson_mut_obj(doc);
- yyjson_mut_arr_add_val(root, obj);
- yyjson_mut_obj_add_strcpy(doc, obj, "mo", mo);
- yyjson_mut_obj_add_strcpy(doc, obj, "mp", mp);
- yyjson_mut_obj_add_strcpy(doc, obj, "show_time", show_time);
- yyjson_mut_obj_add_strcpy(doc, obj, "start_time", start_time);
- yyjson_mut_obj_add_strcpy(doc, obj, "end_time", end_time);
- yyjson_mut_obj_add_strcpy(doc, obj, "mark", mark);
- yyjson_mut_obj_add_int(doc, obj, "curr_val", curr_val);
- yyjson_mut_obj_add_int(doc, obj, "show_val", show_val);
- yyjson_mut_obj_add_int(doc, obj, "idx", idx);
- yyjson_mut_obj_add_int(doc, obj, "posi", posi);
- yyjson_mut_obj_add_int(doc, obj, "type", type);
- num++;
- }
- stmt.Close();
- uint32_t no = 0;
- if (num > 0)
- {
- size_t json_len;
- auto json = yyjson_mut_write(doc, 0, &json_len);
- *len = json_len + sizeof(HJDATAHEAD2) + 2;
- *pack = new uint8_t[*len];
- no = CAppService::Instance()->GetPackNo();
- CHjDataConver::conver_sendpack(*pack, (LPBYTE)json, json_len, 0, no, E_ZL_PROTOCAL::ZL_MOVE,
- OR_DATA_INFO(0, 0, 1, 2, OPT_TYPE::OPT_SYNC));
- free(json);
- }
- yyjson_mut_doc_free(doc);
- return no;
- }
- else
- {
- SPDLOG_ERROR("语句查询错误:{}", sql);
- return 0;
- }
- }
|