This commit is contained in:
zhouhuang 2024-12-11 10:30:30 +08:00
parent 3566343b62
commit cc52b06ef2
9 changed files with 76 additions and 43 deletions

View File

@ -250,6 +250,7 @@ int main(int argc, char** argv)
config.processes[i].option.network.target_addr = INADDR_ANY;
config.units[uid].state = TRUE;
#endif
unsigned int m_runCount = 0;
unsigned int count = 0;

View File

@ -2927,6 +2927,7 @@ bool CRYDevice::publishAnalogData(int uid)
if (archiveValues.size()) {
root["archiveValues"] = archiveValues;
}
vLog(LOG_DEBUG, "publish unit<%d> data analogData, and deviceId is: %s\n", uid, static_units[uid].deviceId);
return publish_sensor_data("", "analogData", root);
}
@ -3027,6 +3028,28 @@ void CRYDevice::releaseAllUnits(void)
}
}
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);
@ -3034,7 +3057,8 @@ BOOLEAN CRYDevice::ry_init(const char *host, const int port, const char *nodeId,
snprintf(m_version, sizeof(m_version), "%s", version);
m_port = port;
status = 2; //0 - 离线, 1 - 在线, 2 - 未配置
m_nCurUnit = 0;
m_status = 2; //0 - 离线, 1 - 在线, 2 - 未配置
m_dataAcquisitionReload = false;
unitname2service_map.clear();
@ -3075,7 +3099,7 @@ BOOLEAN CRYDevice::ry_init(const char *host, const int port, const char *nodeId,
vLog(LOG_WARN, "系统配置了两个相同的设备ID<%s>,请检查。\n", unit_id.c_str());
}
}
status = 1;
m_status = 1;
#ifndef USE_NOPOLL_WEBSOCKET
char url[560];
@ -3101,28 +3125,6 @@ BOOLEAN CRYDevice::ry_init(const char *host, const int port, const char *nodeId,
return TRUE;
}
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();
}
bool CRYDevice::ry_run(void)
{
#ifndef USE_NOPOLL_WEBSOCKET
@ -3234,12 +3236,21 @@ bool CRYDevice::ry_run(void)
}
if (sec_changed) {
if ((last_sec % 20) == 0) {
heart_beat(status);
heart_beat(m_status);
}
}
publishdeviceEventData();
for (int i = 0; i < UNIT_NUM; i++) {
if ((config.units[i].state & 0x01) != TRUE) continue;
int i = 0;
#if 0
for (int i = 0; i < UNIT_NUM; i++)
{
#else
i = m_nCurUnit;
m_nCurUnit++;
#endif
do {
if ((config.units[i].state & 0x01) != TRUE) break;
if (config.units[i].type != MASTER_UNIT) break;
MakeYKFrame(i);
MakeYTFrame(i);
if (sec_changed) {
@ -3247,6 +3258,10 @@ bool CRYDevice::ry_run(void)
publishStateData(i);
}
}
while (0);
#if 0
}
#endif
return m_dataAcquisitionReload;
}

View File

@ -120,7 +120,8 @@ private:
#endif
DWORD last_connect_sec = 0;
int status;
int m_status;
int m_nCurUnit;
bool m_dataAcquisitionReload = false;

View File

