From 3990dd9e012da265d4e67f4ac556ff61a7c1e653 Mon Sep 17 00:00:00 2001 From: zhouhuang Date: Mon, 30 Dec 2024 17:20:28 +0800 Subject: [PATCH] update ads --- das-dn/cmg/main.cpp | 4 +- das-dn/cmg/ry.cpp | 21 +- das-dn/comm/public.cpp | 1095 +---------------- das-dn/hostadsbf/hostadsbf.cpp | 72 +- das-dn/hostadsbf/hostadsbf.h | 3 - das-dn/hostmodbustcpbf/host_modbus_tcp_bf.cpp | 14 +- das-dn/inc/process.h | 17 +- das-dn/inc/public.h | 12 +- das-dn/rtustatusproc/rtustatus.cpp | 6 +- das-dn/third_party/AdsLib/Sockets.cpp | 60 +- das-dn/third_party/AdsLib/Sockets.h | 10 + .../AdsLib/Standalone/AmsConnection.cpp | 12 +- das-dn/zjd3100proc/zjd3100pro.cpp | 8 +- 13 files changed, 172 insertions(+), 1162 deletions(-) diff --git a/das-dn/cmg/main.cpp b/das-dn/cmg/main.cpp index ae326bfe..b69b4af9 100644 --- a/das-dn/cmg/main.cpp +++ b/das-dn/cmg/main.cpp @@ -224,11 +224,11 @@ int main(int argc, char** argv) int i, uid; for (i = 0; i < PROCESSES_NUM; i++) { - if (config.processes[i].state == FALSE) break; + if ((config.processes[i].state & 0x01) == FALSE) break; } for (uid = 0; uid < UNIT_NUM; uid++) { - if (config.units[uid].state == FALSE) break; + if ((config.units[uid].state & 0x01) == FALSE) break; } snprintf(config.processes[i].name, sizeof(config.processes[i].name), "%s", "本地调试"); diff --git a/das-dn/cmg/ry.cpp b/das-dn/cmg/ry.cpp index 46c0e50f..d563c90f 100644 --- a/das-dn/cmg/ry.cpp +++ b/das-dn/cmg/ry.cpp @@ -78,13 +78,15 @@ bool CRYDevice::configInitializeMemory(void) config_config.hardware.ports[i].timeout = 1000; //1s } for (i = 0; i < PROCESSES_NUM; i++) { - config_config.processes[i].softdog = 0; + config_config.processes[i].real_softdog = 0; + config_config.processes[i].ftp_softdog = 0; for (j = 0; j < PROCESS_UNIT_NUM; j++) { config_config.processes[i].units[j] = -1; } } for (i = 0; i < UNIT_NUM; i++) { - config_config.units[i].softdog = 0; + config_config.units[i].real_softdog = 0; + config_config.units[i].ftp_softdog = 0; } return true; @@ -2303,7 +2305,7 @@ bool CRYDevice::dealConfigFile(const Json::Value jsonRoot) config_config.units[uid].irn = strtoll(id.c_str(), NULL, 10); config_config.units[uid].value = SPI_ON; - config_config.units[uid].softdog = UNIT_WATCHDOG_TIME; + config_config.units[uid].real_softdog = UNIT_WATCHDOG_TIME; config_config.units[uid].state = TRUE; config_config.units[uid].type = MASTER_UNIT; config_config.units[uid].yxcount = yxs.size(); @@ -2558,7 +2560,7 @@ bool CRYDevice::dealConfigFile(const Json::Value jsonRoot) break; } config_config.units[sub_uid].value = SPI_ON; - config_config.units[sub_uid].softdog = UNIT_WATCHDOG_TIME; + config_config.units[sub_uid].real_softdog = UNIT_WATCHDOG_TIME; config_config.units[sub_uid].state = TRUE; config_config.units[sub_uid].type = SLAVER_UNIT; config_config.units[sub_uid].yxcount = attrs.yxs.size(); @@ -2826,8 +2828,11 @@ void CRYDevice::heart_beat(int status) snprintf(linkId, sizeof(linkId), "%lld", config.processes[i].irn); #endif jsonValue["linkId"] = linkId; - jsonValue["online"] = (config.processes[i].softdog >= PROCESS_WATCHDOG_TIME) ? false : true; - //vLog(LOG_DEBUG, "link %s, %d\n", linkId, (config.processes[i].softdog >= PROCESS_WATCHDOG_TIME) ? 0 : 1); + jsonValue["online"] = (config.processes[i].real_softdog >= PROCESS_WATCHDOG_TIME) ? false : true; + jsonValue["real"] = (config.processes[i].real_softdog >= PROCESS_WATCHDOG_TIME) ? false : true; + if (config.processes[i].state & 0x20) { + jsonValue["ftp"] = (config.processes[i].real_softdog >= PROCESS_WATCHDOG_TIME) ? false : true; + } jsonLink.append(jsonValue); } } @@ -2846,13 +2851,11 @@ void CRYDevice::heart_beat(int status) snprintf(deviceId, sizeof(deviceId), "%lld", config.units[i].irn); #endif jsonValue["deviceId"] = deviceId; - jsonValue["online"] = (config.units[i].softdog >= PROCESS_WATCHDOG_TIME) ? false : true; - //vLog(LOG_DEBUG, "unit %s, %d\n", deviceId, (config.units[i].softdog >= PROCESS_WATCHDOG_TIME) ? 0 : 1); + jsonValue["online"] = (config.units[i].real_softdog >= UNIT_WATCHDOG_TIME) ? false : true; jsonDevice.append(jsonValue); } } if (jsonLink.size() > 0) { - //vLog(LOG_DEBUG, "device is: %d\n", jsonDevice.size()); payload["devices"] = jsonDevice; } } diff --git a/das-dn/comm/public.cpp b/das-dn/comm/public.cpp index bebc20c5..0deb606e 100644 --- a/das-dn/comm/public.cpp +++ b/das-dn/comm/public.cpp @@ -623,7 +623,9 @@ BOOLEAN InitializeMemory(void) for (i = 0; i < PROCESSES_NUM; i++) { snprintf(config.processes[i].name, MAX_NAME_SIZE, "PROC%02d", i); - config.processes[i].softdog = 0; + //config.processes[i].softdog = 0; + config.processes[i].real_softdog = 0; + config.processes[i].ftp_softdog = 0; for (j = 0; j < PROCESS_UNIT_NUM; j++) { config.processes[i].units[j] = -1; @@ -631,7 +633,9 @@ BOOLEAN InitializeMemory(void) } for (i = 0; i < UNIT_NUM; i++) { - config.units[i].softdog = 0; + //config.units[i].softdog = 0; + config.units[i].real_softdog = 0; + config.units[i].ftp_softdog = 0; } return TRUE; @@ -1071,1070 +1075,6 @@ BOOLEAN WriteDatabaseCFG(void) return FALSE; } -#if 0 -//此处定义一个map来放置irn和协议的order的关系 -typedef std::map mapIRN2Order; -BOOLEAN processUartParam(std::string jsonvalue, int ord) -{ - Json::Value jsonRoot; - char* pData = (char *)jsonvalue.c_str(); - const int rawJsonLength = static_cast(strlen(pData)); - std::string err; - - Json::CharReaderBuilder builder; - builder["emitUTF8"] = true; - Json::CharReader* reader(builder.newCharReader()); - if (!reader->parse(pData, pData + rawJsonLength, &jsonRoot, &err)) - { - vLog(LOG_ERROR, "串口参数:Json 格式错误!\n"); - return FALSE; - } - - config.hardware.ports[ord].state = TRUE; - snprintf(config.hardware.ports[ord].name, sizeof(config.hardware.ports[ord].name), "ttyS%d", ord); - config.hardware.ports[ord].baud = 9600; - config.hardware.ports[ord].data = 8; - config.hardware.ports[ord].parity = 0; - config.hardware.ports[ord].stop = 0; - config.hardware.ports[ord].timeout = 1000; - - if (jsonRoot["name"].isString()) - { - snprintf(config.hardware.ports[ord].name, sizeof(config.hardware.ports[ord].name), "%s", jsonRoot["name"].asCString()); - } - if (jsonRoot["baud"].isInt()) - { - config.hardware.ports[ord].baud = jsonRoot["baud"].asInt(); - } - else if (jsonRoot["baud"].isString()) - { - config.hardware.ports[ord].baud = atoi(jsonRoot["baud"].asCString()); - } - if (jsonRoot["data"].isInt()) - { - config.hardware.ports[ord].data = jsonRoot["data"].asInt(); - } - else if (jsonRoot["data"].isString()) - { - config.hardware.ports[ord].data = atoi(jsonRoot["data"].asCString()); - } - if (jsonRoot["parity"].isInt()) - { - config.hardware.ports[ord].parity = jsonRoot["parity"].asInt(); - } - else if (jsonRoot["parity"].isString()) - { - config.hardware.ports[ord].parity = atoi(jsonRoot["parity"].asCString()); - } - if (jsonRoot["stop"].isInt()) - { - if (jsonRoot["stop"].asInt() == 1) config.hardware.ports[ord].stop = 0; - else if (jsonRoot["stop"].asInt() == 2) config.hardware.ports[ord].stop = 2; - } - else if (jsonRoot["stop"].isString()) - { - if (jsonRoot["stop"].asString() == "1") config.hardware.ports[ord].stop = 0; - else if (jsonRoot["stop"].asString() == "2") config.hardware.ports[ord].stop = 2; - } - if (jsonRoot["timeout"].isInt()) - { - config.hardware.ports[ord].timeout = jsonRoot["timeout"].asInt(); - } - else if (jsonRoot["timeout"].isString()) - { - config.hardware.ports[ord].timeout = atoi(jsonRoot["timeout"].asCString()); - } - - return TRUE; -} - -BOOLEAN processissMqttParam(std::string jsonvalue, const char* client_id, int ord) -{ - Json::Value jsonRoot; - char* pData = (char *)jsonvalue.c_str(); - const int rawJsonLength = static_cast(strlen(pData)); - std::string err; - - Json::CharReaderBuilder builder; - builder["emitUTF8"] = true; - Json::CharReader* reader(builder.newCharReader()); - if (!reader->parse(pData, pData + rawJsonLength, &jsonRoot, &err)) - { - vLog(LOG_ERROR, "软通mqtt协议:Json 格式错误!\n"); - return FALSE; - } - - struISSMQTTOption* issmqttOption = (struISSMQTTOption *)config.processes[ord].option.data; - snprintf(issmqttOption->client_id, MAX_ID_LENGTH, "%s", client_id); - snprintf(issmqttOption->username, sizeof(issmqttOption->username), "%s", "nmu8000"); - snprintf(issmqttOption->password, sizeof(issmqttOption->password), "%s", "spi@84805353"); - snprintf(issmqttOption->host, sizeof(issmqttOption->host), "%s", "localhost"); - //issmqttOption->target_addr = 0x0100007f; - issmqttOption->target_port = MQTT_DEFAULT_PORT; - issmqttOption->connectInterval = 30; //链接等待 - issmqttOption->keepAliveInterval = 60; //心跳链接 - issmqttOption->connectTimeout = 30; //链接等待超时 - issmqttOption->publishInterval = 60; //数据发送间隔 - - if (jsonRoot["host"].isString()) - { - //inet_pton(AF_INET, jsonRoot["host"].asCString(), (struct in_addr*)&issmqttOption->target_addr); - snprintf(issmqttOption->host, sizeof(issmqttOption->host), "%s", jsonRoot["host"].asCString()); - } - if (jsonRoot["port"].isInt()) - { - issmqttOption->target_port = jsonRoot["port"].asInt(); - } - else if (jsonRoot["port"].isString()) - { - issmqttOption->target_port = atoi(jsonRoot["port"].asCString()); - } - if (jsonRoot["username"].isString()) - { - snprintf(issmqttOption->username, sizeof(issmqttOption->username), "%s", jsonRoot["username"].asCString()); - } - if (jsonRoot["password"].isString()) - { - snprintf(issmqttOption->password, sizeof(issmqttOption->password), "%s", jsonRoot["password"].asCString()); - } -#if 0 - if (jsonRoot["clientid"].isString()) - { - snprintf(issmqttOption->client_id, sizeof(issmqttOption->client_id), "%s", jsonRoot["clientid"].asCString()); - } -#endif - if (jsonRoot["pushInterval"].isInt()) - { - issmqttOption->publishInterval = jsonRoot["pushInterval"].asInt(); - } - else if (jsonRoot["pushInterval"].isString()) - { - issmqttOption->publishInterval = atoi(jsonRoot["pushInterval"].asCString()); - } - - return TRUE; -} - -BOOLEAN processLMMqttParam(std::string jsonvalue, int ord) -{ - Json::Value jsonRoot; - char* pData = (char *)jsonvalue.c_str(); - const int rawJsonLength = static_cast(strlen(pData)); - std::string err; - - Json::CharReaderBuilder builder; - builder["emitUTF8"] = true; - Json::CharReader* reader(builder.newCharReader()); - if (!reader->parse(pData, pData + rawJsonLength, &jsonRoot, &err)) - { - vLog(LOG_ERROR, "罗米mqtt协议:Json 格式错误!\n"); - return FALSE; - } - - struLMMQTTOption* lmmqttOption = (struLMMQTTOption *)config.processes[ord].option.data; - snprintf(lmmqttOption->client_id, MAX_ID_LENGTH, "%s", "dianzhong::空港产业城26栋"); - snprintf(lmmqttOption->device_id, MAX_ID_LENGTH, "%s", "空港产业城26栋"); - snprintf(lmmqttOption->topic, sizeof(lmmqttOption->topic), "%s", "v1/dianzhong/de/#"); - snprintf(lmmqttOption->username, sizeof(lmmqttOption->username), "%s", "lmgateway"); - snprintf(lmmqttOption->password, sizeof(lmmqttOption->password), "%s", "spi@84805353"); - snprintf(lmmqttOption->host, sizeof(lmmqttOption->host), "%s", "localhost"); - //lmmqttOption->target_addr = 0x0100007f; - lmmqttOption->target_port = MQTT_DEFAULT_PORT; - lmmqttOption->connectInterval = 30; //链接等待 - lmmqttOption->keepAliveInterval = 60; //心跳链接 - lmmqttOption->connectTimeout = 30; //链接等待超时 - - if (jsonRoot["host"].isString()) - { - //inet_pton(AF_INET, jsonRoot["host"].asCString(), (struct in_addr*)&lmmqttOption->target_addr); - snprintf(lmmqttOption->host, sizeof(lmmqttOption->host), "%s", jsonRoot["host"].asCString()); - } - if (jsonRoot["port"].isInt()) - { - lmmqttOption->target_port = jsonRoot["port"].asInt(); - } - else if (jsonRoot["port"].isString()) - { - lmmqttOption->target_port = atoi(jsonRoot["port"].asCString()); - } - if (jsonRoot["username"].isString()) - { - snprintf(lmmqttOption->username, sizeof(lmmqttOption->username), "%s", jsonRoot["username"].asCString()); - } - if (jsonRoot["password"].isString()) - { - snprintf(lmmqttOption->password, sizeof(lmmqttOption->password), "%s", jsonRoot["password"].asCString()); - } - if (jsonRoot["clientid"].isString()) - { - snprintf(lmmqttOption->client_id, sizeof(lmmqttOption->client_id), "%s", jsonRoot["clientid"].asCString()); - } - if (jsonRoot["deviceid"].isString()) - { - snprintf(lmmqttOption->device_id, sizeof(lmmqttOption->device_id), "%s", jsonRoot["deviceid"].asCString()); - } - if (jsonRoot["topic"].isString()) - { - snprintf(lmmqttOption->topic, sizeof(lmmqttOption->topic), "%s", jsonRoot["topic"].asCString()); - } - - return TRUE; -} - -#define LM_TYPE_YX 0 -#define LM_TYPE_YC 1 -#define LM_TYPE_YM 2 -#define LM_TYPE_YK 3 -#define LM_TYPE_YT 4 -BOOLEAN processLMPointParam(std::string jsonvalue, int uid, int point, int type) -{ - Json::Value jsonRoot; - char* pData = (char *)jsonvalue.c_str(); - const int rawJsonLength = static_cast(strlen(pData)); - std::string err; - - Json::CharReaderBuilder builder; - builder["emitUTF8"] = true; - Json::CharReader* reader(builder.newCharReader()); - if (!reader->parse(pData, pData + rawJsonLength, &jsonRoot, &err)) - { - vLog(LOG_ERROR, "罗米mqtt协议:Json 格式错误!\n"); - return FALSE; - } - - char *description; - switch (type) - { - case LM_TYPE_YX: - description = (char *)config.units[uid].yxs[point].m_param; - break; - case LM_TYPE_YC: - description = (char *)config.units[uid].ycs[point].m_param; - config.units[uid].ycs[point].factor = 100; - config.units[uid].ycs[point].coef = 0.01; - break; - case LM_TYPE_YM: - description = (char *)config.units[uid].yms[point].m_param; - break; - case LM_TYPE_YK: - description = (char *)config.units[uid].yks[point].m_param; - break; - case LM_TYPE_YT: - description = (char *)config.units[uid].yts[point].m_param; - break; - default: break; - } - - if (jsonRoot["col1"].isString()) - { - snprintf(description, MAX_UNIT_POINT_PARAM_SIZE, "%s", jsonRoot["col1"].asCString()); - } - - return TRUE; -} - -BOOLEAN ReadConfigFromSQLite(BOOLEAN enable_auto_platform, const char* issmqtt_config_pathName, char* codeName) -{ - int i; - clock_t start; - double Total_time; - - int count = 0; - char query[512]; - char pathName[(MAX_PATH<<1)+32]; - char szHostCode[128] = {'\0'}; - - BOOLEAN have_local_debug = FALSE; //是否配置了本地调试协议 - short issmqtt_pid = -1; //软通mqtt协议ID - short config_process_count = 0; //配置的协议/链路数量 - short config_unit_count = 0; //配置的单元数量 - short unit_link_proto[UNIT_NUM]; //单元所链接的协议序号 - - mapIRN2Order irn2PID; - mapIRN2Order irn2UID; - - memset(static_units, 0, sizeof(static_units)); - - snprintf(pathName, sizeof(pathName), "%s/%s", configpath, configfile); - try - { - SQLiteDatabase* pDatabase = new SQLiteDatabase(pathName, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); - - pDatabase->MoveDatabaseToMemory(); - SQLiteStatement* pStmt = new SQLiteStatement(pDatabase); - - //获取配置信息 - //1、获取工程信息 - //获取本机节点号,通过节点号获取Node信息。 - char szHostName[32] = ""; - gethostname(szHostName, sizeof(szHostName)); - - memset(szHostCode, '\0', sizeof(szHostCode)); - //2、获取节点信息 - start = clock(); - pStmt->BeginTransaction(); - snprintf(query, sizeof(query), "SELECT cn.NODENO, cn.TCI_TYPE, cn.TARGET_ADDR, cn.TARGET_PORT, co.NAME, co.IRN, cn.CODE FROM CMG_NODE cn JOIN CMG_OBJECT co ON co.IRN = cn.IRN ;"); - pStmt->Sql(query); - count = 0; - while (pStmt->FetchRow()) - { - nodes.m_node[count].m_netnode_no = pStmt->GetColumnInt(0); - nodes.m_node[count].m_tcitype = pStmt->GetColumnInt(1) ? pStmt->GetColumnInt(1) : 1; - nodes.m_node[count].m_target_addr = pStmt->GetColumnInt(2); - nodes.m_node[count].m_target_port = pStmt->GetColumnInt(3); - snprintf(nodes.m_node[count].m_machine_name, sizeof(nodes.m_node[count].m_machine_name), "%s", szHostName); - nodes.m_node[count].irn = pStmt->GetColumnInt64(5); - snprintf(szHostCode, sizeof(szHostCode), "%s", pStmt->GetColumnCString(6)); - snprintf(codeName, sizeof(codeName), "%s", pStmt->GetColumnCString(6)); - - vLog(LOG_DEBUG, "nodes.m_node[%d].m_machine_name is: %s\n", count, nodes.m_node[count].m_machine_name); - count++; - } - pStmt->FreeQuery(); - pStmt->CommitTransaction(); - if (count <= 0) - { - delete pStmt; - delete pDatabase; - - return FALSE; - } - - count = 0; - Total_time = (double)(clock() - start) / CLOCKS_PER_SEC; //单位换算成秒 - vLog(LOG_DEBUG, "获取节点配置 use: %f seconds\n", Total_time); - - //4、获取协议配置 - start = clock(); - count = 0; - int uart_order = 0; - irn2PID.clear(); - pStmt->BeginTransaction(); - pStmt->Sql("SELECT co.NAME, cl.STATE, cl.ACCEPT_TIME, cl.PROTOCOL, cl.TIME_GAP, cl.POLL_GAP, cl.OPTION, cl.IRN FROM CMG_LINEUNIT cl JOIN CMG_OBJECT co ON cl.IRN = co.IRN"); - while (pStmt->FetchRow()) - { - snprintf(config.processes[count].name, sizeof(config.processes[count].name), "%s", pStmt->GetColumnCString(0)); - config.processes[count].state = pStmt->GetColumnInt(1); - config.processes[count].time_accept = pStmt->GetColumnInt(2); - config.processes[count].proto = pStmt->GetColumnInt(3); - config.processes[count].time_gap = pStmt->GetColumnInt(4); - config.processes[count].poll_gap = pStmt->GetColumnInt(5); - std::string option = pStmt->GetColumnString(6); //根据协议来解析 - switch (config.processes[count].proto) - { - case PROTOCOL_HOST_IEC104: - case PROTOCOL_HOST_MODBUS_TCP: - case PROTOCOL_HOST_MODBUS_RTU_TCP: - case PROTOCOL_RTU_STATE: - config.processes[count].mode = PROCESS_MODE_MASTER; - break; - case PROTOCOL_LOCAL_DEBUG: - have_local_debug = TRUE; - config.processes[count].mode = PROCESS_MODE_MASTER; - break; - case PROTOCOL_HOST_NSA: - case PROTOCOL_HOST_MODBUS_RTU: - case PROTOCOL_HOST_MODBUS_RTU_NCY: - case PROTOCOL_HOST_MODBUS_RTU_LIYEZG: - case PROTOCOL_HOST_MODBUS_ASCII: - case PROTOCOL_SUB_MODBUS_RTU: - case PROTOCOL_HOST_DLT645V2007: - processUartParam(option, uart_order); - config.processes[count].mode = PROCESS_MODE_MASTER; - config.processes[count].order = uart_order++; - break; - case PROTOCOL_AGC: - case PROTOCOL_AVC: - config.processes[count].mode = PROCESS_MODE_DUPLEX; - break; - case PROTOCOL_SUB_IEC104: - case PROTOCOL_SUB_GDW104: - case PROTOCOL_SUB_XT9712: - case PROTOCOL_SUB_MODBUS_TCP: - case PROTOCOL_DLT_1867: - case PROTOCOL_HW_MQTT: - config.processes[count].mode = PROCESS_MODE_SLAVE; - break; - case PROTOCOL_ISS_MQTT: - issmqtt_pid = count; - processissMqttParam(option, szHostCode, count); - config.processes[count].mode = PROCESS_MODE_SLAVE; - break; - case PROTOCOL_LM_MQTT: - processLMMqttParam(option, count); - config.processes[count].mode = PROCESS_MODE_MASTER; - break; - case PROTOCOL_BF_FTP: - processissMqttParam(option, szHostCode, count); - config.processes[count].mode = PROCESS_MODE_SLAVE; - break; - default: break; - } - QWORD irn = pStmt->GetColumnInt64(7); - config.processes[count].irn = irn; - if (irn2PID.find(irn) == irn2PID.end()) - { - irn2PID.insert(std::map::value_type(irn, count)); - } - - count++; - } - config_process_count = count; - pStmt->FreeQuery(); - pStmt->CommitTransaction(); - count = 0; - - Total_time = (double)(clock() - start) / CLOCKS_PER_SEC; //单位换算成秒 - vLog(LOG_DEBUG, "获取协议配置 use: %f seconds\n", Total_time); - - //5、获取单元配置 - start = clock(); - count = 0; - irn2UID.clear(); - pStmt->BeginTransaction(); - pStmt->Sql("SELECT co.NAME, cu.MODEL, cu.MANUFACTURERID, cu.DEVICEID, cu.STATE, cu.UTYPE, cu.ADDRESS, cu.YXCOUNT, cu.YCCOUNT, cu.YMCOUNT, cu.YKCOUNT, cu.YTCOUNT, co.PARENT, co.IRN FROM CMG_UNIT cu JOIN CMG_OBJECT co ON cu.IRN = co.IRN;"); - while (pStmt->FetchRow()) - { - snprintf(static_units[count].name, sizeof(static_units[count].name), "%s", pStmt->GetColumnCString(0)); - snprintf(static_units[count].model, sizeof(static_units[count].model), "%s", pStmt->GetColumnCString(1)); - snprintf(static_units[count].manufacturerId, sizeof(static_units[count].manufacturerId), "%s", pStmt->GetColumnCString(2)); - snprintf(static_units[count].deviceId, sizeof(static_units[count].deviceId), "%s", pStmt->GetColumnCString(3)); - config.units[count].state = pStmt->GetColumnInt(4); - if (pStmt->GetColumnType(5) == 5) - { - config.units[count].type = 1; - } - else - { - config.units[count].type = pStmt->GetColumnInt(5); - } - std::string address = pStmt->GetColumnString(6); - - config.units[count].yxs = NULL; - config.units[count].ycs = NULL; - config.units[count].yms = NULL; - config.units[count].yks = NULL; - config.units[count].yts = NULL; - - config.units[count].yxcount = pStmt->GetColumnInt(7); - config.units[count].yccount = pStmt->GetColumnInt(8); - config.units[count].ymcount = pStmt->GetColumnInt(9); - config.units[count].ykcount = pStmt->GetColumnInt(10); - config.units[count].ytcount = pStmt->GetColumnInt(11); - QWORD parentIRN = pStmt->GetColumnInt64(12); - QWORD IRN = pStmt->GetColumnInt64(13); - config.units[count].irn = IRN; - if (irn2UID.find(IRN) == irn2UID.end()) - { - irn2UID.insert(std::map::value_type(IRN, count)); - } - //根据父IRN确定该设备所链接的协议 - int pid = irn2PID[parentIRN]; - if (pid >= 0 && pid < PROCESSES_NUM) - { - //此处得到该单元所链接的协议。 - //根据协议,选择默认的单元配置 - #if 0 - case PROTOCOL_HOST_MODBUS_RTU_NCY: - case PROTOCOL_HOST_MODBUS_RTU_LIYEZG: - std::ifstream ifile; - char pathName[MAX_PATH]; - snprintf(pathName, sizeof(pathName), "%s../etc/devices.json", configpath); - fprintf(stderr, "pathName is: %s\n", pathName); - ifile.open(pathName); - //2.创建json读工厂对象 - Json::CharReaderBuilder ReaderBuilder; - ReaderBuilder["emitUTF8"] = true;//utf8支持,不加这句,utf8的中文字符会编程\uxxx - //3.创建json对象,等会要返回这个对象 - Json::Value root; - //4.把文件转变为json对象,要用到上面的三个变量,数据写入root - std::string strerr; - bool ok = Json::parseFromStream(ReaderBuilder, ifile, &root, &strerr); - if (!ok) - { - vLog(LOG_DEBUG, "json解析错误<%s>\n", strerr.c_str()); - } - ifile.close(); - switch (config.processes[pid].proto) - { - case PROTOCOL_HOST_MODBUS_RTU_NCY: - break; - case PROTOCOL_HOST_MODBUS_RTU_LIYEZG: - break; - default: - break; - } - #endif - for (i = 0; i < PROCESS_UNIT_NUM; i++) - { - if (config.processes[pid].units[i] < 0) - { - config.processes[pid].units[i] = count; - break; - } - } - } - unit_link_proto[count] = config.processes[pid].proto; - - BYTE addrType = ADDR_TYPE_NORMAL; //根据协议设定单元地址类型 - switch (config.processes[pid].proto) - { - case PROTOCOL_HOST_IEC104: - case PROTOCOL_SUB_IEC104: - case PROTOCOL_SUB_GDW104: - case PROTOCOL_SUB_XT9712: - addrType = ADDR_TYPE_IPV4_FACNO; - break; - case PROTOCOL_HOST_NSA: - case PROTOCOL_HOST_MODBUS_RTU: - case PROTOCOL_HOST_MODBUS_ASCII: - case PROTOCOL_HOST_MODBUS_RTU_NCY: - case PROTOCOL_HOST_MODBUS_RTU_LIYEZG: - addrType = ADDR_TYPE_NORMAL; - break; - case PROTOCOL_HOST_MODBUS_TCP: - case PROTOCOL_HOST_MODBUS_RTU_TCP: - case PROTOCOL_DLT_1867: - case PROTOCOL_LOCAL_DEBUG: - case PROTOCOL_HW_MQTT: - case PROTOCOL_ISS_MQTT: - case PROTOCOL_LM_MQTT: - addrType = ADDR_TYPE_IPV4; - break; - case PROTOCOL_HOST_DLT645V2007: - addrType = ADDR_TYPE_HEX; - break; - default: - break; - } - if (addrType == ADDR_TYPE_HEX) - { - StringToHex(address.c_str(), config.units[count].addr); - } - else if (addrType == ADDR_TYPE_IPV4) - { - DWORD addr; - inet_pton(AF_INET, address.c_str(), (struct in_addr*)&addr); - memcpy(config.units[count].addr, &addr, sizeof(addr)); - } - else if (addrType == ADDR_TYPE_IPV4_FACNO) - { - char* pData = (char*)address.c_str(); - char* pBuf = strchr(pData, ':'); - if (pBuf != NULL) - { - *pBuf = '\0'; pBuf++; - DWORD addr; - inet_pton(AF_INET, pData, (struct in_addr*)&addr); - memcpy(config.units[count].addr, &addr, sizeof(addr)); - addr = (DWORD)atoi(pBuf); - memcpy(&config.units[count].addr[4], &addr, sizeof(addr)); - } - } - else - { - DWORD addr = (DWORD)atoi(address.c_str()); - memcpy(config.units[count].addr, &addr, sizeof(addr)); - } - - count++; - } - config_unit_count = count; - pStmt->FreeQuery(); - pStmt->CommitTransaction(); - - Total_time = (double)(clock() - start) / CLOCKS_PER_SEC; //单位换算成秒 - vLog(LOG_DEBUG, "获取单元配置 use: %f seconds\n", Total_time); - - start = clock(); - count = 0; - int total_yx = 0; - int total_yc = 0; - int total_ym = 0; - int total_yk = 0; - int total_yt = 0; - //pStmt->BeginTransaction(); - for (std::map::iterator it = irn2UID.begin(); it != irn2UID.end(); it++) - { //此处先获取遥信、遥测等测点数量来初始化。 - pStmt->Sql("SELECT COUNT(*) FROM CMG_ANALOG ca join CMG_OBJECT co ON ca.IRN = co.IRN AND co.PARENT = @IRN;"); - pStmt->BindInt64(1, it->first); - while (pStmt->FetchRow()) - { - config.units[it->second].yccount = pStmt->GetColumnInt(0); - } - pStmt->ExecuteAndFree(); - pStmt->Sql("SELECT COUNT(*) FROM CMG_ACCUMULATOR ca join CMG_OBJECT co ON ca.IRN = co.IRN AND co.PARENT = @IRN;"); - pStmt->BindInt64(1, it->first); - while (pStmt->FetchRow()) - { - config.units[it->second].ymcount = pStmt->GetColumnInt(0); - } - pStmt->ExecuteAndFree(); - pStmt->Sql("SELECT COUNT(*) FROM CMG_CONTROL cc join CMG_OBJECT co ON cc.IRN = co.IRN AND co.PARENT = @IRN;"); - pStmt->BindInt64(1, it->first); - while (pStmt->FetchRow()) - { - config.units[it->second].ykcount = pStmt->GetColumnInt(0); - } - pStmt->ExecuteAndFree(); - pStmt->Sql("SELECT COUNT(*) FROM CMG_DISCRETE cd join CMG_OBJECT co ON cd.IRN = co.IRN AND co.PARENT = @IRN;"); - pStmt->BindInt64(1, it->first); - while (pStmt->FetchRow()) - { - config.units[it->second].yxcount = pStmt->GetColumnInt(0); - } - pStmt->ExecuteAndFree(); - pStmt->Sql("SELECT COUNT(*) FROM CMG_SETPOINT cs join CMG_OBJECT co ON cs.IRN = co.IRN AND co.PARENT = @IRN;"); - pStmt->BindInt64(1, it->first); - while (pStmt->FetchRow()) - { - config.units[it->second].ytcount = pStmt->GetColumnInt(0); - } - pStmt->ExecuteAndFree(); - //此处想增加一些读取物模型的代码 - //获取测点信息 - if (config.units[it->second].yccount > 0) - { - pStmt->Sql("SELECT co.IRN, co.NAME, ca.PTORDER, ca.FACTOR, ca.CHANGEPOS, ca.PARAMETER, ca.COEF, ca.BASE FROM CMG_ANALOG ca JOIN CMG_OBJECT co ON ca.IRN = co.IRN AND co.PARENT = @parent ;"); - config.units[it->second].ycs = new struUnitYC[config.units[it->second].yccount]; - if (NULL != config.units[it->second].ycs) - { - memset(config.units[it->second].ycs, 0, sizeof(struUnitYC) * config.units[it->second].yccount); - pStmt->BindInt64(1, it->first); - count = 0; - std::vector irns; - irns.clear(); - - char filename[512]; - snprintf(filename, sizeof(filename), FILE_UNIT_YC_STATIC, configpath, it->second); - FILE *pf = fopen(filename, "wb+"); - - while (pStmt->FetchRow()) - { - irns.push_back(pStmt->GetColumnInt64(0)); - config.units[it->second].ycs[count].irn = pStmt->GetColumnInt64(0); - if (pf) - { - fwrite(pStmt->GetColumnCString(1), (MAX_NAME_SIZE << 2), 1, pf); - } - //config.units[it->second].ycs[count].order = pStmt->GetColumnInt(2); - config.units[it->second].ycs[count].order = total_yc++; - config.units[it->second].ycs[count].factor = pStmt->GetColumnInt(3); - config.units[it->second].ycs[count].change_pos = pStmt->GetColumnInt(4); - std::string parameter = pStmt->GetColumnString(5); - config.units[it->second].ycs[count].coef = (float)pStmt->GetColumnDouble(6); - config.units[it->second].ycs[count].base = (float)pStmt->GetColumnDouble(7); - if (config.units[it->second].type == 1) - { - database.ycs[config.units[it->second].ycs[count].order].coef = config.units[it->second].ycs[count].coef; - database.ycs[config.units[it->second].ycs[count].order].base = config.units[it->second].ycs[count].base; - } - - switch (unit_link_proto[it->second]) - { - case PROTOCOL_HOST_IEC104: - case PROTOCOL_SUB_IEC104: - case PROTOCOL_HOST_MODBUS_TCP: - case PROTOCOL_SUB_MODBUS_TCP: - case PROTOCOL_HOST_MODBUS_RTU_TCP: - case PROTOCOL_RTU_STATE: - case PROTOCOL_LOCAL_DEBUG: - case PROTOCOL_SUB_XT9712: - case PROTOCOL_HOST_NSA: - case PROTOCOL_HOST_MODBUS_RTU: - case PROTOCOL_HOST_MODBUS_RTU_NCY: - case PROTOCOL_HOST_MODBUS_RTU_LIYEZG: - case PROTOCOL_HOST_MODBUS_ASCII: - case PROTOCOL_SUB_MODBUS_RTU: - case PROTOCOL_HOST_DLT645V2007: - case PROTOCOL_AGC: - case PROTOCOL_AVC: - case PROTOCOL_DLT_1867: - case PROTOCOL_SUB_GDW104: - case PROTOCOL_HW_MQTT: - case PROTOCOL_ISS_MQTT: - break; - case PROTOCOL_LM_MQTT: - processLMPointParam(parameter, it->second, count, LM_TYPE_YC); - config.processes[count].mode = PROCESS_MODE_MASTER; - break; - default: break; - } - count++; - } - pStmt->ExecuteAndFree(); - if (pf) fclose(pf); - } - } - if (config.units[it->second].ymcount > 0) - { - pStmt->Sql("SELECT co.IRN, co.NAME, ca.PTORDER, ca.PARAMETER, ca.COEF, ca.BASE FROM CMG_ACCUMULATOR ca JOIN CMG_OBJECT co ON ca.IRN = co.IRN and co.PARENT = @parent ;"); - config.units[it->second].yms = new struUnitYM[config.units[it->second].ymcount]; - if (NULL != config.units[it->second].yms) - { - memset(config.units[it->second].yms, 0, sizeof(struUnitYM) * config.units[it->second].ymcount); - pStmt->BindInt64(1, it->first); - count = 0; - std::vector irns; - irns.clear(); - - char filename[512]; - snprintf(filename, sizeof(filename), FILE_UNIT_YM_STATIC, configpath, it->second); - FILE *pf = fopen(filename, "wb+"); - - while (pStmt->FetchRow()) - { - irns.push_back(pStmt->GetColumnInt64(0)); - config.units[it->second].yms[count].irn = pStmt->GetColumnInt64(0); - if (pf) - { - fwrite(pStmt->GetColumnCString(1), (MAX_NAME_SIZE << 2), 1, pf); - } - //config.units[it->second].yms[count].order = pStmt->GetColumnInt(2); - config.units[it->second].yms[count].order = total_ym++; - std::string parameter = pStmt->GetColumnString(3); - config.units[it->second].yms[count].coef = (float)pStmt->GetColumnDouble(4); - config.units[it->second].yms[count].base = (float)pStmt->GetColumnDouble(5); - if (config.units[it->second].type == 1) - { - database.yms[config.units[it->second].yms[count].order].coef = config.units[it->second].yms[count].coef; - database.yms[config.units[it->second].yms[count].order].base = config.units[it->second].yms[count].base; - } - count++; - } - pStmt->ExecuteAndFree(); - if (pf) fclose(pf); - } - } - if (config.units[it->second].ykcount > 0) - { - pStmt->Sql("SELECT co.IRN, co.NAME, cc.PTORDER, cc.PARAMETER FROM CMG_CONTROL cc JOIN CMG_OBJECT co ON cc.IRN = co.IRN and co.PARENT = @parent ;"); - config.units[it->second].yks = new struUnitYK[config.units[it->second].ykcount]; - if (NULL != config.units[it->second].yks) - { - memset(config.units[it->second].yks, 0, sizeof(struUnitYK) * config.units[it->second].ykcount); - pStmt->BindInt64(1, it->first); - count = 0; - std::vector irns; - irns.clear(); - - char filename[512]; - snprintf(filename, sizeof(filename), FILE_UNIT_YK_STATIC, configpath, it->second); - FILE *pf = fopen(filename, "wb+"); - - while (pStmt->FetchRow()) - { - irns.push_back(pStmt->GetColumnInt64(0)); - config.units[it->second].yks[count].irn = pStmt->GetColumnInt64(0); - if (pf) - { - fwrite(pStmt->GetColumnCString(1), (MAX_NAME_SIZE << 2), 1, pf); - } - //config.units[it->second].yks[count].order = pStmt->GetColumnInt(2); - config.units[it->second].yks[count].order = total_yk++; - std::string parameter = pStmt->GetColumnString(3); - count++; - } - pStmt->ExecuteAndFree(); - if (pf) fclose(pf); - } - } - if (config.units[it->second].ytcount > 0) - { - pStmt->Sql("SELECT co.IRN, co.NAME, cs.PTORDER, cs.PARAMETER FROM CMG_SETPOINT cs JOIN CMG_OBJECT co ON cs.IRN = co.IRN and co.PARENT = @parent ;"); - config.units[it->second].yts = new struUnitYT[config.units[it->second].ytcount]; - if (NULL != config.units[it->second].yts) - { - memset(config.units[it->second].yts, 0, sizeof(struUnitYT) * config.units[it->second].ytcount); - pStmt->BindInt64(1, it->first); - count = 0; - std::vector irns; - irns.clear(); - - char filename[512]; - snprintf(filename, sizeof(filename), FILE_UNIT_YT_STATIC, configpath, it->second); - FILE *pf = fopen(filename, "wb+"); - - while (pStmt->FetchRow()) - { - irns.push_back(pStmt->GetColumnInt64(0)); - config.units[it->second].yts[count].irn = pStmt->GetColumnInt64(0); - if (pf) - { - fwrite(pStmt->GetColumnCString(1), (MAX_NAME_SIZE << 2), 1, pf); - } - //config.units[it->second].yts[count].order = pStmt->GetColumnInt(2); - config.units[it->second].yts[count].order = total_yt++; - std::string parameter = pStmt->GetColumnString(3); - count++; - } - pStmt->ExecuteAndFree(); - if (pf) fclose(pf); - } - } - if (config.units[it->second].yxcount > 0) - { - pStmt->Sql("SELECT co.IRN, co.NAME, cd.PTORDER, cd.PARAMETER FROM CMG_DISCRETE cd JOIN CMG_OBJECT co ON cd.IRN = co.IRN and co.PARENT = @parent ;"); - config.units[it->second].yxs = new struUnitYX[config.units[it->second].yxcount]; - if (NULL != config.units[it->second].yxs) - { - memset(config.units[it->second].yxs, 0, sizeof(struUnitYX) * config.units[it->second].yxcount); - pStmt->BindInt64(1, it->first); - count = 0; - std::vector irns; - irns.clear(); - - char filename[512]; - snprintf(filename, sizeof(filename), FILE_UNIT_YX_STATIC, configpath, it->second); - FILE *pf = fopen(filename, "wb+"); - - while (pStmt->FetchRow()) - { - irns.push_back(pStmt->GetColumnInt64(0)); - config.units[it->second].yxs[count].irn = pStmt->GetColumnInt64(0); - if (pf) - { - fwrite(pStmt->GetColumnCString(1), (MAX_NAME_SIZE << 2), 1, pf); - } - //config.units[it->second].yxs[count].order = pStmt->GetColumnInt(2); - config.units[it->second].yxs[count].order = total_yx++; - database.yxs[config.units[it->second].yxs[count].order].auto_reset = 0; - std::string parameter = pStmt->GetColumnString(3); - count++; - } - pStmt->ExecuteAndFree(); - if (pf) fclose(pf); - } - } - } - - Total_time = (double)(clock() - start) / CLOCKS_PER_SEC; //单位换算成秒 - vLog(LOG_DEBUG, "获取测点名称 use: %f seconds\n", Total_time); - - delete pStmt; - delete pDatabase; - } - catch (SQLiteException& exception) - { - vLog(LOG_ERROR, "Exception Occured SQLite result code: %d,%s\n", exception.GetSqliteResultCode(), exception.GetErrorDescription().c_str()); - return FALSE; - } - - //判断是否配置了本地调试协议 - int local_debug_pid = (PROCESSES_NUM - 1); //config_process_count; - if (!have_local_debug) - { //没有配置本地协议 - snprintf(config.processes[local_debug_pid].name, sizeof(config.processes[local_debug_pid].name), "%s", "本地调试"); - config.processes[local_debug_pid].state = TRUE; - config.processes[local_debug_pid].time_accept = FALSE; - config.processes[local_debug_pid].proto = PROTOCOL_LOCAL_DEBUG; - config.processes[local_debug_pid].mode = PROCESS_MODE_MASTER; - config.processes[local_debug_pid].time_gap = 300; - config.processes[local_debug_pid].poll_gap = 5; - config.processes[local_debug_pid].order = 0; - config.processes[local_debug_pid].units[0] = -1; - config.processes[local_debug_pid].option.network.ignored_source = TRUE; - config.processes[local_debug_pid].option.network.socket_type = SOCK_STREAM; - config.processes[local_debug_pid].option.network.bind_port = 9001; - config.processes[local_debug_pid].option.network.bind_addr = INADDR_ANY; - config.processes[local_debug_pid].option.network.target_port = 0; - config.processes[local_debug_pid].option.network.target_addr = INADDR_ANY; - - config_process_count++; - } - //判断本地调试协议是否链接了单元 - if (config.processes[local_debug_pid].units[0] == -1) - { - int local_debug_uid = (UNIT_NUM - 1);//config_unit_count; - config.units[local_debug_uid].state = TRUE; - config.units[local_debug_uid].type = 1; - config.units[local_debug_uid].addr[0] = 0; - memset(config.units[local_debug_uid].m_param, 0, sizeof(config.units[local_debug_uid].m_param)); - config.units[local_debug_uid].yxcount = 0; - config.units[local_debug_uid].yccount = 0; - config.units[local_debug_uid].ymcount = 0; - config.units[local_debug_uid].ykcount = 0; - config.units[local_debug_uid].ytcount = 0; - config.units[local_debug_uid].softdog = 0; - config.units[local_debug_uid].yxbwload = 0; - config.units[local_debug_uid].soeload = 0; - config.units[local_debug_uid].ycbwload = 0; - config.units[local_debug_uid].value = 0; - config.units[local_debug_uid].yxs = NULL; - config.units[local_debug_uid].ycs = NULL; - config.units[local_debug_uid].yms = NULL; - config.units[local_debug_uid].yks = NULL; - config.units[local_debug_uid].yts = NULL; - snprintf(static_units[local_debug_uid].name, sizeof(static_units[local_debug_uid].name), "%s", "本地调试"); - snprintf(static_units[local_debug_uid].model, sizeof(static_units[local_debug_uid].model), "%s", "本地调试"); - snprintf(static_units[local_debug_uid].manufacturerId, sizeof(static_units[local_debug_uid].manufacturerId), "%s", "iSoftStone"); - snprintf(static_units[local_debug_uid].deviceId, sizeof(static_units[local_debug_uid].deviceId), "%s", ""); - - config.processes[local_debug_pid].units[0] = local_debug_uid; - - config_unit_count++; - } - if (enable_auto_platform) - { - if (issmqtt_pid < 0) - { - issmqtt_pid = config_process_count; - snprintf(config.processes[issmqtt_pid].name, sizeof(config.processes[issmqtt_pid].name), "%s", "软通USP平台MQTT"); - config.processes[issmqtt_pid].state = TRUE; - config.processes[issmqtt_pid].time_accept = FALSE; - config.processes[issmqtt_pid].proto = PROTOCOL_ISS_MQTT; - config.processes[issmqtt_pid].mode = PROCESS_MODE_SLAVE; - config.processes[issmqtt_pid].time_gap = 300; - config.processes[issmqtt_pid].poll_gap = 5; - config.processes[issmqtt_pid].order = 0; - config.processes[issmqtt_pid].units[0] = -1; - - struISSMQTTOption* issmqttOption = (struISSMQTTOption *)config.processes[issmqtt_pid].option.data; - - //默认配置 - snprintf(issmqttOption->client_id, MAX_ID_LENGTH, "%s", szHostCode); - snprintf(issmqttOption->username, sizeof(issmqttOption->username), "%s", "nmu8000"); - snprintf(issmqttOption->password, sizeof(issmqttOption->password), "%s", "spi@84805353"); - snprintf(issmqttOption->host, sizeof(issmqttOption->host), "%s", "192.168.109.177"); - snprintf(issmqttOption->topic_version, sizeof(issmqttOption->topic_version), "%s", "v100"); - issmqttOption->target_port = MQTT_DEFAULT_PORT; - issmqttOption->connectInterval = 30; //链接等待 - issmqttOption->keepAliveInterval = 60; //心跳链接 - issmqttOption->connectTimeout = 30; //链接等待超时 - issmqttOption->publishInterval = 60; //数据发送间隔 - - //读取配置文件内容 - std::ifstream ifs; - Json::Value jsonRoot; - Json::Reader jsonReader; - ifs.open(issmqtt_config_pathName); - if (ifs.is_open()) - { - if (jsonReader.parse(ifs, jsonRoot, false)) - { - if (jsonRoot["port"].isInt()) - { - issmqttOption->target_port = jsonRoot["port"].asInt(); - } - if (jsonRoot["host"].isString()) - { - snprintf(issmqttOption->host, sizeof(issmqttOption->host), "%s", jsonRoot["host"].asCString()); - } - if (jsonRoot["username"].isString()) - { - snprintf(issmqttOption->username, sizeof(issmqttOption->username), "%s", jsonRoot["username"].asCString()); - } - if (jsonRoot["password"].isString()) - { - snprintf(issmqttOption->password, sizeof(issmqttOption->password), "%s", jsonRoot["password"].asCString()); - } - if (jsonRoot["topic_version"].isString()) - { - snprintf(issmqttOption->topic_version, sizeof(issmqttOption->topic_version), "%s", jsonRoot["topic_version"].asCString()); - } - } - else - { - fprintf(stderr, "configure file format is error.\r\n"); - } - ifs.close(); - } - config_process_count++; - } - - if (issmqtt_pid >= 0 && config.processes[issmqtt_pid].units[0] == -1) - { //配置了软通mqtt协议 - int issmqtt_uid = config_unit_count; - config.units[issmqtt_uid].state = TRUE; - config.units[issmqtt_uid].type = 1; - config.units[issmqtt_uid].addr[0] = 0; - memset(config.units[issmqtt_uid].m_param, 0, sizeof(config.units[issmqtt_uid].m_param)); - config.units[issmqtt_uid].yxcount = 0; - config.units[issmqtt_uid].yccount = 0; - config.units[issmqtt_uid].ymcount = 0; - config.units[issmqtt_uid].ykcount = 0; - config.units[issmqtt_uid].ytcount = 0; - config.units[issmqtt_uid].softdog = 0; - config.units[issmqtt_uid].yxbwload = 0; - config.units[issmqtt_uid].soeload = 0; - config.units[issmqtt_uid].ycbwload = 0; - config.units[issmqtt_uid].value = 0; - config.units[issmqtt_uid].yxs = NULL; - config.units[issmqtt_uid].ycs = NULL; - config.units[issmqtt_uid].yms = NULL; - config.units[issmqtt_uid].yks = NULL; - config.units[issmqtt_uid].yts = NULL; - snprintf(static_units[issmqtt_uid].name, sizeof(static_units[issmqtt_uid].name), "%s", "本地USP平台"); - snprintf(static_units[issmqtt_uid].model, sizeof(static_units[issmqtt_uid].model), "%s", "本地USP平台"); - snprintf(static_units[issmqtt_uid].manufacturerId, sizeof(static_units[issmqtt_uid].manufacturerId), "%s", "iSoftStone"); - snprintf(static_units[issmqtt_uid].deviceId, sizeof(static_units[issmqtt_uid].deviceId), "%s", ""); - - config.processes[issmqtt_pid].units[0] = issmqtt_uid; - config.units[issmqtt_uid].yxs = new struUnitYX[UNIT_YX_MAX]; - memset(config.units[issmqtt_uid].yxs, 0, sizeof(struUnitYX) * UNIT_YX_MAX); - config.units[issmqtt_uid].ycs = new struUnitYC[UNIT_YC_MAX]; - memset(config.units[issmqtt_uid].ycs, 0, sizeof(struUnitYC) * UNIT_YC_MAX); - config.units[issmqtt_uid].yms = new struUnitYM[UNIT_YM_MAX]; - memset(config.units[issmqtt_uid].yms, 0, sizeof(struUnitYM) * UNIT_YM_MAX); - config.units[issmqtt_uid].yks = new struUnitYK[UNIT_YK_MAX]; - memset(config.units[issmqtt_uid].yks, 0, sizeof(struUnitYK) * UNIT_YK_MAX); - config.units[issmqtt_uid].yts = new struUnitYT[UNIT_YT_MAX]; - memset(config.units[issmqtt_uid].yts, 0, sizeof(struUnitYT) * UNIT_YT_MAX); - - //将其他单元数据挂载在此单元上 - for (i = 0; i < UNIT_NUM; i++) - { - if (config.units[i].state == TRUE && i != issmqtt_uid) - { - if (config.units[i].yxcount > 0) - { - if (config.units[issmqtt_uid].yxcount + config.units[i].yxcount < UNIT_YX_MAX) - { - memcpy(&config.units[issmqtt_uid].yxs[config.units[issmqtt_uid].yxcount], config.units[i].yxs, sizeof(struUnitYX)*config.units[i].yxcount); - config.units[issmqtt_uid].yxcount += config.units[i].yxcount; - } - } - if (config.units[i].yccount > 0) - { - if (config.units[issmqtt_uid].yccount + config.units[i].yccount < UNIT_YC_MAX) - { - memcpy(&config.units[issmqtt_uid].ycs[config.units[issmqtt_uid].yccount], config.units[i].ycs, sizeof(struUnitYC)*config.units[i].yccount); - config.units[issmqtt_uid].yccount += config.units[i].yccount; - } - } - if (config.units[i].ymcount > 0) - { - if (config.units[issmqtt_uid].ymcount + config.units[i].ymcount < UNIT_YM_MAX) - { - memcpy(&config.units[issmqtt_uid].yms[config.units[issmqtt_uid].ymcount], config.units[i].yms, sizeof(struUnitYM)*config.units[i].ymcount); - config.units[issmqtt_uid].ymcount += config.units[i].ymcount; - } - } - if (config.units[i].ykcount > 0) - { - if (config.units[issmqtt_uid].ykcount + config.units[i].ykcount < UNIT_YK_MAX) - { - memcpy(&config.units[issmqtt_uid].yks[config.units[issmqtt_uid].ykcount], config.units[i].yks, sizeof(struUnitYK)*config.units[i].ykcount); - config.units[issmqtt_uid].ykcount += config.units[i].ykcount; - } - } - if (config.units[i].ytcount > 0) - { - if (config.units[issmqtt_uid].ytcount + config.units[i].ytcount < UNIT_YT_MAX) - { - memcpy(&config.units[issmqtt_uid].yts[config.units[issmqtt_uid].ytcount], config.units[i].yts, sizeof(struUnitYT)*config.units[i].ytcount); - config.units[issmqtt_uid].ytcount += config.units[i].ytcount; - } - } - } - } - } - } - - return TRUE; -} -#endif - void dumpLogs(void) { if (system32.log_enabled) @@ -2305,22 +1245,27 @@ void* idle_process(void* param) } } for (int i = 0; i < PROCESSES_NUM; i++) { - config.processes[i].softdog++; - if (config.processes[i].softdog > PROCESS_WATCHDOG_TIME) { - config.processes[i].softdog = PROCESS_WATCHDOG_TIME; - } else { + config.processes[i].real_softdog++; + if (config.processes[i].real_softdog > PROCESS_WATCHDOG_TIME) { + config.processes[i].real_softdog = PROCESS_WATCHDOG_TIME; + } + config.processes[i].ftp_softdog++; + if (config.processes[i].ftp_softdog > PROCESS_WATCHDOG_TIME) { + config.processes[i].ftp_softdog = PROCESS_WATCHDOG_TIME; } } for (int i = 0; i < UNIT_NUM; i++) { - config.units[i].softdog++; - if (config.units[i].softdog > UNIT_WATCHDOG_TIME) { - config.units[i].softdog = UNIT_WATCHDOG_TIME; - //config.units[i].value = SPI_ON; + config.units[i].real_softdog++; + if (config.units[i].real_softdog > UNIT_WATCHDOG_TIME) { + config.units[i].real_softdog = UNIT_WATCHDOG_TIME; config.units[i].value |= SPI_ON; } else { - //config.units[i].value = SPI_OFF; config.units[i].value &= ~SPI_ON; } + config.units[i].ftp_softdog++; + if (config.units[i].ftp_softdog > UNIT_WATCHDOG_TIME) { + config.units[i].ftp_softdog = UNIT_WATCHDOG_TIME; + } } } pthread_exit(0); diff --git a/das-dn/hostadsbf/hostadsbf.cpp b/das-dn/hostadsbf/hostadsbf.cpp index 0804bf58..036269be 100644 --- a/das-dn/hostadsbf/hostadsbf.cpp +++ b/das-dn/hostadsbf/hostadsbf.cpp @@ -17,7 +17,7 @@ #define ADSP_READ_ID_REGISTER_ADDRESS 300 //读取文件及文件夹ID寄存器地址。 #define ADSP_READ_ID_REGISTER_LENGTH 32 //读取文件及文件夹ID寄存器长度。 - +#define vLog(...) static datalen2mbaddrmap m_datalen2mbaddr_map; static struDataLengthADSAddr m_datalen_adsaddr[] = { { 2, 421, 0.1 }, @@ -493,6 +493,8 @@ static void* ryftp_process(void* param) while (TRUE) { sleep(1); //每秒执行一次 if (!mbt->m_bFtpRun) break; + + mbt->FeedFTPDog(); //ftp获取文件 if (mbt->m_iv == 1) { //文件目录无效 @@ -527,8 +529,8 @@ static void* ryftp_process(void* param) } struRYDeviceData *data = (struRYDeviceData *)chunk.response; - int uid = mbt->GetCurUnitID(); + mbt->UnitFeedFTPDog(uid); for (int i = 0; i < 250; i++, data++) { //每个文件有250个数据块 QLONG unix_time = filetime_to_unix(data->localtime); DWORD localtime = ((unix_time / 1000) - 28800L); @@ -711,12 +713,6 @@ CHostADSBFProcess::~CHostADSBFProcess() pthread_join(m_cpid, NULL); vLog(LOG_DEBUG, "child protocol thread [%s] is destroyed.\n", (name[0] != '\0') ? name : "unknow"); } -#if 0 - if (m_threadRun) { - m_threadRun = FALSE; - m_startup.join(); - } -#endif } BOOLEAN CHostADSBFProcess::calc(void) @@ -734,8 +730,6 @@ BOOLEAN CHostADSBFProcess::calc(void) m_adsDatas[1].adsDataMemAddr = 65535; m_adsDatas[2].adsDataMemAddr = 65535; m_adsDatas[3].adsDataMemAddr = 65535; - //需要给单元添加一个是否有ftp数据的标识 - if (m_bHaveFTP) config.units[uid].state |= 0x20; //存在ftp标识 int yccount, ymcount, yxcount; yccount = GetUnitYCCount(uid); @@ -830,33 +824,6 @@ BOOLEAN CHostADSBFProcess::calc(void) return TRUE; } -void CHostADSBFProcess::tryStartup() -{ - m_turbine = NULL; - pthread_setname_np(pthread_self(), m_pidName.c_str()); - while (m_threadRun) { - if (m_turbine == NULL) { - SetLocalAmsNetId(AmsNetId(m_localNetId)); - m_turbine = new AdsDevice{m_remoteIp, AmsNetId(m_remoteNetId), m_remotePort}; - const auto state = m_turbine->GetState(); - vLog(LOG_DEBUG, "%s ADS state: %d devState: %d\n", m_pidName.c_str(), (uint16_t)state.ads, (uint16_t)state.device); - if ((uint16_t)state.ads >= ADSSTATE::ADSSTATE_MAXSTATES || (uint16_t)state.device >= ADSSTATE::ADSSTATE_MAXSTATES) - { - vLog(LOG_DEBUG, "read device state error.\n"); - long ret = AddRemoteRoute(m_remoteIp, m_localNetId, m_localIp, std::string("isoftstone"), std::string("admin"), std::string("admin")); - vLog(LOG_DEBUG, "bbb %s add route return value is: %d\n", m_pidName.c_str(), ret); - delete m_turbine; - m_turbine = NULL; - } - } else { - if (m_turbine->IsConnected()) continue; - delete m_turbine; - m_turbine = NULL; - } - sleep(5); - } -} - BOOLEAN CHostADSBFProcess::OnPreCreate(int id) { if (!CProcess::OnPreCreate(id)) return FALSE; @@ -887,6 +854,14 @@ BOOLEAN CHostADSBFProcess::OnPreCreate(int id) m_adsPassword = std::string(m_nOptions.password); m_bHaveFTP = m_nOptions.bHaveFTP; + //需要给单元添加一个是否有ftp数据的标识 + + int uid = GetCurUnitID(); + if (uid < 0 || uid >= UNIT_NUM) return FALSE; + if (m_bHaveFTP) { + config.units[uid].state |= 0x20; //存在ftp标识 + config.processes[id].state |= 0x20; + } calc(); //vLog(LOG_DEBUG, "%s local ip is: %s, netid is: %s, remote ip is: %s, and netid is: %s\n", m_pidName.c_str(), m_localIp.c_str(), m_localNetId.c_str(), m_remoteIp.c_str(), m_remoteNetId.c_str()); @@ -896,10 +871,7 @@ BOOLEAN CHostADSBFProcess::OnPreCreate(int id) m_connect_count = 0; m_apdu_t0_begin = 10; SetLocalAmsNetId(AmsNetId(m_localNetId)); -#if 0 - m_threadRun = TRUE; - m_startup = std::thread(&CHostADSBFProcess::tryStartup, this); -#endif + if (m_bHaveFTP) { snprintf(m_user, sizeof(m_user), "%s", m_nOptions.ftp.user); @@ -967,24 +939,32 @@ BOOLEAN CHostADSBFProcess::OnTimer(void) last_sec = system32.timers; sec_changed = TRUE; } + FeedDog(); if (sec_changed) { + //if (GetCurID() == 52) fprintf(stderr, "here second changed m_turbine is: %lu\n", m_turbine); if (m_turbine == NULL) { if ((m_apdu_t0_begin + 20) < system32.timers) { + //if (GetCurID() == 52) fprintf(stderr, "here apdu t0 timeout, start to connect\n"); m_apdu_t0_begin = system32.timers; //先添加一条路由 if (m_bRouteAdded == FALSE) { + //if (GetCurID() == 52) fprintf(stderr, "here route not add, start to add a route: %s to %s\n", m_localIp.c_str(), m_remoteIp.c_str()); long ret = AddRemoteRoute(m_remoteIp, AmsNetId(m_localNetId), m_localIp, std::string("isoftstone"), m_adsUser, m_adsPassword); + //if (GetCurID() == 52) fprintf(stderr, "here add route return value is: %d\n", ret); vLog(LOG_DEBUG, "%s add route(%s to %s) with remote user name: %s, and remote password: %s, return value is: %d\n", m_pidName.c_str(), m_localIp.c_str(), m_remoteIp.c_str(), m_adsUser.c_str(), m_adsPassword.c_str(), ret); if (ret != 0) return TRUE; } //添加成功 + //if (GetCurID() == 52) fprintf(stderr, "here start to connect(%s:%d)\n", m_remoteIp.c_str(), m_remotePort); vLog(LOG_DEBUG, "%s start to connect(%s:%d)\n", m_pidName.c_str(), m_remoteIp.c_str(), m_remotePort); m_turbine = new AdsDevice{m_remoteIp, AmsNetId(m_remoteNetId), m_remotePort}; + + //if (GetCurID() == 52) fprintf(stderr, "here start to get device state\n"); const auto state = m_turbine->GetState(); if ((uint16_t)state.ads >= ADSSTATE::ADSSTATE_MAXSTATES || (uint16_t)state.device >= ADSSTATE::ADSSTATE_MAXSTATES) { @@ -1003,7 +983,7 @@ BOOLEAN CHostADSBFProcess::OnTimer(void) { vLog(LOG_DEBUG, "%s device not connected.\n", m_pidName.c_str()); m_connect_count++; - if (m_connect_count >= 10) + //if (m_connect_count >= 10) { m_connect_count = 10; m_bRouteAdded = FALSE; @@ -1051,6 +1031,7 @@ BOOLEAN CHostADSBFProcess::readFileID() myAdsVariable myPathInfoInvalid {*m_turbine, ".gwPathInfoInvalid"}; if (!myPathInfoInvalid.Read(2, &wPathInfoInvalid)) { + m_bRouteAdded = FALSE; delete m_turbine; m_turbine = NULL; return TRUE; @@ -1061,6 +1042,7 @@ BOOLEAN CHostADSBFProcess::readFileID() if (!myCurrentFolderNo.Read(4, &wCurrentFolderNo)) { vLog(LOG_ERROR, "Read .gwCurrentFolderNo error.\n"); + m_bRouteAdded = FALSE; delete m_turbine; m_turbine = NULL; return TRUE; @@ -1071,6 +1053,7 @@ BOOLEAN CHostADSBFProcess::readFileID() if (!myCurrentFileNo.Read(4, &wCurrentFileNo)) { vLog(LOG_ERROR, "Read .gwCurrentFileNo error.\n"); + m_bRouteAdded = FALSE; delete m_turbine; m_turbine = NULL; return TRUE; @@ -1081,6 +1064,7 @@ BOOLEAN CHostADSBFProcess::readFileID() if (!myFirstFileNoInFolder.Read(4, &wFirstFileNoInFolder)) { vLog(LOG_ERROR, "Read .gwFirstFileNoInFolder error.\n"); + m_bRouteAdded = FALSE; delete m_turbine; m_turbine = NULL; return TRUE; @@ -1091,6 +1075,7 @@ BOOLEAN CHostADSBFProcess::readFileID() if (!myFileCountInFolder.Read(4, &wFileCountInFolder)) { vLog(LOG_ERROR, "Read .gwFileCountInFolder error.\n"); + m_bRouteAdded = FALSE; delete m_turbine; m_turbine = NULL; return TRUE; @@ -1101,6 +1086,7 @@ BOOLEAN CHostADSBFProcess::readFileID() if (!myFirstFolderNoRecoverable.Read(4, &wFirstFolderNoRecoverable)) { vLog(LOG_ERROR, "Read .gwFirstFolderNoRecoverable error.\n"); + m_bRouteAdded = FALSE; delete m_turbine; m_turbine = NULL; return TRUE; @@ -1111,6 +1097,7 @@ BOOLEAN CHostADSBFProcess::readFileID() if (!myFirstFolderNoRecoverable.Read(4, &wFirstFileNoRecoverable)) { vLog(LOG_ERROR, "Read .gwFirstFileNoRecoverable error.\n"); + m_bRouteAdded = FALSE; delete m_turbine; m_turbine = NULL; return TRUE; @@ -1174,6 +1161,7 @@ BOOLEAN CHostADSBFProcess::readRealData() if (!turbineData.Read(1024, buffer)) { vLog(LOG_ERROR, "%s Read data error\n", m_pidName.c_str()); + m_bRouteAdded = FALSE; delete m_turbine; m_turbine = NULL; return TRUE; diff --git a/das-dn/hostadsbf/hostadsbf.h b/das-dn/hostadsbf/hostadsbf.h index a4b2e2b8..176429a3 100644 --- a/das-dn/hostadsbf/hostadsbf.h +++ b/das-dn/hostadsbf/hostadsbf.h @@ -99,9 +99,6 @@ private: int m_total_length; DWORD last_sec; - BOOLEAN m_threadRun; - std::thread m_startup; - void tryStartup(); struADSData m_adsDatas[4]; public: diff --git a/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.cpp b/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.cpp index 2b7af798..e1160797 100644 --- a/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.cpp +++ b/das-dn/hostmodbustcpbf/host_modbus_tcp_bf.cpp @@ -498,6 +498,8 @@ static void* ryftp_process(void* param) while (TRUE) { sleep(1); //每秒执行一次 if (!mbt->m_bFtpRun) break; + + mbt->FeedFTPDog(); //ftp获取文件 if (mbt->m_iv == 1) { //文件目录无效 @@ -532,8 +534,8 @@ static void* ryftp_process(void* param) } struRYDeviceData *data = (struRYDeviceData *)chunk.response; - int uid = mbt->GetCurUnitID(); + mbt->UnitFeedFTPDog(uid); for (int i = 0; i < 250; i++, data++) { //每个文件有250个数据块 QLONG unix_time = filetime_to_unix(data->localtime); DWORD localtime = ((unix_time / 1000) - 28800L); @@ -867,12 +869,13 @@ void CHostModbusTcpBFProcess::calc2(void) uid = pItem->GetUnitID(); if (uid < 0 || uid >= UNIT_NUM) continue; + //需要给单元添加一个是否有ftp数据的标识 + if (m_bHaveFTP) config.units[uid].state |= 0x20; //存在ftp标识 + if (GetUnitAddr(uid, &addr, 1)) { //获取单元地址成功,则该段原地址采用配置地址,否则该单元为无效地址。 pItem->m_addr = addr; } - //需要给单元添加一个是否有ftp数据的标识 - if (m_bHaveFTP) config.units[uid].state |= 0x20; //存在ftp标识 struModbusExtFrame ycframes[MODBUS_RTU_AUTOMATIC_FRAME]; struModbusExtFrame ymframes[MODBUS_RTU_AUTOMATIC_FRAME]; @@ -1130,6 +1133,11 @@ BOOLEAN CHostModbusTcpBFProcess::OnPreCreate(int id) m_bHaveFTP = m_nOptions.bHaveFTP; m_nTimeout = 200; + //需要给单元添加一个是否有ftp数据的标识 + if (m_bHaveFTP) + { + config.processes[id].state |= 0x20; + } calc2(); if (m_bHaveFTP) diff --git a/das-dn/inc/process.h b/das-dn/inc/process.h index d65fc99e..a4fdcbfd 100644 --- a/das-dn/inc/process.h +++ b/das-dn/inc/process.h @@ -44,7 +44,15 @@ public: inline void FeedDog(void) { if (m_nProcess < 0 || m_nProcess >= PROCESSES_NUM) return; - config.processes[m_nProcess].softdog = 0; + //config.processes[m_nProcess].softdog = 0; + config.processes[m_nProcess].real_softdog = 0; + } + + inline void FeedFTPDog(void) + { + if (m_nProcess < 0 || m_nProcess >= PROCESSES_NUM) return; + //config.processes[m_nProcess].softdog = 0; + config.processes[m_nProcess].ftp_softdog = 0; } inline char* GetCurProcessName(void) const @@ -134,7 +142,12 @@ public: inline void UnitFeedDog(int uid) { if (uid < 0 || uid >= UNIT_NUM) return; - config.units[uid].softdog = 0; + config.units[uid].real_softdog = 0; + } + inline void UnitFeedFTPDog(int uid) + { + if (uid < 0 || uid >= UNIT_NUM) return; + config.units[uid].ftp_softdog = 0; } inline int GetUnitYXCount(int uid) const diff --git a/das-dn/inc/public.h b/das-dn/inc/public.h index 45492fe5..b07b97ed 100644 --- a/das-dn/inc/public.h +++ b/das-dn/inc/public.h @@ -94,8 +94,8 @@ typedef int SOCKET; #define DATABASE_YT_LOG_NUM 1024 #define DATABASE_CTRL_LOG_NUM 1024 #define DATABASE_LIMIT_NUM 1024 -#define PROCESS_WATCHDOG_TIME 90 -#define UNIT_WATCHDOG_TIME 180 +#define PROCESS_WATCHDOG_TIME 60 +#define UNIT_WATCHDOG_TIME 120 #define PROCESS_MODE_MASTER 0 #define PROCESS_MODE_SLAVE 1 @@ -752,7 +752,9 @@ typedef struct BYTE mode; //slave mode or master mode or duplex mode short time_gap; //send out time from this proc 18.2c/s short poll_gap; //send out command 18.2c/s - short softdog; //software watchdog counter + //short softdog; //software watchdog counter + BYTE real_softdog; + BYTE ftp_softdog; short units[PROCESS_UNIT_NUM]; unionProcOption option; QLONG irn; @@ -868,7 +870,9 @@ typedef struct short ymcount; short ykcount; short ytcount; - short softdog; + //short softdog; + BYTE real_softdog; + BYTE ftp_softdog; LONG yxbwload; LONG soeload; LONG ycbwload; diff --git a/das-dn/rtustatusproc/rtustatus.cpp b/das-dn/rtustatusproc/rtustatus.cpp index fc3ca81b..c0ff757e 100644 --- a/das-dn/rtustatusproc/rtustatus.cpp +++ b/das-dn/rtustatusproc/rtustatus.cpp @@ -37,10 +37,10 @@ BOOLEAN CRTUStatusProcess::OnTimer(void) //更新协议运行状态 for (i = 1; i < PROCESSES_NUM; i++) { - config.processes[i].softdog++; - if (config.processes[i].softdog > PROCESS_WATCHDOG_TIME) + config.processes[i].real_softdog++; + if (config.processes[i].real_softdog > PROCESS_WATCHDOG_TIME) { - config.processes[i].softdog = PROCESS_WATCHDOG_TIME; + config.processes[i].real_softdog = PROCESS_WATCHDOG_TIME; SetUnitYX(uid, i, SPI_ON); // 协议中断 } else diff --git a/das-dn/third_party/AdsLib/Sockets.cpp b/das-dn/third_party/AdsLib/Sockets.cpp index d5c86012..c1f24353 100644 --- a/das-dn/third_party/AdsLib/Sockets.cpp +++ b/das-dn/third_party/AdsLib/Sockets.cpp @@ -125,12 +125,56 @@ Socket::Socket(const struct addrinfo* const host, const int type) : continue; } if (SOCK_STREAM == type) { - if (::connect(m_Socket, rp->ai_addr, rp->ai_addrlen)) { - LOG_WARN("Socket connect["<(rp->ai_addr)->sin_addr)) << "] timeout"); + fprintf(stderr, "start to connect.\n"); +#if 0 + net_socket_nonblock(m_Socket, true); //设置为非阻塞模式 + int errormsg = -1; + int len; + len = sizeof(int); + timeval tm; + fd_set set; +#endif + if (::connect(m_Socket, rp->ai_addr, rp->ai_addrlen) == SOCKET_ERROR) { + LOG_WARN("Socket connect["<(rp->ai_addr)->sin_addr)) << "," << reinterpret_cast(rp->ai_addr)->sin_port << "] timeout"); +#if 1 closesocket(m_Socket); m_Socket = INVALID_SOCKET; continue; +#else + if (errno != EINPROGRESS && errno != EWOULDBLOCK) + { + net_socket_nonblock(m_Socket, false); //设置为阻塞模式 + closesocket(m_Socket); + m_Socket = INVALID_SOCKET; + continue; + } + else + { + tm.tv_sec = 5; + tm.tv_usec = 0; + FD_ZERO(&set); + FD_SET(m_Socket, &set); + if (select(m_Socket + 1, NULL, &set, NULL, &tm) > 0) + { + getsockopt(m_Socket, SOL_SOCKET, SO_ERROR, &errormsg, (socklen_t *)&len); + if (errormsg == 0) + { + net_socket_nonblock(m_Socket, false); //设置为阻塞模式 + m_Connected = true; + m_HostAddr = *(reinterpret_cast(rp->ai_addr)); + memcpy(&m_SockAddress, rp->ai_addr, std::min(sizeof(m_SockAddress), rp->ai_addrlen)); + return; + } + } + net_socket_nonblock(m_Socket, false); //设置为阻塞模式 + closesocket(m_Socket); + m_Socket = INVALID_SOCKET; + continue; + } +#endif } else { + LOG_WARN("Socket connect["<(rp->ai_addr)->sin_addr)) << "] timeout"); + net_socket_nonblock(m_Socket, false); //设置为阻塞模式 m_Connected = true; m_HostAddr = *(reinterpret_cast(rp->ai_addr)); } @@ -223,7 +267,7 @@ size_t Socket::read(uint8_t* buffer, size_t maxBytes, timeval* timeout) LOG_ERROR("Socket read frame failed with error: " << std::dec << std::strerror(m_LastError)); } - Shutdown(); +// Shutdown(); return 0; } @@ -261,7 +305,7 @@ bool Socket::Select(timeval* timeout) m_LastError = WSAGetLastError(); if (m_LastError == WSAENOTSOCK) { //throw std::runtime_error("connection closed"); - Shutdown(); + // Shutdown(); return false; } @@ -272,7 +316,7 @@ bool Socket::Select(timeval* timeout) } if (m_Socket < 0 || m_Socket >= FD_SETSIZE) { - LOG_ERROR("received a non valid socket (" << m_Socket << "), unable to add to the set"); + LOG_ERROR("received a non valid socket (" << m_Socket << "), unable to add to the set 2"); return false; } if (!FD_ISSET(m_Socket, &readSockets)) @@ -302,7 +346,7 @@ size_t Socket::write(const Frame& frame) if(0 == len){ LOG_INFO("write error, Socket: " << m_Socket << " connection closed by remote with error: " << std::dec << std::strerror(m_LastError)); - Shutdown(); + //Shutdown(); return 0; } @@ -326,8 +370,10 @@ uint32_t TcpSocket::GetLocalSockAddr() const struct sockaddr_storage source; socklen_t len = sizeof(source); - if(m_Socket == INVALID_SOCKET) + if(m_Socket == INVALID_SOCKET) { LOG_WARN("SocketTcp is invalid."); + return 0; + } if (getsockname(m_Socket, reinterpret_cast(&source), &len)) { LOG_ERROR("TcpSocket read local tcp/ip address failed."); diff --git a/das-dn/third_party/AdsLib/Sockets.h b/das-dn/third_party/AdsLib/Sockets.h index 027fc99c..2e32668d 100644 --- a/das-dn/third_party/AdsLib/Sockets.h +++ b/das-dn/third_party/AdsLib/Sockets.h @@ -9,6 +9,8 @@ #include "wrap_socket.h" #include #include +#include +#include namespace Beckhoff { @@ -49,6 +51,14 @@ struct Socket { */ bool IsConnectedTo(const struct addrinfo* targetAddresses) const; + bool net_socket_nonblock(SOCKET sock, bool blocking) + { + uint32_t ul = 1; + if (blocking) ul = 1; + else ul = 0; + return ioctl(sock, FIONBIO, &ul); + } + protected: bool m_Connected; int m_WSAInitialized; diff --git a/das-dn/third_party/AdsLib/Standalone/AmsConnection.cpp b/das-dn/third_party/AdsLib/Standalone/AmsConnection.cpp index 3ef91c9f..3e6bc9ef 100644 --- a/das-dn/third_party/AdsLib/Standalone/AmsConnection.cpp +++ b/das-dn/third_party/AdsLib/Standalone/AmsConnection.cpp @@ -71,7 +71,7 @@ AmsConnection::AmsConnection(Router& __router, const struct addrinfo* const dest refCount(0), invokeId(0) { - if(socket.IsConnected()) +// if(socket.IsConnected()) { localIp = socket.GetLocalSockAddr(); remoteIp = socket.GetHostSockAddr(); @@ -79,25 +79,21 @@ AmsConnection::AmsConnection(Router& __router, const struct addrinfo* const dest struct in_addr ss{htonl(remoteIp)}; LOG_INFO("Socket connect["< notification) diff --git a/das-dn/zjd3100proc/zjd3100pro.cpp b/das-dn/zjd3100proc/zjd3100pro.cpp index 30b50d3c..db55ba2a 100644 --- a/das-dn/zjd3100proc/zjd3100pro.cpp +++ b/das-dn/zjd3100proc/zjd3100pro.cpp @@ -709,13 +709,13 @@ BOOLEAN CZJD3100Process::OnSyntanProcessInfo(char *pData, int ord) snprintf(buffer, sizeof(buffer), ",%s", "unknow protocol"); pItem->WriteData((BYTE *)buffer, strlen(buffer), ord); } - snprintf(buffer, sizeof(buffer), ", softdog is: %d, link units: ", config.processes[pid].softdog); + snprintf(buffer, sizeof(buffer), ", softdog is: %d, link units: ", config.processes[pid].real_softdog); pItem->WriteData((BYTE *)buffer, strlen(buffer), ord); for (int i = 0; i < PROCESS_UNIT_NUM; i++) { if (config.processes[pid].units[i] < 0) continue; - if (i == 0) snprintf(buffer, sizeof(buffer), "%d-%d", config.processes[pid].units[i], config.units[config.processes[pid].units[i]].softdog); - else snprintf(buffer, sizeof(buffer), ", %d-%d", config.processes[pid].units[i], config.units[config.processes[pid].units[i]].softdog); + if (i == 0) snprintf(buffer, sizeof(buffer), "%d-%d", config.processes[pid].units[i], config.units[config.processes[pid].units[i]].real_softdog); + else snprintf(buffer, sizeof(buffer), ", %d-%d", config.processes[pid].units[i], config.units[config.processes[pid].units[i]].real_softdog); pItem->WriteData((BYTE *)buffer, strlen(buffer), ord); } pItem->WriteData((BYTE *)"\r\n", strlen("\r\n"), ord); @@ -1865,7 +1865,7 @@ BOOLEAN CZJD3100Process::OnSyntaxExtInfoStatus(char *pData, int ord) for (int i = 0; i < PROCESSES_NUM; i++) { buffer[i].irn = GetProcessIRNByPid(i); - buffer[i].value = ((config.processes[i].softdog >= PROCESS_WATCHDOG_TIME) ? false : true) | (config.processes[i].state ? 0x00 : 0x80); + buffer[i].value = ((config.processes[i].real_softdog >= PROCESS_WATCHDOG_TIME) ? false : true) | (config.processes[i].state ? 0x00 : 0x80); } pItem->WriteData((BYTE*)buffer, sizeof(buffer), ord); return TRUE;