definitions.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #pragma once
  2. #include<windows.h>
  3. #include <string>
  4. #include <sstream>
  5. #include <map>
  6. #include "ResDataObject.h"
  7. using namespace std;
  8. #define SYSTEM_CALL
  9. #define OEM_IF
  10. #define DEVICE_ACTION __stdcall
  11. #define DATA_ACTION DEVICE_ACTION
  12. #define HW_ACTION
  13. #define DEVICE_SUPPORT
  14. #define PARAM_IN
  15. #define PARAM_OUT
  16. #define TIMEOUT_REQ (10000)
  17. #define THREAD_PRIORITY_NONE (0xFFFF0000)
  18. //64M as max Buff
  19. #define SCF_MAX_LIMITED_BUFF_SIZE (1024*1024*32*2)
  20. /*
  21. 返回值的状态解释
  22. 简单解释的话,一条命令的目的是让设备从【状态A】 -> 【状态B】.
  23. 1.设备处于【状态A】.
  24. 2.发送命令给设备 -> 【设备进入PENDING状态】,PENDING期间,设备端不再接收命令,或没有反馈.
  25. 3.设备执行完命令且有反馈 -> 【设备执行命令SUCCEED】
  26. 4.再次发送相同命令给设备 -> 【设备进入PENDING状态】
  27. 5.设备执行完命令且有反馈 -> 【设备进入ONGOING状态】
  28. 6.设备自行从【状态A】切换到【状态B】 -> 【设备进入FINISHED状态】
  29. 一次状态变迁一般有如下流程.(PENDING状态是暂时的,所以大部分情况不予考虑,除非命令Send后TIMEOUT了)
  30. 【状态A】-----接收---------再次------无反馈-------处理-----执行完成-------开始变迁-----再次---------反馈---------------->【状态B】----再次------反馈
  31. ↑ ↑ ↓ ↓ ↑ ↓ ↓ ↑ ↓
  32. CMDA2B ------>CMDA2B----->PENDING ------------> SUCCEED ------------------>CMDA2B----->ONGOING ------------> FINISHED-->CMDA2B----->FINISHED.
  33. */
  34. typedef enum _Ret_Status {
  35. RET_THREAD_INVALID = -4,//the calling thread is not owner
  36. RET_INVALID = -3,//the handle is invalid
  37. RET_NOSUPPORT = -2,//device not support
  38. RET_TIMEOUT = -1,//超时(无接收命令)
  39. RET_FAILED = 0,//执行命令失败
  40. RET_PENDING,//设备有接收命令,但无反馈(在一定时间内无反馈情况)
  41. RET_SUCCEED,//设备执行命令成功
  42. RET_ONGOING,//设备执行命令已经完成,但是没有达成目标.
  43. RET_FINISHED,//设备执行命令已经完成,且有达成目标.
  44. RET_WARNING
  45. }RET_STATUS;
  46. typedef enum _Dios_Proc_Type {
  47. DIOS_PROC_SLAVE,//not used
  48. DIOS_PROC_MASTER,//Driver proc
  49. DIOS_PROC_CHANNEL,//local channel proc
  50. DIOS_PROC_CLIENT,//not used
  51. DIOS_PROC_MAX
  52. }DIOS_PROC_TYPE;
  53. template<typename T> class ValueDef : public BaseJsonDataObject<T>
  54. {
  55. public:
  56. BaseJsonDataObject<T> m_value;//实际值
  57. BaseJsonDataObject<T> m_UpThreshold;//上阀值
  58. BaseJsonDataObject<T> m_DownThreshold;//下阀值
  59. BaseJsonDataObject<T> m_Precision;//精度
  60. //度量单位保留缺省单位
  61. //距离:mm
  62. //温度:度
  63. //角度:度
  64. ValueDef(void)
  65. {
  66. }
  67. virtual ~ValueDef(void)
  68. {
  69. }
  70. virtual const char *GetVal()
  71. {
  72. ResDataObject obj;
  73. obj.add(m_value.GetKey(),m_value.GetVal());
  74. obj.add(m_UpThreshold.GetKey(),m_UpThreshold.GetVal());
  75. obj.add(m_DownThreshold.GetKey(),m_DownThreshold.GetVal());
  76. obj.add(m_Precision.GetKey(),m_Precision.GetVal());
  77. (*m_pValString) = obj.encode();
  78. return m_pValString->c_str();
  79. };
  80. virtual bool SetVal(const char* pValString)
  81. {
  82. ResDataObject obj;
  83. if(obj.decode(pValString))
  84. {
  85. int idx;
  86. idx = obj.GetFirstOf(m_value.GetKey());
  87. if(idx >= 0)
  88. {
  89. m_value.SetVal(obj[idx]);
  90. }
  91. idx = obj.GetFirstOf(m_UpThreshold.GetKey());
  92. if(idx >= 0)
  93. {
  94. m_UpThreshold.SetVal(obj[idx]);
  95. }
  96. idx = obj.GetFirstOf(m_DownThreshold.GetKey());
  97. if(idx >= 0)
  98. {
  99. m_DownThreshold.SetVal(obj[idx]);
  100. }
  101. idx = obj.GetFirstOf(m_Precision.GetKey());
  102. if(idx >= 0)
  103. {
  104. m_Precision.SetVal(obj[idx]);
  105. }
  106. return true;
  107. }
  108. return false;
  109. };
  110. virtual void SetKey(const char* pKeyString)
  111. {
  112. std::string temp = pKeyString;
  113. std::string keystring;
  114. BaseJsonDataObject<T>::SetKey(pKeyString);
  115. //make key
  116. keystring = temp + "_value";
  117. m_UpThreshold.SetKey(keystring.c_str());
  118. m_value.SetKey(pKeyString);
  119. //make UpThreshold
  120. keystring = temp + "_UpThreshold";
  121. m_UpThreshold.SetKey(keystring.c_str());
  122. //make DownThreshold
  123. keystring = temp + "_DownThreshold";
  124. m_DownThreshold.SetKey(keystring.c_str());
  125. //make Precision
  126. keystring = temp + "_Precision";
  127. m_Precision.SetKey(keystring.c_str());
  128. };
  129. };
  130. //class MessageAddress : public BaseJsonDataObject<int>
  131. // {
  132. // public:
  133. //
  134. // BaseJsonDataObject<int> DevID;
  135. // BaseJsonDataObject<string> DevType;
  136. // BaseJsonDataObject<int> UnitID;
  137. // BaseJsonDataObject<string> UnitType;
  138. // MessageAddress(void)
  139. // {
  140. // SetKey("MessageAddress");
  141. //
  142. // DevType.SetKey("DevType");
  143. // DevID.SetKey("DevID");
  144. // UnitType.SetKey("UnitType");
  145. // UnitID.SetKey("UnitID");
  146. // }
  147. // virtual ~MessageAddress(void)
  148. // {
  149. //
  150. // }
  151. // virtual bool SetResDataObject(ResDataObject &obj)
  152. // {
  153. // bool ret = true;
  154. // try {
  155. // DevType = (string)obj[DevType.GetKey()];
  156. // DevID = obj[DevID.GetKey()];
  157. // UnitType = (string)obj[UnitType.GetKey()];
  158. // UnitID = obj[UnitID.GetKey()];
  159. // }
  160. // catch (...)
  161. // {
  162. // ret = false;
  163. // }
  164. // return ret;
  165. // };
  166. // virtual void GetResDataObject(ResDataObject &obj)
  167. // {
  168. // obj.add(DevID.GetKey(), DevID);
  169. // obj.add(DevType.GetKey(), ((string)DevType).c_str());
  170. // obj.add(UnitType.GetKey(), ((string)UnitType).c_str());
  171. // obj.add(UnitID.GetKey(), UnitID);
  172. // };
  173. // virtual const char *GetVal()
  174. // {
  175. //
  176. // ResDataObject obj;
  177. // obj.add(DevID.GetKey(), DevID.GetVal());
  178. // obj.add(DevType.GetKey(), DevType.GetVal());
  179. // obj.add(UnitType.GetKey(), UnitType.GetVal());
  180. // obj.add(UnitID.GetKey(), UnitID.GetVal());
  181. // (m_ValString) = obj.encode();
  182. //
  183. // return m_ValString.c_str();
  184. // };
  185. //
  186. // virtual bool SetVal(const char* pValString)
  187. // {
  188. //
  189. // ResDataObject obj;
  190. // if (obj.decode(pValString))
  191. // {
  192. // int idx;
  193. // idx = obj.GetFirstOf(DevID.GetKey());
  194. // if (idx >= 0)
  195. // {
  196. // DevID.SetVal(obj[idx]);
  197. // }
  198. // idx = obj.GetFirstOf(DevType.GetKey());
  199. // if (idx >= 0)
  200. // {
  201. // DevType.SetVal(obj[idx]);
  202. // }
  203. // idx = obj.GetFirstOf(UnitType.GetKey());
  204. // if (idx >= 0)
  205. // {
  206. // UnitType.SetVal(obj[idx]);
  207. // }
  208. // idx = obj.GetFirstOf(UnitID.GetKey());
  209. // if (idx >= 0)
  210. // {
  211. // UnitID.SetVal(obj[idx]);
  212. // }
  213. // return true;
  214. // }
  215. // return false;
  216. // }
  217. //
  218. // };
  219. //class MessageInfo : public BaseJsonDataObject<int>
  220. // {
  221. // public:
  222. // BaseJsonDataObject<int> CodeID;
  223. // BaseJsonDataObject<int> Level;
  224. // BaseJsonDataObject<string> Resouceinfo;
  225. // BaseJsonDataObject<string> Description;
  226. // MessageInfo(void)
  227. // {
  228. // SetKey("MessageInfo");
  229. // CodeID.SetKey("CodeID");
  230. // Level.SetKey("Level");
  231. // Resouceinfo.SetKey("Resouceinfo");
  232. // Description.SetKey("Description");
  233. // }
  234. // virtual ~MessageInfo(void)
  235. // {
  236. //
  237. // }
  238. // virtual bool SetResDataObject(ResDataObject &obj)
  239. // {
  240. // bool ret = true;
  241. // try {
  242. // CodeID = obj[CodeID.GetKey()];
  243. // Level = obj[Level.GetKey()];
  244. // Resouceinfo = (string)obj[Resouceinfo.GetKey()];
  245. // Description = (string)obj[Description.GetKey()];
  246. // }
  247. // catch (...)
  248. // {
  249. // ret = false;
  250. // }
  251. // return ret;
  252. // };
  253. // virtual void GetResDataObject(ResDataObject &obj)
  254. // {
  255. // obj.add(CodeID.GetKey(), CodeID);
  256. // obj.add(Level.GetKey(), Level);
  257. // obj.add(Resouceinfo.GetKey(), ((string)Resouceinfo).c_str());
  258. // obj.add(Description.GetKey(), ((string)Description).c_str());
  259. //
  260. // };
  261. // virtual const char *GetVal()
  262. // {
  263. //
  264. // ResDataObject obj;
  265. // obj.add(CodeID.GetKey(), CodeID.GetVal());
  266. // obj.add(Level.GetKey(), Level.GetVal());
  267. // obj.add(Resouceinfo.GetKey(), Resouceinfo.GetVal());
  268. // obj.add(Description.GetKey(), Description.GetVal());
  269. //
  270. // (m_ValString) = obj.encode();
  271. //
  272. // return m_ValString.c_str();
  273. // };
  274. //
  275. // virtual bool SetVal(const char* pValString)
  276. // {
  277. //
  278. // ResDataObject obj;
  279. // if (obj.decode(pValString))
  280. // {
  281. // int idx;
  282. // idx = obj.GetFirstOf(CodeID.GetKey());
  283. // if (idx >= 0)
  284. // {
  285. // CodeID.SetVal(obj[idx]);
  286. // }
  287. // idx = obj.GetFirstOf(Level.GetKey());
  288. // if (idx >= 0)
  289. // {
  290. // Level.SetVal(obj[idx]);
  291. // }
  292. // idx = obj.GetFirstOf(Resouceinfo.GetKey());
  293. // if (idx >= 0)
  294. // {
  295. // Resouceinfo.SetVal(obj[idx]);
  296. // }
  297. // idx = obj.GetFirstOf(Description.GetKey());
  298. // if (idx >= 0)
  299. // {
  300. // Description.SetVal(obj[idx]);
  301. // }
  302. // return true;
  303. // }
  304. // return false;
  305. // }
  306. //
  307. // };
  308. //
  309. //struct Message
  310. //{
  311. // MessageAddress Address;
  312. // MessageInfo Info;
  313. // virtual void GetResDataObject(ResDataObject &obj)
  314. // {
  315. // obj.add(Address.DevID.GetKey(), Address.DevID);
  316. // obj.add(Address.DevType.GetKey(), ((string)Address.DevType).c_str());
  317. // obj.add(Address.UnitType.GetKey(), ((string)Address.UnitType).c_str());
  318. // obj.add(Address.UnitID.GetKey(), Address.UnitID);
  319. // obj.add(Info.CodeID.GetKey(), Info.CodeID);
  320. // obj.add(Info.Level.GetKey(), Info.Level);
  321. // obj.add(Info.Resouceinfo.GetKey(), ((string)Info.Resouceinfo).c_str());
  322. // obj.add(Info.Description.GetKey(), ((string)Info.Description).c_str());
  323. //
  324. // };
  325. //};