WheelClient.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #include "StdAfx.h"
  2. #include "PacketAnalizer.h"
  3. #include "WheelClient.h"
  4. WheelUnitLogicClient::WheelUnitLogicClient(void)
  5. {
  6. pLogicDeivce = (LogicDevice*)this;
  7. }
  8. WheelUnitLogicClient::~WheelUnitLogicClient(void)
  9. {
  10. }
  11. WheelUnitLogicClient& WheelUnitLogicClient::operator = (WheelUnitLogicClient &tValue)
  12. {
  13. //strongly recommended do not assign a client to another one
  14. if (this != &tValue)
  15. {
  16. m_FileFlags = tValue.m_FileFlags;
  17. (*m_pFilePath) = (*(tValue.m_pFilePath));
  18. //do open
  19. if (tValue.IsClosed() == false)
  20. {
  21. if (Open((tValue.m_pFilePath)->c_str(), (CCOS_FILE_FLAGS)tValue.m_FileFlags) <= 0)
  22. {
  23. //failed here
  24. //put some log
  25. printf("Thread:%d,Open Failed\n", GetCurrentThreadId());
  26. }
  27. }
  28. }
  29. return (*this);
  30. }
  31. RET_STATUS SYSTEM_CALL WheelUnitLogicClient::GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource)
  32. {
  33. if (IsClosed())
  34. {
  35. return RET_FAILED;
  36. }
  37. return ((LogicClient*)this)->GetDeviceResource(pDeviceResource);
  38. }
  39. int WheelUnitLogicClient::Open(const char *pPath, int flags, DWORD timeout)
  40. {
  41. RET_STATUS ret = (RET_STATUS)LogicClient::Open(pPath, flags, timeout);
  42. if (ret == RET_SUCCEED)
  43. {
  44. //need update local data!!!!
  45. bool sRet = true;
  46. ResDataObject Attribute;
  47. ResDataObject Temp;
  48. sRet &= TryGetValue(m_DeviceResource, "Attribute", Attribute);
  49. if (sRet)
  50. {
  51. sRet &= TryGetValue(Attribute, "WHELLSTATUS", Temp);
  52. if (sRet)
  53. {
  54. ret = (RET_STATUS)WheelUnitLogic::SetStatus((bool)Temp);
  55. }
  56. else
  57. {
  58. ret = RET_FAILED;
  59. }
  60. if (ret != RET_SUCCEED)
  61. {
  62. LogicClient::Close();
  63. }
  64. }
  65. sRet &= TryGetValue(m_DeviceResource, "Action", Attribute);
  66. }
  67. return ret;
  68. }
  69. //ResourceCommand Request In and Response Out
  70. RET_STATUS SYSTEM_CALL WheelUnitLogicClient::Request(ResDataObject PARAM_IN *pRequest, ResDataObject PARAM_OUT *pResponse)
  71. {
  72. //do nothing
  73. return RET_NOSUPPORT;
  74. }
  75. //notify to lower layer
  76. RET_STATUS SYSTEM_CALL WheelUnitLogicClient::CmdToLogicDev(ResDataObject PARAM_IN *pCmd)
  77. {
  78. //response and notify comes here
  79. PacketArrived(pCmd);
  80. return RET_SUCCEED;
  81. }
  82. PACKET_CMD WheelUnitLogicClient::ReadCmd(ResDataObject &CmdObject)
  83. {
  84. PACKET_CMD ret = PACKET_CMD_NONE;
  85. while (IsDataArrived())
  86. {
  87. ret = LogicClient::ReadCmd(CmdObject);
  88. if (ret == PACKET_CMD_UPDATE)
  89. {
  90. //only one update
  91. string key = PacketAnalizer::GetPacketKey(&CmdObject);
  92. assert(key == "WHELLSTATUS");
  93. ResDataObject Context;
  94. if (PacketAnalizer::GetPacketContext(&CmdObject, Context))
  95. {
  96. WheelUnitLogic::SetStatus((bool)Context);
  97. }
  98. return ret;
  99. }
  100. else if (ret != PACKET_CMD_NONE)
  101. {
  102. return ret;
  103. }
  104. }
  105. return ret;
  106. }
  107. //resource
  108. // DeviceType:GUID
  109. // attribute:{WHELLSTATUS:0}
  110. // action:StartRoll
  111. // action:StopRoll
  112. //Data Access
  113. int DATA_ACTION WheelUnitLogicClient::GetStatus(bool PARAM_OUT &status)
  114. {
  115. //use the request
  116. if (Support_GetStatus())
  117. {
  118. //pack get status
  119. ResDataObject req, res;
  120. RET_STATUS ret = (RET_STATUS)Action("GetStatus", req, res);
  121. if (ret == RET_SUCCEED && GetRequestSyncMode() == ACTION_SYNC)
  122. {
  123. try {
  124. status = res["P0"];
  125. }
  126. //catch (ResDataObjectExption &exp)
  127. //{
  128. // //exp.what();
  129. //}
  130. catch (...)
  131. {
  132. //exp
  133. }
  134. }
  135. return ret;
  136. }
  137. return RET_NOSUPPORT;
  138. }
  139. int DATA_ACTION WheelUnitLogicClient::SetStatus(bool PARAM_IN status)
  140. {
  141. //use the request
  142. if (Support_SetStatus())
  143. {
  144. //pack set status
  145. ResDataObject req, res;
  146. //put params here
  147. req.add("P0", status);
  148. //exec action here
  149. return (RET_STATUS)Action("SetStatus", req, res);
  150. }
  151. return RET_NOSUPPORT;
  152. }
  153. //Actions
  154. int DEVICE_ACTION WheelUnitLogicClient::StartRoll()
  155. {
  156. //use the request
  157. if (Support_StartRoll())
  158. {
  159. //pack set status
  160. ResDataObject req, res;
  161. //put params here
  162. //exec action here
  163. return Action("StartRoll", req, res);
  164. }
  165. return RET_NOSUPPORT;
  166. }
  167. int DEVICE_ACTION WheelUnitLogicClient::StopRoll()
  168. {
  169. //use the request
  170. if (Support_StopRoll())
  171. {
  172. //pack set status
  173. ResDataObject req, res;
  174. //put params here
  175. //exec action here
  176. return Action("StopRoll", req, res);
  177. }
  178. return RET_NOSUPPORT;
  179. }
  180. bool DEVICE_SUPPORT WheelUnitLogicClient::Support_GetStatus()
  181. {
  182. //use the resource of LogicDevice
  183. try{
  184. int idx = m_DeviceResource.GetFirstOf("Action");
  185. if (idx >= 0)
  186. {
  187. idx = m_DeviceResource[idx].GetFirstOf("GetStatus");
  188. return (idx >= 0);
  189. }
  190. }
  191. //catch (ResDataObjectExption &exp)
  192. //{
  193. // //log here
  194. // //exp.what();
  195. //}
  196. catch (...)
  197. {
  198. //log here
  199. }
  200. return false;
  201. }
  202. bool DEVICE_SUPPORT WheelUnitLogicClient::Support_SetStatus()
  203. {
  204. //use the resource of LogicDevice
  205. try{
  206. int idx = m_DeviceResource.GetFirstOf("Action");
  207. if (idx >= 0)
  208. {
  209. idx = m_DeviceResource[idx].GetFirstOf("SetStatus");
  210. return (idx >= 0);
  211. }
  212. }
  213. //catch (ResDataObjectExption &exp)
  214. //{
  215. // //log here
  216. // //exp.what();
  217. //}
  218. catch (...)
  219. {
  220. //log here
  221. }
  222. return false;
  223. }
  224. bool DEVICE_SUPPORT WheelUnitLogicClient::Support_StartRoll()
  225. {
  226. //use the resource of LogicDevice
  227. try{
  228. int idx = m_DeviceResource.GetFirstOf("Action");
  229. if (idx >= 0)
  230. {
  231. idx = m_DeviceResource[idx].GetFirstOf("StartRoll");
  232. return (idx >= 0);
  233. }
  234. }
  235. //catch (ResDataObjectExption &exp)
  236. //{
  237. // //log here
  238. // //exp.what();
  239. //}
  240. catch (...)
  241. {
  242. //log here
  243. }
  244. return false;
  245. }
  246. bool DEVICE_SUPPORT WheelUnitLogicClient::Support_StopRoll()
  247. {
  248. //use the resource of LogicDevice
  249. try{
  250. int idx = m_DeviceResource.GetFirstOf("Action");
  251. if (idx >= 0)
  252. {
  253. idx = m_DeviceResource[idx].GetFirstOf("StopRoll");
  254. return (idx >= 0);
  255. }
  256. }
  257. //catch (ResDataObjectExption &exp)
  258. //{
  259. // //log here
  260. // //exp.what();
  261. //}
  262. catch (...)
  263. {
  264. //log here
  265. }
  266. return false;
  267. }
  268. int SYSTEM_CALL WheelUnitLogicClient::GetLocalStatus(bool PARAM_OUT &status)
  269. {
  270. return WheelUnitLogic::GetStatus(status);
  271. }