deviceGasV2.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include <iostream>
  2. #include "deviceBase.h"
  3. using namespace std;
  4. #define DATA_HEAD (0x40) // 数据头
  5. //控制设备类型
  6. // device_TYPE
  7. #define DEVICE_TYPE_ID (0x05) // 煤气炮
  8. // 命令类型
  9. // msgId
  10. #define MSG_ID_COMMAND (0x08) //命令
  11. #define MSG_ID_INQUIRE (0x04) //查询
  12. #define MSG_ID_DEPLOY (0x03) //配置
  13. #define MSG_ID_COMMAND_ACK (0X09) //命令应答
  14. #define MSG_ID_INQUIRE_ACK (0X0A) //查询应答
  15. #define MSG_ID_DEPLOY_ACK (0x0b) //配置应答
  16. //data
  17. #define DATA_FIRE (0x55)//开启
  18. #define DATA_STOP (0xAA)//停止
  19. #define DATA_INQUIRE (0xAB)//查询监控数据
  20. class deviceGasV2:public deviceBase
  21. {
  22. private:
  23. enum RECV_STEP{
  24. STEP_HEAD = 0,
  25. STEP_1 = 1,
  26. STEP_2 = 2,
  27. STEP_TYPE = 3,
  28. STEP_ADDR = 4,
  29. STEP_COM = 5,
  30. STEP_ACK = 6,
  31. STEP_STA = 7,
  32. STEP_DATA = 8,
  33. STEP_RLC = 9
  34. };
  35. typedef struct {
  36. uint8_t dataCount;
  37. uint8_t dataLength;
  38. uint8_t dataCheckSum;
  39. uint8_t data[40];
  40. enum RECV_STEP step = STEP_HEAD;
  41. }ExpelComm;
  42. ExpelComm _comm;
  43. public:
  44. deviceGasV2():deviceBase(){
  45. this->setDeviceToken(serial);
  46. this->setDeviceInfo("兰州中川机场 煤气炮驱动板协议");
  47. this->setBaudRate(38400);
  48. }
  49. public:
  50. // 电压
  51. float voltage = 0;
  52. // 气压
  53. float pressure = 0;
  54. // 状态
  55. int state = 0;
  56. // 纬度
  57. float latitude = 0;
  58. // 经度
  59. float longitude = 0;
  60. // LRC校验
  61. static unsigned char LRC_check(unsigned char *pSendBuf,unsigned char num)
  62. {
  63. char bylrc = 0,i = 0;
  64. for(i = 0; i < num; i++)
  65. {
  66. bylrc += pSendBuf[i];
  67. }
  68. bylrc = ~bylrc;
  69. bylrc++;
  70. return bylrc;
  71. }
  72. static void serialization(const GasV1Control gasV1Control,char *data){
  73. data[0] = DATA_HEAD;
  74. data[1] = 0x23;
  75. data[2] = 0x24;
  76. data[3] = DEVICE_TYPE_ID;
  77. data[4] = gasV1Control.deviceId;
  78. data[5] = gasV1Control.msgId;
  79. data[6] = gasV1Control.data;
  80. if(gasV1Control.msgId == MSG_ID_COMMAND){
  81. data[7] = 0xb3;
  82. data[8] = 0xff;
  83. }else if(gasV1Control.msgId == MSG_ID_INQUIRE){
  84. data[7] = 0xAA;
  85. data[8] = 0xAA;
  86. }
  87. data[9] = LRC_check((unsigned char *)data, 9);
  88. }
  89. bool deserialization()
  90. {
  91. return true;
  92. }
  93. bool Consumer(uint8_t *data, int len, int &msg_id)
  94. {
  95. bool ReceiveSuccessful = false;
  96. if(this->Type != 1 && len > 0) return true;
  97. for (int i = 0; i < len; i++)
  98. {
  99. switch(_comm.step){
  100. case STEP_HEAD:
  101. if(data[i] == DATA_HEAD){
  102. _comm.data[0] = DATA_HEAD;
  103. _comm.step = STEP_1;
  104. }
  105. break;
  106. case STEP_1:
  107. if(data[i] == 0x23){
  108. _comm.data[1] = data[i];
  109. _comm.step = STEP_2;
  110. }
  111. break;
  112. case STEP_2:
  113. if(data[i] == 0x24){
  114. _comm.data[2] = data[i];
  115. _comm.step = STEP_TYPE;
  116. }
  117. break;
  118. case STEP_TYPE:
  119. _comm.data[3] = data[i];
  120. _comm.step = STEP_ADDR;
  121. break;
  122. case STEP_ADDR:
  123. _comm.data[4] = data[i];
  124. _comm.step = STEP_COM;
  125. break;
  126. case STEP_COM:
  127. _comm.data[5] = data[i];
  128. _comm.step = STEP_ACK;
  129. break;
  130. case STEP_ACK:
  131. _comm.data[6] = data[i];
  132. _comm.step = STEP_STA;
  133. break;
  134. case STEP_STA:
  135. _comm.data[7] = data[i];
  136. _comm.step = STEP_DATA;
  137. _comm.dataCount = 8;
  138. break;
  139. case STEP_DATA:
  140. _comm.data[_comm.dataCount++] = data[i];
  141. if(_comm.dataCount == 40){
  142. ReceiveSuccessful = true;
  143. _comm.step = STEP_HEAD;
  144. }
  145. break;
  146. default:
  147. _comm.step = STEP_HEAD;
  148. break;
  149. }
  150. }
  151. if(!ReceiveSuccessful) return false;
  152. return deserialization(_comm.data, _comm.dataCount, msg_id);
  153. }
  154. bool deserialization(const uint8_t *data, int len, int &msg_id){
  155. if(data[0] != DATA_HEAD && data[1] != 0x23 && data[2] != 0x24 && data[3] != DEVICE_TYPE_ID) return false;
  156. if(deviceGasV2::LRC_check((unsigned char *)data, len - 1) != data[len - 1]) return false;
  157. setDeviceId(std::to_string(data[4]));
  158. if(data[5] == 0x00){
  159. msg_id = data[6];
  160. switch (msg_id)
  161. {
  162. // 命令应答
  163. case MSG_ID_COMMAND_ACK:
  164. break;
  165. // 查询应答
  166. case MSG_ID_INQUIRE_ACK:
  167. this->voltage = (float)data[8] + (float)(data[9]/100);
  168. this->pressure = (float)data[10] + (float)(data[11]/100);
  169. this->state = data[12];
  170. break;
  171. // 配置应答
  172. case MSG_ID_DEPLOY_ACK:
  173. break;
  174. }
  175. return true;
  176. }
  177. return false;
  178. }
  179. /**
  180. * @description: 打开
  181. * @param {int} deivceId
  182. * @return {*}
  183. */
  184. int32_t Open(std::string DeviceId, uint8_t* data, int& length){
  185. GasV1Control gasControl;
  186. gasControl.deviceId = atoi(DeviceId.c_str());
  187. gasControl.msgId = 0x00;
  188. gasControl.data = 0x00;
  189. length = 11;
  190. serialization(gasControl, (char *)data);
  191. return 0;
  192. }
  193. /**
  194. * @description: 关闭
  195. * @param {int} deivceId
  196. * @return {*}
  197. */
  198. int32_t Close(std::string DeviceId, uint8_t* data, int& length){
  199. GasV1Control gasControl;
  200. gasControl.deviceId = atoi(DeviceId.c_str());
  201. gasControl.msgId = MSG_ID_COMMAND;
  202. gasControl.data = DATA_STOP;
  203. length = 11;
  204. serialization(gasControl, (char *)data);
  205. return 0;
  206. }
  207. /**
  208. * @description: 开炮
  209. * @param {int} deivceId
  210. * @return {*}
  211. */
  212. int32_t Fire(std::string DeviceId, uint8_t* data, int& length){
  213. GasV1Control gasControl;
  214. gasControl.deviceId = atoi(DeviceId.c_str());
  215. gasControl.msgId = MSG_ID_COMMAND;
  216. gasControl.data = DATA_FIRE;
  217. length = 11;
  218. serialization(gasControl, (char *)data);
  219. return 0;
  220. }
  221. /**
  222. * @description: 状态
  223. * @param {int} deivceId
  224. * @return {*}
  225. */
  226. int32_t Status(std::string DeviceId, uint8_t* data, int& length){
  227. GasV1Control gasControl;
  228. gasControl.deviceId = atoi(DeviceId.c_str());
  229. gasControl.msgId = MSG_ID_INQUIRE;
  230. gasControl.data = DATA_INQUIRE;
  231. length = 11;
  232. serialization(gasControl, (char *)data);
  233. return 0;
  234. }
  235. /**
  236. * @description: 判定msg是否为status
  237. * @param {int} &msg_id
  238. * @return {*}
  239. */
  240. bool getStatusMsg(int &msg_id){
  241. if(msg_id != MSG_ID_INQUIRE_ACK) return false;
  242. return true;
  243. }
  244. /**
  245. * @description: 构建设备信息
  246. * @param {SendDevice&} sendDevice
  247. * @return {*}
  248. */
  249. void BuildDeviceInfo(SendDevice& sendDevice){
  250. sendDevice.deviceId = atoi(this->getDeviceId().c_str());
  251. sendDevice.commandCode = "1";
  252. sendDevice.status = 1;
  253. sendDevice.workStatus = 1;
  254. sendDevice.powerStatus = 1;
  255. sendDevice.batteryStatus = 1;
  256. sendDevice.num = 0;
  257. sendDevice.oxyPressure = 0;
  258. sendDevice.gasPressure = 0;
  259. sendDevice.remark = "";
  260. sendDevice.attr1 = std::to_string(this->voltage); // 电压
  261. sendDevice.attr2 = std::to_string(this->pressure); // 声压
  262. }
  263. };