106 lines
3.4 KiB
C++
106 lines
3.4 KiB
C++
|
#include "UPSClient.h"
|
||
|
#include "../utils/StringHelper.h"
|
||
|
#include "UpsComand.h"
|
||
|
|
||
|
UPSClient::UPSClient(CommunicationCfg* pconfig) :TcpClient(pconfig)
|
||
|
{
|
||
|
m_Freq = 500;
|
||
|
}
|
||
|
|
||
|
|
||
|
UPSClient::~UPSClient()
|
||
|
{
|
||
|
Shutdown();
|
||
|
//OutputDebugString("ups close\n");
|
||
|
}
|
||
|
|
||
|
|
||
|
void UPSClient::InitCommand() {
|
||
|
Command* pCommand = new UpsComand("Q1\r");
|
||
|
pCommand->m_Fun = &UPSClient::ProcInfo;
|
||
|
pCommand->m_Ref = this;
|
||
|
pCommand->isNeedDel = false;
|
||
|
m_CycleCommands.push_back(pCommand);
|
||
|
}
|
||
|
|
||
|
|
||
|
void UPSClient::ShutDownUps(float minute)
|
||
|
{
|
||
|
if (minute < 0.2f || minute>10)return;
|
||
|
char buffer[10];
|
||
|
if (minute < 1.0f)
|
||
|
{
|
||
|
int tempv = (int)(minute * 10 + 0.5);
|
||
|
sprintf_s(buffer, sizeof(buffer), "S.%d\r", tempv);
|
||
|
}
|
||
|
else sprintf_s(buffer, sizeof(buffer), "S%f\r", minute);
|
||
|
EnterCriticalSection(&m_RtcCS);
|
||
|
Command* pCommand = new UpsComand(string(buffer));
|
||
|
pCommand->m_Fun = &UPSClient::ProcShutDown;
|
||
|
pCommand->m_Ref = this;
|
||
|
pCommand->isNeedRead = false;
|
||
|
m_RTCommands.push(pCommand);
|
||
|
LeaveCriticalSection(&m_RtcCS);
|
||
|
}
|
||
|
|
||
|
void UPSClient::ProcInfo(void* pobject, Command* pcommand)
|
||
|
{
|
||
|
if (pobject == NULL)return;
|
||
|
UPSClient* pUw = (UPSClient*)pobject;
|
||
|
unsigned char* rseq = pcommand->m_RespSeq;
|
||
|
unsigned int dlength = pcommand->m_RespLen;
|
||
|
|
||
|
float inputVol = (float)((rseq[1] - '0') * 1000 + (rseq[2] - '0') * 100 + (rseq[3] - '0') * 10 + (rseq[5] - '0')) / 10.0f;
|
||
|
float lastVol = (float)((rseq[7] - '0') * 1000 + (rseq[8] - '0') * 100 + (rseq[9] - '0') * 10 + (rseq[11] - '0')) / 10.0f;
|
||
|
float outputVol = (float)((rseq[13] - '0') * 1000 + (rseq[14] - '0') * 100 + (rseq[15] - '0') * 10 + (rseq[17] - '0')) / 10.0f;
|
||
|
int outputLoad = (rseq[19] - '0') * 100 + (rseq[20] - '0') * 10 + (rseq[21] - '0');
|
||
|
float outputF = (float)((rseq[23] - '0') * 100 + (rseq[24] - '0') * 10 + (rseq[26] - '0')) / 10.0f;
|
||
|
float unitVol = (float)((rseq[28] - '0') * 100 + (rseq[30] - '0') * 10 + (rseq[31] - '0')) / 100.0f;
|
||
|
float tempValue = (float)((rseq[33] - '0') * 100 + (rseq[34] - '0') * 10 + (rseq[36] - '0')) / 10.0f;
|
||
|
|
||
|
EnterCriticalSection(&pUw->m_ValueCS);
|
||
|
pUw->m_Stat.inputVol = inputVol;
|
||
|
pUw->m_Stat.lastVol = lastVol;
|
||
|
pUw->m_Stat.outputVol = outputVol;
|
||
|
pUw->m_Stat.outputLoad = outputLoad;
|
||
|
pUw->m_Stat.outputF = outputF;
|
||
|
pUw->m_Stat.unitVol = unitVol;
|
||
|
pUw->m_Stat.tempValue = tempValue;
|
||
|
|
||
|
pUw->m_Stat.isVolError = (rseq[38] == '1' ? true : false);
|
||
|
pUw->m_Stat.isBatteryVolLow = (rseq[39] == '1' ? true : false);
|
||
|
pUw->m_Stat.isBypassMode = (rseq[40] == '1' ? true : false);
|
||
|
pUw->m_Stat.isUpsError = (rseq[41] == '1' ? true : false);
|
||
|
pUw->m_Stat.upsType = (rseq[42] == '1' ? true : false);
|
||
|
pUw->m_Stat.isTesting = (rseq[43] == '1' ? true : false);
|
||
|
pUw->m_Stat.isShutdown = (rseq[44] == '1' ? true : false);
|
||
|
|
||
|
LeaveCriticalSection(&pUw->m_ValueCS);
|
||
|
}
|
||
|
|
||
|
void UPSClient::ProcShutDown(void* pobject, Command* pcommand)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
void UPSClient::GetStat(Upsstat& stat)
|
||
|
{
|
||
|
EnterCriticalSection(&m_ValueCS);
|
||
|
stat.baseStat = m_BaseStat;
|
||
|
stat.inputVol = m_Stat.inputVol;
|
||
|
stat.isBatteryVolLow = m_Stat.isBatteryVolLow;
|
||
|
stat.isBypassMode = m_Stat.isBypassMode;
|
||
|
stat.isShutdown = m_Stat.isShutdown;
|
||
|
stat.isTesting = m_Stat.isTesting;
|
||
|
stat.isUpsError = m_Stat.isUpsError;
|
||
|
stat.isVolError = m_Stat.isVolError;
|
||
|
stat.lastVol = m_Stat.lastVol;
|
||
|
stat.outputF = m_Stat.outputF;
|
||
|
stat.outputLoad = m_Stat.outputLoad;
|
||
|
stat.outputVol = m_Stat.outputVol;
|
||
|
stat.tempValue = m_Stat.tempValue;
|
||
|
stat.unitVol = m_Stat.unitVol;
|
||
|
stat.upsType = m_Stat.upsType;
|
||
|
LeaveCriticalSection(&m_ValueCS);
|
||
|
}
|