From b60f768479cfac44b2ed1b5df7cf2eb9ef33c603 Mon Sep 17 00:00:00 2001 From: BlueMatthew Date: Mon, 20 Nov 2023 11:48:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=81=A5=E4=BF=A1=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iec103.vcxproj | 1 + iec103.vcxproj.filters | 96 ++++++++++++++++----------------- src/MySQLAdo.cpp | 117 +++++++++++++++++++++++++++++++++++++++-- src/MySQLAdo.h | 5 +- 4 files changed, 166 insertions(+), 53 deletions(-) diff --git a/iec103.vcxproj b/iec103.vcxproj index eacaf50..684254d 100644 --- a/iec103.vcxproj +++ b/iec103.vcxproj @@ -110,6 +110,7 @@ USING_MYSQL;_DEBUG;_CONSOLE;OS_WINDOWS;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS;DISABLE_OS_HEAP true /utf-8 %(AdditionalOptions) + MultiThreadedDebug Console diff --git a/iec103.vcxproj.filters b/iec103.vcxproj.filters index 641f3fd..1ba8c3b 100644 --- a/iec103.vcxproj.filters +++ b/iec103.vcxproj.filters @@ -15,150 +15,150 @@ - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files diff --git a/src/MySQLAdo.cpp b/src/MySQLAdo.cpp index 4285568..5102740 100644 --- a/src/MySQLAdo.cpp +++ b/src/MySQLAdo.cpp @@ -18,6 +18,9 @@ using namespace std; std::vector g_iec_points; std::map g_iec_point_map; +std::vector g_iec_points_yx; +std::map g_iec_point_map_yx; + #ifdef USING_MYSQL bool LoadIecPoints(); #endif @@ -187,8 +190,7 @@ bool PingDatabase() #ifdef USING_MYSQL bool LoadIecPoints() { - // const char* sql = "SELECT t1.`id`,t1.sadr,t1.`site_id`,t1.`sensor_id`,t1.item_no,t2.sensor_code,t2.grp_no FROM iec_points AS t1 LEFT JOIN iec_sensors AS t2 ON t1.sensor_id=t2.`id` WHERE t1.`status`=1 AND t2.`status`=1 ORDER BY t1.site_id,t1.sadr"; - const char* sql = "SELECT t1.`id`,t1.sadr,t1.`site_id`,t1.`sensor_id`,t1.item_no,t2.sensor_code,t2.grp_no FROM niec_points AS t1 LEFT JOIN niec_sensors AS t2 ON t1.sensor_id=t2.`id` WHERE t1.`status`=1 AND t2.`status`=1 ORDER BY t1.site_id,t1.sadr"; + const char* sql = "SELECT t1.`id`,t1.sadr,t1.`site_id`,t1.`sensor_id`,t1.item_no,t1.yx_bit,t1.stype,t2.sensor_code,t2.grp_no FROM niec_points AS t1 LEFT JOIN niec_sensors AS t2 ON t1.sensor_id=t2.`id` WHERE t1.`status`=1 AND t2.`status`=1 ORDER BY t1.site_id,t1.sadr,t2.`grp_no`,t1.`item_no`"; if (mysql_query(mysql, sql)) { @@ -214,13 +216,24 @@ bool LoadIecPoints() pt.siteId = strtoul(row[fieldIdx++], NULL, 10); pt.sensorId = strtoul(row[fieldIdx++], NULL, 10); pt.itemNo = strtoul(row[fieldIdx++], NULL, 10); + pt.yxBit = strtoul(row[fieldIdx++], NULL, 10); + pt.stype = strtoul(row[fieldIdx++], NULL, 10); fieldIdx++; // sensor code 17位编码 pt.sensorGroupNo = strtoul(row[fieldIdx++], NULL, 10); - g_iec_points.push_back(pt); + if (pt.stype == 1) // YX + { + g_iec_points_yx.push_back(pt); + } + else + { + g_iec_points.push_back(pt); + } + } + mysql_free_result(res); - printf("IEC Points: %lld\n", (long long)g_iec_points.size()); + printf("IEC Points YC: %d, YX: %d\n", (int)g_iec_points.size(), (int)g_iec_points_yx.size()); } #endif @@ -302,3 +315,99 @@ int GetAIPntMsg(int j, unsigned int *igno, unsigned int* iItemNo, DAY_TIME* sCur #endif return 0; } + +/* +int j // 对应数据库数据点号 +int *igno // 组号 +int* iItemNo 条目号 +DAY_TIME* sCurTime 时间 +u_char* uValue 数据值 +u_char *iBitNo 数据位号 +typedef struct +{ + WORD Year; + u_char Month; + u_char Day; + u_char WeekDay; + u_char MonthDay; + u_char Hour; + u_char Min; + u_char Sec; + u_char reserved; + WORD mSec; +} DAY_TIME; +返回值1:成功取值 -1:取值失败 +*/ +int GetDIPntMsg(int j, unsigned int *igno, unsigned int* iItemNo, DAY_TIME* sCurTime, u_char* uValue, unsigned int *iaddr, u_char *iBitNo) +{ +#ifdef USING_MYSQL + + if (j < 0) + { + return -1; + } + if (j >= g_iec_points_yx.size()) + { + return 0; + } + + const IEC_POINT_TABLE& pt = g_iec_points_yx[j]; + + if (!PingDatabase()) + { + return -1; + } + + char buf[16] = { 0 }; + snprintf(buf, sizeof(buf), "%u", pt.sadr); + std::string sql = "SELECT sadr,state,stype,ival,fval,d_time FROM niec_latest_org_data WHERE sadr="; + sql += buf; + + if (mysql_query(mysql, sql.c_str())) + { + // error + show_mysql_error(mysql, "GetDIPntMsg"); + return -1; + } + MYSQL_RES* res = mysql_store_result(mysql); + if (res == NULL) + { + return -1; + } + int year = 0, mon = 0, day = 0, hour = 0, min = 0, sec = 0; + + MYSQL_ROW row = mysql_fetch_row(res); + if (row) + { + if (igno != NULL) *igno = pt.sensorGroupNo; + if (iItemNo != NULL) *iItemNo = pt.itemNo; + if (iaddr != NULL) *iaddr = pt.sensorId; + if (iBitNo != NULL) *iBitNo = pt.yxBit; + if (uValue != NULL) + { + if (row[4] != NULL && strlen(row[4]) > 0) + { + *uValue = (unsigned char)atoi(row[4]); + } + else + { + *uValue = 0; + } + } + if (sCurTime != NULL && row[5] != NULL) + { + sscanf(row[5], "%04d-%02d-%2d %02d:%02d:%02d", &year, &mon, &day, &hour, &min, &sec); + sCurTime->Year = year; + sCurTime->Month = mon; + sCurTime->Day = day; + sCurTime->Hour = hour; + sCurTime->Min = min; + sCurTime->Sec = sec; + sCurTime->mSec = 0; + } + } + mysql_free_result(res); + return row != NULL ? 1 : -1; +#endif + return 0; +} \ No newline at end of file diff --git a/src/MySQLAdo.h b/src/MySQLAdo.h index 2444cfc..0ceb2ec 100644 --- a/src/MySQLAdo.h +++ b/src/MySQLAdo.h @@ -7,7 +7,9 @@ #include using namespace std; +#ifdef USING_MYSQL #include +#endif #include "basetype.h" #include #include @@ -20,6 +22,7 @@ struct IEC_POINT_TABLE unsigned char sensorGroupNo; unsigned char stype; // 1: 1:遥信 2:遥测 unsigned char itemNo; + unsigned char yxBit; }; extern std::vector g_iec_points; @@ -50,6 +53,6 @@ bool UninitDatabase(); -1: 出错 */ int GetAIPntMsg(int j, unsigned int *igno, unsigned int* iItemNo, DAY_TIME* sCurTime, float* pfValue, unsigned int *iaddr); - +int GetDIPntMsg(int j, unsigned int *igno, unsigned int* iItemNo, DAY_TIME* sCurTime, u_char* uValue, unsigned int *iaddr, u_char *iBitNo); #endif // __MYSQL_ADO_H__ \ No newline at end of file