WheelUnitLogic.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "StdAfx.h"
  2. #include <assert.h>
  3. #include "common_api.h"
  4. #include "PacketAnalizer.h"
  5. #include "WheelUnitLogic.h"
  6. #define CRTDBG_MAP_ALLOC
  7. #include <stdlib.h>
  8. #include <crtdbg.h>
  9. #define WHEELUNITLOGICID "{1830DD9F-88B3-442A-9DDA-82D0F0B647F7}"
  10. //------------------unit device----------------------------
  11. WheelUnitLogic::WheelUnitLogic(void)
  12. {
  13. m_Status = new BaseJsonDataObject<bool>();
  14. m_Status->SetKey("WHELLSTATUS");
  15. (*m_Status) = false;
  16. }
  17. WheelUnitLogic::~WheelUnitLogic(void)
  18. {
  19. delete m_Status;
  20. }
  21. //get device type
  22. bool WheelUnitLogic::GetDeviceType(GUID &DevType)
  23. {
  24. return string_2_guid(WHEELUNITLOGICID, DevType);
  25. }
  26. //resource
  27. // DeviceType:GUID
  28. // attribute:{WHELLSTATUS:0}
  29. // action:StartRoll
  30. // action:StopRoll
  31. //get device resource
  32. RET_STATUS WheelUnitLogic::GetDeviceResource(ResDataObject *pDeviceResource)
  33. {
  34. bool ret = true;
  35. //make device type
  36. ret &= pDeviceResource->add("ClientType", DPC_UnitClient);
  37. ret &= pDeviceResource->add("DeviceType", WHEELUNITLOGICID);
  38. //make attributes
  39. ResDataObject val;
  40. ResDataObject Actions;
  41. val.add(m_Status->GetKey(), m_Status->GetVal());
  42. ret &= pDeviceResource->add("Attribute", val);
  43. if (Support_GetStatus())
  44. {
  45. //make resource
  46. ret &= Actions.add("GetStatus", "");
  47. }
  48. if (Support_SetStatus())
  49. {
  50. //make resource
  51. ret &= Actions.add("SetStatus", "");
  52. }
  53. if (Support_StartRoll())
  54. {
  55. //make resource
  56. ret &= Actions.add("StartRoll", "");
  57. }
  58. if (Support_StopRoll())
  59. {
  60. //make resource
  61. ret &= Actions.add("StopRoll", "");
  62. }
  63. if (ret)
  64. {
  65. pDeviceResource->add("Action", Actions);
  66. return RET_SUCCEED;
  67. }
  68. return RET_FAILED;
  69. }
  70. //ResourceCommand Request In and Response Out
  71. RET_STATUS WheelUnitLogic::Request(ResDataObject *pRequest, ResDataObject *pResponse)
  72. {
  73. ResDataObject res;
  74. RET_STATUS ret = RET_NOSUPPORT;
  75. //1. analize request
  76. PACKET_CMD cmd = PacketAnalizer::GetPacketCmd(pRequest);
  77. //cmd:get,set,add,del,exe,
  78. //ignore open/close
  79. //Action和Attr应该做成一个MAP,经过MAP访问这些资源,不应该是switch case
  80. //为了快速做一版,先保留switch case!!!
  81. string keystr = PacketAnalizer::GetPacketKey(pRequest);
  82. ACTION_SYNC_MODE syncmode = PacketAnalizer::GetPacketSyncMode(pRequest);
  83. switch (cmd){
  84. case PACKET_CMD_EXE:
  85. {
  86. if (keystr == "GetStatus")
  87. {
  88. //1. 获取参数
  89. //2. call api
  90. bool status = false;
  91. ret = (RET_STATUS)GetStatus(status);
  92. if (syncmode == ACTION_SYNC)
  93. {
  94. res.add("P0", status);
  95. //3. 打包
  96. pResponse->update("CONTEXT", res);
  97. }
  98. else
  99. {
  100. //Async
  101. //make notify
  102. //问题:Notify会发往很多地方,所以这种情况下要考虑清楚怎么发!!!
  103. //点对点情况,Notify要有个返回的地点,不广播
  104. ResDataObject NotifyData;
  105. PacketAnalizer::MakeNotifyP2P(NotifyData, PACKET_CMD_UPDATE, pRequest, m_Status->GetKey(), m_Status->GetVal());
  106. CmdFromLogicDev(&NotifyData);
  107. }
  108. }
  109. else if (keystr == "SetStatus")
  110. {
  111. //1. 获取参数
  112. ResDataObject obj;
  113. if (PacketAnalizer::GetParam(pRequest, 0, obj))
  114. {
  115. //2. call api
  116. ret = (RET_STATUS)SetStatus((bool)obj);
  117. //3. 打包
  118. }
  119. }
  120. else if (keystr == "StartRoll")
  121. {
  122. //1. 获取参数
  123. //2. call api
  124. ret = (RET_STATUS)StartRoll();
  125. //3. 打包
  126. //无参数
  127. }
  128. else if (keystr == "StopRoll")
  129. {
  130. //1. 获取参数
  131. //2. call api
  132. ret = (RET_STATUS)StopRoll();
  133. //3. 打包
  134. //无参数
  135. }
  136. else if (keystr == "ChangeStatus")
  137. {
  138. //1. 获取参数
  139. ResDataObject obj;
  140. if (PacketAnalizer::GetParam(pRequest, 0, obj))
  141. {
  142. //2. call api
  143. ret = (RET_STATUS)ChangeStatus((bool)obj);
  144. //ret = (RET_STATUS)ChangeStatus(true);
  145. //3. 打包
  146. bool status;
  147. GetStatus(status);
  148. res.add("P0", status);
  149. //3. 打包
  150. //printf("call in ChangeStatus\n");
  151. pResponse->update("CONTEXT", res);
  152. }
  153. }
  154. }
  155. break;
  156. default:
  157. //对当前设备来讲,其他都是浮云
  158. break;
  159. }
  160. //return status
  161. PacketAnalizer::MakeRetCode(ret, pResponse);
  162. return ret;
  163. }
  164. //notify to lower layer
  165. RET_STATUS WheelUnitLogic::CmdToLogicDev(ResDataObject *pCmd)
  166. {
  167. //这个命令只有一种情况下发生(作为客户端存在的时候)
  168. //1.设备的Notify消息
  169. //发往当前客户端的Notify
  170. //Data&Action notify!!!
  171. //Data Notify分两种
  172. //一种是本数据对象的更新,直接更新
  173. //一种是数据传递给上层,比如图像数据,探测器的剂量需求数据
  174. //Action Notify是发给Owner的
  175. //通知事件,让Owner知道,另外ActionNotify先保留下来
  176. //保留到哪里???
  177. //2.Response
  178. //命令Request发出后的反馈
  179. //1. analize request
  180. //2. call api
  181. //3. check results
  182. //put error log here
  183. assert(0);//not happening
  184. return RET_FAILED;
  185. }
  186. //errors,warnings
  187. void WheelUnitLogic::SetErrorInfo(int errCode, char *pErrInfo)
  188. {
  189. //errors
  190. //调用点要进行检讨
  191. assert(0);//NOT FINISHED YET
  192. }
  193. void WheelUnitLogic::SetWarningInfo(int warningCode, char *pWarningInfo)
  194. {
  195. //warnings
  196. //调用点要进行检讨
  197. assert(0);//NOT FINISHED YET
  198. }
  199. //Data Access
  200. int WheelUnitLogic::GetStatus(bool &status)
  201. {
  202. status = (*m_Status);
  203. return RET_SUCCEED;
  204. }
  205. int WheelUnitLogic::SetStatus(bool status)
  206. {
  207. RET_STATUS ret = RET_SUCCEED;
  208. if ((*m_Status) != status)
  209. {
  210. //make notify
  211. ResDataObject NotifyData;
  212. PacketAnalizer::MakeNotify(NotifyData, PACKET_CMD_UPDATE, m_Status->GetKey(), m_Status->GetVal());
  213. ret = CmdFromLogicDev(&NotifyData);
  214. }
  215. (*m_Status) = status;
  216. return ret;
  217. }
  218. //Actions
  219. int WheelUnitLogic::StartRoll()
  220. {
  221. return RET_NOSUPPORT;
  222. }
  223. int WheelUnitLogic::StopRoll()
  224. {
  225. return RET_NOSUPPORT;
  226. }
  227. bool WheelUnitLogic::Support_GetStatus()
  228. {
  229. return false;
  230. }
  231. bool WheelUnitLogic::Support_SetStatus()
  232. {
  233. return false;
  234. }
  235. bool WheelUnitLogic::Support_StartRoll()
  236. {
  237. return false;
  238. }
  239. bool WheelUnitLogic::Support_StopRoll()
  240. {
  241. return false;
  242. }
  243. int DEVICE_ACTION WheelUnitLogic::ChangeStatus(bool PARAM_IN st)
  244. {
  245. return RET_NOSUPPORT;
  246. }
  247. bool DEVICE_SUPPORT WheelUnitLogic::Support_ChangeStatus()
  248. {
  249. return false;
  250. }