deviceGasV3.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #ifndef __DEVICEGASV3_HPP_
  2. #define __DEVICEGASV3_HPP_
  3. #include <iostream>
  4. #include "deviceBase.h"
  5. #define DEVICEGASV3_DATA_HEAD (0XC5AB) // 数据头
  6. #define DEVICEGASV3_DATA_DEVTYPE (0x05) // 设备类型
  7. #define DEVICEGASV3_COMMAND_PLAY_NOW 3
  8. #define DEVICEGASV3_COMMAND_PAUSE 4
  9. #define DEVICEGASV3_COMMAND_STOP 5
  10. #define DEVICEGASV3_COMMAND_NEXT 6
  11. #define DEVICEGASV3_COMMAND_UP 7
  12. #define DEVICEGASV3_COMMAND_SET_VOLUME 14
  13. #define DEVICEGASV3_COMMAND_PLAY_SET 15
  14. class deviceGasV3:public deviceBase
  15. {
  16. private:
  17. enum RECV_STEP{
  18. STEP_HEAD_1 = 0,
  19. STEP_HEAD_2 = 1,
  20. STEP_TYPE = 2,
  21. STEP_DEVID = 3,
  22. STEP_COM = 4,
  23. STEP_ACK = 5
  24. };
  25. typedef struct {
  26. uint8_t dataCount;
  27. uint8_t dataLength;
  28. uint8_t dataCheckSum;
  29. uint8_t data[6];
  30. enum RECV_STEP step = STEP_HEAD_1;
  31. }ExpelComm;
  32. ExpelComm _comm;
  33. public:
  34. deviceGasV3():deviceBase(){
  35. this->setDeviceToken(tcp);
  36. this->setDeviceInfo("赛为智能 驱鸟器协议");
  37. }
  38. public:
  39. // 电压
  40. float voltage = 0;
  41. // 气压
  42. float pressure = 0;
  43. // 状态
  44. int state = 0;
  45. // 纬度
  46. float latitude = 0;
  47. // 经度
  48. float longitude = 0;
  49. // LRC校验
  50. static unsigned char check(unsigned char *pSendBuf,unsigned char num)
  51. {
  52. char bylrc = 0,i = 0;
  53. for(i = 0; i < num; i++){
  54. bylrc += pSendBuf[i];
  55. }
  56. return (bylrc & 0x00ff);
  57. }
  58. static void serialization(const GasV1Control gasV1Control,char *data){
  59. data[0] = 0XC5;
  60. data[1] = 0XAB;
  61. data[2] = DEVICEGASV3_DATA_DEVTYPE;
  62. data[3] = gasV1Control.deviceId;
  63. data[4] = gasV1Control.msgId;
  64. data[5] = 0;
  65. data[6] = 0;
  66. data[7] = 0;
  67. data[8] = 0;
  68. data[9] = check((unsigned char *)(data + 2), 7);
  69. }
  70. bool deserialization()
  71. {
  72. return true;
  73. }
  74. bool Consumer(uint8_t *data, int len, int &msg_id)
  75. {
  76. bool ReceiveSuccessful = false;
  77. for (int i = 0; i < len; i++)
  78. {
  79. switch(_comm.step){
  80. case STEP_HEAD_1:
  81. if(data[i] == 0xAB){
  82. _comm.data[0] = 0xAB;
  83. _comm.step = STEP_HEAD_2;
  84. }
  85. break;
  86. case STEP_HEAD_2:
  87. if(data[i] == 0xC5){
  88. _comm.data[1] = 0xC5;
  89. _comm.step = STEP_TYPE;
  90. }
  91. break;
  92. case STEP_TYPE:
  93. _comm.data[2] = data[i];
  94. _comm.step = STEP_DEVID;
  95. break;
  96. case STEP_DEVID:
  97. _comm.data[3] = data[i];
  98. _comm.step = STEP_COM;
  99. break;
  100. case STEP_COM:
  101. _comm.data[4] = data[i];
  102. _comm.step = STEP_ACK;
  103. break;
  104. case STEP_ACK:
  105. _comm.data[5] = data[i];
  106. _comm.step = STEP_HEAD_1;
  107. ReceiveSuccessful = true;
  108. _comm.dataCount = 6;
  109. break;
  110. default:
  111. _comm.step = STEP_HEAD_1;
  112. break;
  113. }
  114. }
  115. if(!ReceiveSuccessful) return false;
  116. return deserialization(_comm.data, _comm.dataCount, msg_id);
  117. }
  118. bool deserialization(const uint8_t *data, int len, int &msg_id){
  119. if(len == 6 && data[5] == 0x01){
  120. this->deviceId = data[3];
  121. return true;
  122. }
  123. return false;
  124. }
  125. /**
  126. * @description: 打开
  127. * @param {int} deivceId
  128. * @return {*}
  129. */
  130. int32_t Open(std::string DeviceId, uint8_t* data, int& length){
  131. GasV1Control gasControl;
  132. gasControl.deviceId = atoi(DeviceId.c_str());
  133. gasControl.msgId = DEVICEGASV3_COMMAND_PLAY_NOW;
  134. gasControl.data = 0x00;
  135. length = 10;
  136. serialization(gasControl, (char *)data);
  137. return 0;
  138. }
  139. /**
  140. * @description: 关闭
  141. * @param {int} deivceId
  142. * @return {*}
  143. */
  144. int32_t Close(std::string DeviceId, uint8_t* data, int& length){
  145. GasV1Control gasControl;
  146. gasControl.deviceId = atoi(DeviceId.c_str());
  147. gasControl.msgId = DEVICEGASV3_COMMAND_STOP;
  148. gasControl.data = 0x00;
  149. length = 10;
  150. serialization(gasControl, (char *)data);
  151. return 0;
  152. }
  153. /**
  154. * @description: 开炮
  155. * @param {int} deivceId
  156. * @return {*}
  157. */
  158. int32_t Fire(std::string DeviceId, uint8_t* data, int& length){
  159. GasV1Control gasControl;
  160. gasControl.deviceId = atoi(DeviceId.c_str());
  161. gasControl.msgId = DEVICEGASV3_COMMAND_PLAY_NOW;
  162. gasControl.data = 0x00;
  163. length = 10;
  164. serialization(gasControl, (char *)data);
  165. return 0;
  166. }
  167. /**
  168. * @description: 状态
  169. * @param {int} deivceId
  170. * @return {*}
  171. */
  172. int32_t Status(std::string DeviceId, uint8_t* data, int& length){
  173. return 0;
  174. }
  175. /**
  176. * @description: 判定msg是否为status
  177. * @param {int} &msg_id
  178. * @return {*}
  179. */
  180. bool getStatusMsg(int &msg_id){
  181. return true;
  182. }
  183. /**
  184. * @description: 构建设备信息
  185. * @param {SendDevice&} sendDevice
  186. * @return {*}
  187. */
  188. void BuildDeviceInfo(SendDevice& sendDevice){
  189. sendDevice.deviceId = atoi(this->getDeviceId().c_str());
  190. sendDevice.commandCode = "1";
  191. sendDevice.status = 1;
  192. sendDevice.workStatus = 1;
  193. sendDevice.powerStatus = 1;
  194. sendDevice.batteryStatus = 1;
  195. sendDevice.num = 0;
  196. sendDevice.oxyPressure = 0;
  197. sendDevice.gasPressure = 0;
  198. sendDevice.remark = "";
  199. sendDevice.attr1 = std::to_string(this->voltage); // 电压
  200. sendDevice.attr2 = std::to_string(this->pressure); // 声压
  201. }
  202. };
  203. #endif