UtilBase.hpp 6.5 KB

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