| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504 | #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 _DEBUGbool g_bLog = true;#elsebool g_bLog;#endif // _DEBUGtime_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;  //是否启用外部数据CString g_strIniPath;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);	g_strIniPath = 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);		}		//move 记录检查type 索引		{			SYSTEMTIME lastSt;			GetLocalTime(&lastSt);			string table_today = fmt::format("rm_move_{:0>4}{:0>2}", lastSt.wYear, lastSt.wMonth);			sql = fmt::format("IF NOT EXISTS (\				SELECT * FROM(\					SELECT\					i.name AS IndexName,\					OBJECT_NAME(ic.OBJECT_ID) AS TableName,\					COL_NAME(ic.OBJECT_ID, ic.column_id) AS ColumnName\					FROM\					sys.indexes AS i\					INNER JOIN sys.index_columns AS ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id\					WHERE\					OBJECT_NAME(ic.OBJECT_ID) = '{0}') AS tb WHERE tb.IndexName = 'IDX_MO_MP' AND ColumnName = 'type')\				BEGIN\				if exists(select 1\					from  sysindexes\			where  id = object_id('{0}')\				and name = 'IDX_MO_MP'\				and indid > 0\				and indid < 255)\					drop index {0}.IDX_MO_MP\				create unique clustered index IDX_MO_MP on {0}(\					mo ASC,\					mp ASC,\					show_time ASC,\					type ASC)\				END", table_today).c_str();            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);		sql = "update rm_alarm_set set conf = REPLACE(conf, 'true', 'false') where type in(1, 2)";		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(m_strIniPath))	{		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;", tablename);							CDBConnectPool::Instance()->DBExecuteSQL(qq);							qq.Format("UPDATE %s SET type = 0 where type 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;	}}
 |