UtilBase.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #if !defined(_UTILLBASE_HPP_)
  2. #define _UTILLBASE_HPP_
  3. #include <iostream>
  4. #include <ctime>
  5. #include <memory>
  6. #include <vector>
  7. #include "SendDevice.h"
  8. #include "NonCopyAble.hpp"
  9. #include "httplib.h"
  10. namespace gsd{
  11. /**
  12. * @brief 控制设备基础
  13. *
  14. */
  15. class ContorlGear:private NonCopyAble
  16. {
  17. public:
  18. struct ControlerMsg
  19. {
  20. int deviceId;
  21. uint8_t msgId;
  22. uint8_t data;
  23. };
  24. enum DeviceTools{
  25. serial = 0,
  26. http = 1,
  27. tcp = 2
  28. };
  29. enum CommmandCodeGear{
  30. _STATUS,
  31. _OPEN,
  32. _CLOSE,
  33. _FIRE
  34. };
  35. protected:
  36. std::string deviceId;
  37. std::string DeviceInfo = "";
  38. enum DeviceTools tool = DeviceTools::serial;
  39. enum CommmandCodeGear code = CommmandCodeGear::_OPEN;
  40. int TypeId;
  41. public:
  42. ContorlGear(){}
  43. ~ContorlGear(){}
  44. /**
  45. * @description: 序列化
  46. * @return {*}
  47. */
  48. virtual bool serialization(){
  49. return true;
  50. }
  51. /**
  52. * @description: 反序列化
  53. * @return {*}
  54. */
  55. virtual bool deserialization(){
  56. return true;
  57. }
  58. /**
  59. * @description: 消费数据
  60. * @param {uint8_t} *data
  61. * @param {int} len
  62. * @param {int} &msg_id
  63. * @return {*}
  64. */
  65. virtual bool Consumer(uint8_t *data, int len, int &msg_id){
  66. return true;
  67. }
  68. /**
  69. * @description: 设备响应码是否为状态码
  70. * @param {int} &msg_id
  71. * @return {*}
  72. */
  73. virtual bool getStatusMsg(int &msg_id){
  74. return false;
  75. };
  76. /**
  77. * @description: 构建设备信息
  78. * @param {SendDevice&} sendDevice
  79. * @return {*}
  80. */
  81. virtual void BuildDeviceInfo(SendDevice& sendDevice) {};
  82. /**
  83. * @description: 常规控制
  84. * @param {string} _deviceId 设备Id
  85. * @param {uint8_t*} data 数据
  86. * @param {int&} length 数据长度
  87. * @return {*}
  88. */
  89. virtual int8_t Open(std::string _deviceId,uint8_t* data, int& length){return 0;};
  90. virtual int8_t Close(std::string _deviceId, uint8_t* data, int&length){return 0;};
  91. virtual int8_t Fire(std::string _deviceId, uint8_t* data, int& length){return 0;};
  92. virtual int8_t Status(std::string _deviceId, uint8_t* data, int& length){return 0;};
  93. /**
  94. * @description: 设置设备ID
  95. * @param {string} DeviceId
  96. * @return {*}
  97. */
  98. void setDeviceId(std::string DeviceId){
  99. deviceId = DeviceId;
  100. }
  101. /**
  102. * @description: 获取设备ID
  103. * @return {*}
  104. */
  105. std::string getDeviceId(){
  106. return deviceId;
  107. }
  108. /**
  109. * @description: 获取设备工具
  110. * @return {*}
  111. */
  112. enum DeviceTools getDeviceTool(){
  113. return tool;
  114. }
  115. /**
  116. * @description: 获取数据信息
  117. * @return {*}
  118. */
  119. std::string getDeviceInfo(){
  120. return DeviceInfo;
  121. }
  122. /**
  123. * @description: 获取类型
  124. * @return {*}
  125. */
  126. int getTypeId(){
  127. return TypeId;
  128. }
  129. /**
  130. * @description: 设置类型
  131. * @param {int} typeId
  132. * @return {*}
  133. */
  134. void setTypeId(int typeId){
  135. this->TypeId = typeId;
  136. }
  137. };
  138. /**
  139. * @brief 串口工具
  140. *
  141. */
  142. class ContorlSerialBase:public ContorlGear
  143. {
  144. protected:
  145. int baudRate;
  146. public:
  147. ContorlSerialBase(){}
  148. ~ContorlSerialBase(){}
  149. /**
  150. * @description: 获取波特率
  151. * @return {*}
  152. */
  153. int getBaudRate(){
  154. return this->baudRate;
  155. }
  156. };
  157. /**
  158. * @brief ContorlHttplBase
  159. *
  160. */
  161. class ContorlHttplBase: public ContorlGear
  162. {
  163. protected:
  164. std::string serverIP;
  165. int port;
  166. std::string serverCname;
  167. std::string serverCpwd;
  168. int commandCode = 0;
  169. public:
  170. ContorlHttplBase(){}
  171. ~ContorlHttplBase(){}
  172. void setServerIP(std::string _serverIP){
  173. serverIP = _serverIP;
  174. }
  175. void setServerCname(std::string _serverCname){
  176. serverCname = _serverCname;
  177. }
  178. void setServerCpwd(std::string _serverCpwd){
  179. serverCpwd = _serverCpwd;
  180. }
  181. httplib::Params getParms(){
  182. httplib::Params parms;
  183. return parms;
  184. }
  185. httplib::Headers getHeaders(){
  186. httplib::Headers headers;
  187. return headers;
  188. }
  189. std::string getUrl(){
  190. return "";
  191. }
  192. };
  193. /**
  194. * @brief ContorlTcplBase
  195. *
  196. */
  197. class ContorlTcplBase: public ContorlGear{
  198. protected:
  199. std::string serverIP;
  200. int port;
  201. public:
  202. ContorlTcplBase(){};
  203. ~ContorlTcplBase(){};
  204. void setServerIP(std::string _serverIP){
  205. serverIP = _serverIP;
  206. }
  207. void setPort(int _port){
  208. this->port = _port;
  209. }
  210. };
  211. /**
  212. * @brief ModuleBase
  213. *
  214. */
  215. class ModuleBase: private NonCopyAble
  216. {
  217. public:
  218. ModuleBase(){}
  219. ~ModuleBase(){}
  220. /**
  221. * @description: 初始化
  222. * @return {*}
  223. */
  224. virtual bool Init() = 0;
  225. /**
  226. * @description: 释放
  227. * @return {*}
  228. */
  229. virtual void Destroy() = 0;
  230. };
  231. /**
  232. * @brief PlginsBase
  233. *
  234. */
  235. class PluginBase: private ModuleBase
  236. {
  237. private:
  238. /* data */
  239. public:
  240. PluginBase(){}
  241. ~PluginBase(){}
  242. /**
  243. * @description: StartTask
  244. * @return {*}
  245. */
  246. virtual bool StartTask() = 0;
  247. /**
  248. * @description: Alive
  249. * @return {*}
  250. */
  251. virtual bool Alive() = 0;
  252. };
  253. };
  254. #endif // _UTILLBASE_HPP_