@ -842,6 +842,7 @@ BOOLEAN CHostADSBFProcess::OnPreCreate(int id)
//vLog(LOG_DEBUG, "%s local ip is: %s, netid is: %s, remote ip is: %s, and netid is: %s\n", m_pidName.c_str(), m_localIp.c_str(), m_localNetId.c_str(), m_remoteIp.c_str(), m_remoteNetId.c_str());
m_turbine = NULL;
m_bRouteAdded = FALSE;
SetLocalAmsNetId(AmsNetId(m_localNetId));
#if 0
m_threadRun = TRUE;
@ -930,15 +931,19 @@ BOOLEAN CHostADSBFProcess::OnTimer(void)
}
#else
//先添加一条路由
if (m_bRouteAdded == FALSE)
{
long ret = AddRemoteRoute(m_remoteIp, m_localNetId, m_localIp, std::string("isoftstone"), std::string("admin"), std::string("admin"));
vLog(LOG_DEBUG, "bbb %s add route return value is: %d\n", m_pidName.c_str(), ret);
vLog(LOG_DEBUG, "%s add route return value is: %d\n", m_pidName.c_str(), ret);
if (ret != 0) return TRUE;
}
m_bRouteAdded = TRUE;
//添加成功
m_turbine = new AdsDevice{m_remoteIp, AmsNetId(m_remoteNetId), m_remotePort};
const auto state = m_turbine->GetState();
if ((uint16_t)state.ads >= ADSSTATE::ADSSTATE_MAXSTATES || (uint16_t)state.device >= ADSSTATE::ADSSTATE_MAXSTATES)
{
vLog(LOG_DEBUG, "read device state error.\n");
vLog(LOG_DEBUG, "%s read device state error.\n", m_pidName.c_str());
delete m_turbine;
m_turbine = NULL;
}

View File

@ -45,6 +45,7 @@ private:
std::string m_remoteNetId;
AdsDevice *m_turbine;
BOOLEAN m_bRouteAdded; //路由是否添加成功
int m_total_length;
DWORD last_sec;

View File

@ -33,7 +33,7 @@ AdsDevice::AdsDevice(const std::string& ipV4, AmsNetId amsNetId, uint16_t port)
m_LocalPort(new long { OpenLocalPort() }, { CloseLocalPort }),
m_Connected(false)
{
if(*m_NetId.get() == amsNetId) {
if (*m_NetId.get() == amsNetId) {
m_Connected = true;
}
}
@ -47,6 +47,15 @@ AdsDevice::~AdsDevice()
}
}
void AdsDevice::DisconnectDevice()
{
if (m_Connected == true)
{
DisconnectTarget(*m_NetId.get());
m_Connected = false;
}
}
long AdsDevice::DeleteNotificationHandle(uint32_t handle) const
{
if (handle) {
@ -239,8 +248,7 @@ AdsHandle AdsDevice::OpenFile(const std::string& filename, const uint32_t flags)
long AdsDevice::ReadReqEx2(uint32_t group, uint32_t offset, size_t length, void* buffer, uint32_t* bytesRead) const
{
if(m_Connected == false)
return ROUTERERR_HOSTDENY;
if (m_Connected == false) return ROUTERERR_HOSTDENY;
if (length > std::numeric_limits<uint32_t>::max()) {
return ADSERR_DEVICE_INVALIDSIZE;

View File

@ -100,6 +100,7 @@ struct AdsDevice {
const AmsAddr m_Addr;
bool IsConnected() const { return m_Connected; }
void DisconnectDevice();
private:
AdsResource<const long> m_LocalPort;
long CloseFile(uint32_t handle) const;

View File

@ -63,6 +63,7 @@ struct AdsVariable {
if (error || (size != bytesRead)) {
LOG_ERROR("AdsVariable read failed: "<< std::dec << error);
((AdsDevice&)m_Route).DisconnectDevice();
//throw AdsException(error);
}
}
@ -73,6 +74,7 @@ struct AdsVariable {
auto error = m_Route.WriteReqEx(m_IndexGroup, *m_Handle, size, data);
if (error) {
LOG_ERROR("AdsVariable write failed:" << std::dec << error);
((AdsDevice&)m_Route).DisconnectDevice();
//throw AdsException(error);
}
}

View File

@ -153,8 +153,7 @@ AmsResponse* AmsConnection::Write(AmsRequest& request, const AmsAddr srcAddr)
long AmsConnection::SendRequest(AmsRequest& request, const uint32_t timeout)
{
if(IsConnected() == false)
return -1;
if (IsConnected() == false) return -1;
AmsAddr srcAddr;
const auto status = router.GetAmsAddr(request.srcPort, &srcAddr);