187 lines
5.8 KiB
C++
187 lines
5.8 KiB
C++
#include "ScannerPowerClient.h"
|
|
#include "Modbus.h"
|
|
//#include "../ScannerCtrl/BaseCtrl.h"
|
|
#include "../Logger.h"
|
|
#include "../config/ConfigManager.h"
|
|
#include "../PLC/SignalService.h"
|
|
#include "../utils/DataByte.h"
|
|
|
|
ScannerPowerClient::ScannerPowerClient(CommunicationCfg* pconfig) :TcpClient(pconfig)
|
|
{
|
|
m_RunCfg = ConfigManager::GetInstance()->GetRunCfg();
|
|
m_ExtCfg = ConfigManager::GetInstance()->GetExtCfg();
|
|
m_VoltageAlarmCfg = ConfigManager::GetInstance()->GetAlarmCfg()->m_ScannerVoltageAlarm;
|
|
|
|
|
|
}
|
|
|
|
|
|
ScannerPowerClient::~ScannerPowerClient()
|
|
{
|
|
Shutdown();
|
|
|
|
for (map<unsigned char, ScannerPowerBean*>::iterator it = m_Datas.begin(); it != m_Datas.end(); it++) {
|
|
ScannerPowerBean* bean = it->second;
|
|
delete bean;
|
|
}
|
|
m_Datas.clear();
|
|
}
|
|
|
|
|
|
void ScannerPowerClient::Init()
|
|
{
|
|
size_t scsize = ConfigManager::GetInstance()->GetMatchScannerControlCfg()->size();
|
|
#ifdef _DEBUG
|
|
//size_t scsize = 4;
|
|
#endif
|
|
for (size_t i = 0; i < scsize; i++) {
|
|
if (m_Datas.find(i + 1) == m_Datas.end()) {
|
|
m_Datas[i + 1] = new ScannerPowerBean();
|
|
|
|
InsertMp(&m_Datas[i + 1]->m_Voltage, 1,"_"+to_string(i+1));
|
|
InsertMp(&m_Datas[i + 1]->m_Current, 1,"_"+to_string(i+1));
|
|
InsertMp(&m_Datas[i + 1]->m_VoltageAlarm, 1,"_"+to_string(i+1));
|
|
}
|
|
}
|
|
InitCommand();
|
|
}
|
|
|
|
void ScannerPowerClient::InitCommand()
|
|
{
|
|
if (m_ExtCfg->m_ScanPowerType == ExtCfg::JSY_V0)
|
|
{
|
|
for (map<unsigned char, ScannerPowerBean*>::iterator it = m_Datas.begin(); it != m_Datas.end(); it++) {
|
|
Command* pPointInfo = new ReadModbus(it->first, 0x0048, 2);
|
|
pPointInfo->m_Fun = &ScannerPowerClient::PorcInfo;
|
|
pPointInfo->m_Ref = this;
|
|
pPointInfo->isNeedDel = false;
|
|
m_CycleCommands.push_back(pPointInfo);
|
|
}
|
|
}
|
|
else if (m_ExtCfg->m_ScanPowerType == ExtCfg::JSY_V1) {
|
|
for (map<unsigned char, ScannerPowerBean*>::iterator it = m_Datas.begin(); it != m_Datas.end(); it++) {
|
|
Command* pPointInfo = new ReadModbus(it->first, 0x0100, 4);
|
|
pPointInfo->m_Fun = &ScannerPowerClient::PorcInfo2;
|
|
pPointInfo->m_Ref = this;
|
|
pPointInfo->isNeedDel = false;
|
|
m_CycleCommands.push_back(pPointInfo);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void ScannerPowerClient::PorcInfo(void* pobject, Command* pcommand)
|
|
{
|
|
if (pobject == NULL)return;
|
|
ScannerPowerClient* spc = (ScannerPowerClient*)pobject;
|
|
unsigned char* rseq = pcommand->m_RespSeq;
|
|
unsigned char addr = rseq[0];
|
|
if (spc->m_Datas.find(addr)==spc->m_Datas.end()) {
|
|
return;
|
|
}
|
|
int voltage = ((rseq[3] & 0xff) << 8) + (rseq[4] & 0xff);
|
|
int current = ((rseq[5] & 0xff) << 8) + (rseq[6] & 0xff);
|
|
float fvoltage= (float)voltage / 100.0f;
|
|
float fcurrent= (float)current / 1000.0f;
|
|
uint64_t tnow = GetTickCount64();
|
|
ScannerPowerBean* bean= spc->m_Datas[addr];
|
|
EnterCriticalSection(&spc->m_ValueCS);
|
|
bean->m_Voltage->SetValue(fvoltage);
|
|
bean->m_Current->SetValue(fcurrent);
|
|
if (fvoltage<spc->m_RunCfg->m_ScannerVoltageMinLimit->GetValue() || fvoltage > spc->m_RunCfg->m_ScannerVoltageMaxLimit->GetValue()) {
|
|
if (tnow - bean->m_VoltageAlarmTick > 3000) {
|
|
bean->m_VoltageAlarm->SetValue(true);
|
|
}
|
|
}
|
|
else {
|
|
bean->m_VoltageAlarmTick = tnow;
|
|
bean->m_VoltageAlarm->SetValue(false);
|
|
}
|
|
SignalService& alarmService = SignalService::GetInstance();
|
|
if (spc->m_VoltageAlarmCfg)alarmService.SetAlarm(spc->m_VoltageAlarmCfg, bean->m_VoltageAlarm);
|
|
LeaveCriticalSection(&spc->m_ValueCS);
|
|
|
|
//if (BaseCtrl::IsStart())
|
|
{
|
|
if (tnow - bean->m_LogTick > spc->m_RunCfg->m_ScannerPowerLogTick->GetValue()) {
|
|
bean->m_Log->m_VoltageDatas.push_back(fvoltage);
|
|
bean->m_Log->m_CurrentDatas.push_back(fcurrent);
|
|
bean->m_LogTick = tnow;
|
|
}
|
|
if (bean->m_Log->m_VoltageDatas.size()>=spc->m_RunCfg->m_ScannerPowerLogCount->GetValue()) {
|
|
time(&bean->m_Log->m_InsertTime);
|
|
bean->m_Log->m_JobId = spc->m_JobId;
|
|
bean->m_Log->m_Sno = addr;
|
|
g_log->m_LogDao->SaveScannerPower(bean->m_Log);
|
|
bean->m_Log->m_VoltageDatas.clear();
|
|
bean->m_Log->m_CurrentDatas.clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
void ScannerPowerClient::PorcInfo2(void* pobject, Command* pcommand)
|
|
{
|
|
if (pobject == NULL)return;
|
|
ScannerPowerClient* spc = (ScannerPowerClient*)pobject;
|
|
unsigned char* rseq = pcommand->m_RespSeq;
|
|
unsigned char addr = rseq[0];
|
|
if (spc->m_Datas.find(addr) == spc->m_Datas.end()) {
|
|
return;
|
|
}
|
|
|
|
INTDATA voltage;
|
|
INTDATA current;
|
|
voltage.data[0] = rseq[6];
|
|
voltage.data[1] = rseq[5];
|
|
voltage.data[2] = rseq[4];
|
|
voltage.data[3] = rseq[3];
|
|
|
|
current.data[0] = rseq[10];
|
|
current.data[1] = rseq[9];
|
|
current.data[2] = rseq[8];
|
|
current.data[3] = rseq[7];
|
|
|
|
float fvoltage = (float)voltage.iValue / 10000.0f;
|
|
float fcurrent = (float)current.iValue / 10000.0f;
|
|
uint64_t tnow = GetTickCount64();
|
|
ScannerPowerBean* bean = spc->m_Datas[addr];
|
|
EnterCriticalSection(&spc->m_ValueCS);
|
|
bean->m_Voltage->SetValue(fvoltage);
|
|
bean->m_Current->SetValue(fcurrent);
|
|
if (fvoltage<spc->m_RunCfg->m_ScannerVoltageMinLimit->GetValue() || fvoltage > spc->m_RunCfg->m_ScannerVoltageMaxLimit->GetValue()) {
|
|
if (tnow - bean->m_VoltageAlarmTick > 3000) {
|
|
bean->m_VoltageAlarm->SetValue(true);
|
|
}
|
|
}
|
|
else {
|
|
bean->m_VoltageAlarmTick = tnow;
|
|
bean->m_VoltageAlarm->SetValue(false);
|
|
}
|
|
SignalService& alarmService = SignalService::GetInstance();
|
|
if (spc->m_VoltageAlarmCfg)alarmService.SetAlarm(spc->m_VoltageAlarmCfg, bean->m_VoltageAlarm);
|
|
LeaveCriticalSection(&spc->m_ValueCS);
|
|
|
|
//if (BaseCtrl::IsStart())
|
|
{
|
|
|
|
if (tnow - bean->m_LogTick > spc->m_RunCfg->m_ScannerPowerLogTick->GetValue()) {
|
|
bean->m_Log->m_VoltageDatas.push_back(fvoltage);
|
|
bean->m_Log->m_CurrentDatas.push_back(fcurrent);
|
|
bean->m_LogTick = tnow;
|
|
}
|
|
if (bean->m_Log->m_VoltageDatas.size() >= spc->m_RunCfg->m_ScannerPowerLogCount->GetValue()) {
|
|
time(&bean->m_Log->m_InsertTime);
|
|
bean->m_Log->m_JobId = spc->m_JobId;
|
|
bean->m_Log->m_Sno = addr;
|
|
g_log->m_LogDao->SaveScannerPower(bean->m_Log);
|
|
bean->m_Log->m_VoltageDatas.clear();
|
|
bean->m_Log->m_CurrentDatas.clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
void ScannerPowerClient::ResetCycleCommand()
|
|
{
|
|
ClearCycleCommand();
|
|
InitCommand();
|
|
} |