123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- #include "ups_impl.h"
- #include <cstring>
- #include <cstdio>
- #include "log4vendor.h"
- UpsClassImpl::UpsClassImpl()
- :m_mode(0)
- {
- pObj = new OUpsDev();
- }
- UpsClassImpl::~UpsClassImpl()
- {
- if(pObj)
- delete pObj;
- }
- void UpsClassImpl::ZeroDevErrorInfo()
- {
- memset(&m_DevErrorInfo, 0, sizeof(m_DevErrorInfo));
- }
- ErrorCodeEnum UpsClassImpl::DevOpen(DWORD dwPort, DWORD dwBaudRate)
- {
- LOG4VTM_FUNCTION();
- LOG4VTM(INFO,__FUNCTION__);
- ErrorCodeEnum err = Error_Succeed;
- tDevReturn l_tDevReturn[8] = { 0 };
- int ret = pObj->iSetCommPara(l_tDevReturn);
- if(ret != 0)
- {
- FormatDevErrorInfo(__FUNCTION__, __LINE__, l_tDevReturn[0].iLogicCode, "iSetCommPara Failed");
- return Error_DevConnFailed;
- }
- memset(l_tDevReturn,0,sizeof(tDevReturn)*8);
- ret = pObj->iInit(l_tDevReturn);
- if(ret != 0)
- {
- FormatDevErrorInfo(__FUNCTION__, __LINE__, l_tDevReturn[0].iLogicCode, "iInit Failed");
- return Error_DevConnFailed;
- }
- LOG4VTM(INFO,__FUNCTION__<<" OK");
- return err;
- }
- ErrorCodeEnum UpsClassImpl::GetDevCategory(DevCategoryInfo &devCategory)
- {
- LOG4VTM_FUNCTION();
- LOG4VTM(INFO,__FUNCTION__);
- ErrorCodeEnum err = Error_Succeed;
- const char *pType = "PVER=Cyber#MID=UT600Eb";
- const char *pMode = "FWID=0";
- const char *pVendor = "grg";
- strcpy(devCategory.szType, pType);
- strcpy(devCategory.szModel, pMode);
- strcpy(devCategory.szVendor, pVendor);
- devCategory.version.wMajor = 1;
- devCategory.version.wMinor = 1;
- devCategory.version.wRevision = 65535;
- devCategory.version.wBuild = 1;
- LOG4VTM(INFO,__FUNCTION__<<" OK");
- return err;
- }
- ErrorCodeEnum UpsClassImpl::Reset()
- {
- LOG4VTM_FUNCTION();
- LOG4VTM(INFO,__FUNCTION__);
- ErrorCodeEnum err = Error_Succeed;
- LOG4VTM(INFO,__FUNCTION__<<" OK");
- return err;
- }
- ErrorCodeEnum UpsClassImpl::DevClose()
- {
- LOG4VTM_FUNCTION();
- LOG4VTM(INFO,__FUNCTION__);
- ErrorCodeEnum err = Error_Succeed;
- pObj->iCloseComm();
- LOG4VTM(INFO,__FUNCTION__<<" OK");
- return err;
- }
- ErrorCodeEnum UpsClassImpl::GetLastErr(DevErrorInfo &devErrInfo)
- {
- memset(&devErrInfo, 0, sizeof(devErrInfo));
- memcpy(&devErrInfo, &m_DevErrorInfo, sizeof(devErrInfo));
- return Error_Succeed;
- }
- //
- // Get device status
- //
- ErrorCodeEnum UpsClassImpl::GetDevStatus(UpsStatusEnum& eStatus)
- {
- LOG4VTM_FUNCTION();
- LOG4VTM(INFO,__FUNCTION__);
- tUpsDevStatus status;
- tDevReturn l_tDevReturn[8] = { 0 };
- int ret = pObj->iGetDevStatus(l_tDevReturn,&status);
- if(ret != 0){
- eStatus = UPS_STATUS_ERROR_TO_GET_STATUS;
- printf("GetDevStatus ret = %d,errcode = %d\n",ret,l_tDevReturn[0].iLogicCode);
- }else{
- if(!status.bCityPower){
- eStatus = UPS_STATUS_NORMAL;
- }else{
- eStatus = UPS_STATUS_NO_ELECTOR;
- }
- }
- LOG4VTM(INFO,__FUNCTION__<<" bCityPower = "<<status.bCityPower);
- LOG4VTM(INFO,__FUNCTION__<<" bBatteryVolt = "<<status.bBatteryVolt);
- LOG4VTM(INFO,__FUNCTION__<<" bByPass = "<<status.bByPass);
- LOG4VTM(INFO,__FUNCTION__<<" bUpsDevFail = "<<status.bUpsDevFail);
- LOG4VTM(INFO,__FUNCTION__<<" bUpsType = "<<status.bUpsType);
- LOG4VTM(INFO,__FUNCTION__<<" bTestStatus = "<<status.bTestStatus);
- LOG4VTM(INFO,__FUNCTION__<<" bTurnOff = "<<status.bTurnOff);
- LOG4VTM(INFO,__FUNCTION__<<" bBuzzer = "<<status.bBuzzer);
-
- printf("bCityPower = %d\n",status.bCityPower);
- printf("bBatteryVolt = %d\n",status.bBatteryVolt);
- printf("bByPass = %d\n",status.bByPass);
- printf("bUpsDevFail = %d\n",status.bUpsDevFail);
- printf("bUpsType = %d\n",status.bUpsType);
- printf("bTestStatus = %d\n",status.bTestStatus);
- printf("bTurnOff = %d\n",status.bTurnOff);
- printf("bBuzzer = %d\n",status.bBuzzer);
- LOG4VTM(INFO,__FUNCTION__<<" OK");
- return Error_Succeed;
- }
- //
- // Shutdown ups and restart it in minutes.
- // Arguments:
- // - dwShutTime:time to shutdown (in minutes)
- // - dwRestartTime:time to restart (in minutes)
- //
- ErrorCodeEnum UpsClassImpl::Shutdown(DWORD dwShutTime, DWORD dwRestartTime)
- {
- LOG4VTM_FUNCTION();
- LOG4VTM(INFO,__FUNCTION__);
- tDevReturn l_tDevReturn[8] = { 0 };
- int ret = 0;
- ret = pObj->iDelayTurnOff(l_tDevReturn,dwShutTime*60);
- if(ret != 0)
- {
- FormatDevErrorInfo(__FUNCTION__, __LINE__, l_tDevReturn[0].iLogicCode, "iDelayTurnOff Failed");
- return Error_Unexpect;
- }
- /*
- if(dwRestartTime >=0)
- {
- ret = pObj->iTurnOn(l_tDevReturn);
- if(ret != 0)
- {
- FormatDevErrorInfo(__FUNCTION__, __LINE__, l_tDevReturn[0].iLogicCode, "iTurnOn Failed");
- return Error_IgnoreAll;
- }
- }
- */
- LOG4VTM(INFO,__FUNCTION__<<" OK");
- return Error_Succeed;
- }
- void UpsClassImpl::FormatDevErrorInfo(const char* funcname, int line, int errCode,char* errStr)
- {
- ZeroDevErrorInfo();
- snprintf(m_DevErrorInfo.szErrMsg, sizeof(m_DevErrorInfo.szErrMsg), "ErrCode:0x%x,Description:Func:%s,Line:%d,Msg:%s", errCode, funcname, line, errStr);
- m_DevErrorInfo.dwErrMsgLen = strlen(m_DevErrorInfo.szErrMsg);
- LOG4VTM(ERROR,m_DevErrorInfo.szErrMsg);
- }
- DEVICEBASE_API ErrorCodeEnum CreateDevComponent(DeviceBaseClass *&baseObj)
- {
- baseObj = new UpsClassImpl();
- if(baseObj == NULL) {
- return Error_Resource;
- } else {
- cmb::log_init_config config;
- config.dev_name = "Ups";
- config.log_level = CMB_LOG_LEVEL_TRACE;
- #if defined(_MSC_VER)
- config.log_dir = ("C:\\rvc\\dbg\\");
- #else
- config.log_dir = ("/opt/rvc/dbg/");
- #endif //_MSC_VER
- std::string str;
- cmb::log4vendor::init(config, str);
- printf("init after: %s\n", str.c_str());
- return Error_Succeed;
- }
- }
- DEVICEBASE_API ErrorCodeEnum ReleaseDevComponent(DeviceBaseClass *&pBaseObj)
- {
- if(pBaseObj == NULL) {
- return Error_Param;
- }
- if(UpsClassImpl* pTmp = dynamic_cast<UpsClassImpl*>(pBaseObj))
- {
- delete pTmp;
- pTmp = NULL;
- return Error_Succeed;
- }
- return Error_Param;
- }
|