deviceGasV1.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #pragma once
  2. #include <iostream>
  3. #include "deviceBase.h"
  4. #include "Crc.h"
  5. using namespace std;
  6. // 煤气驱鸟炮
  7. #define EXPEL_DATA_HEAD (0xFE) // 数据头
  8. #define EXPEL_HEART_BEAT (0x01) // 心跳
  9. #define EXPEL_GENERAL_ACK (0X02) // 通用应答
  10. #define EXPEL_SYS_STATE (0x14) // 获取设备信息
  11. #define EXPEL_SYS_STATE_ACK (0X15) // 获取设备信息应答
  12. #define EXPEL_POWER_OUTPUT_CTRL (0X16) // 电源输出控制 <-: 通用应答
  13. #define EXPEL_POWER_OUTPUT_ON (0x01) // 电源开启
  14. #define EXPEL_POWER_OUTPUT_OFF (0x00) // 电源关闭
  15. #define EXPEL_BOMB_CTRL (0X17) // 煤气炮控制 <-: 通用应答
  16. #define EXPEL_BOMB_ON (0X01) // 煤气炮开炮
  17. #define EXPEL_GET_BOMB_STATE (0X18) // 获取开炮结果
  18. #define EXPEL_GET_BOMB_STATE_ACK (0X19) // 煤气炮开炮应答 <-:通用应答(因为状态为阶段状态,过时会丢失状态。需确认应答收到,未收到应带会重传3次
  19. class deviceGasV1:public deviceBase
  20. {
  21. private:
  22. unsigned short voltage;
  23. short current;
  24. short pressure;
  25. uint8_t power_out_state;
  26. private:
  27. enum RECV_STEP{
  28. STEP_HEAD = 0,
  29. STEP_LEN = 1,
  30. STEP_SEQ = 2,
  31. STEP_SAddr = 3,
  32. STEP_DAddr = 4,
  33. STEP_MSGId = 5,
  34. STEP_DATA = 6
  35. };
  36. typedef struct {
  37. uint8_t dataCount;
  38. uint8_t dataLength;
  39. uint8_t dataCheckSum;
  40. uint8_t data[15];
  41. enum RECV_STEP step = STEP_HEAD;
  42. }ExpelComm;
  43. ExpelComm _comm;
  44. public:
  45. deviceGasV1():deviceBase()
  46. {
  47. this->setDeviceToken(serial);
  48. this->setDeviceInfo("北京大兴机场 民航小板协议");
  49. this->setBaudRate(115200);
  50. }
  51. ~deviceGasV1(){}
  52. void setVoltage(unsigned short Voltage){
  53. voltage = Voltage;
  54. }
  55. void setCurent(short Current){
  56. current = Current;
  57. }
  58. void setPressure(short Pressure){
  59. pressure = Pressure;
  60. }
  61. void setPowerOutState(uint8_t PowerOutState){
  62. power_out_state = PowerOutState;
  63. }
  64. unsigned short getVoltage(){
  65. return voltage;
  66. }
  67. short getCurent(){
  68. return current;
  69. }
  70. short getPressure(){
  71. return pressure;
  72. }
  73. uint8_t getPowerOutState(){
  74. return power_out_state;
  75. }
  76. void serialization(){}
  77. bool Consumer(uint8_t *data, int len, int &msg_id)
  78. {
  79. bool ReceiveSuccessful = false;
  80. if(this->Type != 1 && len > 0) return true;
  81. for (int i = 0; i < len; i++)
  82. {
  83. switch(_comm.step){
  84. case STEP_HEAD:
  85. if(data[i] == EXPEL_DATA_HEAD){
  86. _comm.data[0] = EXPEL_DATA_HEAD;
  87. _comm.step = STEP_LEN;
  88. }
  89. break;
  90. case STEP_LEN:
  91. _comm.data[1] = data[i];
  92. _comm.dataLength = data[i];
  93. _comm.step = STEP_SEQ;
  94. break;
  95. case STEP_SEQ:
  96. _comm.data[2] = data[i];
  97. _comm.step = STEP_SAddr;
  98. break;
  99. case STEP_SAddr:
  100. _comm.data[3] = data[i];
  101. _comm.step = STEP_DAddr;
  102. break;
  103. case STEP_DAddr:
  104. _comm.data[4] = data[i];
  105. _comm.step = STEP_MSGId;
  106. break;
  107. case STEP_MSGId:
  108. _comm.data[5] = data[i];
  109. _comm.step = STEP_DATA;
  110. _comm.dataCount = 6;
  111. break;
  112. case STEP_DATA:
  113. _comm.data[_comm.dataCount++] = data[i];
  114. if(_comm.dataCount == _comm.dataLength + 2 + 6){
  115. ReceiveSuccessful = true;
  116. _comm.step = STEP_HEAD;
  117. }
  118. break;
  119. default:
  120. _comm.step = STEP_HEAD;
  121. break;
  122. }
  123. }
  124. if(!ReceiveSuccessful) return false;
  125. return deserialization(_comm.data, _comm.dataLength + 2 + 6, msg_id);
  126. }
  127. static void serialization(const GasV1Control gasControl, char* data){
  128. data[0] = EXPEL_DATA_HEAD;
  129. data[1] = 0x01;
  130. data[2] = 0x01;
  131. data[3] = 0xAC;
  132. data[4] = (char)gasControl.deviceId;
  133. data[5] = gasControl.msgId;
  134. data[6] = gasControl.data;
  135. int16_t checksum = Crc::crc_calculate((uint8_t *)(data + 1), data[1] + 4 + 1);
  136. data[7] = (char)(checksum&0x00ff);
  137. data[8] = (char)(checksum >> 8);
  138. data[9] = 0x0D;
  139. data[10] = 0x0A;
  140. }
  141. bool deserialization(){
  142. return true;
  143. }
  144. bool deserialization(const uint8_t *data, int len, int &msg_id){
  145. if(data[0] != EXPEL_DATA_HEAD) return false;
  146. int16_t checksum = Crc::crc_calculate((uint8_t *)(data + 1), data[1] + 4 + 1);
  147. if(data[len - 2] != (uint8_t)(checksum&0x00ff) || data[len - 1] != (uint8_t)(checksum >> 8)){
  148. return false;
  149. }
  150. setDeviceId(std::to_string(data[4]));
  151. msg_id = data[5];
  152. if(msg_id == EXPEL_SYS_STATE_ACK){
  153. // 获取系统状态
  154. voltage = data[6] + (data[7]*255);
  155. current = data[8] + (data[9]*255);
  156. pressure = data[10] + (data[11]*255);
  157. power_out_state = data[12];
  158. }
  159. return true;
  160. }
  161. /**
  162. * @description: 打开
  163. * @param {int} deivceId
  164. * @return {*}
  165. */
  166. int32_t Open(std::string DeviceId, uint8_t* data, int& length){
  167. GasV1Control gasControl;
  168. gasControl.deviceId = atoi(DeviceId.c_str());
  169. gasControl.msgId = EXPEL_POWER_OUTPUT_CTRL;
  170. gasControl.data = EXPEL_POWER_OUTPUT_ON;
  171. length = 11;
  172. serialization(gasControl, (char*)data);
  173. return 0;
  174. }
  175. /**
  176. * @description: 关闭
  177. * @param {int} deivceId
  178. * @return {*}
  179. */
  180. int32_t Close(std::string DeviceId, uint8_t* data, int& length){
  181. GasV1Control gasControl;
  182. gasControl.deviceId = atoi(DeviceId.c_str());
  183. gasControl.msgId = EXPEL_POWER_OUTPUT_CTRL;
  184. gasControl.data = EXPEL_POWER_OUTPUT_OFF;
  185. length = 11;
  186. serialization(gasControl, (char*)data);
  187. return 0;
  188. }
  189. /**
  190. * @description: 开炮
  191. * @param {int} deivceId
  192. * @return {*}
  193. */
  194. int32_t Fire(std::string DeviceId, uint8_t* data, int& length){
  195. // GasV1Control gasControl;
  196. // gasControl.deviceId = atoi(DeviceId.c_str());
  197. // gasControl.msgId = EXPEL_BOMB_CTRL;
  198. // gasControl.data = EXPEL_BOMB_ON;
  199. // length = 11;
  200. // serialization(gasControl, (char*)data);
  201. //return Status(DeviceId, data, length);
  202. return Open(DeviceId, data, length);
  203. }
  204. /**
  205. * @description: 状态
  206. * @param {int} deivceId
  207. * @return {*}
  208. */
  209. int32_t Status(std::string DeviceId, uint8_t* data, int& length){
  210. GasV1Control gasControl;
  211. gasControl.deviceId = atoi(DeviceId.c_str());
  212. gasControl.msgId = EXPEL_SYS_STATE;
  213. gasControl.data = 0x00;
  214. length = 11;
  215. serialization(gasControl, (char*)data);
  216. return 0;
  217. }
  218. /**
  219. * @description: 判定msg是否为status
  220. * @param {int} &msg_id
  221. * @return {*}
  222. */
  223. bool getStatusMsg(int &msg_id){
  224. if(msg_id != EXPEL_SYS_STATE_ACK) return false;
  225. return true;
  226. }
  227. /**
  228. * @description: 构建设备信息
  229. * @param {SendDevice&} sendDevice
  230. * @return {*}
  231. */
  232. void BuildDeviceInfo(SendDevice& sendDevice){
  233. sendDevice.deviceId = atoi(this->getDeviceId().c_str());
  234. sendDevice.commandCode = "1";
  235. sendDevice.status = 1;
  236. sendDevice.workStatus = 1;
  237. sendDevice.powerStatus = 1;
  238. sendDevice.batteryStatus = 1;
  239. sendDevice.num = 0;
  240. sendDevice.oxyPressure = 0;
  241. sendDevice.gasPressure = 0;
  242. sendDevice.remark = "";
  243. sendDevice.attr1 = std::to_string(this->getVoltage()); // 电压
  244. sendDevice.attr2 = std::to_string(this->getCurent()); // 电流
  245. sendDevice.attr3 = std::to_string(this->getPressure()); // 声压
  246. }
  247. };