ups_impl.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "ups_impl.h"
  2. #include <cstring>
  3. #include <cstdio>
  4. #include "log4vendor.h"
  5. UpsClassImpl::UpsClassImpl()
  6. :m_mode(0)
  7. {
  8. pObj = new OUpsDev();
  9. }
  10. UpsClassImpl::~UpsClassImpl()
  11. {
  12. if(pObj)
  13. delete pObj;
  14. }
  15. void UpsClassImpl::ZeroDevErrorInfo()
  16. {
  17. memset(&m_DevErrorInfo, 0, sizeof(m_DevErrorInfo));
  18. }
  19. ErrorCodeEnum UpsClassImpl::DevOpen(DWORD dwPort, DWORD dwBaudRate)
  20. {
  21. LOG4VTM_FUNCTION();
  22. LOG4VTM(INFO,__FUNCTION__);
  23. ErrorCodeEnum err = Error_Succeed;
  24. tDevReturn l_tDevReturn[8] = { 0 };
  25. int ret = pObj->iSetCommPara(l_tDevReturn);
  26. if(ret != 0)
  27. {
  28. FormatDevErrorInfo(__FUNCTION__, __LINE__, l_tDevReturn[0].iLogicCode, "iSetCommPara Failed");
  29. return Error_DevConnFailed;
  30. }
  31. memset(l_tDevReturn,0,sizeof(tDevReturn)*8);
  32. ret = pObj->iInit(l_tDevReturn);
  33. if(ret != 0)
  34. {
  35. FormatDevErrorInfo(__FUNCTION__, __LINE__, l_tDevReturn[0].iLogicCode, "iInit Failed");
  36. return Error_DevConnFailed;
  37. }
  38. LOG4VTM(INFO,__FUNCTION__<<" OK");
  39. return err;
  40. }
  41. ErrorCodeEnum UpsClassImpl::GetDevCategory(DevCategoryInfo &devCategory)
  42. {
  43. LOG4VTM_FUNCTION();
  44. LOG4VTM(INFO,__FUNCTION__);
  45. ErrorCodeEnum err = Error_Succeed;
  46. const char *pType = "PVER=Cyber#MID=UT600Eb";
  47. const char *pMode = "FWID=0";
  48. const char *pVendor = "grg";
  49. strcpy(devCategory.szType, pType);
  50. strcpy(devCategory.szModel, pMode);
  51. strcpy(devCategory.szVendor, pVendor);
  52. devCategory.version.wMajor = 1;
  53. devCategory.version.wMinor = 1;
  54. devCategory.version.wRevision = 65535;
  55. devCategory.version.wBuild = 1;
  56. LOG4VTM(INFO,__FUNCTION__<<" OK");
  57. return err;
  58. }
  59. ErrorCodeEnum UpsClassImpl::Reset()
  60. {
  61. LOG4VTM_FUNCTION();
  62. LOG4VTM(INFO,__FUNCTION__);
  63. ErrorCodeEnum err = Error_Succeed;
  64. LOG4VTM(INFO,__FUNCTION__<<" OK");
  65. return err;
  66. }
  67. ErrorCodeEnum UpsClassImpl::DevClose()
  68. {
  69. LOG4VTM_FUNCTION();
  70. LOG4VTM(INFO,__FUNCTION__);
  71. ErrorCodeEnum err = Error_Succeed;
  72. pObj->iCloseComm();
  73. LOG4VTM(INFO,__FUNCTION__<<" OK");
  74. return err;
  75. }
  76. ErrorCodeEnum UpsClassImpl::GetLastErr(DevErrorInfo &devErrInfo)
  77. {
  78. memset(&devErrInfo, 0, sizeof(devErrInfo));
  79. memcpy(&devErrInfo, &m_DevErrorInfo, sizeof(devErrInfo));
  80. return Error_Succeed;
  81. }
  82. //
  83. // Get device status
  84. //
  85. ErrorCodeEnum UpsClassImpl::GetDevStatus(UpsStatusEnum& eStatus)
  86. {
  87. LOG4VTM_FUNCTION();
  88. LOG4VTM(INFO,__FUNCTION__);
  89. tUpsDevStatus status;
  90. tDevReturn l_tDevReturn[8] = { 0 };
  91. int ret = pObj->iGetDevStatus(l_tDevReturn,&status);
  92. if(ret != 0){
  93. eStatus = UPS_STATUS_ERROR_TO_GET_STATUS;
  94. printf("GetDevStatus ret = %d,errcode = %d\n",ret,l_tDevReturn[0].iLogicCode);
  95. }else{
  96. if(!status.bCityPower){
  97. eStatus = UPS_STATUS_NORMAL;
  98. }else{
  99. eStatus = UPS_STATUS_NO_ELECTOR;
  100. }
  101. }
  102. LOG4VTM(INFO,__FUNCTION__<<" bCityPower = "<<status.bCityPower);
  103. LOG4VTM(INFO,__FUNCTION__<<" bBatteryVolt = "<<status.bBatteryVolt);
  104. LOG4VTM(INFO,__FUNCTION__<<" bByPass = "<<status.bByPass);
  105. LOG4VTM(INFO,__FUNCTION__<<" bUpsDevFail = "<<status.bUpsDevFail);
  106. LOG4VTM(INFO,__FUNCTION__<<" bUpsType = "<<status.bUpsType);
  107. LOG4VTM(INFO,__FUNCTION__<<" bTestStatus = "<<status.bTestStatus);
  108. LOG4VTM(INFO,__FUNCTION__<<" bTurnOff = "<<status.bTurnOff);
  109. LOG4VTM(INFO,__FUNCTION__<<" bBuzzer = "<<status.bBuzzer);
  110. printf("bCityPower = %d\n",status.bCityPower);
  111. printf("bBatteryVolt = %d\n",status.bBatteryVolt);
  112. printf("bByPass = %d\n",status.bByPass);
  113. printf("bUpsDevFail = %d\n",status.bUpsDevFail);
  114. printf("bUpsType = %d\n",status.bUpsType);
  115. printf("bTestStatus = %d\n",status.bTestStatus);
  116. printf("bTurnOff = %d\n",status.bTurnOff);
  117. printf("bBuzzer = %d\n",status.bBuzzer);
  118. LOG4VTM(INFO,__FUNCTION__<<" OK");
  119. return Error_Succeed;
  120. }
  121. //
  122. // Shutdown ups and restart it in minutes.
  123. // Arguments:
  124. // - dwShutTime:time to shutdown (in minutes)
  125. // - dwRestartTime:time to restart (in minutes)
  126. //
  127. ErrorCodeEnum UpsClassImpl::Shutdown(DWORD dwShutTime, DWORD dwRestartTime)
  128. {
  129. LOG4VTM_FUNCTION();
  130. LOG4VTM(INFO,__FUNCTION__);
  131. tDevReturn l_tDevReturn[8] = { 0 };
  132. int ret = 0;
  133. ret = pObj->iDelayTurnOff(l_tDevReturn,dwShutTime*60);
  134. if(ret != 0)
  135. {
  136. FormatDevErrorInfo(__FUNCTION__, __LINE__, l_tDevReturn[0].iLogicCode, "iDelayTurnOff Failed");
  137. return Error_Unexpect;
  138. }
  139. /*
  140. if(dwRestartTime >=0)
  141. {
  142. ret = pObj->iTurnOn(l_tDevReturn);
  143. if(ret != 0)
  144. {
  145. FormatDevErrorInfo(__FUNCTION__, __LINE__, l_tDevReturn[0].iLogicCode, "iTurnOn Failed");
  146. return Error_IgnoreAll;
  147. }
  148. }
  149. */
  150. LOG4VTM(INFO,__FUNCTION__<<" OK");
  151. return Error_Succeed;
  152. }
  153. void UpsClassImpl::FormatDevErrorInfo(const char* funcname, int line, int errCode,char* errStr)
  154. {
  155. ZeroDevErrorInfo();
  156. snprintf(m_DevErrorInfo.szErrMsg, sizeof(m_DevErrorInfo.szErrMsg), "ErrCode:0x%x,Description:Func:%s,Line:%d,Msg:%s", errCode, funcname, line, errStr);
  157. m_DevErrorInfo.dwErrMsgLen = strlen(m_DevErrorInfo.szErrMsg);
  158. LOG4VTM(ERROR,m_DevErrorInfo.szErrMsg);
  159. }
  160. DEVICEBASE_API ErrorCodeEnum CreateDevComponent(DeviceBaseClass *&baseObj)
  161. {
  162. baseObj = new UpsClassImpl();
  163. if(baseObj == NULL) {
  164. return Error_Resource;
  165. } else {
  166. cmb::log_init_config config;
  167. config.dev_name = "Ups";
  168. config.log_level = CMB_LOG_LEVEL_TRACE;
  169. #if defined(_MSC_VER)
  170. config.log_dir = ("C:\\rvc\\dbg\\");
  171. #else
  172. config.log_dir = ("/opt/rvc/dbg/");
  173. #endif //_MSC_VER
  174. std::string str;
  175. cmb::log4vendor::init(config, str);
  176. printf("init after: %s\n", str.c_str());
  177. return Error_Succeed;
  178. }
  179. }
  180. DEVICEBASE_API ErrorCodeEnum ReleaseDevComponent(DeviceBaseClass *&pBaseObj)
  181. {
  182. if(pBaseObj == NULL) {
  183. return Error_Param;
  184. }
  185. if(UpsClassImpl* pTmp = dynamic_cast<UpsClassImpl*>(pBaseObj))
  186. {
  187. delete pTmp;
  188. pTmp = NULL;
  189. return Error_Succeed;
  190. }
  191. return Error_Param;
  192. }