3430 lines
157 KiB
C++
3430 lines
157 KiB
C++
#include "ry.h"
|
||
#include <math.h>
|
||
|
||
CRYDevice::CRYDevice()
|
||
{
|
||
#ifdef USE_NOPOLL_WEBSOCKET
|
||
ctx = NULL;
|
||
conn = NULL;
|
||
msg_count = 0;
|
||
#endif
|
||
|
||
pthread_mutex_init(&m_SendQueueMutex, NULL);
|
||
pthread_mutex_init(&m_RecvQueueMutex, NULL);
|
||
}
|
||
|
||
CRYDevice::~CRYDevice()
|
||
{
|
||
pthread_mutex_destroy(&m_SendQueueMutex);
|
||
pthread_mutex_destroy(&m_RecvQueueMutex);
|
||
}
|
||
|
||
bool CRYDevice::configInitializeMemory(void)
|
||
{
|
||
int i, j;
|
||
|
||
if (config_database.yxs) delete [] config_database.yxs;
|
||
if (config_database.ycs) delete [] config_database.ycs;
|
||
if (config_database.yms) delete [] config_database.yms;
|
||
if (config_database.yks) delete [] config_database.yks;
|
||
if (config_database.yts) delete [] config_database.yts;
|
||
|
||
for (i = 0; i < UNIT_NUM; i++) {
|
||
if (config_config.units[i].yxs) delete [] config_config.units[i].yxs;
|
||
if (config_config.units[i].ycs) delete [] config_config.units[i].ycs;
|
||
if (config_config.units[i].yms) delete [] config_config.units[i].yms;
|
||
if (config_config.units[i].yks) delete [] config_config.units[i].yks;
|
||
if (config_config.units[i].yts) delete [] config_config.units[i].yts;
|
||
}
|
||
|
||
memset(&config_system32, 0, sizeof(config_system32));
|
||
memset(&config_config, 0, sizeof(config_config));
|
||
memset(&config_database, 0, sizeof(config_database));
|
||
memset(&config_nodes, 0, sizeof(config_nodes));
|
||
memset(config_static_units, 0, sizeof(config_static_units));
|
||
|
||
config_database.yxs = new struYX[DATABASE_YX_NUM];
|
||
if (config_database.yxs == NULL) return FALSE;
|
||
memset(config_database.yxs, 0, sizeof(struYX) * DATABASE_YX_NUM);
|
||
|
||
config_database.ycs = new struYC[DATABASE_YC_NUM];
|
||
if (config_database.ycs == NULL) return FALSE;
|
||
memset(config_database.ycs, 0, sizeof(struYC) * DATABASE_YC_NUM);
|
||
|
||
config_database.yms = new struYM[DATABASE_YM_NUM];
|
||
if (config_database.yms == NULL) return FALSE;
|
||
memset(config_database.yms, 0, sizeof(struYM) * DATABASE_YM_NUM);
|
||
|
||
config_database.yks = new struYK[DATABASE_YK_NUM];
|
||
if (config_database.yks == NULL) return FALSE;
|
||
memset(config_database.yks, 0, sizeof(struYK) * DATABASE_YK_NUM);
|
||
|
||
config_database.yts = new struYT[DATABASE_YT_NUM];
|
||
if (config_database.yts == NULL) return FALSE;
|
||
memset(config_database.yts, 0, sizeof(struYT) * DATABASE_YT_NUM);
|
||
|
||
strcpy(config_system32.yk_pass, "12345");
|
||
snprintf(config_system32.version, sizeof(config_system32.version), "%s", "0");
|
||
config_system32.yk_keep = 30;
|
||
config_system32.interval_time = 2;
|
||
config_system32.log_enabled = FALSE;
|
||
config_system32.zjd_log_bind_addr = INADDR_ANY;
|
||
config_system32.zjd_log_bind_port = 0;
|
||
for (i = 0; i < HARDWARE_PORTS_NUM; i++) {
|
||
config_config.hardware.ports[i].baud = 9600;
|
||
config_config.hardware.ports[i].data = 8;
|
||
config_config.hardware.ports[i].parity = 0;
|
||
config_config.hardware.ports[i].stop = 0;
|
||
config_config.hardware.ports[i].timeout = 1000; //1s
|
||
}
|
||
for (i = 0; i < PROCESSES_NUM; i++) {
|
||
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].real_softdog = 0;
|
||
config_config.units[i].ftp_softdog = 0;
|
||
|
||
config_config.units[i].yxs = new struUnitYX[UNIT_YX_MAX];
|
||
memset(config_config.units[i].yxs, 0, sizeof(struUnitYX) * UNIT_YX_MAX);
|
||
for (int k = 0; k < UNIT_YX_MAX; k++) config_config.units[i].yxs[k].order = -1;
|
||
config_config.units[i].ycs = new struUnitYC[UNIT_YC_MAX];
|
||
memset(config_config.units[i].ycs, 0, sizeof(struUnitYC) * UNIT_YC_MAX);
|
||
for (int k = 0; k < UNIT_YC_MAX; k++) config_config.units[i].ycs[k].order = -1;
|
||
config_config.units[i].yms = new struUnitYM[UNIT_YM_MAX];
|
||
memset(config_config.units[i].yms, 0, sizeof(struUnitYM) * UNIT_YM_MAX);
|
||
for (int k = 0; k < UNIT_YM_MAX; k++) config_config.units[i].yms[k].order = -1;
|
||
config_config.units[i].yks = new struUnitYK[UNIT_YK_MAX];
|
||
memset(config_config.units[i].yks, 0, sizeof(struUnitYK) * UNIT_YK_MAX);
|
||
for (int k = 0; k < UNIT_YK_MAX; k++) config_config.units[i].yks[k].order = -1;
|
||
config_config.units[i].yts = new struUnitYT[UNIT_YT_MAX];
|
||
memset(config_config.units[i].yts, 0, sizeof(struUnitYT) * UNIT_YT_MAX);
|
||
for (int k = 0; k < UNIT_YT_MAX; k++) config_config.units[i].yts[k].order = -1;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
|
||
bool CRYDevice::configWriteSystemCFG(void)
|
||
{
|
||
FILE* pf;
|
||
char pathName[512];
|
||
|
||
snprintf(pathName, sizeof(pathName), "%s/%s", configpath, FILE_SYSTEM_CONFIG);
|
||
pf = fopen(pathName, "wb+");
|
||
if (pf != NULL) {
|
||
fwrite(&config_system32, sizeof(config_system32), 1, pf);
|
||
fclose(pf);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool CRYDevice::configWriteNodeCFG(void)
|
||
{
|
||
FILE *pf;
|
||
char pathName[512];
|
||
|
||
snprintf(pathName, sizeof(pathName), "%s/%s", configpath, FILE_NODE_CONFIG);
|
||
pf = fopen(pathName, "wb+");
|
||
if (pf != NULL) {
|
||
fwrite(&config_nodes, sizeof(config_nodes), 1, pf);
|
||
fclose(pf);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool CRYDevice::configWriteHardwareCFG(void)
|
||
{
|
||
FILE* pf;
|
||
char pathName[512];
|
||
|
||
snprintf(pathName, sizeof(pathName), "%s/%s", configpath, FILE_HARDWARE_CONFIG);
|
||
pf = fopen(pathName, "wb+");
|
||
if (pf != NULL) {
|
||
fwrite(&config_config.hardware, sizeof(config_config.hardware), 1, pf);
|
||
fclose(pf);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool CRYDevice::configWriteProcessCFG(void)
|
||
{
|
||
FILE* pf;
|
||
char pathName[512];
|
||
|
||
snprintf(pathName, sizeof(pathName), "%s/%s", configpath, FILE_PROCESS_CONFIG);
|
||
pf = fopen(pathName, "wb+");
|
||
if (pf != NULL) {
|
||
fwrite(&config_config.processes, sizeof(config_config.processes), 1, pf);
|
||
fclose(pf);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool CRYDevice::configWriteUnitCFG(void)
|
||
{
|
||
int i;
|
||
char filename[512];
|
||
char pathName[512];
|
||
FILE *pf = NULL;
|
||
FILE *pfPoint = NULL;
|
||
|
||
snprintf(pathName, sizeof(pathName), "%s/%s", configpath, FILE_UNIT_CONFIG);
|
||
pf = fopen(pathName, "wb+");
|
||
if (pf != NULL) {
|
||
for (i = 0; i < UNIT_NUM; i++) {
|
||
if (fwrite(&config_config.units[i], sizeof(config_config.units[i]), 1, pf) != 1) break;
|
||
if (config_config.units[i].yxcount > 0) {
|
||
if (config_config.units[i].yxs != NULL) {
|
||
snprintf(filename, sizeof(filename), FILE_UNIT_YX_CONFIG, configpath, i);
|
||
pfPoint = fopen(filename, "wb+");
|
||
if (pfPoint != NULL) {
|
||
fwrite(config_config.units[i].yxs, sizeof(struUnitYX)*config_config.units[i].yxcount, 1, pfPoint);
|
||
fclose(pfPoint);
|
||
}
|
||
}
|
||
}
|
||
if (config_config.units[i].yccount > 0) {
|
||
if (config_config.units[i].ycs != NULL) {
|
||
snprintf(filename, sizeof(filename), FILE_UNIT_YC_CONFIG, configpath, i);
|
||
pfPoint = fopen(filename, "wb+");
|
||
if (pfPoint != NULL) {
|
||
fwrite(config_config.units[i].ycs, sizeof(struUnitYC)*config_config.units[i].yccount, 1, pfPoint);
|
||
fclose(pfPoint);
|
||
}
|
||
}
|
||
}
|
||
if (config_config.units[i].ymcount > 0) {
|
||
if (config_config.units[i].yms != NULL) {
|
||
snprintf(filename, sizeof(filename), FILE_UNIT_YM_CONFIG, configpath, i);
|
||
pfPoint = fopen(filename, "wb+");
|
||
if (pfPoint != NULL) {
|
||
fwrite(config_config.units[i].yms, sizeof(struUnitYM)*config_config.units[i].ymcount, 1, pfPoint);
|
||
fclose(pfPoint);
|
||
}
|
||
}
|
||
}
|
||
if (config_config.units[i].ykcount > 0) {
|
||
if (config_config.units[i].yks != NULL) {
|
||
snprintf(filename, sizeof(filename), FILE_UNIT_YK_CONFIG, configpath, i);
|
||
pfPoint = fopen(filename, "wb+");
|
||
if (pfPoint != NULL) {
|
||
fwrite(config_config.units[i].yks, sizeof(struUnitYK)*config_config.units[i].ykcount, 1, pfPoint);
|
||
fclose(pfPoint);
|
||
}
|
||
}
|
||
}
|
||
if (config_config.units[i].ytcount > 0) {
|
||
if (config_config.units[i].yts != NULL) {
|
||
snprintf(filename, sizeof(filename), FILE_UNIT_YT_CONFIG, configpath, i);
|
||
pfPoint = fopen(filename, "wb+");
|
||
if (pfPoint != NULL) {
|
||
fwrite(config_config.units[i].yts, sizeof(struUnitYT)*config_config.units[i].ytcount, 1, pfPoint);
|
||
fclose(pfPoint);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
fclose(pf);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool CRYDevice::configWriteStaticUnitCFG(void)
|
||
{
|
||
FILE *pf;
|
||
char pathName[512];
|
||
|
||
snprintf(pathName, sizeof(pathName), "%s/%s", configpath, FILE_UNIT_STATIC);
|
||
pf = fopen(pathName, "w+");
|
||
if (pf != NULL) {
|
||
fwrite(config_static_units, sizeof(config_static_units), 1, pf);
|
||
fclose(pf);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool CRYDevice::configWriteDatabaseCFG(void)
|
||
{
|
||
FILE *pf;
|
||
char pathName[512];
|
||
|
||
snprintf(pathName, sizeof(pathName), "%s/%s", configpath, FILE_DATABASE_CONFIG);
|
||
pf = fopen(pathName, "wb+");
|
||
if (pf != NULL) {
|
||
fwrite(config_database.yxs, sizeof(struYX) * DATABASE_YX_NUM, 1, pf);
|
||
fwrite(config_database.ycs, sizeof(struYC) * DATABASE_YC_NUM, 1, pf);
|
||
fwrite(config_database.yms, sizeof(struYM) * DATABASE_YM_NUM, 1, pf);
|
||
fwrite(config_database.yks, sizeof(struYK) * DATABASE_YK_NUM, 1, pf);
|
||
fwrite(config_database.yts, sizeof(struYT) * DATABASE_YT_NUM, 1, pf);
|
||
fclose(pf);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
#ifdef USE_NOPOLL_WEBSOCKET
|
||
BOOLEAN CRYDevice::websocket_msg_join(noPollMsg **msg, int msg_count, BYTE* &buffer, int &buffer_size)
|
||
{
|
||
if (msg_count <= 0 || msg_count >= MAX_MSG_COUNT) return FALSE;
|
||
if (msg[0] == NULL) return FALSE;
|
||
buffer_size = 0;
|
||
for (int i = 0; i < msg_count; i++)
|
||
{
|
||
buffer_size += nopoll_msg_get_payload_size(msg[i]);
|
||
}
|
||
buffer = new BYTE[buffer_size + 1];
|
||
int len = 0;
|
||
for (int i = 0; i < msg_count; i++)
|
||
{
|
||
memcpy(&buffer[len], nopoll_msg_get_payload(msg[i]), nopoll_msg_get_payload_size(msg[i]));
|
||
len += nopoll_msg_get_payload_size(msg[i]);
|
||
}
|
||
buffer[buffer_size] = 0;
|
||
|
||
vLog(LOG_DEBUG, "buffer size is: %ld.\n", buffer_size);
|
||
return TRUE;
|
||
}
|
||
|
||
int CRYDevice::websocket_write(const char * buffer, int buffer_len)
|
||
{
|
||
int result;
|
||
result = nopoll_conn_send_text(conn, (const char *)buffer, buffer_len);
|
||
/* ensure we have written all bytes but limit operation to 2 seconds */
|
||
result = nopoll_conn_flush_writes(conn, 2000000, result);
|
||
return (result == buffer_len) ? 0 : -1;
|
||
}
|
||
#endif
|
||
|
||
bool CRYDevice::publish_sensor_data(const std::string traceId, const char* command, const Json::Value payload)
|
||
{
|
||
Json::StreamWriterBuilder builder;
|
||
builder["indentation"] = "";
|
||
builder["emitUTF8"] = true;
|
||
|
||
Json::Value jsonRoot;
|
||
jsonRoot["cmd"] = command;
|
||
|
||
char str[128];
|
||
#ifdef NOPOLL_64BIT_PLATFORM
|
||
snprintf(str, sizeof(str), "%ld", snowflake_next_id());
|
||
#else
|
||
snprintf(str, sizeof(str), "%lld", snowflake_next_id());
|
||
#endif
|
||
if (traceId == "") {
|
||
jsonRoot["cmdId"] = str;
|
||
} else {
|
||
jsonRoot["cmdId"] = traceId;
|
||
}
|
||
|
||
Json::Int64 mtime = (Json::Int64)time(NULL);
|
||
mtime *= 1000;
|
||
jsonRoot["time"] = mtime;
|
||
jsonRoot["data"] = payload;
|
||
|
||
std::string outputConfig = Json::writeString(builder, jsonRoot);
|
||
#if 0
|
||
if (traceId != "")
|
||
{
|
||
vLog(LOG_DEBUG, "send cmd: %s, payload: %d\n", command, outputConfig.length());
|
||
}
|
||
else {
|
||
std::string com = command;
|
||
if (com == "deviceEvent")
|
||
{
|
||
vLog(LOG_DEBUG, "send cmd: %s, payload: %d\n", command, outputConfig.length());
|
||
}
|
||
}
|
||
#endif
|
||
#ifndef USE_NOPOLL_WEBSOCKET
|
||
//vLog(LOG_DEBUG, "push a frame <%s>\n", command);
|
||
sendMsg(outputConfig);
|
||
#else
|
||
int rc = websocket_write(outputConfig.c_str(), outputConfig.length());
|
||
if (rc != 0) {
|
||
vLog(LOG_DEBUG, "websocket write is error<%d>,insert into database.\n", rc);
|
||
//插入数据库
|
||
return false;
|
||
}
|
||
#endif
|
||
return true;
|
||
}
|
||
|
||
int CRYDevice::GetUnitYXCount(int uid)
|
||
{
|
||
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
||
return config.units[uid].yxcount;
|
||
}
|
||
|
||
int CRYDevice::GetUnitYCCount(int uid)
|
||
{
|
||
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
||
return config.units[uid].yccount;
|
||
}
|
||
|
||
int CRYDevice::GetUnitYMCount(int uid)
|
||
{
|
||
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
||
return config.units[uid].ymcount;
|
||
}
|
||
|
||
LONG CRYDevice::GetUnitYC(int uid, int order) const
|
||
{
|
||
int udb;
|
||
LONG value;
|
||
struUnit* pUnit;
|
||
struUnitYC* pYC;
|
||
|
||
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return 0;
|
||
if (order < 0 || order >= pUnit->yccount) return 0;
|
||
pYC = &pUnit->ycs[order];
|
||
udb = pYC->order;
|
||
if (udb < 0 || udb >= DATABASE_YC_NUM)
|
||
{
|
||
value = pYC->value;
|
||
}
|
||
else
|
||
{
|
||
value = database.ycs[udb].value;
|
||
pYC->value = value;
|
||
pYC->update_time = database.ycs[udb].update_time;
|
||
pYC->qds = database.ycs[udb].qds;
|
||
}
|
||
if (pYC->factor > 1 && (pUnit->type & 0x0f) == 0x00)
|
||
{ //系数有效,且为转发单元
|
||
value /= pYC->factor;
|
||
}
|
||
|
||
return value;
|
||
}
|
||
|
||
float CRYDevice::GetUnitYCReal(int uid, int order) const
|
||
{
|
||
int udb;
|
||
long value;
|
||
float coef;
|
||
float base;
|
||
struUnit* pUnit;
|
||
struUnitYC* pYC;
|
||
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return 0;
|
||
if (order < 0 || order >= pUnit->yccount) return 0;
|
||
pYC = &pUnit->ycs[order];
|
||
udb = pYC->order;
|
||
if (udb < 0 || udb >= DATABASE_YC_NUM) {
|
||
value = pYC->value;
|
||
coef = 1.0f;
|
||
base = 0.0f;
|
||
} else {
|
||
value = database.ycs[udb].value;
|
||
coef = pYC->coef;
|
||
base = pYC->base;
|
||
pYC->value = value;
|
||
pYC->update_time = database.ycs[udb].update_time;
|
||
pYC->qds = database.ycs[udb].qds;
|
||
}
|
||
return (float)((float)value * coef + base);
|
||
}
|
||
|
||
float CRYDevice::GetUnitYCRealFromValue(int uid, int order, long value) const
|
||
{
|
||
int udb;
|
||
float coef;
|
||
float base;
|
||
struUnit* pUnit;
|
||
struUnitYC* pYC;
|
||
|
||
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return 0;
|
||
if (order < 0 || order >= pUnit->yccount) return 0;
|
||
pYC = &pUnit->ycs[order];
|
||
udb = pYC->order;
|
||
if (udb < 0 || udb >= DATABASE_YC_NUM) {
|
||
coef = 1.0f;
|
||
base = 0.0f;
|
||
} else {
|
||
coef = pYC->coef;
|
||
base = pYC->base;
|
||
}
|
||
return (float)(value * coef + base);
|
||
}
|
||
|
||
BOOLEAN CRYDevice::GetUnitYCIsForceArchive(int uid, int order) const
|
||
{
|
||
int udb;
|
||
struUnit* pUnit;
|
||
struUnitYC* pYC;
|
||
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return FALSE;
|
||
if ((pUnit->state & 0x20) == 0x00) return TRUE; //没有启动ftp
|
||
if (order < 0 || order >= pUnit->yccount) return FALSE;
|
||
pYC = &pUnit->ycs[order];
|
||
udb = pYC->order;
|
||
if (udb < 0 || udb >= DATABASE_YC_NUM)
|
||
{
|
||
return FALSE;
|
||
}
|
||
return pYC->forceArchive;
|
||
}
|
||
|
||
BOOLEAN CRYDevice::GetUnitYXIsForceArchive(int uid, int order) const
|
||
{
|
||
int udb;
|
||
struUnit* pUnit;
|
||
struUnitYX* pYX;
|
||
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return FALSE;
|
||
if ((pUnit->state & 0x20) == 0x00) return TRUE; //没有启动ftp
|
||
if (order < 0 || order >= pUnit->yxcount) return FALSE;
|
||
pYX = &pUnit->yxs[order];
|
||
udb = pYX->order;
|
||
if (udb < 0 || udb >= DATABASE_YX_NUM)
|
||
{
|
||
return FALSE;
|
||
}
|
||
return pYX->forceArchive;
|
||
}
|
||
|
||
BOOLEAN CRYDevice::GetUnitYCIsFloat(int uid, int order) const
|
||
{
|
||
int udb;
|
||
float coef = 1.0f;
|
||
struUnit* pUnit;
|
||
struUnitYC* pYC;
|
||
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return 0;
|
||
if (order < 0 || order >= pUnit->yccount) return 0;
|
||
pYC = &pUnit->ycs[order];
|
||
udb = pYC->order;
|
||
if (udb < 0 || udb >= DATABASE_YC_NUM)
|
||
{
|
||
coef = 1.0f;
|
||
}
|
||
else
|
||
{
|
||
coef = pYC->coef;
|
||
}
|
||
|
||
if (fabsf(coef) <= 1E-8) coef = 1.0f;
|
||
if (fabsf(coef - 1.0f) <= 1E-8) return FALSE;
|
||
return TRUE;
|
||
}
|
||
|
||
float CRYDevice::GetUnitYCLimitReal(int uid, int order, int type) const
|
||
{
|
||
float limitValue;
|
||
struUnit* pUnit;
|
||
struUnitYC* pYC;
|
||
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return 0;
|
||
if (order < 0 || order >= pUnit->yccount) return 0;
|
||
pYC = &pUnit->ycs[order];
|
||
|
||
//1-越上限 2-越下限)
|
||
if (type == 1)
|
||
{
|
||
limitValue = pYC->limit1High;
|
||
}
|
||
else if (type == 2)
|
||
{
|
||
limitValue = pYC->limit1Low;
|
||
}
|
||
else if (type == 3)
|
||
{
|
||
limitValue = pYC->limit2High;
|
||
}
|
||
else if (type == 4)
|
||
{
|
||
limitValue = pYC->limit2Low;
|
||
}
|
||
|
||
return limitValue;
|
||
}
|
||
|
||
float CRYDevice::GetUnitYMReal(int uid, int order)
|
||
{
|
||
int udb;
|
||
long value;
|
||
float coef;
|
||
float base;
|
||
struUnit* pUnit;
|
||
struUnitYM* pYM;
|
||
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return 0;
|
||
if (order < 0 || order >= pUnit->ymcount) return 0;
|
||
pYM = &pUnit->yms[order];
|
||
udb = pYM->order;
|
||
if (udb < 0 || udb >= DATABASE_YM_NUM) {
|
||
value = pYM->value;
|
||
coef = 1.0f;
|
||
base = 0.0f;
|
||
} else {
|
||
value = (long)database.yms[udb].value;
|
||
pYM->update_time = database.yms[udb].update_time;
|
||
coef = pYM->coef;
|
||
base = pYM->base;
|
||
pYM->value = value;
|
||
}
|
||
return (float)(value * coef + base);
|
||
}
|
||
|
||
BYTE CRYDevice::GetUnitYX(int uid, int point)
|
||
{
|
||
int udb;
|
||
BOOLEAN value;
|
||
struUnit* pUnit;
|
||
struUnitYX* pYX;
|
||
|
||
if (uid < 0 || uid >= UNIT_NUM) return 0;
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return 0;
|
||
if (point < 0 || point >= pUnit->yxcount) return 0;
|
||
pYX = &pUnit->yxs[point];
|
||
udb = pYX->order;
|
||
if (udb < 0 || udb >= DATABASE_YX_NUM) {
|
||
value = pYX->value;
|
||
} else {
|
||
value = database.yxs[udb].value;
|
||
pYX->value = value;
|
||
pYX->update_time = database.yxs[udb].update_time;
|
||
}
|
||
return value;
|
||
}
|
||
|
||
int CRYDevice::GetUnitYXBW(int& uid, BOOLEAN& value, unionCP56Time& st)
|
||
{
|
||
int order;
|
||
int point;
|
||
int type;
|
||
BYTE qds;
|
||
|
||
|
||
while (yxbw.GetYXBW(m_yxbwload, st, order, value, qds, uid, point, type))
|
||
{
|
||
m_yxbwload++;
|
||
return point;
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
int CRYDevice::GetUnitSOE(int& uid, BOOLEAN& value, BYTE& qds, unionCP56Time& st)
|
||
{
|
||
int order;
|
||
int point;
|
||
|
||
while (soe.GetSOE(m_soeload, st, order, value, qds, uid, point)) {
|
||
m_soeload++;
|
||
return point;
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
|
||
int CRYDevice::GetUnitYCBW(int& uid, LONG& value, int& type, unionCP56Time& st)
|
||
{
|
||
int order;
|
||
int point;
|
||
BYTE qds;
|
||
|
||
while (ycbw.GetYCBW(m_ycbwload, st, order, value, qds, uid, point, type)) {
|
||
m_ycbwload++;
|
||
return point;
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
BOOLEAN CRYDevice::GetUnitYK(int uid, int& order, BYTE& value, BYTE& act, BYTE& result)
|
||
{
|
||
int i;
|
||
int udb;
|
||
struUnit* pUnit;
|
||
struYK* pYK;
|
||
|
||
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
||
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return FALSE;
|
||
for (i = 0; i < pUnit->ykcount; i++) {
|
||
udb = pUnit->yks[i].order;
|
||
if (udb < 0 || udb >= DATABASE_YK_NUM) continue;
|
||
pYK = &database.yks[udb];
|
||
order = i;
|
||
value = pYK->value;
|
||
act = (BYTE)pYK->state;
|
||
result = (BYTE)pYK->result;
|
||
if (pYK->op_unit != uid) continue;
|
||
switch (act) {
|
||
case YKS_SELED:
|
||
if (result == YKR_SUCC) {
|
||
pYK->result = YKR_OPER;
|
||
yklog.PushYKLog(system32.now, udb, value, YKT_SELRET, YKS_PROC, uid);
|
||
return TRUE;
|
||
} else if (result == YKR_FAIL) {
|
||
pYK->state = YKS_IDLE;
|
||
pYK->result = YKR_IDLE;
|
||
pYK->op_unit = -1;
|
||
return TRUE;
|
||
} else if (result == YKR_IDLE) {
|
||
pYK->state = YKS_IDLE;
|
||
pYK->op_unit = -1;
|
||
}
|
||
break;
|
||
case YKS_EXEED:
|
||
pYK->state = YKS_IDLE;
|
||
pYK->result = YKR_IDLE;
|
||
pYK->op_unit = -1;
|
||
if (result == YKR_SUCC || result == YKR_FAIL) {
|
||
return TRUE;
|
||
}
|
||
break;
|
||
case YKS_ABRED:
|
||
pYK->state = YKS_IDLE;
|
||
pYK->result = YKR_IDLE;
|
||
pYK->op_unit = -1;
|
||
return TRUE;
|
||
case YKS_EXEING:
|
||
if (result == YKR_OVER) {
|
||
pYK->state = YKS_IDLE;
|
||
pYK->result = YKR_IDLE;
|
||
pYK->op_unit = -1;
|
||
return TRUE;
|
||
}
|
||
break;
|
||
case YKS_SELREQ:
|
||
if (result == YKR_OVER) {
|
||
pYK->state = YKS_IDLE;
|
||
pYK->result = YKR_IDLE;
|
||
pYK->op_unit = -1;
|
||
return TRUE;
|
||
}
|
||
break;
|
||
case YKS_SELING:
|
||
if (result == YKR_OVER) {
|
||
pYK->state = YKS_IDLE;
|
||
pYK->result = YKR_IDLE;
|
||
pYK->op_unit = -1;
|
||
return TRUE;
|
||
}
|
||
break;
|
||
case YKS_ABRREQ:
|
||
case YKS_EXEREQ:
|
||
case YKS_IDLE:
|
||
break;
|
||
default:
|
||
pYK->state = YKS_IDLE;
|
||
pYK->result = YKR_IDLE;
|
||
pYK->op_unit = -1;
|
||
break;
|
||
}
|
||
}
|
||
|
||
return FALSE;
|
||
}
|
||
|
||
void CRYDevice::SetUnitYK(int uid, int order, BYTE value, BYTE act, BYTE result)
|
||
{
|
||
int udb;
|
||
struUnit* pUnit;
|
||
struYK* pYK;
|
||
|
||
if (uid < 0 || uid >= UNIT_NUM) return;
|
||
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return;
|
||
if (order < 0 || order >= pUnit->ykcount) return;
|
||
udb = pUnit->yks[order].order;
|
||
if (udb < 0 || udb >= DATABASE_YK_NUM) return;
|
||
pYK = &database.yks[udb];
|
||
switch (act) {
|
||
case YKS_SELREQ:
|
||
if (pYK->state == YKS_IDLE /*&& pYK->op_unit == -1*/) {
|
||
pYK->state = act;
|
||
pYK->result = YKR_IDLE;
|
||
pYK->value = value;
|
||
pYK->op_time = system32.timers;
|
||
pYK->op_unit = uid;
|
||
yklog.PushYKLog(system32.now, udb, value, YKT_SELREQ, YKS_PROC, uid);
|
||
}
|
||
break;
|
||
case YKS_ABRREQ:
|
||
if (pYK->op_unit != uid) break;
|
||
if (pYK->value != value) break;
|
||
if (pYK->state != YKS_IDLE && pYK->state != YKS_EXEED) {
|
||
pYK->state = act;
|
||
pYK->result = YKR_IDLE;
|
||
yklog.PushYKLog(system32.now, udb, value, YKT_ABRREQ, YKS_PROC, uid);
|
||
}
|
||
break;
|
||
case YKS_EXEREQ:
|
||
if (pYK->op_unit != uid) break;
|
||
if (pYK->value != value) break;
|
||
if (pYK->state == YKS_SELED && pYK->result == YKR_OPER) {
|
||
pYK->state = act;
|
||
pYK->result = YKR_IDLE;
|
||
yklog.PushYKLog(system32.now, udb, value, YKT_EXEREQ, YKS_PROC, uid);
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
|
||
BOOLEAN CRYDevice::GetUnitYT(int uid, int& order, DWORD& value, BYTE& act, BYTE& result)
|
||
{
|
||
int i;
|
||
int udb;
|
||
struUnit* pUnit;
|
||
struYT* pYT;
|
||
|
||
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
||
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return FALSE;
|
||
for (i = 0; i < pUnit->ytcount; i++) {
|
||
udb = pUnit->yts[i].order;
|
||
if (udb < 0 || udb >= DATABASE_YT_NUM) continue;
|
||
pYT = &database.yts[udb];
|
||
order = i;
|
||
value = (DWORD)pYT->value;
|
||
act = (BYTE)pYT->state;
|
||
result = (BYTE)pYT->result;
|
||
if (pYT->op_unit != uid) continue;
|
||
switch (act) {
|
||
case YTS_SELED:
|
||
if (result == YTR_SUCC) {
|
||
pYT->result = YTR_OPER;
|
||
ytlog.PushYTLog(system32.now, udb, value, YTT_SELRET, YTS_PROC, uid);
|
||
return TRUE;
|
||
} else if (result == YTR_FAIL) {
|
||
pYT->state = YTS_IDLE;
|
||
pYT->result = YTR_IDLE;
|
||
pYT->op_unit = -1;
|
||
return TRUE;
|
||
} else if (result == YTR_IDLE) {
|
||
pYT->state = YTS_IDLE;
|
||
pYT->op_unit = -1;
|
||
}
|
||
break;
|
||
case YTS_EXEED:
|
||
pYT->state = YTS_IDLE;
|
||
pYT->result = YTR_IDLE;
|
||
pYT->op_unit = -1;
|
||
if (result == YTR_SUCC || result == YTR_FAIL) {
|
||
return TRUE;
|
||
}
|
||
break;
|
||
case YTS_ABRED:
|
||
pYT->state = YTS_IDLE;
|
||
pYT->result = YTR_IDLE;
|
||
pYT->op_unit = -1;
|
||
return TRUE;
|
||
case YTS_EXEING:
|
||
if (result == YTR_OVER) {
|
||
pYT->state = YTS_IDLE;
|
||
pYT->result = YTR_IDLE;
|
||
pYT->op_unit = -1;
|
||
return TRUE;
|
||
}
|
||
break;
|
||
case YTS_SELREQ:
|
||
if (result == YTR_OVER) {
|
||
pYT->state = YTS_IDLE;
|
||
pYT->result = YTR_IDLE;
|
||
pYT->op_unit = -1;
|
||
return TRUE;
|
||
}
|
||
break;
|
||
case YTS_SELING:
|
||
if (result == YTR_OVER) {
|
||
pYT->state = YTS_IDLE;
|
||
pYT->result = YTR_IDLE;
|
||
pYT->op_unit = -1;
|
||
return TRUE;
|
||
}
|
||
break;
|
||
case YTS_ABRREQ:
|
||
case YTS_EXEREQ:
|
||
case YTS_IDLE:
|
||
break;
|
||
default:
|
||
pYT->state = YTS_IDLE;
|
||
pYT->result = YTR_IDLE;
|
||
pYT->op_unit = -1;
|
||
break;
|
||
}
|
||
}
|
||
|
||
return FALSE;
|
||
}
|
||
|
||
void CRYDevice::SetUnitYT(int uid, int order, DWORD value, BYTE act, BYTE result)
|
||
{
|
||
int udb;
|
||
struUnit* pUnit;
|
||
struYT* pYT;
|
||
|
||
if (uid < 0 || uid >= UNIT_NUM) return;
|
||
|
||
pUnit = &config.units[uid];
|
||
if ((pUnit->state & 0x01) != TRUE) return;
|
||
if (order < 0 || order >= pUnit->ytcount) return;
|
||
udb = pUnit->yts[order].order;
|
||
if (udb < 0 || udb >= DATABASE_YT_NUM) return;
|
||
pYT = &database.yts[udb];
|
||
switch (act) {
|
||
case YTS_SELREQ:
|
||
if (pYT->state == YTS_IDLE /*&& pYT->op_unit == -1*/) {
|
||
pYT->state = act;
|
||
pYT->result = YTR_IDLE;
|
||
pYT->value = value;
|
||
pYT->op_time = system32.timers;
|
||
pYT->op_unit = uid;
|
||
ytlog.PushYTLog(system32.now, udb, value, YTT_SELREQ, YTS_PROC, uid);
|
||
}
|
||
break;
|
||
case YTS_ABRREQ:
|
||
if (pYT->op_unit != uid) break;
|
||
if (pYT->value != (long)value) break;
|
||
if (pYT->state != YTS_IDLE && pYT->state != YTS_EXEED) {
|
||
pYT->state = act;
|
||
pYT->result = YTR_IDLE;
|
||
ytlog.PushYTLog(system32.now, udb, value, YTT_ABRREQ, YTS_PROC, uid);
|
||
}
|
||
break;
|
||
case YTS_EXEREQ:
|
||
if ((pYT->state == YTS_SELED || pYT->state == YTS_IDLE)) {
|
||
pYT->state = act;
|
||
pYT->result = YTR_IDLE;
|
||
pYT->value = value;
|
||
pYT->op_time = system32.timers;
|
||
pYT->op_unit = uid;
|
||
ytlog.PushYTLog(system32.now, udb, value, YTT_EXEREQ, YTS_PROC, uid);
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
|
||
int CRYDevice::MakeYKFrame(int uid)
|
||
{
|
||
int order;
|
||
BYTE value, action, result;
|
||
|
||
//for (int i = 0; i < m_total_units.size(); i++)
|
||
{
|
||
//uid = m_total_units.at(i);
|
||
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
||
if (!GetUnitYK(uid, order, value, action, result)) return -1;
|
||
|
||
vLog(LOG_WARN, "Unit(%d) yk(%d) %s state is %s result is %s\n", uid, order, (value ? "CLOSE" : "TRIP"), val_to_str(action, yk_state, "STATE=%d"), val_to_str(result, yk_result, "RESULT=%d"));
|
||
|
||
//发送确认
|
||
Json::Value jsonRoot;
|
||
|
||
jsonRoot["deviceId"] = static_units[uid].deviceId;
|
||
jsonRoot["serviceName"] = (const char *)config.units[uid].yks[order].name;
|
||
jsonRoot["opValue"] = value;
|
||
if (YKS_SELED == action && YKR_FAIL == result) {
|
||
action = YKS_ABRED;
|
||
} else if (YKS_SELREQ == action && YKR_OVER == result) {
|
||
action = YKS_ABRED;
|
||
} else if (YKS_SELING == action && YKR_OVER == result) {
|
||
action = YKS_ABRED;
|
||
}
|
||
|
||
if (YKS_SELED == action) {
|
||
SetUnitYK(uid, order, value, YKS_EXEREQ, YKR_IDLE);
|
||
vLog(LOG_WARN, "Unit(%d) yk(%d) %s state is YKS_EXEREQ result is YKR_IDLE.\n", uid, order, (value ? "CLOSE" : "TRIP"));
|
||
return 1;
|
||
} else if (YKS_ABRED == action) {
|
||
SetUnitYK(uid, order, value, YKS_ABRREQ, YKR_IDLE);
|
||
vLog(LOG_WARN, "Unit(%d) yk(%d) %s state is YKS_ABRREQ result is YKR_IDLE.\n", uid, order, (value ? "CLOSE" : "TRIP"));
|
||
jsonRoot["result"] = false;
|
||
} else {
|
||
jsonRoot["result"] = true;
|
||
}
|
||
//查找设备serviceName的traceId
|
||
char idName[512];
|
||
snprintf(idName, sizeof(idName), "%s%s", static_units[uid].deviceId, config.units[uid].yks[order].name);
|
||
std::string key(idName);
|
||
std::string traceId;
|
||
if (unitserviceName2cmdId_map.find(key) != unitserviceName2cmdId_map.end()) {
|
||
traceId = unitserviceName2cmdId_map[key];
|
||
unitserviceName2cmdId_map.erase(key);
|
||
}
|
||
else
|
||
{
|
||
return 0;
|
||
}
|
||
publish_sensor_data(traceId, "deviceControlResp", jsonRoot);
|
||
return 1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
int CRYDevice::MakeYTFrame(int uid)
|
||
{
|
||
int order;
|
||
BYTE action, result;
|
||
DWORD value;
|
||
|
||
//for (int i = 0; i < m_total_units.size(); i++)
|
||
{
|
||
if (uid < 0 || uid >= UNIT_NUM) return -1;
|
||
if (!GetUnitYT(uid, order, value, action, result)) return -1;
|
||
|
||
vLog(LOG_DEBUG, "Unit(%d) set point(%d) %d state is %s result is %s\n", uid, order, value, val_to_str(action, yt_state, "STATE=%d"), val_to_str(result, yt_result, "RESULT=%d"));
|
||
|
||
//发送确认
|
||
Json::Value jsonRoot;
|
||
|
||
jsonRoot["deviceId"] = static_units[uid].deviceId;
|
||
jsonRoot["serviceName"] = (const char *)config.units[uid].yts[order].name;
|
||
jsonRoot["opValue"] = value;
|
||
if (YTS_SELED == action && YTR_FAIL == result) {
|
||
action = YTS_ABRED;
|
||
} else if (YTS_SELREQ == action && YTR_OVER == result) {
|
||
action = YTS_ABRED;
|
||
} else if (YTS_SELING == action && YTR_OVER == result) {
|
||
action = YTS_ABRED;
|
||
}
|
||
if (YTS_SELED == action) {
|
||
SetUnitYT(uid, order, value, YTS_EXEREQ, YTR_IDLE);
|
||
vLog(LOG_DEBUG, "Unit(%d) set point(%d) %d state is YTS_EXEREQ result is YTR_IDLE.\n", uid, order, value);
|
||
return 1;
|
||
} else if (YTS_ABRED == action) {
|
||
SetUnitYT(uid, order, value, YTS_ABRREQ, YTR_IDLE);
|
||
vLog(LOG_DEBUG, "Unit(%d) set point(%d) %d state is YTS_ABRREQ result is YTR_IDLE.\n", uid, order, value);
|
||
jsonRoot["result"] = false;
|
||
} else {
|
||
jsonRoot["result"] = true;
|
||
}
|
||
|
||
//查找设备serviceName的traceId
|
||
char idName[512];
|
||
snprintf(idName, sizeof(idName), "%s%s", static_units[uid].deviceId, config.units[uid].yks[order].name);
|
||
std::string key(idName);
|
||
std::string traceId;
|
||
if (unitserviceName2cmdId_map.find(key) != unitserviceName2cmdId_map.end()) {
|
||
traceId = unitserviceName2cmdId_map[key];
|
||
unitserviceName2cmdId_map.erase(key);
|
||
}
|
||
else
|
||
{
|
||
return 0;
|
||
}
|
||
publish_sensor_data(traceId, "deviceControlResp", jsonRoot);
|
||
return 1;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
bool CRYDevice::OnReceivedDeviceCommand(const Json::Value jsonRoot)
|
||
{
|
||
if (jsonRoot["deviceId"].isNull()) return FALSE;
|
||
if (jsonRoot["serviceName"].isNull()) return FALSE;
|
||
if (jsonRoot["opValue"].isNull()) return FALSE;
|
||
|
||
std::string deviceId = jsonRoot["deviceId"].asString();
|
||
std::string serviceName = jsonRoot["serviceName"].asString();
|
||
if (unitname2service_map.find(deviceId) == unitname2service_map.end()) {
|
||
vLog(LOG_WARN, "下发了一个不存在services的控制设备id<%s>。\n", deviceId.c_str());
|
||
return false;
|
||
}
|
||
name2servicemap name2service_map = unitname2service_map[deviceId];
|
||
if (name2service_map.find(serviceName) == name2service_map.end()) {
|
||
vLog(LOG_WARN, "下发了一个不存在的service名称<%s>。\n", serviceName.c_str());
|
||
return false;
|
||
}
|
||
|
||
struct_service_item item = name2service_map[serviceName];
|
||
|
||
int operType = item.type;
|
||
int uid = item.uid;
|
||
int point = item.point;
|
||
|
||
if (operType == CMD_CONTROL_OPERATION) { //遥控
|
||
if (point < 0) {
|
||
vLog(LOG_ERROR, "未能找到对应的遥控点号,请检查并确认。\n");
|
||
return FALSE;
|
||
}
|
||
int operValue = jsonRoot["opValue"].asInt();
|
||
//插入信息显示traceId和serviceName的对组关系
|
||
std::string key = deviceId + serviceName;
|
||
if (unitserviceName2cmdId_map.find(key) == unitserviceName2cmdId_map.end()) {
|
||
unitserviceName2cmdId_map.insert(unitserviceName2cmdIdmap::value_type(key, m_traceId));
|
||
} else {
|
||
vLog(LOG_WARN, "系统同时下发了两个相同的serviceName<%s>控制,请检查。\n", serviceName.c_str());
|
||
return false;
|
||
}
|
||
SetUnitYK(uid, point, (operValue & 0x03), YKS_SELREQ, YKR_IDLE);
|
||
vLog(LOG_WARN, "Unit(%d) yk(%d) %s state is YKS_SELREQ result is YKR_IDLE.\n", uid, point, ((operValue & 0x03) ? "CLOSE" : "TRIP"));
|
||
} else if (operType == CMD_CONTROL_SETTING) { //遥调
|
||
union {
|
||
float fval;
|
||
DWORD dval;
|
||
} operValue;
|
||
if (point < 0) {
|
||
vLog(LOG_ERROR, "未能找到对应的遥调点号,请检查并确认。\n");
|
||
return FALSE;
|
||
}
|
||
if (jsonRoot["opValue"].isInt()) operValue.dval = jsonRoot["opValue"].asInt();
|
||
else if (jsonRoot["opValue"].isDouble()) operValue.fval = jsonRoot["opValue"].asFloat();
|
||
//插入信息显示traceId和serviceName的对组关系
|
||
std::string key = deviceId + serviceName;
|
||
if (unitserviceName2cmdId_map.find(key) == unitserviceName2cmdId_map.end()) {
|
||
unitserviceName2cmdId_map.insert(unitserviceName2cmdIdmap::value_type(key, m_traceId));
|
||
} else {
|
||
vLog(LOG_WARN, "系统同时下发了两个相同的serviceName<%s>调节,请检查。\n", serviceName.c_str());
|
||
return false;
|
||
}
|
||
SetUnitYT(uid, point, operValue.dval, YTS_EXEREQ, YTR_IDLE);
|
||
vLog(LOG_DEBUG, "Unit(%d) set point(%d) %d state is YTS_EXEREQ result is YTR_IDLE.\n", uid, point, operValue.dval);
|
||
} else {
|
||
vLog(LOG_ERROR, "平台下发的<%d>命令错误。operType不是<0-遥控或1-遥调>,系统不支持的命令!\n", operType);
|
||
return FALSE;
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
BOOLEAN CRYDevice::processUartParam(const Json::Value jsonRoot, int ord)
|
||
{
|
||
if (ord < 0 || ord >= HARDWARE_PORTS_NUM) return FALSE;
|
||
|
||
config_config.hardware.ports[ord].state = TRUE;
|
||
snprintf(config_config.hardware.ports[ord].name, sizeof(config_config.hardware.ports[ord].name), "ttyS%d", ord);
|
||
config_config.hardware.ports[ord].baud = 9600;
|
||
config_config.hardware.ports[ord].data = 8;
|
||
config_config.hardware.ports[ord].parity = 0;
|
||
config_config.hardware.ports[ord].stop = 0;
|
||
config_config.hardware.ports[ord].timeout = 1000;
|
||
|
||
if (jsonRoot["name"].isString()) {
|
||
snprintf(config_config.hardware.ports[ord].name, sizeof(config_config.hardware.ports[ord].name), "%s", jsonRoot["name"].asCString());
|
||
}
|
||
if (jsonRoot["baud"].isInt()) {
|
||
config_config.hardware.ports[ord].baud = jsonRoot["baud"].asInt();
|
||
} else if (jsonRoot["baud"].isString()) {
|
||
config_config.hardware.ports[ord].baud = atoi(jsonRoot["baud"].asCString());
|
||
}
|
||
if (jsonRoot["data"].isInt()) {
|
||
config_config.hardware.ports[ord].data = jsonRoot["data"].asInt();
|
||
} else if (jsonRoot["data"].isString()) {
|
||
config_config.hardware.ports[ord].data = atoi(jsonRoot["data"].asCString());
|
||
}
|
||
if (jsonRoot["parity"].isInt()) {
|
||
config_config.hardware.ports[ord].parity = jsonRoot["parity"].asInt();
|
||
} else if (jsonRoot["parity"].isString()) {
|
||
config_config.hardware.ports[ord].parity = atoi(jsonRoot["parity"].asCString());
|
||
}
|
||
if (jsonRoot["stop"].isInt()) {
|
||
if (jsonRoot["stop"].asInt() == 1) config_config.hardware.ports[ord].stop = 0;
|
||
else if (jsonRoot["stop"].asInt() == 2) config_config.hardware.ports[ord].stop = 2;
|
||
} else if (jsonRoot["stop"].isString()) {
|
||
if (jsonRoot["stop"].asString() == "1") config_config.hardware.ports[ord].stop = 0;
|
||
else if (jsonRoot["stop"].asString() == "2") config_config.hardware.ports[ord].stop = 2;
|
||
}
|
||
if (jsonRoot["timeout"].isInt()) {
|
||
config_config.hardware.ports[ord].timeout = jsonRoot["timeout"].asInt();
|
||
} else if (jsonRoot["timeout"].isString()) {
|
||
config_config.hardware.ports[ord].timeout = atoi(jsonRoot["timeout"].asCString());
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
BOOLEAN CRYDevice::processHostModbustcpParam(const Json::Value jsonRoot, int pid)
|
||
{
|
||
if (pid < 0 || pid >= PROCESSES_NUM) return FALSE;
|
||
|
||
config_config.processes[pid].option.network.ignored_source = TRUE;
|
||
config_config.processes[pid].option.network.socket_type = SOCK_STREAM;
|
||
config_config.processes[pid].option.network.bind_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.network.bind_port = 0;
|
||
config_config.processes[pid].option.network.target_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.network.target_port = 502;
|
||
|
||
if (jsonRoot["targetAddr"].isInt()) {
|
||
config_config.processes[pid].option.network.target_addr = jsonRoot["targetAddr"].asInt();
|
||
} else if (jsonRoot["targetAddr"].isString()) {
|
||
if (inet_pton(AF_INET, jsonRoot["targetAddr"].asCString(), &config_config.processes[pid].option.network.target_addr) == 1) {
|
||
vLog(LOG_DEBUG, "IPv4 地址转换成功,网络字节序为: %u.\n", config_config.processes[pid].option.network.target_addr);
|
||
} else {
|
||
vLog(LOG_ERROR, "inet_pton error(%d,%s).\n", errno, strerror(errno));
|
||
}
|
||
}
|
||
if (jsonRoot["targetPort"].isInt()) {
|
||
config_config.processes[pid].option.network.target_port = jsonRoot["targetPort"].asInt();
|
||
} else if (jsonRoot["targetPort"].isString()) {
|
||
config_config.processes[pid].option.network.target_port = atoi(jsonRoot["targetPort"].asCString());
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
|
||
BOOLEAN CRYDevice::processSubModbustcpParam(const Json::Value jsonRoot, int pid)
|
||
{
|
||
if (pid < 0 || pid >= PROCESSES_NUM) return FALSE;
|
||
|
||
config_config.processes[pid].option.network.ignored_source = TRUE;
|
||
config_config.processes[pid].option.network.socket_type = SOCK_STREAM;
|
||
config_config.processes[pid].option.network.bind_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.network.bind_port = 502;
|
||
config_config.processes[pid].option.network.target_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.network.target_port = 0;
|
||
|
||
if (jsonRoot["bindAddr"].isInt()) {
|
||
config_config.processes[pid].option.network.bind_addr = jsonRoot["bindAddr"].asInt();
|
||
} else if (jsonRoot["bindAddr"].isString()) {
|
||
if (inet_pton(AF_INET, jsonRoot["bindAddr"].asCString(), &config_config.processes[pid].option.network.bind_addr) == 1) {
|
||
vLog(LOG_DEBUG, "IPv4 地址转换成功,网络字节序为: %u.\n", config_config.processes[pid].option.network.bind_addr);
|
||
} else {
|
||
vLog(LOG_ERROR, "inet_pton error(%d,%s).\n", errno, strerror(errno));
|
||
}
|
||
}
|
||
if (jsonRoot["bindPort"].isInt()) {
|
||
config_config.processes[pid].option.network.bind_port = jsonRoot["bindPort"].asInt();
|
||
} else if (jsonRoot["bindPort"].isString()) {
|
||
config_config.processes[pid].option.network.bind_port = atoi(jsonRoot["bindPort"].asCString());
|
||
}
|
||
return TRUE;
|
||
}
|
||
|
||
BOOLEAN CRYDevice::processRymodbustcpParam(const Json::Value jsonRoot, int pid)
|
||
{
|
||
if (pid < 0 || pid >= PROCESSES_NUM) return FALSE;
|
||
|
||
config_config.processes[pid].option.rymodbus.net.ignored_source = TRUE;
|
||
config_config.processes[pid].option.rymodbus.net.socket_type = SOCK_STREAM;
|
||
config_config.processes[pid].option.rymodbus.net.bind_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.rymodbus.net.bind_port = 0;
|
||
config_config.processes[pid].option.rymodbus.net.target_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.rymodbus.net.target_port = 502;
|
||
|
||
if (jsonRoot["targetAddr"].isInt()) {
|
||
config_config.processes[pid].option.rymodbus.net.target_addr = jsonRoot["targetAddr"].asInt();
|
||
} else if (jsonRoot["targetAddr"].isString()) {
|
||
if (inet_pton(AF_INET, jsonRoot["targetAddr"].asCString(), &config_config.processes[pid].option.rymodbus.net.target_addr) == 1) {
|
||
vLog(LOG_DEBUG, "IPv4 地址转换成功,网络字节序为: %u.\n", config_config.processes[pid].option.rymodbus.net.target_addr);
|
||
} else {
|
||
vLog(LOG_ERROR, "inet_pton error(%d,%s).\n", errno, strerror(errno));
|
||
}
|
||
}
|
||
if (jsonRoot["targetPort"].isInt()) {
|
||
config_config.processes[pid].option.rymodbus.net.target_port = jsonRoot["targetPort"].asInt();
|
||
} else if (jsonRoot["targetPort"].isString()) {
|
||
config_config.processes[pid].option.rymodbus.net.target_port = atoi(jsonRoot["targetPort"].asCString());
|
||
}
|
||
|
||
//增加ftp协议参数
|
||
if (jsonRoot["ftpMode"].isInt()) {
|
||
config_config.processes[pid].option.rymodbus.bHaveFTP = jsonRoot["ftpMode"].asInt();
|
||
} else if (jsonRoot["ftpMode"].isString()) {
|
||
config_config.processes[pid].option.rymodbus.bHaveFTP = atoi(jsonRoot["ftpMode"].asCString());
|
||
} else { //默认存在允许ftp功能
|
||
config_config.processes[pid].option.rymodbus.bHaveFTP = TRUE;
|
||
}
|
||
//用户名
|
||
if (jsonRoot["ftpUser"].isString()) {
|
||
snprintf(config_config.processes[pid].option.rymodbus.ftp.user, sizeof(config_config.processes[pid].option.rymodbus.ftp.user), "%s", jsonRoot["ftpUser"].asCString());
|
||
} else { //默认存在允许ftp功能
|
||
snprintf(config_config.processes[pid].option.rymodbus.ftp.user, sizeof(config_config.processes[pid].option.rymodbus.ftp.user), "%s", "administrator");
|
||
}
|
||
//密码
|
||
if (jsonRoot["ftpPassword"].isString()) {
|
||
snprintf(config_config.processes[pid].option.rymodbus.ftp.password, sizeof(config_config.processes[pid].option.rymodbus.ftp.password), "%s", jsonRoot["ftpPassword"].asCString());
|
||
} else { //默认存在允许ftp功能
|
||
snprintf(config_config.processes[pid].option.rymodbus.ftp.password, sizeof(config_config.processes[pid].option.rymodbus.ftp.password), "%s", "123456");
|
||
}
|
||
//远程路径
|
||
if (jsonRoot["rootDir"].isString()) {
|
||
snprintf(config_config.processes[pid].option.rymodbus.ftp.remotePath, sizeof(config_config.processes[pid].option.rymodbus.ftp.remotePath), "%s", jsonRoot["rootDir"].asCString());
|
||
} else { //默认存在允许ftp功能
|
||
snprintf(config_config.processes[pid].option.rymodbus.ftp.remotePath, sizeof(config_config.processes[pid].option.rymodbus.ftp.remotePath), "%s", "Hard Disk2/data/rtdatalog");
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
BOOLEAN CRYDevice::processRyFTP2MinioParam(const Json::Value jsonRoot, int pid)
|
||
{
|
||
if (pid < 0 || pid >= PROCESSES_NUM) return FALSE;
|
||
|
||
config_config.processes[pid].option.ftp2minio.net.ignored_source = TRUE;
|
||
config_config.processes[pid].option.ftp2minio.net.socket_type = SOCK_STREAM;
|
||
config_config.processes[pid].option.ftp2minio.net.bind_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.ftp2minio.net.bind_port = 0;
|
||
config_config.processes[pid].option.ftp2minio.net.target_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.ftp2minio.net.target_port = 21;
|
||
|
||
if (jsonRoot["ftpHost"].isInt()) {
|
||
config_config.processes[pid].option.ftp2minio.net.target_addr = jsonRoot["ftpHost"].asInt();
|
||
} else if (jsonRoot["ftpHost"].isString()) {
|
||
if (inet_pton(AF_INET, jsonRoot["ftpHost"].asCString(), &config_config.processes[pid].option.ftp2minio.net.target_addr) == 1) {
|
||
vLog(LOG_DEBUG, "IPv4 地址转换成功,网络字节序为: %u.\n", config_config.processes[pid].option.ftp2minio.net.target_addr);
|
||
} else {
|
||
vLog(LOG_ERROR, "inet_pton error(%d,%s).\n", errno, strerror(errno));
|
||
}
|
||
}
|
||
|
||
//FTP参数
|
||
//用户名
|
||
if (jsonRoot["ftpUser"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.ftp.user, sizeof(config_config.processes[pid].option.ftp2minio.ftp.user), "%s", jsonRoot["ftpUser"].asCString());
|
||
} else {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.ftp.user, sizeof(config_config.processes[pid].option.ftp2minio.ftp.user), "%s", "guest");
|
||
}
|
||
//密码
|
||
if (jsonRoot["ftpPassword"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.ftp.password, sizeof(config_config.processes[pid].option.ftp2minio.ftp.password), "%s", jsonRoot["ftpPassword"].asCString());
|
||
} else {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.ftp.password, sizeof(config_config.processes[pid].option.ftp2minio.ftp.password), "%s", "1");
|
||
}
|
||
//远程路径
|
||
if (jsonRoot["rootDir"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.ftp.remotePath, sizeof(config_config.processes[pid].option.ftp2minio.ftp.remotePath), "%s", jsonRoot["rootDir"].asCString());
|
||
} else {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.ftp.remotePath, sizeof(config_config.processes[pid].option.ftp2minio.ftp.remotePath), "%s", "Hard Disk/data/Tracelog;Hard Disk/data/Statuscode");
|
||
}
|
||
//本地路径
|
||
if (jsonRoot["localPath"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.ftp.localPath, sizeof(config_config.processes[pid].option.ftp2minio.ftp.localPath), "%s", jsonRoot["localPath"].asCString());
|
||
} else {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.ftp.localPath, sizeof(config_config.processes[pid].option.ftp2minio.ftp.localPath), "%s", "./");
|
||
}
|
||
|
||
//Minio参数
|
||
//URL
|
||
if (jsonRoot["URL"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.minio.url, sizeof(config_config.processes[pid].option.ftp2minio.minio.url), "%s", jsonRoot["URL"].asCString());
|
||
} else {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.minio.url, sizeof(config_config.processes[pid].option.ftp2minio.minio.url), "%s", "http://192.168.109.187:9000");
|
||
}
|
||
//用户名
|
||
if (jsonRoot["minioUserName"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.minio.user, sizeof(config_config.processes[pid].option.ftp2minio.minio.user), "%s", jsonRoot["minioUserName"].asCString());
|
||
} else {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.minio.user, sizeof(config_config.processes[pid].option.ftp2minio.minio.user), "%s", "das");
|
||
}
|
||
//密码
|
||
if (jsonRoot["minioPassWord"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.minio.password, sizeof(config_config.processes[pid].option.ftp2minio.minio.password), "%s", jsonRoot["minioPassWord"].asCString());
|
||
} else {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.minio.password, sizeof(config_config.processes[pid].option.ftp2minio.minio.password), "%s", "zaq12WSX");
|
||
}
|
||
//桶
|
||
if (jsonRoot["bucket"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.minio.bucket, sizeof(config_config.processes[pid].option.ftp2minio.minio.bucket), "%s", jsonRoot["bucket"].asCString());
|
||
} else {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.minio.bucket, sizeof(config_config.processes[pid].option.ftp2minio.minio.bucket), "%s", "das");
|
||
}
|
||
//桶中的绝对路径
|
||
if (jsonRoot["deviceCode"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.minio.object, sizeof(config_config.processes[pid].option.ftp2minio.minio.object), "%s", jsonRoot["deviceCode"].asCString());
|
||
} else {
|
||
snprintf(config_config.processes[pid].option.ftp2minio.minio.object, sizeof(config_config.processes[pid].option.ftp2minio.minio.object), "%s", "/A-001/Alarm/");
|
||
}
|
||
return TRUE;
|
||
}
|
||
|
||
BOOLEAN CRYDevice::processRyADSParam(const Json::Value jsonRoot, int pid)
|
||
{
|
||
if (pid < 0 || pid >= PROCESSES_NUM) return FALSE;
|
||
|
||
//vLog(LOG_DEBUG, "here.\n %s\n", jsonRoot.toStyledString().c_str());
|
||
|
||
//增加ftp协议参数
|
||
if (jsonRoot["ftpMode"].isInt()) {
|
||
config_config.processes[pid].option.ryads.bHaveFTP = jsonRoot["ftpMode"].asInt();
|
||
} else if (jsonRoot["ftpMode"].isString()) {
|
||
config_config.processes[pid].option.ryads.bHaveFTP = atoi(jsonRoot["ftpMode"].asCString());
|
||
} else { //默认存在允许ftp功能
|
||
config_config.processes[pid].option.ryads.bHaveFTP = TRUE;
|
||
}
|
||
//用户名
|
||
if (jsonRoot["ftpUser"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ryads.ftp.user, sizeof(config_config.processes[pid].option.ryads.ftp.user), "%s", jsonRoot["ftpUser"].asCString());
|
||
} else { //默认存在允许ftp功能
|
||
snprintf(config_config.processes[pid].option.ryads.ftp.user, sizeof(config_config.processes[pid].option.ryads.ftp.user), "%s", "guest");
|
||
}
|
||
//密码
|
||
if (jsonRoot["ftpPassword"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ryads.ftp.password, sizeof(config_config.processes[pid].option.ryads.ftp.password), "%s", jsonRoot["ftpPassword"].asCString());
|
||
} else { //默认存在允许ftp功能
|
||
snprintf(config_config.processes[pid].option.ryads.ftp.password, sizeof(config_config.processes[pid].option.ryads.ftp.password), "%s", "1");
|
||
}
|
||
//远程路径
|
||
if (jsonRoot["rootDir"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ryads.ftp.remotePath, sizeof(config_config.processes[pid].option.ryads.ftp.remotePath), "%s", jsonRoot["rootDir"].asCString());
|
||
} else { //默认存在允许ftp功能
|
||
snprintf(config_config.processes[pid].option.ryads.ftp.remotePath, sizeof(config_config.processes[pid].option.ryads.ftp.remotePath), "%s", "/data/rtdatalog");
|
||
}
|
||
|
||
//目标地址
|
||
if (jsonRoot["targetAddr"].isInt()) {
|
||
config_config.processes[pid].option.ryads.net.target_addr = jsonRoot["targetAddr"].asInt();
|
||
} else if (jsonRoot["targetAddr"].isString()) {
|
||
if (inet_pton(AF_INET, jsonRoot["targetAddr"].asCString(), &config_config.processes[pid].option.ryads.net.target_addr) == 1) {
|
||
vLog(LOG_DEBUG, "IPv4 地址转换成功,网络字节序为: %u.\n", config_config.processes[pid].option.ryads.net.target_addr);
|
||
} else {
|
||
vLog(LOG_ERROR, "inet_pton error(%d,%s).\n", errno, strerror(errno));
|
||
}
|
||
}
|
||
//端口
|
||
if (jsonRoot["targetPort"].isInt()) {
|
||
config_config.processes[pid].option.ryads.net.target_port = jsonRoot["targetPort"].asInt();
|
||
} else if (jsonRoot["targetPort"].isString()) {
|
||
config_config.processes[pid].option.ryads.net.target_port = atoi(jsonRoot["targetPort"].asCString());
|
||
}
|
||
//本机地址
|
||
if (jsonRoot["bindAddr"].isInt()) {
|
||
config_config.processes[pid].option.ryads.net.bind_addr = jsonRoot["bindAddr"].asInt();
|
||
} else if (jsonRoot["bindAddr"].isString()) {
|
||
if (inet_pton(AF_INET, jsonRoot["bindAddr"].asCString(), &config_config.processes[pid].option.ryads.net.bind_addr) == 1) {
|
||
vLog(LOG_DEBUG, "IPv4 地址转换成功,网络字节序为: %u.\n", config_config.processes[pid].option.ryads.net.bind_addr);
|
||
} else {
|
||
vLog(LOG_ERROR, "inet_pton error(%d,%s).\n", errno, strerror(errno));
|
||
}
|
||
}
|
||
//ads添加路由的用户名密码
|
||
//用户名
|
||
if (jsonRoot["adsUser"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ryads.user, sizeof(config_config.processes[pid].option.ryads.user), "%s", jsonRoot["adsUser"].asCString());
|
||
} else { //默认存在允许ftp功能
|
||
snprintf(config_config.processes[pid].option.ryads.user, sizeof(config_config.processes[pid].option.ryads.user), "%s", "admin");
|
||
}
|
||
//密码
|
||
if (jsonRoot["adsPassword"].isString()) {
|
||
snprintf(config_config.processes[pid].option.ryads.password, sizeof(config_config.processes[pid].option.ryads.password), "%s", jsonRoot["adsPassword"].asCString());
|
||
} else { //默认存在允许ftp功能
|
||
snprintf(config_config.processes[pid].option.ryads.password, sizeof(config_config.processes[pid].option.ryads.password), "%s", "admin");
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
BOOLEAN CRYDevice::processHostIEC104ProcessParam(const Json::Value jsonRoot, int pid)
|
||
{
|
||
if (pid < 0 || pid >= PROCESSES_NUM) return FALSE;
|
||
|
||
config_config.processes[pid].option.iec104.net.ignored_source = TRUE;
|
||
config_config.processes[pid].option.iec104.net.socket_type = SOCK_STREAM;
|
||
config_config.processes[pid].option.iec104.net.target_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.iec104.net.target_port = 2404;
|
||
config_config.processes[pid].option.iec104.net.bind_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.iec104.net.bind_port = 0;
|
||
config_config.processes[pid].option.iec104.asdu_addr_size = 2;
|
||
config_config.processes[pid].option.iec104.cot_size = 2;
|
||
config_config.processes[pid].option.iec104.info_addr_size = 3;
|
||
config_config.processes[pid].option.iec104.t0 = 30;
|
||
config_config.processes[pid].option.iec104.t1 = 15;
|
||
config_config.processes[pid].option.iec104.t2 = 10;
|
||
config_config.processes[pid].option.iec104.t3 = 20;
|
||
config_config.processes[pid].option.iec104.k = 12;
|
||
config_config.processes[pid].option.iec104.w = 8;
|
||
|
||
if (jsonRoot["host"].isInt()) {
|
||
config_config.processes[pid].option.iec104.net.target_addr = jsonRoot["host"].asInt();
|
||
} else if (jsonRoot["host"].isString()) {
|
||
if (inet_pton(AF_INET, jsonRoot["host"].asCString(), &config_config.processes[pid].option.iec104.net.target_addr) == 1) {
|
||
vLog(LOG_DEBUG, "IPv4 地址转换成功,网络字节序为: %u.\n", config_config.processes[pid].option.iec104.net.target_addr);
|
||
} else {
|
||
vLog(LOG_ERROR, "inet_pton error(%d,%s).\n", errno, strerror(errno));
|
||
}
|
||
}
|
||
if (jsonRoot["port"].isInt()) {
|
||
config_config.processes[pid].option.iec104.net.target_port = jsonRoot["port"].asInt();
|
||
} else if (jsonRoot["port"].isString()) {
|
||
config_config.processes[pid].option.iec104.net.target_port = atoi(jsonRoot["port"].asCString());
|
||
}
|
||
if (jsonRoot["t0"].isInt()) {
|
||
config_config.processes[pid].option.iec104.t0 = jsonRoot["t0"].asInt();
|
||
} else if (jsonRoot["t0"].isString()) {
|
||
config_config.processes[pid].option.iec104.t0 = atoi(jsonRoot["t0"].asCString());
|
||
}
|
||
if (jsonRoot["t1"].isInt()) {
|
||
config_config.processes[pid].option.iec104.t0 = jsonRoot["t1"].asInt();
|
||
} else if (jsonRoot["t1"].isString()) {
|
||
config_config.processes[pid].option.iec104.t0 = atoi(jsonRoot["t1"].asCString());
|
||
}
|
||
if (jsonRoot["t2"].isInt()) {
|
||
config_config.processes[pid].option.iec104.t0 = jsonRoot["t2"].asInt();
|
||
} else if (jsonRoot["t2"].isString()) {
|
||
config_config.processes[pid].option.iec104.t0 = atoi(jsonRoot["t2"].asCString());
|
||
}
|
||
if (jsonRoot["t3"].isInt()) {
|
||
config_config.processes[pid].option.iec104.t0 = jsonRoot["t3"].asInt();
|
||
} else if (jsonRoot["t3"].isString()) {
|
||
config_config.processes[pid].option.iec104.t0 = atoi(jsonRoot["t3"].asCString());
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
BOOLEAN CRYDevice::processSubIEC104ProcessParam(const Json::Value jsonRoot, int pid)
|
||
{
|
||
if (pid < 0 || pid >= PROCESSES_NUM) return FALSE;
|
||
|
||
config_config.processes[pid].option.iec104.net.ignored_source = TRUE;
|
||
config_config.processes[pid].option.iec104.net.socket_type = SOCK_STREAM;
|
||
config_config.processes[pid].option.iec104.net.target_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.iec104.net.target_port = 0;
|
||
config_config.processes[pid].option.iec104.net.bind_addr = INADDR_ANY;
|
||
config_config.processes[pid].option.iec104.net.bind_port = 0;
|
||
config_config.processes[pid].option.iec104.asdu_addr_size = 2;
|
||
config_config.processes[pid].option.iec104.cot_size = 2;
|
||
config_config.processes[pid].option.iec104.info_addr_size = 3;
|
||
config_config.processes[pid].option.iec104.t0 = 30;
|
||
config_config.processes[pid].option.iec104.t1 = 15;
|
||
config_config.processes[pid].option.iec104.t2 = 10;
|
||
config_config.processes[pid].option.iec104.t3 = 20;
|
||
config_config.processes[pid].option.iec104.k = 12;
|
||
config_config.processes[pid].option.iec104.w = 8;
|
||
|
||
if (jsonRoot["port"].isInt()) {
|
||
config_config.processes[pid].option.iec104.net.bind_port = jsonRoot["port"].asInt();
|
||
} else if (jsonRoot["port"].isString()) {
|
||
config_config.processes[pid].option.iec104.net.bind_port = atoi(jsonRoot["port"].asCString());
|
||
}
|
||
if (jsonRoot["t1"].isInt()) {
|
||
config_config.processes[pid].option.iec104.t0 = jsonRoot["t1"].asInt();
|
||
} else if (jsonRoot["t1"].isString()) {
|
||
config_config.processes[pid].option.iec104.t0 = atoi(jsonRoot["t1"].asCString());
|
||
}
|
||
if (jsonRoot["t2"].isInt()) {
|
||
config_config.processes[pid].option.iec104.t0 = jsonRoot["t2"].asInt();
|
||
} else if (jsonRoot["t2"].isString()) {
|
||
config_config.processes[pid].option.iec104.t0 = atoi(jsonRoot["t2"].asCString());
|
||
}
|
||
if (jsonRoot["t3"].isInt()) {
|
||
config_config.processes[pid].option.iec104.t0 = jsonRoot["t3"].asInt();
|
||
} else if (jsonRoot["t3"].isString()) {
|
||
config_config.processes[pid].option.iec104.t0 = atoi(jsonRoot["t3"].asCString());
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
|
||
#define POINT_TYPE_YX 0
|
||
#define POINT_TYPE_YC 1
|
||
#define POINT_TYPE_YM 2
|
||
#define POINT_TYPE_YK 3
|
||
#define POINT_TYPE_YT 4
|
||
|
||
BOOLEAN CRYDevice::processHostModbusPointParam(const Json::Value jsonRoot, int uid, int point, int type)
|
||
{
|
||
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
||
if (point < 0) return FALSE;
|
||
switch (type)
|
||
{
|
||
case POINT_TYPE_YX:
|
||
if (point >= config_config.units[uid].yxcount) return FALSE;
|
||
if (jsonRoot["col1"].isString()) {
|
||
BYTE funcCode = (BYTE)atoi(jsonRoot["col1"].asCString());
|
||
config_config.units[uid].yxs[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["col1"].isInt()) {
|
||
BYTE funcCode = (BYTE)jsonRoot["col1"].asInt();
|
||
config_config.units[uid].yxs[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["funcCode"].isString()) {
|
||
BYTE funcCode = (BYTE)atoi(jsonRoot["funcCode"].asCString());
|
||
config_config.units[uid].yxs[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["funcCode"].isInt()) {
|
||
BYTE funcCode = (BYTE)jsonRoot["funcCode"].asInt();
|
||
config_config.units[uid].yxs[point].m_param[0] = funcCode;
|
||
} else {
|
||
config_config.units[uid].yxs[point].m_param[0] = 0;
|
||
}
|
||
//
|
||
if (jsonRoot["col2"].isString()) {
|
||
BYTE offSet = (BYTE)atoi(jsonRoot["col2"].asCString());
|
||
config_config.units[uid].yxs[point].m_param[3] = offSet;
|
||
} else if (jsonRoot["col2"].isInt()) {
|
||
BYTE offSet = (BYTE)jsonRoot["col2"].asInt();
|
||
config_config.units[uid].yxs[point].m_param[3] = offSet;
|
||
} else if (jsonRoot["offSet"].isString()) {
|
||
BYTE offSet = (BYTE)atoi(jsonRoot["offSet"].asCString());
|
||
config_config.units[uid].yxs[point].m_param[3] = offSet;
|
||
} else if (jsonRoot["offSet"].isInt()) {
|
||
BYTE offSet = (BYTE)jsonRoot["offSet"].asInt();
|
||
config_config.units[uid].yxs[point].m_param[3] = offSet;
|
||
} else {
|
||
config_config.units[uid].yxs[point].m_param[3] = 0;
|
||
}
|
||
//
|
||
if (jsonRoot["col3"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["col3"].asCString());
|
||
config_config.units[uid].yxs[point].m_param[1] = (registerAddr & 0xff);
|
||
config_config.units[uid].yxs[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} if (jsonRoot["col3"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["col3"].asInt();
|
||
config_config.units[uid].yxs[point].m_param[1] = (registerAddr & 0xff);
|
||
config_config.units[uid].yxs[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["registerAddr"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["registerAddr"].asCString());
|
||
config_config.units[uid].yxs[point].m_param[1] = (registerAddr & 0xff);
|
||
config_config.units[uid].yxs[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["registerAddr"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["registerAddr"].asInt();
|
||
config_config.units[uid].yxs[point].m_param[1] = (registerAddr & 0xff);
|
||
config_config.units[uid].yxs[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} else {
|
||
config_config.units[uid].yxs[point].m_param[1] = 0;
|
||
config_config.units[uid].yxs[point].m_param[2] = 0;
|
||
}
|
||
break;
|
||
case POINT_TYPE_YC:
|
||
if (point >= config_config.units[uid].yccount) return FALSE;
|
||
if (jsonRoot["col1"].isString()) {
|
||
BYTE funcCode = (BYTE)atoi(jsonRoot["col1"].asCString());
|
||
config_config.units[uid].ycs[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["col1"].isInt()) {
|
||
BYTE funcCode = jsonRoot["col1"].asInt();
|
||
config_config.units[uid].ycs[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["funcCode"].isString()) {
|
||
BYTE funcCode = (BYTE)atoi(jsonRoot["funcCode"].asCString());
|
||
config_config.units[uid].ycs[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["funcCode"].isInt()) {
|
||
BYTE funcCode = jsonRoot["funcCode"].asInt();
|
||
config_config.units[uid].ycs[point].m_param[0] = funcCode;
|
||
} else {
|
||
config_config.units[uid].ycs[point].m_param[0] = 0;
|
||
}
|
||
//
|
||
if (jsonRoot["col2"].isString()) {
|
||
BYTE dataType = (BYTE)atoi(jsonRoot["col2"].asCString());
|
||
config_config.units[uid].ycs[point].m_param[4] = dataType;
|
||
if (dataType == 2 || dataType == 8) config_config.units[uid].ycs[point].m_param[3] = 1;
|
||
else config_config.units[uid].ycs[point].m_param[3] = 2;
|
||
} else if (jsonRoot["col2"].isInt()) {
|
||
BYTE dataType = jsonRoot["col2"].asInt();
|
||
config_config.units[uid].ycs[point].m_param[4] = dataType;
|
||
if (dataType == 2 || dataType == 8) config_config.units[uid].ycs[point].m_param[3] = 1;
|
||
else config_config.units[uid].ycs[point].m_param[3] = 2;
|
||
} else if (jsonRoot["dataType"].isString()) {
|
||
BYTE dataType = (BYTE)atoi(jsonRoot["dataType"].asCString());
|
||
config_config.units[uid].ycs[point].m_param[4] = dataType;
|
||
if (dataType == 2 || dataType == 8) config_config.units[uid].ycs[point].m_param[3] = 1;
|
||
else config_config.units[uid].ycs[point].m_param[3] = 2;
|
||
} else if (jsonRoot["dataType"].isInt()) {
|
||
BYTE dataType = jsonRoot["dataType"].asInt();
|
||
config_config.units[uid].ycs[point].m_param[4] = dataType;
|
||
if (dataType == 2 || dataType == 8) config_config.units[uid].ycs[point].m_param[3] = 1;
|
||
else config_config.units[uid].ycs[point].m_param[3] = 2;
|
||
} else {
|
||
config_config.units[uid].ycs[point].m_param[4] = 0;
|
||
config_config.units[uid].ycs[point].m_param[3] = 1;
|
||
}
|
||
//
|
||
if (jsonRoot["col3"].isString()) {
|
||
BYTE signMark = (BYTE)atoi(jsonRoot["col3"].asCString());
|
||
config_config.units[uid].ycs[point].m_param[5] = signMark;
|
||
} else if (jsonRoot["col3"].isInt()) {
|
||
BYTE signMark = jsonRoot["col3"].asInt();
|
||
config_config.units[uid].ycs[point].m_param[5] = signMark;
|
||
} else if (jsonRoot["signMark"].isString()) {
|
||
BYTE signMark = (BYTE)atoi(jsonRoot["signMark"].asCString());
|
||
config_config.units[uid].ycs[point].m_param[5] = signMark;
|
||
} else if (jsonRoot["signMark"].isInt()) {
|
||
BYTE signMark = jsonRoot["signMark"].asInt();
|
||
config_config.units[uid].ycs[point].m_param[5] = signMark;
|
||
} else {
|
||
config_config.units[uid].ycs[point].m_param[5] = 0;
|
||
}
|
||
//
|
||
if (jsonRoot["col4"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["col4"].asCString());
|
||
config_config.units[uid].ycs[point].m_param[1] = (registerAddr & 0xff);
|
||
config_config.units[uid].ycs[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["col4"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["col4"].asInt();
|
||
config_config.units[uid].ycs[point].m_param[1] = (registerAddr & 0xff);
|
||
config_config.units[uid].ycs[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["registerAddr"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["registerAddr"].asCString());
|
||
config_config.units[uid].ycs[point].m_param[1] = (registerAddr & 0xff);
|
||
config_config.units[uid].ycs[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["registerAddr"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["registerAddr"].asInt();
|
||
config_config.units[uid].ycs[point].m_param[1] = (registerAddr & 0xff);
|
||
config_config.units[uid].ycs[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} else {
|
||
config_config.units[uid].ycs[point].m_param[1] = 0;
|
||
config_config.units[uid].ycs[point].m_param[2] = 0;
|
||
}
|
||
//
|
||
break;
|
||
case POINT_TYPE_YM:
|
||
if (point >= config_config.units[uid].ymcount) return FALSE;
|
||
if (jsonRoot["col1"].isString()) {
|
||
BYTE funcCode = (BYTE)atoi(jsonRoot["col1"].asCString());
|
||
config.units[uid].yms[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["col1"].isInt()) {
|
||
BYTE funcCode = jsonRoot["col1"].asInt();
|
||
config_config.units[uid].yms[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["funcCode"].isString()) {
|
||
BYTE funcCode = (BYTE)atoi(jsonRoot["funcCode"].asCString());
|
||
config.units[uid].yms[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["funcCode"].isInt()) {
|
||
BYTE funcCode = jsonRoot["funcCode"].asInt();
|
||
config_config.units[uid].yms[point].m_param[0] = funcCode;
|
||
} else {
|
||
config.units[uid].yms[point].m_param[0] = 0;
|
||
}
|
||
//
|
||
if (jsonRoot["col2"].isString()) {
|
||
BYTE dataType = (BYTE)atoi(jsonRoot["col2"].asCString());
|
||
config.units[uid].yms[point].m_param[4] = dataType;
|
||
if (dataType == 0) config.units[uid].yms[point].m_param[3] = 1;
|
||
if (dataType == 7 || dataType == 8) config.units[uid].yms[point].m_param[3] = 4;
|
||
else config.units[uid].yms[point].m_param[3] = 2;
|
||
} else if (jsonRoot["col2"].isInt()) {
|
||
BYTE dataType = jsonRoot["col2"].asInt();
|
||
config.units[uid].yms[point].m_param[4] = dataType;
|
||
if (dataType == 0) config.units[uid].yms[point].m_param[3] = 1;
|
||
if (dataType == 7 || dataType == 8) config.units[uid].yms[point].m_param[3] = 4;
|
||
else config.units[uid].yms[point].m_param[3] = 2;
|
||
} else if (jsonRoot["dataType"].isString()) {
|
||
BYTE dataType = (BYTE)atoi(jsonRoot["dataType"].asCString());
|
||
config.units[uid].yms[point].m_param[4] = dataType;
|
||
if (dataType == 0) config.units[uid].yms[point].m_param[3] = 1;
|
||
if (dataType == 7 || dataType == 8) config.units[uid].yms[point].m_param[3] = 4;
|
||
else config.units[uid].yms[point].m_param[3] = 2;
|
||
} else if (jsonRoot["dataType"].isInt()) {
|
||
BYTE dataType = jsonRoot["dataType"].asInt();
|
||
config.units[uid].yms[point].m_param[4] = dataType;
|
||
if (dataType == 0) config.units[uid].yms[point].m_param[3] = 1;
|
||
if (dataType == 7 || dataType == 8) config.units[uid].yms[point].m_param[3] = 4;
|
||
else config.units[uid].yms[point].m_param[3] = 2;
|
||
} else {
|
||
config.units[uid].yms[point].m_param[4] = 0;
|
||
config.units[uid].yms[point].m_param[3] = 1;
|
||
}
|
||
//
|
||
if (jsonRoot["col4"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["col4"].asCString());
|
||
config.units[uid].yms[point].m_param[1] = (registerAddr & 0xff);
|
||
config.units[uid].yms[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["col4"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["col4"].asInt();
|
||
config_config.units[uid].yms[point].m_param[1] = (registerAddr & 0xff);
|
||
config_config.units[uid].yms[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["registerAddr"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["registerAddr"].asCString());
|
||
config.units[uid].yms[point].m_param[1] = (registerAddr & 0xff);
|
||
config.units[uid].yms[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["registerAddr"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["registerAddr"].asInt();
|
||
config_config.units[uid].yms[point].m_param[1] = (registerAddr & 0xff);
|
||
config_config.units[uid].yms[point].m_param[2] = ((registerAddr >> 8) & 0xff);
|
||
} else {
|
||
config.units[uid].yms[point].m_param[1] = 0;
|
||
config.units[uid].yms[point].m_param[2] = 0;
|
||
}
|
||
//
|
||
break;
|
||
case POINT_TYPE_YK:
|
||
if (point >= config_config.units[uid].ykcount) return FALSE;
|
||
config_config.units[uid].yks[point].m_param[1] = 0;
|
||
config_config.units[uid].yks[point].m_param[2] = 0;
|
||
config_config.units[uid].yks[point].m_param[4] = 0xFF;
|
||
config_config.units[uid].yks[point].m_param[5] = 0xFF;
|
||
config_config.units[uid].yks[point].m_param[8] = 0xFF;
|
||
config_config.units[uid].yks[point].m_param[9] = 0xFF;
|
||
config_config.units[uid].yks[point].m_param[12] = 0xFF;
|
||
config_config.units[uid].yks[point].m_param[13] = 0xFF;
|
||
config_config.units[uid].yks[point].m_param[16] = 0xFF;
|
||
config_config.units[uid].yks[point].m_param[17] = 0xFF;
|
||
if (jsonRoot["col1"].isString()) {
|
||
BYTE funcCode = (BYTE)atoi(jsonRoot["col1"].asCString());
|
||
config_config.units[uid].yks[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["col1"].isInt()) {
|
||
BYTE funcCode = (BYTE)jsonRoot["col1"].asInt();
|
||
config_config.units[uid].yks[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["funcCode"].isString()) {
|
||
BYTE funcCode = (BYTE)atoi(jsonRoot["funcCode"].asCString());
|
||
config_config.units[uid].yks[point].m_param[0] = funcCode;
|
||
} else if (jsonRoot["funcCode"].isInt()) {
|
||
BYTE funcCode = (BYTE)jsonRoot["funcCode"].asInt();
|
||
config_config.units[uid].yks[point].m_param[0] = funcCode;
|
||
} else {
|
||
config_config.units[uid].yks[point].m_param[0] = 0;
|
||
}
|
||
//
|
||
if (jsonRoot["col4"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["col4"].asCString());
|
||
config_config.units[uid].yks[point].m_param[6] = (registerAddr & 0xff);
|
||
config_config.units[uid].yks[point].m_param[7] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["col4"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["col4"].asInt();
|
||
config_config.units[uid].yks[point].m_param[6] = (registerAddr & 0xff);
|
||
config_config.units[uid].yks[point].m_param[7] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["closeRegisterAddr"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["closeRegisterAddr"].asCString());
|
||
config_config.units[uid].yks[point].m_param[6] = (registerAddr & 0xff);
|
||
config_config.units[uid].yks[point].m_param[7] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["closeRegisterAddr"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["closeRegisterAddr"].asInt();
|
||
config_config.units[uid].yks[point].m_param[6] = (registerAddr & 0xff);
|
||
config_config.units[uid].yks[point].m_param[7] = ((registerAddr >> 8) & 0xff);
|
||
} else {
|
||
config_config.units[uid].yks[point].m_param[6] = 0xFF;
|
||
config_config.units[uid].yks[point].m_param[7] = 0xFF;
|
||
}
|
||
//
|
||
if (jsonRoot["col6"].isString()) {
|
||
WORD closeVal = (WORD)atoi(jsonRoot["col6"].asCString());
|
||
config_config.units[uid].yks[point].m_param[10] = (closeVal & 0xff);
|
||
config_config.units[uid].yks[point].m_param[11] = ((closeVal >> 8) & 0xff);
|
||
} else if (jsonRoot["col6"].isInt()) {
|
||
WORD closeVal = (WORD)jsonRoot["col6"].asInt();
|
||
config_config.units[uid].yks[point].m_param[10] = (closeVal & 0xff);
|
||
config_config.units[uid].yks[point].m_param[11] = ((closeVal >> 8) & 0xff);
|
||
} else if (jsonRoot["closeValue"].isString()) {
|
||
WORD closeVal = (WORD)atoi(jsonRoot["closeValue"].asCString());
|
||
config_config.units[uid].yks[point].m_param[10] = (closeVal & 0xff);
|
||
config_config.units[uid].yks[point].m_param[11] = ((closeVal >> 8) & 0xff);
|
||
} else if (jsonRoot["closeValue"].isInt()) {
|
||
WORD closeVal = (WORD)jsonRoot["closeValue"].asInt();
|
||
config_config.units[uid].yks[point].m_param[10] = (closeVal & 0xff);
|
||
config_config.units[uid].yks[point].m_param[11] = ((closeVal >> 8) & 0xff);
|
||
} else {
|
||
config_config.units[uid].yks[point].m_param[10] = 0xFF;
|
||
config_config.units[uid].yks[point].m_param[11] = 0xFF;
|
||
}
|
||
//
|
||
if (jsonRoot["col8"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["col8"].asCString());
|
||
config_config.units[uid].yks[point].m_param[14] = (registerAddr & 0xff);
|
||
config_config.units[uid].yks[point].m_param[15] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["col8"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["col8"].asInt();
|
||
config_config.units[uid].yks[point].m_param[14] = (registerAddr & 0xff);
|
||
config_config.units[uid].yks[point].m_param[15] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["openRegisterAddr"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["openRegisterAddr"].asCString());
|
||
config_config.units[uid].yks[point].m_param[14] = (registerAddr & 0xff);
|
||
config_config.units[uid].yks[point].m_param[15] = ((registerAddr >> 8) & 0xff);
|
||
} if (jsonRoot["openRegisterAddr"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["openRegisterAddr"].asInt();
|
||
config_config.units[uid].yks[point].m_param[14] = (registerAddr & 0xff);
|
||
config_config.units[uid].yks[point].m_param[15] = ((registerAddr >> 8) & 0xff);
|
||
} else {
|
||
config_config.units[uid].yks[point].m_param[14] = 0xFF;
|
||
config_config.units[uid].yks[point].m_param[15] = 0xFF;
|
||
}
|
||
//
|
||
if (jsonRoot["col10"].isString()) {
|
||
WORD openVal = (WORD)atoi(jsonRoot["col10"].asCString());
|
||
config_config.units[uid].yks[point].m_param[18] = (openVal & 0xff);
|
||
config_config.units[uid].yks[point].m_param[19] = ((openVal >> 8) & 0xff);
|
||
} else if (jsonRoot["col10"].isInt()) {
|
||
WORD openVal = (WORD)jsonRoot["col10"].asInt();
|
||
config_config.units[uid].yks[point].m_param[18] = (openVal & 0xff);
|
||
config_config.units[uid].yks[point].m_param[19] = ((openVal >> 8) & 0xff);
|
||
} else if (jsonRoot["openValue"].isString()) {
|
||
WORD openVal = (WORD)atoi(jsonRoot["openValue"].asCString());
|
||
config_config.units[uid].yks[point].m_param[18] = (openVal & 0xff);
|
||
config_config.units[uid].yks[point].m_param[19] = ((openVal >> 8) & 0xff);
|
||
} else if (jsonRoot["openValue"].isInt()) {
|
||
WORD openVal = (WORD)jsonRoot["openValue"].asInt();
|
||
config_config.units[uid].yks[point].m_param[18] = (openVal & 0xff);
|
||
config_config.units[uid].yks[point].m_param[19] = ((openVal >> 8) & 0xff);
|
||
} else {
|
||
config_config.units[uid].yks[point].m_param[18] = 0xFF;
|
||
config_config.units[uid].yks[point].m_param[19] = 0xFF;
|
||
}
|
||
//
|
||
break;
|
||
case POINT_TYPE_YT:
|
||
if (point >= config_config.units[uid].ytcount) return FALSE;
|
||
config_config.units[uid].yts[point].m_param[2] = 0;
|
||
config_config.units[uid].yts[point].m_param[4] = 0xFF;
|
||
config_config.units[uid].yts[point].m_param[5] = 0xFF;
|
||
config_config.units[uid].yts[point].m_param[8] = 0xFF;
|
||
config_config.units[uid].yts[point].m_param[9] = 0xFF;
|
||
if (jsonRoot["col1"].isString()) {
|
||
BYTE setType = (BYTE)atoi(jsonRoot["col1"].asCString());
|
||
config_config.units[uid].yts[point].m_param[0] = setType;
|
||
} else if (jsonRoot["col1"].isInt()) {
|
||
BYTE setType = (BYTE)jsonRoot["col1"].asInt();
|
||
config_config.units[uid].yts[point].m_param[0] = setType;
|
||
} else if (jsonRoot["setType"].isString()) {
|
||
BYTE setType = (BYTE)atoi(jsonRoot["setType"].asCString());
|
||
config_config.units[uid].yts[point].m_param[0] = setType;
|
||
} else if (jsonRoot["setType"].isInt()) {
|
||
BYTE setType = (BYTE)jsonRoot["setType"].asInt();
|
||
config_config.units[uid].yts[point].m_param[0] = setType;
|
||
} else {
|
||
config_config.units[uid].yts[point].m_param[0] = 0;
|
||
}
|
||
//
|
||
if (jsonRoot["col2"].isString()) {
|
||
BYTE funcCode = (BYTE)atoi(jsonRoot["col2"].asCString());
|
||
config_config.units[uid].yts[point].m_param[1] = funcCode;
|
||
} else if (jsonRoot["col2"].isInt()) {
|
||
BYTE funcCode = (BYTE)jsonRoot["col2"].asInt();
|
||
config_config.units[uid].yts[point].m_param[1] = funcCode;
|
||
} else if (jsonRoot["funcCode"].isString()) {
|
||
BYTE funcCode = (BYTE)atoi(jsonRoot["funcCode"].asCString());
|
||
config_config.units[uid].yts[point].m_param[1] = funcCode;
|
||
} else if (jsonRoot["funcCode"].isInt()) {
|
||
BYTE funcCode = (BYTE)jsonRoot["funcCode"].asInt();
|
||
config_config.units[uid].yts[point].m_param[1] = funcCode;
|
||
} else {
|
||
config_config.units[uid].yts[point].m_param[1] = 0;
|
||
}
|
||
//
|
||
if (jsonRoot["col5"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["col5"].asCString());
|
||
config_config.units[uid].yts[point].m_param[6] = (registerAddr & 0xff);
|
||
config_config.units[uid].yts[point].m_param[7] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["col5"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["col5"].asInt();
|
||
config_config.units[uid].yts[point].m_param[6] = (registerAddr & 0xff);
|
||
config_config.units[uid].yts[point].m_param[7] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["registerAddr"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["registerAddr"].asCString());
|
||
config_config.units[uid].yts[point].m_param[6] = (registerAddr & 0xff);
|
||
config_config.units[uid].yts[point].m_param[7] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["registerAddr"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["registerAddr"].asInt();
|
||
config_config.units[uid].yts[point].m_param[6] = (registerAddr & 0xff);
|
||
config_config.units[uid].yts[point].m_param[7] = ((registerAddr >> 8) & 0xff);
|
||
} else {
|
||
config_config.units[uid].yts[point].m_param[6] = 0xFF;
|
||
config_config.units[uid].yts[point].m_param[7] = 0xFF;
|
||
}
|
||
//
|
||
break;
|
||
default: break;
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
BOOLEAN CRYDevice::processHostADSPointParam(const Json::Value jsonRoot, int uid, int point, int type)
|
||
{
|
||
if (uid < 0 || uid >= UNIT_NUM) return FALSE;
|
||
if (point < 0) return FALSE;
|
||
|
||
switch (type)
|
||
{
|
||
case POINT_TYPE_YX:
|
||
if (point >= config_config.units[uid].yxcount) return FALSE;
|
||
if (jsonRoot["offSet"].isString()) {
|
||
BYTE offSet = (BYTE)atoi(jsonRoot["offSet"].asCString());
|
||
config_config.units[uid].yxs[point].m_param[3] = offSet;
|
||
} else if (jsonRoot["offSet"].isInt()) {
|
||
BYTE offSet = (BYTE)jsonRoot["offSet"].asInt();
|
||
config_config.units[uid].yxs[point].m_param[3] = offSet;
|
||
} else {
|
||
config_config.units[uid].yxs[point].m_param[3] = 0;
|
||
}
|
||
if (jsonRoot["registerAddr"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["registerAddr"].asCString());
|
||
config_config.units[uid].yxs[point].m_param[0] = (registerAddr & 0xff);
|
||
config_config.units[uid].yxs[point].m_param[1] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["registerAddr"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["registerAddr"].asInt();
|
||
config_config.units[uid].yxs[point].m_param[0] = (registerAddr & 0xff);
|
||
config_config.units[uid].yxs[point].m_param[1] = ((registerAddr >> 8) & 0xff);
|
||
} else {
|
||
config_config.units[uid].yxs[point].m_param[0] = 0;
|
||
config_config.units[uid].yxs[point].m_param[1] = 0;
|
||
}
|
||
if (jsonRoot["dataType"].isString()) {
|
||
BYTE dataType = (BYTE)atoi(jsonRoot["dataType"].asCString());
|
||
config_config.units[uid].yxs[point].m_param[2] = dataType;
|
||
} else if (jsonRoot["dataType"].isInt()) {
|
||
BYTE dataType = (BYTE)jsonRoot["dataType"].asInt();
|
||
config_config.units[uid].yxs[point].m_param[2] = dataType;
|
||
} else {
|
||
config_config.units[uid].yxs[point].m_param[2] = 0;
|
||
}
|
||
break;
|
||
case POINT_TYPE_YC:
|
||
if (point >= config_config.units[uid].yccount) return FALSE;
|
||
if (jsonRoot["signMark"].isString()) {
|
||
BYTE signMark = (BYTE)atoi(jsonRoot["signMark"].asCString());
|
||
config_config.units[uid].ycs[point].m_param[3] = signMark;
|
||
} else if (jsonRoot["signMark"].isInt()) {
|
||
BYTE signMark = (BYTE)jsonRoot["signMark"].asInt();
|
||
config_config.units[uid].ycs[point].m_param[3] = signMark;
|
||
} else {
|
||
config_config.units[uid].ycs[point].m_param[3] = 0;
|
||
}
|
||
if (jsonRoot["registerAddr"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["registerAddr"].asCString());
|
||
config_config.units[uid].ycs[point].m_param[0] = (registerAddr & 0xff);
|
||
config_config.units[uid].ycs[point].m_param[1] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["registerAddr"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["registerAddr"].asInt();
|
||
config_config.units[uid].ycs[point].m_param[0] = (registerAddr & 0xff);
|
||
config_config.units[uid].ycs[point].m_param[1] = ((registerAddr >> 8) & 0xff);
|
||
} else {
|
||
config_config.units[uid].ycs[point].m_param[0] = 0;
|
||
config_config.units[uid].ycs[point].m_param[1] = 0;
|
||
}
|
||
if (jsonRoot["dataType"].isString()) {
|
||
BYTE dataType = (BYTE)atoi(jsonRoot["dataType"].asCString());
|
||
config_config.units[uid].ycs[point].m_param[2] = dataType;
|
||
} else if (jsonRoot["dataType"].isInt()) {
|
||
BYTE dataType = (BYTE)jsonRoot["dataType"].asInt();
|
||
config_config.units[uid].ycs[point].m_param[2] = dataType;
|
||
} else {
|
||
config_config.units[uid].ycs[point].m_param[2] = 9; //8位数据,占一个寄存器
|
||
}
|
||
{
|
||
BYTE data_type = config_config.units[uid].ycs[point].m_param[2];
|
||
if ((data_type == 5) || (data_type == 0) || (data_type == 1))
|
||
{ //浮点数
|
||
config_config.units[uid].ycs[point].factor = 1000;
|
||
config_config.units[uid].ycs[point].coef = 0.001;
|
||
}
|
||
}
|
||
break;
|
||
case POINT_TYPE_YM:
|
||
if (point >= config_config.units[uid].ymcount) return FALSE;
|
||
if (jsonRoot["registerAddr"].isString()) {
|
||
WORD registerAddr = (WORD)atoi(jsonRoot["registerAddr"].asCString());
|
||
config_config.units[uid].yms[point].m_param[0] = (registerAddr & 0xff);
|
||
config_config.units[uid].yms[point].m_param[1] = ((registerAddr >> 8) & 0xff);
|
||
} else if (jsonRoot["registerAddr"].isInt()) {
|
||
WORD registerAddr = (WORD)jsonRoot["registerAddr"].asInt();
|
||
config_config.units[uid].yms[point].m_param[0] = (registerAddr & 0xff);
|
||
config_config.units[uid].yms[point].m_param[1] = ((registerAddr >> 8) & 0xff);
|
||
} else {
|
||
config_config.units[uid].yms[point].m_param[0] = 0;
|
||
config_config.units[uid].yms[point].m_param[1] = 0;
|
||
}
|
||
if (jsonRoot["dataType"].isString()) {
|
||
BYTE dataType = (BYTE)atoi(jsonRoot["dataType"].asCString());
|
||
config_config.units[uid].yms[point].m_param[2] = dataType;
|
||
} else if (jsonRoot["dataType"].isInt()) {
|
||
BYTE dataType = (BYTE)jsonRoot["dataType"].asInt();
|
||
config_config.units[uid].yms[point].m_param[2] = dataType;
|
||
} else {
|
||
config_config.units[uid].yms[point].m_param[2] = 0;
|
||
}
|
||
break;
|
||
case POINT_TYPE_YK:
|
||
if (point >= config_config.units[uid].ykcount) return FALSE;
|
||
break;
|
||
case POINT_TYPE_YT:
|
||
if (point >= config_config.units[uid].ytcount) return FALSE;
|
||
break;
|
||
default: break;
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
bool CRYDevice::dealConfigFile(const Json::Value jsonRoot)
|
||
{
|
||
bool result = false;
|
||
do {
|
||
FILE* pf = fopen("aaa.json", "w+");
|
||
if (pf) {
|
||
fwrite(jsonRoot.toStyledString().c_str(), jsonRoot.toStyledString().length(), 1, pf);
|
||
fclose(pf);
|
||
}
|
||
if (!configInitializeMemory()) {
|
||
vLog(LOG_ERROR, "Fail initialize memory!\n");
|
||
break;
|
||
}
|
||
if (!jsonRoot["nodeId"].isString()) {
|
||
vLog(LOG_ERROR, "配置文件格式错误,缺少节点ID的配置信息,请检查。\n");
|
||
break;
|
||
}
|
||
if (jsonRoot["version"].isNull()) {
|
||
vLog(LOG_WARN, "配置文件格式错误,缺少版本信息,请检查。\n");
|
||
}
|
||
if (jsonRoot["version"].isInt()) {
|
||
snprintf(config_system32.version, sizeof(config_system32.version), "%d", jsonRoot["version"].asInt());
|
||
} else if (jsonRoot["version"].isString()) {
|
||
snprintf(config_system32.version, sizeof(config_system32.version), "%s", jsonRoot["version"].asCString());
|
||
}
|
||
|
||
//更新节点配置
|
||
config_nodes.m_node[0].m_netnode_no = 0;
|
||
config_nodes.m_node[0].m_tcitype = 1;
|
||
config_nodes.m_node[0].m_target_addr = INADDR_LOOPBACK;
|
||
config_nodes.m_node[0].m_target_port = 15000;
|
||
snprintf(config_nodes.m_node[0].m_machine_name, sizeof(config_nodes.m_node[0].m_machine_name), "%s", jsonRoot["nodeId"].asCString());
|
||
//更新链路配置文件
|
||
const Json::Value links = jsonRoot["links"];
|
||
int count = links.size();
|
||
int uartId = 0;
|
||
|
||
int uid = 0;
|
||
uid2pid_map.clear();
|
||
|
||
long yxorder = 0, ycorder = 0, ymorder = 0, ykorder = 0, ytorder = 0;
|
||
string2intmap map_yxorders, map_ycorders, map_ymorders, map_ykorders, map_ytorders;
|
||
|
||
pid2attrvectormap map_pid2attrserice;
|
||
|
||
char dbyxname[512];
|
||
snprintf(dbyxname, sizeof(dbyxname), "%s/%s", configpath, FILE_DATABASE_YX_STATIC);
|
||
FILE *pfdbyxname = fopen(dbyxname, "wb+");
|
||
char dbycname[512];
|
||
snprintf(dbycname, sizeof(dbycname), "%s/%s", configpath, FILE_DATABASE_YC_STATIC);
|
||
FILE *pfdbycname = fopen(dbycname, "wb+");
|
||
char dbymname[512];
|
||
snprintf(dbymname, sizeof(dbymname), "%s/%s", configpath, FILE_DATABASE_YM_STATIC);
|
||
FILE *pfdbymname = fopen(dbymname, "wb+");
|
||
char dbykname[512];
|
||
snprintf(dbykname, sizeof(dbykname), "%s/%s", configpath, FILE_DATABASE_YK_STATIC);
|
||
FILE *pfdbykname = fopen(dbykname, "wb+");
|
||
char dbytname[512];
|
||
snprintf(dbytname, sizeof(dbytname), "%s/%s", configpath, FILE_DATABASE_YT_STATIC);
|
||
FILE *pfdbytname = fopen(dbytname, "wb+");
|
||
|
||
map_yxorders.clear(); map_ycorders.clear(); map_ymorders.clear(); map_ykorders.clear(); map_ytorders.clear();
|
||
|
||
for (int i = 0; i < count; i++) {
|
||
const Json::Value link = links[i];
|
||
|
||
config_config.processes[i].state = TRUE;
|
||
config_config.processes[i].time_gap = 300; //默认参数
|
||
config_config.processes[i].poll_gap = 5;
|
||
|
||
if (link["linkName"].isString()) {
|
||
snprintf(config_config.processes[i].name, sizeof(config_config.processes[i].name), "%s", link["linkName"].asCString());
|
||
}
|
||
if (link["linkId"].isString()) {
|
||
config_config.processes[i].irn = strtoll(link["linkId"].asCString(), NULL, 10);
|
||
}
|
||
if (link["protocol"].isInt()) {
|
||
config_config.processes[i].proto = link["protocol"].asInt();
|
||
}
|
||
//处理链路参数,根据协议参数的不同来处理
|
||
BYTE addrType = ADDR_TYPE_NORMAL; //根据协议设定单元地址类型
|
||
const Json::Value params = link["params"];
|
||
if (!params.isNull()) {
|
||
switch (config_config.processes[i].proto) {
|
||
case PROTOCOL_HOST_MODBUS_RTU:
|
||
processUartParam(params, uartId);
|
||
config_config.processes[i].order = uartId++;
|
||
config_config.processes[i].type = 1;
|
||
config_config.processes[i].mode = PROCESS_MODE_MASTER;
|
||
break;
|
||
case PROTOCOL_HOST_MODBUS_TCP:
|
||
processHostModbustcpParam(params, i);
|
||
config_config.processes[i].type = 3;
|
||
config_config.processes[i].mode = PROCESS_MODE_MASTER;
|
||
addrType = ADDR_TYPE_IPV4;
|
||
break;
|
||
case PROTOCOL_HOST_BF_MODBUSTCP:
|
||
processRymodbustcpParam(params, i);
|
||
config_config.processes[i].type = 3;
|
||
config_config.processes[i].mode = PROCESS_MODE_MASTER;
|
||
addrType = ADDR_TYPE_IPV4;
|
||
break;
|
||
case PROTOCOL_FTP2MINIO:
|
||
processRyFTP2MinioParam(params, i);
|
||
break;
|
||
case PROTOCOL_HOST_BF_ADS:
|
||
processRyADSParam(params, i);
|
||
config_config.processes[i].type = 3;
|
||
config_config.processes[i].mode = PROCESS_MODE_MASTER;
|
||
break;
|
||
case PROTOCOL_HOST_IEC104:
|
||
processHostIEC104ProcessParam(params, i);
|
||
config_config.processes[i].type = 3;
|
||
config_config.processes[i].mode = PROCESS_MODE_MASTER;
|
||
addrType = ADDR_TYPE_IPV4_FACNO;
|
||
break;
|
||
case PROTOCOL_SUB_IEC104:
|
||
processSubIEC104ProcessParam(params, i);
|
||
config_config.processes[i].type = 3;
|
||
config_config.processes[i].mode = PROCESS_MODE_SLAVE;
|
||
addrType = ADDR_TYPE_IPV4_FACNO;
|
||
break;
|
||
case PROTOCOL_SUB_MODBUS_TCP:
|
||
processSubModbustcpParam(params, i);
|
||
config_config.processes[i].type = 3;
|
||
config_config.processes[i].mode = PROCESS_MODE_SLAVE;
|
||
addrType = ADDR_TYPE_IPV4;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
//处理链接设备
|
||
const Json::Value devices = link["devices"];
|
||
if (devices.isArray()) {
|
||
int size = devices.size();
|
||
if (config_config.processes[i].mode == PROCESS_MODE_SLAVE)
|
||
{
|
||
attrvectorGroup attrsItem;
|
||
for (int j = 0; j < size; j++) {
|
||
const Json::Value device = devices[j];
|
||
std::string id = "";
|
||
std::string address = "";
|
||
if (device["id"].isString()) {
|
||
id = device["id"].asString();
|
||
}
|
||
if (device["addr"].isString()) {
|
||
address = device["addr"].asString();
|
||
}
|
||
//此处先将数据缓存。然后在处理
|
||
const Json::Value attrs = device["attrs"];
|
||
if (!attrs.isNull()) {
|
||
int size = attrs.size();
|
||
for (int k = 0; k < size; k++) {
|
||
const Json::Value attr = attrs[k];
|
||
std::string type = "";
|
||
if (attr["type"].isString()) type = attr["type"].asString();
|
||
struct_attr name_param;
|
||
name_param.name = "";
|
||
name_param.highSpeed = 0;
|
||
name_param.order = -1;
|
||
name_param.deviceId = id;
|
||
if (attr["name"].isString()) name_param.name = id + attr["name"].asString();
|
||
if (attr["order"].isInt()) name_param.order = attr["order"].asInt();
|
||
if (attr["params"].isObject()) name_param.params = attr["params"];
|
||
if (type == "yc") attrsItem.ycs.push_back(name_param);
|
||
else if (type == "yx") attrsItem.yxs.push_back(name_param);
|
||
else if (type == "ym") attrsItem.yms.push_back(name_param);
|
||
}
|
||
}
|
||
const Json::Value services = device["services"];
|
||
if (!services.isNull()) {
|
||
int size = services.size();
|
||
for (int k = 0; k < size; k++) {
|
||
const Json::Value service = services[k];
|
||
std::string type = "";
|
||
if (service["type"].isString()) type = service["type"].asString();
|
||
struct_attr name_param;
|
||
name_param.name = "";
|
||
name_param.order = -1;
|
||
name_param.deviceId = id;
|
||
if (service["name"].isString()) name_param.name = id + service["name"].asString();
|
||
if (service["order"].isInt()) name_param.order = service["order"].asInt();
|
||
if (service["params"].isObject()) name_param.params = service["params"];
|
||
if (type == "yk") attrsItem.yks.push_back(name_param);
|
||
else if (type == "yt") attrsItem.yts.push_back(name_param);
|
||
}
|
||
}
|
||
}
|
||
if (map_pid2attrserice.find(i) == map_pid2attrserice.end()) {
|
||
map_pid2attrserice.insert(pid2attrvectormap::value_type(i, attrsItem));
|
||
}
|
||
}
|
||
else if (config_config.processes[i].mode == PROCESS_MODE_MASTER)
|
||
{
|
||
for (int j = 0; j < size; j++) {
|
||
const Json::Value device = devices[j];
|
||
std::string id = "";
|
||
std::string address = "";
|
||
if (device["id"].isString()) {
|
||
id = device["id"].asString();
|
||
}
|
||
if (device["addr"].isString()) {
|
||
address = device["addr"].asString();
|
||
}
|
||
snprintf(config_static_units[uid].name, sizeof(config_static_units[uid].name), "device_%d", uid);
|
||
snprintf(config_static_units[uid].model, sizeof(config_static_units[uid].model), "model_%d", uid);
|
||
snprintf(config_static_units[uid].manufacturerId, sizeof(config_static_units[uid].manufacturerId), "%s", "iss");
|
||
if (id != "") {
|
||
snprintf(config_static_units[uid].deviceId, sizeof(config_static_units[uid].deviceId), "%s", id.c_str());
|
||
} else {
|
||
snprintf(config_static_units[uid].deviceId, sizeof(config_static_units[uid].deviceId), "iss_%d", uid);
|
||
}
|
||
|
||
if (uid2pid_map.find(id) == uid2pid_map.end()) {
|
||
uid2pid_map.insert(string2intmap::value_type(id, i));
|
||
}
|
||
config_config.processes[i].units[j] = uid;
|
||
//根据协议修改地址
|
||
if (address != "") {
|
||
if (addrType == ADDR_TYPE_HEX) {
|
||
StringToHex(address.c_str(), config_config.units[uid].addr);
|
||
} else if (addrType == ADDR_TYPE_IPV4) {
|
||
DWORD addr;
|
||
inet_pton(AF_INET, address.c_str(), (struct in_addr*)&addr);
|
||
memcpy(config_config.units[uid].addr, &addr, sizeof(addr));
|
||
} else if (addrType == ADDR_TYPE_IPV4_FACNO) {
|
||
std::vector<std::string> tokens = split(address, ':');
|
||
if (tokens.size() >= 2) {
|
||
DWORD addr;
|
||
inet_pton(AF_INET, tokens.at(0).c_str(), (struct in_addr*)&addr);
|
||
memcpy(config_config.units[uid].addr, &addr, sizeof(addr));
|
||
addr = (DWORD)atoi(tokens.at(1).c_str());
|
||
memcpy(&config_config.units[uid].addr[4], &addr, sizeof(addr));
|
||
} else {
|
||
DWORD addr;
|
||
inet_pton(AF_INET, tokens.at(0).c_str(), (struct in_addr*)&addr);
|
||
memcpy(config_config.units[uid].addr, &addr, sizeof(addr));
|
||
addr = 1;
|
||
memcpy(&config_config.units[uid].addr[4], &addr, sizeof(addr));
|
||
}
|
||
} else {
|
||
DWORD addr = (DWORD)atoi(address.c_str());
|
||
memcpy(config_config.units[uid].addr, &addr, sizeof(addr));
|
||
}
|
||
}
|
||
attrvector yxs, ycs, yms, yks, yts;
|
||
const Json::Value attrs = device["attrs"];
|
||
if (!attrs.isNull()) {
|
||
int size = attrs.size();
|
||
for (int k = 0; k < size; k++) {
|
||
const Json::Value attr = attrs[k];
|
||
std::string type = "";
|
||
if (attr["type"].isString()) type = attr["type"].asString();
|
||
struct_attr name_param;
|
||
name_param.name = "";
|
||
name_param.highSpeed = 0;
|
||
name_param.order = -1;
|
||
if (attr["name"].isString()) name_param.name = attr["name"].asString();
|
||
if (attr["highSpeed"].isInt()) name_param.highSpeed = attr["highSpeed"].asInt();
|
||
if (attr["order"].isInt()) name_param.order = attr["order"].asInt();
|
||
if (attr["params"].isObject()) name_param.params = attr["params"];
|
||
if (type == "yc") ycs.push_back(name_param);
|
||
else if (type == "yx") yxs.push_back(name_param);
|
||
else if (type == "ym") yms.push_back(name_param);
|
||
}
|
||
}
|
||
const Json::Value services = device["services"];
|
||
if (!services.isNull()) {
|
||
int size = services.size();
|
||
for (int k = 0; k < size; k++) {
|
||
const Json::Value service = services[k];
|
||
std::string type = "";
|
||
if (service["type"].isString()) type = service["type"].asString();
|
||
struct_attr name_param;
|
||
name_param.name = "";
|
||
name_param.order = -1;
|
||
if (service["name"].isString()) name_param.name = service["name"].asString();
|
||
if (service["order"].isInt()) name_param.order = service["order"].asInt();
|
||
if (service["params"].isObject()) name_param.params = service["params"];
|
||
if (type == "yk") yks.push_back(name_param);
|
||
else if (type == "yt") yts.push_back(name_param);
|
||
}
|
||
}
|
||
|
||
config_config.units[uid].irn = strtoll(id.c_str(), NULL, 10);
|
||
config_config.units[uid].value = SPI_ON;
|
||
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();
|
||
config_config.units[uid].yccount = ycs.size();
|
||
config_config.units[uid].ymcount = yms.size();
|
||
config_config.units[uid].ykcount = yks.size();
|
||
config_config.units[uid].ytcount = yts.size();
|
||
if (config_config.units[uid].yccount > 0) {
|
||
//config_config.units[uid].ycs = new struUnitYC[config_config.units[uid].yccount];
|
||
if (NULL != config_config.units[uid].ycs) {
|
||
for (int k = 0; k < config_config.units[uid].yccount; k++) {
|
||
snprintf(config_config.units[uid].ycs[k].name, sizeof(config_config.units[uid].ycs[k].name), "%s", ycs[k].name.c_str());
|
||
config_config.units[uid].ycs[k].order = ycorder++;
|
||
std::string idattrname = id + ycs[k].name;
|
||
if (map_ycorders.find(idattrname) == map_ycorders.end()) {
|
||
map_ycorders.insert(string2intmap::value_type(idattrname, config_config.units[uid].ycs[k].order));
|
||
}
|
||
config_config.units[uid].ycs[k].value = 0;
|
||
config_config.units[uid].ycs[k].qds = 0x80; //默认为无效
|
||
config_config.units[uid].ycs[k].factor = 1;
|
||
config_config.units[uid].ycs[k].change_pos = 2;
|
||
config_config.units[uid].ycs[k].coef = 1.0f;
|
||
config_config.units[uid].ycs[k].base = 0;
|
||
config_config.units[uid].ycs[k].upBound = 9999999.999f;
|
||
config_config.units[uid].ycs[k].lowBound = -9999999.999f;
|
||
config_config.units[uid].ycs[k].limit1Enable = FALSE;
|
||
config_config.units[uid].ycs[k].limit1Low = 0;
|
||
config_config.units[uid].ycs[k].limit1High = 0.0f;
|
||
config_config.units[uid].ycs[k].limit2Enable = FALSE;
|
||
config_config.units[uid].ycs[k].limit2Low = 0;
|
||
config_config.units[uid].ycs[k].limit2High = 0.0f;
|
||
config_config.units[uid].ycs[k].forceArchive = FALSE;
|
||
config_config.units[uid].ycs[k].highSpeed = ycs[k].highSpeed;
|
||
Json::Value param = ycs[k].params;
|
||
if (!param.isNull()) {
|
||
if (param["coef"].isDouble()) config_config.units[uid].ycs[k].coef = param["coef"].asFloat();
|
||
if (param["base"].isDouble()) config_config.units[uid].ycs[k].base = param["base"].asFloat();
|
||
if (param["base"].isDouble()) config_config.units[uid].ycs[k].base = param["base"].asFloat();
|
||
if (param["upBound"].isDouble()) config_config.units[uid].ycs[k].upBound = param["upBound"].asFloat();
|
||
if (param["lowBound"].isDouble()) config_config.units[uid].ycs[k].lowBound = param["lowBound"].asFloat();
|
||
if (param["limit1Enable"].isInt()) config_config.units[uid].ycs[k].limit1Enable = param["limit1Enable"].asInt();
|
||
if (param["limit2Enable"].isInt()) config_config.units[uid].ycs[k].limit2Enable = param["limit2Enable"].asInt();
|
||
if (param["limit1Low"].isDouble()) config_config.units[uid].ycs[k].limit1Low = param["limit1Low"].asFloat();
|
||
if (param["limit2Low"].isDouble()) config_config.units[uid].ycs[k].limit2Low = param["limit2Low"].asFloat();
|
||
if (param["limit1High"].isDouble()) config_config.units[uid].ycs[k].limit1High = param["limit1High"].asFloat();
|
||
if (param["limit2High"].isDouble()) config_config.units[uid].ycs[k].limit2High = param["limit2High"].asFloat();
|
||
if (param["forceArchive"].isInt()) config_config.units[uid].ycs[k].forceArchive = param["forceArchive"].asInt();
|
||
|
||
//vLog(LOG_DEBUG, "config_config.units[%d].ycs[%d].coef is: %f\n", uid, k, config_config.units[uid].ycs[k].coef);
|
||
switch (config_config.processes[i].proto) {
|
||
case PROTOCOL_HOST_MODBUS_RTU:
|
||
case PROTOCOL_HOST_MODBUS_TCP:
|
||
case PROTOCOL_HOST_MODBUS_RTU_TCP:
|
||
case PROTOCOL_HOST_BF_MODBUSTCP:
|
||
processHostModbusPointParam(param, uid, k, POINT_TYPE_YC);
|
||
break;
|
||
case PROTOCOL_HOST_IEC104:
|
||
break;
|
||
case PROTOCOL_HOST_BF_ADS:
|
||
processHostADSPointParam(param, uid, k, POINT_TYPE_YC);
|
||
break;
|
||
}
|
||
}
|
||
if (config_config.units[uid].type == MASTER_UNIT) {
|
||
config_database.ycs[config_config.units[uid].ycs[k].order].coef = config_config.units[uid].ycs[k].coef;
|
||
config_database.ycs[config_config.units[uid].ycs[k].order].base = config_config.units[uid].ycs[k].base;
|
||
}
|
||
if (pfdbycname) {
|
||
fseek(pfdbycname, sizeof(struYCStatic) * config_config.units[uid].ycs[k].order, SEEK_SET);
|
||
if (!ycs[k].params["measPointName"].isNull())
|
||
{
|
||
std::string measPointName = std::string(config_config.processes[i].name) + std::string("_");
|
||
if (ycs[k].params["measPointName"].isString()) measPointName += ycs[k].params["measPointName"].asString();
|
||
else measPointName += std::string("备用");
|
||
fwrite(measPointName.c_str(), (MAX_NAME_SIZE << 2), 1, pfdbycname);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if (config_config.units[uid].ymcount > 0) {
|
||
if (NULL != config_config.units[uid].yms) {
|
||
memset(config_config.units[uid].yms, 0, sizeof(struUnitYM) * config_config.units[uid].ymcount);
|
||
for (int k = 0; k < config_config.units[uid].ymcount; k++) {
|
||
snprintf(config_config.units[uid].yms[k].name, sizeof(config_config.units[uid].yms[k].name), "%s", yms[k].name.c_str());
|
||
config_config.units[uid].yms[k].order = ymorder++;
|
||
std::string idattrname = id + yms[k].name;
|
||
if (map_ymorders.find(idattrname) == map_ymorders.end()) {
|
||
map_ymorders.insert(string2intmap::value_type(idattrname, config_config.units[uid].yms[k].order));
|
||
}
|
||
config_config.units[uid].yms[k].value = 0;
|
||
config_config.units[uid].yms[k].coef = 1.0f;
|
||
config_config.units[uid].yms[k].base = 0;
|
||
|
||
Json::Value param = yms[k].params;
|
||
if (!param.isNull()) {
|
||
switch (config_config.processes[i].proto) {
|
||
case PROTOCOL_HOST_MODBUS_RTU:
|
||
case PROTOCOL_HOST_MODBUS_TCP:
|
||
case PROTOCOL_HOST_MODBUS_RTU_TCP:
|
||
case PROTOCOL_HOST_BF_MODBUSTCP:
|
||
processHostModbusPointParam(param, uid, k, POINT_TYPE_YM);
|
||
break;
|
||
case PROTOCOL_HOST_IEC104:
|
||
break;
|
||
case PROTOCOL_HOST_BF_ADS:
|
||
processHostADSPointParam(param, uid, k, POINT_TYPE_YM);
|
||
break;
|
||
}
|
||
}
|
||
if (config_config.units[uid].type == MASTER_UNIT) {
|
||
config_database.yms[config_config.units[uid].yms[k].order].coef = config_config.units[uid].yms[k].coef;
|
||
config_database.yms[config_config.units[uid].yms[k].order].base = config_config.units[uid].yms[k].base;
|
||
}
|
||
|
||
if (pfdbymname) {
|
||
fseek(pfdbymname, sizeof(struYMStatic) * config_config.units[uid].yms[k].order, SEEK_SET);
|
||
if (!yms[k].params["measPointName"].isNull())
|
||
{
|
||
std::string measPointName = std::string(config_config.processes[i].name) + std::string("_");
|
||
if (yms[k].params["measPointName"].isString()) measPointName += yms[k].params["measPointName"].asString();
|
||
else measPointName += std::string("备用");
|
||
fwrite(measPointName.c_str(), (MAX_NAME_SIZE << 2), 1, pfdbymname);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if (config_config.units[uid].ykcount > 0) {
|
||
if (NULL != config_config.units[uid].yks) {
|
||
memset(config_config.units[uid].yks, 0, sizeof(struUnitYK) * config_config.units[uid].ykcount);
|
||
for (int k = 0; k < config_config.units[uid].ykcount; k++) {
|
||
snprintf(config_config.units[uid].yks[k].name, sizeof(config_config.units[uid].yks[k].name), "%s", yks[k].name.c_str());
|
||
config_config.units[uid].yks[k].order = ykorder++;
|
||
std::string idserverrname = id + yks[k].name;
|
||
if (map_ykorders.find(idserverrname) == map_ykorders.end()) {
|
||
map_ykorders.insert(string2intmap::value_type(idserverrname, config_config.units[uid].yks[k].order));
|
||
}
|
||
|
||
Json::Value param = yks[k].params;
|
||
if (!param.isNull()) {
|
||
switch (config_config.processes[i].proto) {
|
||
case PROTOCOL_HOST_MODBUS_RTU:
|
||
case PROTOCOL_HOST_MODBUS_TCP:
|
||
case PROTOCOL_HOST_MODBUS_RTU_TCP:
|
||
case PROTOCOL_HOST_BF_MODBUSTCP:
|
||
processHostModbusPointParam(param, uid, k, POINT_TYPE_YK);
|
||
break;
|
||
case PROTOCOL_HOST_IEC104:
|
||
break;
|
||
case PROTOCOL_HOST_BF_ADS:
|
||
processHostADSPointParam(param, uid, k, POINT_TYPE_YK);
|
||
break;
|
||
}
|
||
}
|
||
if (pfdbykname) {
|
||
fseek(pfdbykname, sizeof(struYKStatic) * config_config.units[uid].yks[k].order, SEEK_SET);
|
||
if (!yks[k].params["measPointName"].isNull())
|
||
{
|
||
std::string measPointName = std::string(config_config.processes[i].name) + std::string("_");
|
||
if (yks[k].params["measPointName"].isString()) measPointName += yks[k].params["measPointName"].asString();
|
||
else measPointName += std::string("备用");
|
||
fwrite(measPointName.c_str(), (MAX_NAME_SIZE << 2), 1, pfdbykname);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if (config_config.units[uid].ytcount > 0) {
|
||
if (NULL != config_config.units[uid].yts) {
|
||
memset(config_config.units[uid].yts, 0, sizeof(struUnitYT) * config_config.units[uid].ytcount);
|
||
for (int k = 0; k < config_config.units[uid].ytcount; k++) {
|
||
snprintf(config_config.units[uid].yts[k].name, sizeof(config_config.units[uid].yts[k].name), "%s", yts[k].name.c_str());
|
||
config_config.units[uid].yts[k].order = ytorder++;
|
||
std::string idserverrname = id + yts[k].name;
|
||
if (map_ytorders.find(idserverrname) == map_ytorders.end()) {
|
||
map_ytorders.insert(string2intmap::value_type(idserverrname, config_config.units[uid].yts[k].order));
|
||
}
|
||
|
||
Json::Value param = yts[k].params;
|
||
if (!param.isNull()) {
|
||
switch (config_config.processes[i].proto) {
|
||
case PROTOCOL_HOST_MODBUS_RTU:
|
||
case PROTOCOL_HOST_MODBUS_TCP:
|
||
case PROTOCOL_HOST_MODBUS_RTU_TCP:
|
||
case PROTOCOL_HOST_BF_MODBUSTCP:
|
||
processHostModbusPointParam(param, uid, k, POINT_TYPE_YT);
|
||
break;
|
||
case PROTOCOL_HOST_IEC104:
|
||
break;
|
||
case PROTOCOL_HOST_BF_ADS:
|
||
processHostADSPointParam(param, uid, k, POINT_TYPE_YT);
|
||
break;
|
||
}
|
||
}
|
||
if (pfdbytname) {
|
||
fseek(pfdbytname, sizeof(struYTStatic) * config_config.units[uid].yts[k].order, SEEK_SET);
|
||
if (!yts[k].params["measPointName"].isNull())
|
||
{
|
||
std::string measPointName = std::string(config_config.processes[i].name) + std::string("_");
|
||
if (yts[k].params["measPointName"].isString()) measPointName += yts[k].params["measPointName"].asString();
|
||
else measPointName += std::string("备用");
|
||
fwrite(measPointName.c_str(), (MAX_NAME_SIZE << 2), 1, pfdbytname);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if (config_config.units[uid].yxcount > 0) {
|
||
if (NULL != config_config.units[uid].yxs) {
|
||
memset(config_config.units[uid].yxs, 0, sizeof(struUnitYX) * config_config.units[uid].yxcount);
|
||
for (int k = 0; k < config_config.units[uid].yxcount; k++) {
|
||
snprintf(config_config.units[uid].yxs[k].name, sizeof(config_config.units[uid].yxs[k].name), "%s", yxs[k].name.c_str());
|
||
config_config.units[uid].yxs[k].order = yxorder++;
|
||
std::string idattrname = id + yxs[k].name;
|
||
if (map_yxorders.find(idattrname) == map_yxorders.end()) {
|
||
map_yxorders.insert(string2intmap::value_type(idattrname, config_config.units[uid].yxs[k].order));
|
||
}
|
||
config_config.units[uid].yxs[k].value = 0;
|
||
config_config.units[uid].yxs[k].qds = 0x80; //默认为无效
|
||
config_config.units[uid].yxs[k].invert = FALSE;
|
||
config_config.units[uid].yxs[k].forceArchive = FALSE;
|
||
config_database.yxs[config_config.units[uid].yxs[k].order].auto_reset = 0;
|
||
|
||
Json::Value param = yxs[k].params;
|
||
if (!param.isNull()) {
|
||
if (param["invert"].asInt()) config_config.units[uid].yxs[k].invert = param["invert"].asInt();
|
||
if (param["forceArchive"].isInt()) config_config.units[uid].yxs[k].forceArchive = param["forceArchive"].asInt();
|
||
switch (config_config.processes[i].proto) {
|
||
case PROTOCOL_HOST_MODBUS_RTU:
|
||
case PROTOCOL_HOST_MODBUS_TCP:
|
||
case PROTOCOL_HOST_MODBUS_RTU_TCP:
|
||
case PROTOCOL_HOST_BF_MODBUSTCP:
|
||
processHostModbusPointParam(param, uid, k, POINT_TYPE_YX);
|
||
break;
|
||
case PROTOCOL_HOST_IEC104:
|
||
break;
|
||
case PROTOCOL_HOST_BF_ADS:
|
||
processHostADSPointParam(param, uid, k, POINT_TYPE_YX);
|
||
break;
|
||
}
|
||
}
|
||
if (pfdbyxname) {
|
||
fseek(pfdbyxname, sizeof(struYXStatic) * config_config.units[uid].yxs[k].order, SEEK_SET);
|
||
if (!yxs[k].params["measPointName"].isNull())
|
||
{
|
||
std::string measPointName = std::string(config_config.processes[i].name) + std::string("_");
|
||
if (yxs[k].params["measPointName"].isString()) measPointName += yxs[k].params["measPointName"].asString();
|
||
else measPointName += std::string("备用");
|
||
fwrite(measPointName.c_str(), (MAX_NAME_SIZE << 2), 1, pfdbyxname);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
uid++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
#if 1
|
||
//判断从设备列表
|
||
int sub_uid = uid;
|
||
for (pid2attrvectormap::iterator it = map_pid2attrserice.begin(); it != map_pid2attrserice.end(); it++)
|
||
{
|
||
int pid = it->first;
|
||
attrvectorGroup attrs = it->second;
|
||
|
||
switch (config_config.processes[pid].proto)
|
||
{
|
||
case PROTOCOL_SUB_MODBUS_TCP:
|
||
config_config.units[sub_uid].addr[4] = 1;
|
||
config_config.processes[pid].option.smodbus.defaultYCType = 1;
|
||
break;
|
||
case PROTOCOL_SUB_IEC104:
|
||
config_config.units[sub_uid].addr[4] = 1;
|
||
break;
|
||
}
|
||
|
||
config_config.units[sub_uid].value = SPI_ON;
|
||
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();
|
||
config_config.units[sub_uid].yccount = attrs.ycs.size();
|
||
config_config.units[sub_uid].ymcount = attrs.yms.size();
|
||
config_config.units[sub_uid].ykcount = attrs.yks.size();
|
||
config_config.units[sub_uid].ytcount = attrs.yts.size();
|
||
config_config.processes[pid].units[0] = sub_uid;
|
||
|
||
if (config_config.units[sub_uid].yccount > 0)
|
||
{
|
||
for (int i = 0; i < config_config.units[sub_uid].yccount; i++) {
|
||
std::string key;
|
||
int order = -1;
|
||
int point = -1;
|
||
Json::Value param = attrs.ycs[i].params;
|
||
if (!param["order"].isNull())
|
||
{
|
||
if (param["order"].isInt()) point = param["order"].asInt();
|
||
if (param["order"].isString()) point = atoi(param["order"].asCString());
|
||
}
|
||
if (point < 0 || point >= config_config.units[sub_uid].yccount) continue;
|
||
key = attrs.ycs[i].name;
|
||
if (map_ycorders.find(key) != map_ycorders.end())
|
||
{
|
||
order = map_ycorders[key];
|
||
}
|
||
snprintf(config_config.units[sub_uid].ycs[point].name, sizeof(config_config.units[sub_uid].ycs[point].name), "%s", key.c_str());
|
||
config_config.units[sub_uid].ycs[point].order = order;
|
||
config_config.units[sub_uid].ycs[point].value = 0;
|
||
config_config.units[sub_uid].ycs[point].qds = 0x80; //默认为无效
|
||
config_config.units[sub_uid].ycs[point].factor = 1;
|
||
config_config.units[sub_uid].ycs[point].change_pos = 2;
|
||
if (order >= 0) {
|
||
config_config.units[sub_uid].ycs[point].coef = config_database.ycs[order].coef;
|
||
config_config.units[sub_uid].ycs[point].base = config_database.ycs[order].base;
|
||
} else {
|
||
config_config.units[sub_uid].ycs[point].coef = 1.0f;
|
||
config_config.units[sub_uid].ycs[point].base = 0.0f;
|
||
}
|
||
config_config.units[sub_uid].ycs[point].upBound = 9999999.999f;
|
||
config_config.units[sub_uid].ycs[point].lowBound = -9999999.999f;
|
||
config_config.units[sub_uid].ycs[point].limit1Enable = FALSE;
|
||
config_config.units[sub_uid].ycs[point].limit1Low = 0;
|
||
config_config.units[sub_uid].ycs[point].limit1High = 0.0f;
|
||
config_config.units[sub_uid].ycs[point].limit2Enable = FALSE;
|
||
config_config.units[sub_uid].ycs[point].limit2Low = 0;
|
||
config_config.units[sub_uid].ycs[point].limit2High = 0.0f;
|
||
config_config.units[sub_uid].ycs[point].highSpeed = FALSE;
|
||
}
|
||
}
|
||
if (config_config.units[sub_uid].ymcount > 0)
|
||
{
|
||
for (int i = 0; i < config_config.units[sub_uid].ymcount; i++) {
|
||
std::string key;
|
||
int order = -1;
|
||
int point = -1;
|
||
Json::Value param = attrs.yms[i].params;
|
||
if (!param["order"].isNull())
|
||
{
|
||
if (param["order"].isInt()) point = param["order"].asInt();
|
||
if (param["order"].isString()) point = atoi(param["order"].asCString());
|
||
}
|
||
if (point < 0 || point >= config_config.units[sub_uid].ymcount) continue;
|
||
key = attrs.yms[i].name;
|
||
if (map_ymorders.find(key) != map_ymorders.end())
|
||
{
|
||
order = map_ymorders[key];
|
||
}
|
||
snprintf(config_config.units[sub_uid].yms[point].name, sizeof(config_config.units[sub_uid].yms[point].name), "%s", key.c_str());
|
||
config_config.units[sub_uid].yms[point].order = order;
|
||
config_config.units[sub_uid].yms[point].value = 0;
|
||
if (order >= 0) {
|
||
config_config.units[sub_uid].yms[point].coef = config_database.yms[order].coef;
|
||
config_config.units[sub_uid].yms[point].base = config_database.yms[order].base;
|
||
} else {
|
||
config_config.units[sub_uid].yms[point].coef = 1.0f;
|
||
config_config.units[sub_uid].yms[point].base = 0.0f;
|
||
}
|
||
}
|
||
}
|
||
if (config_config.units[sub_uid].ykcount > 0)
|
||
{
|
||
for (int i = 0; i < config_config.units[sub_uid].ykcount; i++) {
|
||
std::string key;
|
||
int order = -1;
|
||
int point = -1;
|
||
Json::Value param = attrs.yks[i].params;
|
||
if (!param["order"].isNull())
|
||
{
|
||
if (param["order"].isInt()) point = param["order"].asInt();
|
||
if (param["order"].isString()) point = atoi(param["order"].asCString());
|
||
}
|
||
if (point < 0 || point >= config_config.units[sub_uid].ykcount) continue;
|
||
key = attrs.yks[i].name;
|
||
if (map_ykorders.find(key) != map_ykorders.end())
|
||
{
|
||
order = map_ykorders[key];
|
||
}
|
||
if (map_ykorders.find(key) != map_ykorders.end())
|
||
{
|
||
order = map_ykorders[key];
|
||
}
|
||
snprintf(config_config.units[sub_uid].yks[point].name, sizeof(config_config.units[sub_uid].yks[point].name), "%s", key.c_str());
|
||
config_config.units[sub_uid].yks[point].order = order;
|
||
}
|
||
}
|
||
if (config_config.units[sub_uid].ytcount > 0)
|
||
{
|
||
for (int i = 0; i < config_config.units[sub_uid].ytcount; i++) {
|
||
std::string key;
|
||
int order = -1;
|
||
int point = -1;
|
||
Json::Value param = attrs.yts[i].params;
|
||
if (!param["order"].isNull())
|
||
{
|
||
if (param["order"].isInt()) point = param["order"].asInt();
|
||
if (param["order"].isString()) point = atoi(param["order"].asCString());
|
||
}
|
||
if (point < 0 || point >= config_config.units[sub_uid].ytcount) continue;
|
||
key = attrs.yts[i].name;
|
||
if (map_ytorders.find(key) != map_ytorders.end())
|
||
{
|
||
order = map_ytorders[key];
|
||
}
|
||
snprintf(config_config.units[sub_uid].yts[point].name, sizeof(config_config.units[sub_uid].yts[point].name), "%s", key.c_str());
|
||
config_config.units[sub_uid].yts[point].order = order;
|
||
}
|
||
}
|
||
if (config_config.units[sub_uid].yxcount > 0)
|
||
{
|
||
for (int i = 0; i < config_config.units[sub_uid].yxcount; i++) {
|
||
std::string key;
|
||
int order = -1;
|
||
int point = -1;
|
||
Json::Value param = attrs.yxs[i].params;
|
||
if (!param["order"].isNull())
|
||
{
|
||
if (param["order"].isInt()) point = param["order"].asInt();
|
||
if (param["order"].isString()) point = atoi(param["order"].asCString());
|
||
}
|
||
if (point < 0 || point >= config_config.units[sub_uid].yxcount) continue;
|
||
key = attrs.yxs[i].name;
|
||
if (map_yxorders.find(key) != map_yxorders.end())
|
||
{
|
||
order = map_yxorders[key];
|
||
}
|
||
snprintf(config_config.units[sub_uid].yxs[point].name, sizeof(config_config.units[sub_uid].yxs[point].name), "%s", key.c_str());
|
||
config_config.units[sub_uid].yxs[point].order = order;
|
||
config_config.units[sub_uid].yxs[point].value = 0;
|
||
config_config.units[sub_uid].yxs[point].value = 0;
|
||
config_config.units[sub_uid].yxs[point].qds = 0x80; //默认为无效
|
||
config_config.units[sub_uid].yxs[point].invert = 0;
|
||
}
|
||
}
|
||
//
|
||
char csvFileName[260];
|
||
snprintf(csvFileName, sizeof(csvFileName), "%s/%s%d.csv", configpath, config_config.processes[pid].name, sub_uid);
|
||
FILE* csvFile = fopen(csvFileName, "wb+");
|
||
if (csvFile)
|
||
{
|
||
fprintf(csvFile, "[遥测量]\n");
|
||
for (int i = 0; i < config_config.units[sub_uid].yccount; i++)
|
||
{
|
||
struYCStatic yc;
|
||
memset(&yc, '\0', sizeof(yc));
|
||
if (pfdbycname && (config_config.units[sub_uid].ycs[i].order >= 0))
|
||
{
|
||
if (fseek(pfdbycname, sizeof(struYCStatic) * config_config.units[sub_uid].ycs[i].order, SEEK_SET) == 0)
|
||
{
|
||
fread(&yc, sizeof(yc), 1, pfdbycname);
|
||
}
|
||
}
|
||
if (yc.name[0] == '\0')
|
||
{
|
||
fprintf(csvFile, "%d=备用,1,0\n", i);
|
||
}
|
||
else
|
||
{
|
||
fprintf(csvFile, "%d=%s,%f,%f\n", i, yc.name, config_config.units[sub_uid].ycs[i].coef, config_config.units[sub_uid].ycs[i].base);
|
||
}
|
||
}
|
||
fprintf(csvFile, "[遥脉量]\n");
|
||
for (int i = 0; i < config_config.units[sub_uid].ymcount; i++)
|
||
{
|
||
struYMStatic ym;
|
||
memset(&ym, '\0', sizeof(ym));
|
||
if (pfdbymname && (config_config.units[sub_uid].yms[i].order >= 0))
|
||
{
|
||
if (fseek(pfdbymname, sizeof(struYMStatic) * config_config.units[sub_uid].yms[i].order, SEEK_SET) == 0)
|
||
{
|
||
fread(&ym, sizeof(ym), 1, pfdbymname);
|
||
}
|
||
}
|
||
if (ym.name[0] == '\0')
|
||
{
|
||
fprintf(csvFile, "%d=备用\n", i);
|
||
}
|
||
else
|
||
{
|
||
fprintf(csvFile, "%d=%s\n", i, ym.name);
|
||
}
|
||
}
|
||
fprintf(csvFile, "[遥控量]\n");
|
||
for (int i = 0; i < config_config.units[sub_uid].ykcount; i++)
|
||
{
|
||
struYKStatic yk;
|
||
memset(&yk, '\0', sizeof(yk));
|
||
if (pfdbykname && (config_config.units[sub_uid].yks[i].order >= 0))
|
||
{
|
||
if (fseek(pfdbykname, sizeof(struYKStatic) * config_config.units[sub_uid].yks[i].order, SEEK_SET) == 0)
|
||
{
|
||
fread(&yk, sizeof(yk), 1, pfdbykname);
|
||
}
|
||
}
|
||
if (yk.name[0] == '\0')
|
||
{
|
||
fprintf(csvFile, "%d=备用\n", i);
|
||
}
|
||
else
|
||
{
|
||
fprintf(csvFile, "%d=%s\n", i, yk.name);
|
||
}
|
||
}
|
||
fprintf(csvFile, "[遥调量]\n");
|
||
for (int i = 0; i < config_config.units[sub_uid].ytcount; i++)
|
||
{
|
||
struYTStatic yt;
|
||
memset(&yt, '\0', sizeof(yt));
|
||
if (pfdbytname && (config_config.units[sub_uid].yts[i].order >= 0))
|
||
{
|
||
if (fseek(pfdbytname, sizeof(struYTStatic) * config_config.units[sub_uid].yts[i].order, SEEK_SET) == 0)
|
||
{
|
||
fread(&yt, sizeof(yt), 1, pfdbytname);
|
||
}
|
||
}
|
||
if (yt.name[0] == '\0')
|
||
{
|
||
fprintf(csvFile, "%d=备用\n", i);
|
||
}
|
||
else
|
||
{
|
||
fprintf(csvFile, "%d=%s\n", i, yt.name);
|
||
}
|
||
}
|
||
fprintf(csvFile, "[遥信量]\n");
|
||
for (int i = 0; i < config_config.units[sub_uid].yxcount; i++)
|
||
{
|
||
struYXStatic yx;
|
||
if (pfdbyxname && (config_config.units[sub_uid].yxs[i].order >= 0))
|
||
{
|
||
if (fseek(pfdbyxname, sizeof(struYXStatic) * config_config.units[sub_uid].yxs[i].order, SEEK_SET) == 0)
|
||
{
|
||
fread(&yx, sizeof(yx), 1, pfdbyxname);
|
||
}
|
||
}
|
||
if (yx.name[0] == '\0')
|
||
{
|
||
fprintf(csvFile, "%d=备用,1,0\n", i);
|
||
}
|
||
else
|
||
{
|
||
fprintf(csvFile, "%d=%s\n", i, yx.name);
|
||
}
|
||
}
|
||
fclose(csvFile);
|
||
}
|
||
sub_uid++;
|
||
}
|
||
#endif
|
||
//保存数据库静态数据
|
||
if (pfdbyxname) fclose(pfdbyxname);
|
||
if (pfdbycname) fclose(pfdbycname);
|
||
if (pfdbymname) fclose(pfdbymname);
|
||
if (pfdbykname) fclose(pfdbykname);
|
||
if (pfdbytname) fclose(pfdbytname);
|
||
|
||
//保存数据
|
||
configWriteNodeCFG();
|
||
configWriteSystemCFG();
|
||
configWriteHardwareCFG();
|
||
configWriteProcessCFG();
|
||
configWriteUnitCFG();
|
||
configWriteDatabaseCFG();
|
||
//保存静态文件
|
||
configWriteStaticUnitCFG();
|
||
|
||
result = true;
|
||
} while (0);
|
||
|
||
//重启application
|
||
if (result) {
|
||
m_dataAcquisitionReload = true;
|
||
} else {
|
||
vLog(LOG_WARN, "配置文件读取失败,请检查后下发!\n");
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
bool CRYDevice::OnReceivedSystemAction(const std::string cmdId, const std::string cmd, const Json::Value data)
|
||
{
|
||
do {
|
||
if (cmd == "configUpdate") { //配置更新
|
||
m_traceId = cmdId;
|
||
dealConfigFile(data);
|
||
} else if (cmd == "deviceControl") {
|
||
m_traceId = cmdId; //response命令的traceId
|
||
OnReceivedDeviceCommand(data);
|
||
} else {
|
||
vLog(LOG_DEBUG, "command: %s is not supported.\n", cmd.c_str());
|
||
}
|
||
} while (0);
|
||
|
||
return true;
|
||
}
|
||
|
||
void CRYDevice::on_message(const char *msg, const int size)
|
||
{
|
||
if (msg == NULL) return;
|
||
if (size <= 0) return;
|
||
|
||
Json::Value jsonRoot;
|
||
jsonRoot.clear();
|
||
std::string err;
|
||
Json::CharReaderBuilder builder;
|
||
Json::CharReader* reader(builder.newCharReader());
|
||
|
||
//vLog(LOG_DEBUG, "Received: %s.\n", msg);
|
||
|
||
do {
|
||
if (!reader->parse(msg, msg + size, &jsonRoot, &err)) {
|
||
vLog(LOG_ERROR, "on_message reader->parse(msg, msg + size, &jsonRoot, &err) error<%d,%s>。\n", errno, err.c_str());
|
||
break;
|
||
}
|
||
if (jsonRoot["cmd"].isNull()) {
|
||
vLog(LOG_ERROR, "json format lost cmd part.\n");
|
||
break;
|
||
}
|
||
if (jsonRoot["data"].isNull()) {
|
||
vLog(LOG_ERROR, "json format lost data part.\n");
|
||
break;
|
||
}
|
||
std::string cmd = jsonRoot["cmd"].asString();
|
||
std::string cmdId = jsonRoot["cmdId"].asString();
|
||
#if 0
|
||
Json::Int64 mtime = jsonRoot["time"].asInt64();
|
||
#endif
|
||
Json::Value datas = jsonRoot["data"];
|
||
OnReceivedSystemAction(cmdId, cmd, datas);
|
||
} while (0);
|
||
}
|
||
|
||
void CRYDevice::heart_beat(int status)
|
||
{ //发送心跳报文
|
||
Json::Value payload;
|
||
payload["ttl"] = 30000;
|
||
payload["status"] = status;
|
||
|
||
if (status == 1) {
|
||
Json::Value jsonLink;
|
||
Json::Value jsonDevice;
|
||
for (int i = 0; i < PROCESSES_NUM; i++) {
|
||
if ((config.processes[i].state & 0x01) == TRUE) {
|
||
char linkId[32];
|
||
if (config.processes[i].irn == 0) continue;
|
||
Json::Value jsonValue;
|
||
#ifdef NOPOLL_64BIT_PLATFORM
|
||
snprintf(linkId, sizeof(linkId), "%ld", config.processes[i].irn);
|
||
#else
|
||
snprintf(linkId, sizeof(linkId), "%lld", config.processes[i].irn);
|
||
#endif
|
||
jsonValue["linkId"] = linkId;
|
||
jsonValue["online"] = (config.processes[i].real_softdog >= PROCESS_WATCHDOG_TIME) ? false : true;
|
||
#if 1
|
||
jsonValue["real"] = (config.processes[i].real_softdog >= PROCESS_WATCHDOG_TIME) ? false : true;
|
||
if ((config.processes[i].state & 0x20) == 0x20) {
|
||
jsonValue["ftp"] = (config.processes[i].ftp_softdog >= PROCESS_WATCHDOG_TIME) ? false : true;
|
||
}
|
||
#endif
|
||
jsonLink.append(jsonValue);
|
||
}
|
||
}
|
||
if (jsonLink.size() > 0) {
|
||
//vLog(LOG_DEBUG, "link is: %d\n", jsonLink.size());
|
||
payload["links"] = jsonLink;
|
||
}
|
||
for (int i = 0; i < UNIT_NUM; i++) {
|
||
//vLog(LOG_DEBUG, "unit (%d), state is: %d\n", i, config.units[i].state);
|
||
if ((config.units[i].state & 0x01) == TRUE) {
|
||
char deviceId[32];
|
||
if (config.units[i].irn == 0) continue;
|
||
Json::Value jsonValue;
|
||
#ifdef NOPOLL_64BIT_PLATFORM
|
||
snprintf(deviceId, sizeof(deviceId), "%ld", config.units[i].irn);
|
||
#else
|
||
snprintf(deviceId, sizeof(deviceId), "%lld", config.units[i].irn);
|
||
#endif
|
||
jsonValue["deviceId"] = deviceId;
|
||
jsonValue["online"] = (config.units[i].real_softdog >= UNIT_WATCHDOG_TIME) ? false : true;
|
||
#if 1
|
||
jsonValue["real"] = (config.units[i].real_softdog >= UNIT_WATCHDOG_TIME) ? false : true;
|
||
if ((config.units[i].state & 0x20) == 0x20) {
|
||
jsonValue["ftp"] = (config.units[i].ftp_softdog >= UNIT_WATCHDOG_TIME) ? false : true;
|
||
}
|
||
#endif
|
||
jsonDevice.append(jsonValue);
|
||
}
|
||
}
|
||
if (jsonLink.size() > 0) {
|
||
payload["devices"] = jsonDevice;
|
||
}
|
||
}
|
||
|
||
publish_sensor_data("", "heartbeat", payload);
|
||
}
|
||
|
||
|
||
bool CRYDevice::publishinitDeviceData(int uid)
|
||
{
|
||
if (uid < 0 || uid >= UNIT_NUM) return false;
|
||
|
||
Json::Value root;
|
||
Json::Value values;
|
||
|
||
int i;
|
||
int count = GetUnitYCCount(uid);
|
||
if (count) {
|
||
for (i = 0; i < count; i++) {
|
||
values[(const char *)config.units[uid].ycs[i].name] = GetUnitYCReal(uid, i);
|
||
}
|
||
}
|
||
count = GetUnitYMCount(uid);
|
||
if (count) {
|
||
for (i = 0; i < count; i++) {
|
||
values[(const char *)config.units[uid].yms[i].name] = GetUnitYMReal(uid, i);
|
||
}
|
||
}
|
||
count = GetUnitYXCount(uid);
|
||
if (count) {
|
||
for (i = 0; i < count; i++) {
|
||
values[(const char *)config.units[uid].yxs[i].name] = GetUnitYX(uid, i);
|
||
}
|
||
}
|
||
if (values.size()) {
|
||
root["deviceId"] = static_units[uid].deviceId;
|
||
root["values"] = values;
|
||
|
||
return publish_sensor_data("", "initDeviceData", root);
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool CRYDevice::publishAnalogData(int uid)
|
||
{
|
||
if (uid < 0 || uid >= UNIT_NUM) return false;
|
||
Json::Value root;
|
||
Json::Value values;
|
||
Json::Value archiveValues;
|
||
int count = GetUnitYCCount(uid);
|
||
if (count) {
|
||
for (int i = 0; i < count; i++) {
|
||
if (GetUnitYCIsForceArchive(uid, i))
|
||
{
|
||
if (GetUnitYCIsFloat(uid, i)) {
|
||
archiveValues[(const char *)config.units[uid].ycs[i].name] = GetUnitYCReal(uid, i);
|
||
} else {
|
||
archiveValues[(const char *)config.units[uid].ycs[i].name] = GetUnitYC(uid, i);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (GetUnitYCIsFloat(uid, i)) {
|
||
values[(const char *)config.units[uid].ycs[i].name] = GetUnitYCReal(uid, i);
|
||
} else {
|
||
values[(const char *)config.units[uid].ycs[i].name] = GetUnitYC(uid, i);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
Json::Int64 datatime = (Json::Int64)time(NULL);
|
||
datatime *= 1000;
|
||
root["dataTime"] = datatime;
|
||
root["deviceId"] = static_units[uid].deviceId;
|
||
// root["isStore"] = config.units[uid].state & 0x20 ? false : true;
|
||
|
||
if (values.size()) {
|
||
root["values"] = values;
|
||
}
|
||
if (archiveValues.size()) {
|
||
root["archiveValues"] = archiveValues;
|
||
}
|
||
|
||
return publish_sensor_data("", "analogData", root);
|
||
}
|
||
|
||
bool CRYDevice::publishStateData(int uid)
|
||
{
|
||
if (uid < 0 || uid >= UNIT_NUM) return false;
|
||
Json::Value root;
|
||
Json::Value values;
|
||
Json::Value archiveValues;
|
||
int count = GetUnitYXCount(uid);
|
||
if (count) {
|
||
for (int i = 0; i < count; i++) {
|
||
//判断是否要加入values或是archiveValues
|
||
if (GetUnitYXIsForceArchive(uid, i))
|
||
{
|
||
archiveValues[(const char *)config.units[uid].yxs[i].name] = GetUnitYX(uid, i);
|
||
}
|
||
else
|
||
{
|
||
values[(const char *)config.units[uid].yxs[i].name] = GetUnitYX(uid, i);
|
||
}
|
||
}
|
||
}
|
||
Json::Int64 datatime = (Json::Int64)time(NULL);
|
||
datatime *= 1000;
|
||
root["dataTime"] = datatime;
|
||
root["deviceId"] = static_units[uid].deviceId;
|
||
// root["isStore"] = config.units[uid].state & 0x20 ? false : true;
|
||
if (values.size()) {
|
||
root["values"] = values;
|
||
}
|
||
if (archiveValues.size()) {
|
||
root["archiveValues"] = archiveValues;
|
||
}
|
||
return publish_sensor_data("", "stateData", root);
|
||
}
|
||
|
||
bool CRYDevice::publishdeviceEventData(void)
|
||
{
|
||
int i;
|
||
int uid;
|
||
int yxbw_point;
|
||
BOOLEAN yxbw_value;
|
||
unionCP56Time yxbw_time;
|
||
|
||
Json::Value root;
|
||
Json::Value value;
|
||
|
||
for (i = 0; i < DATABASE_YXBW_NUM; i++)
|
||
{
|
||
yxbw_point = GetUnitYXBW(uid, yxbw_value, yxbw_time);
|
||
if (yxbw_point < 0) break;
|
||
value["deviceId"] = static_units[uid].deviceId;
|
||
value["attrCode"] = (const char *)config.units[uid].yxs[yxbw_point].name;
|
||
value["attrValue"] = yxbw_value;
|
||
value["eventType"] = 0;
|
||
Json::Int64 eventTime = (Json::Int64)time(NULL);
|
||
eventTime *= 1000;
|
||
value["eventTime"] = eventTime;
|
||
value["limitValue"] = Json::Value::null;
|
||
|
||
root.append(value);
|
||
}
|
||
|
||
int ycbw_point;
|
||
int ycbw_type;
|
||
LONG ycbw_value;
|
||
unionCP56Time ycbw_time;
|
||
|
||
for (i = 0; i < DATABASE_YCBW_NUM; i++)
|
||
{
|
||
ycbw_point = GetUnitYCBW(uid, ycbw_value, ycbw_type, ycbw_time);
|
||
if (ycbw_point < 0) break;
|
||
value["deviceId"] = static_units[uid].deviceId;
|
||
value["attrCode"] = (const char *)config.units[uid].ycs[ycbw_point].name;
|
||
value["attrValue"] = GetUnitYCRealFromValue(uid, ycbw_point, ycbw_value);
|
||
value["eventType"] = ycbw_type;
|
||
Json::Int64 eventTime = (Json::Int64)time(NULL);
|
||
eventTime *= 1000;
|
||
value["eventTime"] = eventTime;
|
||
value["limitValue"] = GetUnitYCLimitReal(uid, ycbw_point, ycbw_type);
|
||
|
||
root.append(value);
|
||
}
|
||
|
||
if (root.size()) {
|
||
return publish_sensor_data("", "deviceEvent", root);
|
||
}
|
||
|
||
return FALSE;
|
||
}
|
||
|
||
int CRYDevice::sendMsg(std::string sMessage)
|
||
{
|
||
if (NULL != m_ws && m_ws->getReadyState() == easywsclient::WebSocket::OPEN)
|
||
{
|
||
pthread_mutex_lock(&m_SendQueueMutex);
|
||
m_SendQueue.push(sMessage);
|
||
pthread_mutex_unlock(&m_SendQueueMutex);
|
||
|
||
return (int)sMessage.length();
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
int CRYDevice::recvMsg(std::string sMessage)
|
||
{
|
||
pthread_mutex_lock(&m_RecvQueueMutex);
|
||
m_recvMsgQueue.push(sMessage);
|
||
pthread_mutex_unlock(&m_RecvQueueMutex);
|
||
|
||
return (int)sMessage.length();
|
||
}
|
||
|
||
BOOLEAN CRYDevice::ry_init(const char *host, const int port, const char *nodeId, const char *version)
|
||
{
|
||
snprintf(m_host, sizeof(m_host), "%s", host);
|
||
snprintf(m_nodeId, sizeof(m_nodeId), "%s", nodeId);
|
||
snprintf(m_version, sizeof(m_version), "%s", version);
|
||
m_port = port;
|
||
|
||
m_nCurUnit = 0;
|
||
m_status = 2; //0 - 离线, 1 - 在线, 2 - 未配置
|
||
m_dataAcquisitionReload = false;
|
||
|
||
unitname2service_map.clear();
|
||
for (int i = 0; i < UNIT_NUM; i++) {
|
||
if (config.units[i].state != TRUE) continue;
|
||
if ((config.units[i].type & 0x0f) == SLAVER_UNIT) continue;
|
||
std::string unit_id = static_units[i].deviceId;
|
||
name2servicemap name2service_map;
|
||
for (int j = 0; j < config.units[i].ykcount; j++) {
|
||
std::string name = config.units[i].yks[j].name;
|
||
if (name2service_map.find(name) == name2service_map.end()) {
|
||
struct_service_item item;
|
||
item.type = CMD_CONTROL_OPERATION;
|
||
item.uid = i;
|
||
item.point = j;
|
||
item.order = config.units[i].yks[j].order;
|
||
name2service_map.insert(name2servicemap::value_type(name, item));
|
||
} else {
|
||
vLog(LOG_WARN, "同一个设备不能有两个相同的遥控名<%s>,请检查。\n", name.c_str());
|
||
}
|
||
}
|
||
for (int j = 0; j < config.units[i].ytcount; j++) {
|
||
std::string name = config.units[i].yts[j].name;
|
||
if (name2service_map.find(name) == name2service_map.end()) {
|
||
struct_service_item item;
|
||
item.type = CMD_CONTROL_SETTING;
|
||
item.uid = i;
|
||
item.point = j;
|
||
item.order = config.units[i].yts[j].order;
|
||
name2service_map.insert(name2servicemap::value_type(name, item));
|
||
} else {
|
||
vLog(LOG_WARN, "同一个设备不能有两个相同的遥调名<%s>,请检查。\n", name.c_str());
|
||
}
|
||
}
|
||
if (unitname2service_map.find(unit_id) == unitname2service_map.end()) {
|
||
unitname2service_map.insert(unitname2servicemap::value_type(unit_id, name2service_map));
|
||
} else {
|
||
vLog(LOG_WARN, "系统配置了两个相同的设备ID<%s>,请检查。\n", unit_id.c_str());
|
||
}
|
||
}
|
||
m_status = 1;
|
||
|
||
#ifndef USE_NOPOLL_WEBSOCKET
|
||
char url[560];
|
||
snprintf(url, sizeof(url), "ws://%s:%d/node/%s/%s", m_host, m_port, m_nodeId, m_version);
|
||
m_ws = WebSocket::from_url(url);
|
||
last_connect_sec = system32.timers;
|
||
#else
|
||
ctx = nopoll_ctx_new();
|
||
nopoll_log_enable(ctx, nopoll_false);
|
||
nopoll_log_color_enable(ctx, nopoll_false);
|
||
if (ctx != NULL) {
|
||
nopoll_conn_connect_timeout(ctx, 50000);
|
||
char url[512];
|
||
char cPort[64];
|
||
snprintf(url, sizeof(url), "/node/%s/%s", m_nodeId, m_version);
|
||
snprintf(cPort, sizeof(cPort), "%d", m_port);
|
||
vLog(LOG_DEBUG, "%d here to connect:%s:%s.\n", __LINE__, m_host, cPort);
|
||
conn = nopoll_conn_new(ctx, m_host, cPort, NULL, url, NULL, NULL);
|
||
g_conn = conn;
|
||
}
|
||
#endif
|
||
return TRUE;
|
||
}
|
||
|
||
bool CRYDevice::ry_run(void)
|
||
{
|
||
#ifndef USE_NOPOLL_WEBSOCKET
|
||
if (m_ws != NULL)
|
||
{
|
||
if (m_ws->getReadyState() != WebSocket::CLOSED)
|
||
{
|
||
CRYDevice *rydevice = this;
|
||
std::string data;
|
||
m_ws->poll();
|
||
m_ws->dispatch([rydevice](const std::string & message) {
|
||
//printf(">>> %s\n", message.c_str());
|
||
rydevice->on_message((const char *)message.c_str(), message.length());
|
||
});
|
||
//如果有数据待发
|
||
if (!m_SendQueue.empty())
|
||
{
|
||
std::string sMessage;
|
||
while (1)
|
||
{
|
||
pthread_mutex_lock(&m_SendQueueMutex);
|
||
if (!m_SendQueue.empty())
|
||
{
|
||
sMessage = m_SendQueue.front();
|
||
m_SendQueue.pop();
|
||
}
|
||
else
|
||
{
|
||
pthread_mutex_unlock(&m_SendQueueMutex);
|
||
break;
|
||
}
|
||
pthread_mutex_unlock(&m_SendQueueMutex);
|
||
m_ws->send(sMessage);
|
||
}
|
||
}
|
||
}
|
||
else if (m_ws->getReadyState() == WebSocket::CLOSED)
|
||
{
|
||
delete m_ws;
|
||
m_ws = NULL;
|
||
char url[560];
|
||
snprintf(url, sizeof(url), "ws://%s:%d/node/%s/%s", m_host, m_port, m_nodeId, m_version);
|
||
m_ws = WebSocket::from_url(url);
|
||
last_connect_sec = system32.timers;
|
||
vLog(LOG_DEBUG, "here reconnect.\n");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (last_connect_sec > 0 && system32.timers > (last_connect_sec + 8)) {
|
||
char url[560];
|
||
snprintf(url, sizeof(url), "ws://%s:%d/node/%s/%s", m_host, m_port, m_nodeId, m_version);
|
||
m_ws = WebSocket::from_url(url);
|
||
last_connect_sec = system32.timers;
|
||
vLog(LOG_DEBUG, "here reconnect.\n");
|
||
}
|
||
}
|
||
#else
|
||
nopoll_bool isOk = nopoll_conn_is_ready(conn);
|
||
if (isOk) {
|
||
last_connect_sec = system32.timers;
|
||
|
||
while ((msg[msg_count] = nopoll_conn_get_msg(conn)) != NULL) {
|
||
#if 1
|
||
vLog(LOG_DEBUG, "recv %d length = %d, final is: %d, fragment is: %d\n", msg_count, nopoll_msg_get_payload_size(msg[msg_count]), nopoll_msg_is_final(msg[msg_count]), nopoll_msg_is_fragment(msg[msg_count]));
|
||
#endif
|
||
if (nopoll_msg_is_final(msg[msg_count])) {
|
||
msg_count++;
|
||
if (msg_count > 0) {
|
||
int buffer_len;
|
||
BYTE *buffer = NULL;
|
||
vLog(LOG_DEBUG, "here... msg count is: %d\n", msg_count);
|
||
if (websocket_msg_join(msg, msg_count, buffer, buffer_len)) {
|
||
if (buffer) {
|
||
on_message((const char *)buffer, buffer_len);
|
||
delete [] buffer;
|
||
buffer = NULL;
|
||
}
|
||
for (int i = 0; i < msg_count; i++) {
|
||
nopoll_msg_unref(msg[i]);
|
||
msg_count = 0;
|
||
}
|
||
}
|
||
}
|
||
break;
|
||
} else {
|
||
msg_count++;
|
||
}
|
||
}
|
||
} else {
|
||
if (last_connect_sec > 0 && system32.timers > (last_connect_sec + 8)) {
|
||
nopoll_conn_connect_timeout(ctx, 50000);
|
||
char url[512];
|
||
char cPort[64];
|
||
snprintf(url, sizeof(url), "/node/%s/%s", m_nodeId, m_version);
|
||
snprintf(cPort, sizeof(cPort), "%d", m_port);
|
||
vLog(LOG_DEBUG, "%d here to connect:%s:%s.\n", __LINE__, m_host, cPort);
|
||
conn = nopoll_conn_new(ctx, m_host, cPort, NULL, url, NULL, NULL);
|
||
g_conn = conn;
|
||
last_connect_sec = system32.timers;
|
||
}
|
||
}
|
||
#endif
|
||
BOOLEAN sec_changed = FALSE;
|
||
if (last_sec != (time_t)system32.timers) {
|
||
last_sec = system32.timers;
|
||
sec_changed = TRUE;
|
||
}
|
||
if (sec_changed) {
|
||
if ((last_sec % 5) == 0) {
|
||
heart_beat(m_status);
|
||
}
|
||
}
|
||
publishdeviceEventData();
|
||
|
||
for (int i = 0; i < UNIT_NUM; i++)
|
||
{
|
||
if ((config.units[i].state & 0x01) != TRUE) continue;
|
||
if (config.units[i].type != MASTER_UNIT) continue;
|
||
MakeYKFrame(i);
|
||
MakeYTFrame(i);
|
||
if (sec_changed) {
|
||
publishAnalogData(i);
|
||
publishStateData(i);
|
||
}
|
||
usleep(300);
|
||
}
|
||
|
||
return m_dataAcquisitionReload;
|
||
}
|
||
|
||
void CRYDevice::ry_destroy(void)
|
||
{
|
||
#ifdef USE_NOPOLL_WEBSOCKET
|
||
nopoll_conn_close(conn);
|
||
nopoll_ctx_unref(ctx);
|
||
#endif
|
||
}
|