| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464 |
- #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; //是否启用外部数据
- 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);
- }
- 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 recovery_time=GETDATE() WHERE type = 6 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 = 6 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;
- }
- }
|