tcp_client5084.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * tcp_client.cpp
  3. *
  4. * Created on: 2022年10月5日
  5. * Author: Allen
  6. */
  7. #include "tcp_client5084.h"
  8. #include "entry/EasyuiContext.h"
  9. #include "utils/Log.h"
  10. #include "utils/ByteUtil.h"
  11. #include "base/os.hpp"
  12. #include "base/strings.hpp"
  13. #include <system/Thread.h>
  14. #include <string>
  15. #include "net/NetManager.h"
  16. #include "service/BusinessConfig.h"
  17. static std::string sn = "";
  18. static byte code[4] = {0};
  19. static int dataConut = 0;
  20. #define ETHERNETMANAGER NETMANAGER->getEthernetManager()
  21. //异或校验 返回一个字节
  22. unsigned char CheckXor(const char *strData, int len)
  23. {
  24. char checksum = 0;
  25. for (int i = 0;i < len;i++)
  26. {
  27. checksum = checksum ^ strData[i];
  28. }
  29. return (unsigned char)checksum;
  30. }
  31. void handleMsg(byte* msg);
  32. TcpClient5084::TcpClient5084() {
  33. busy_ = false;
  34. }
  35. TcpClient5084::~TcpClient5084() {
  36. }
  37. TcpClient5084* TcpClient5084::instance() {
  38. static TcpClient5084 singleton;
  39. return &singleton;
  40. }
  41. static net::Conn* conn;
  42. void TcpClient5084::sendMsg(byte* msg, int size){
  43. instance()->internalSendMsg(msg, size);
  44. }
  45. void TcpClient5084::internalSendMsg(byte* msg, int size){
  46. if (busy_) {
  47. LOGD("TcpClient5084 client is busy");
  48. return;
  49. }
  50. busy_ = true;
  51. if (conn){
  52. conn->Write(msg, size);
  53. // char _buf[1024];
  54. // std::string msgStr = "";
  55. // for (int i = 0; i < size; ++i) {
  56. // sprintf(_buf, "%02x", msg[i]);
  57. // msgStr += _buf;
  58. // }
  59. //
  60. // LOGD("TcpClient5084 sended msg : %s", msgStr.c_str());
  61. } else {
  62. LOGD("tcp disconnect");
  63. }
  64. busy_ = false;
  65. }
  66. static bool isLogin = false;
  67. static bool isWhile = true;
  68. class SleepThread: public Thread {
  69. public:
  70. /**
  71. * 线程创建成功后会调用该函数,可以在该函数中做一些初始化操作
  72. * return true 继续线程
  73. * false 退出线程
  74. */
  75. virtual bool readyToRun() {
  76. LOGD("Thread 已经创建完成");
  77. return true;
  78. }
  79. /**
  80. * 线程循环函数
  81. *
  82. * return true 继续线程循环
  83. * false 推出线程
  84. */
  85. virtual bool threadLoop() {
  86. LOGD("线程循环函数");
  87. //检查是否有退出线程的请求,如果有,则返回false,立即退出线程
  88. if (exitPending()) {
  89. return false;
  90. }
  91. conn = net::Dial("tcp", "192.168.1.199:5084");
  92. if (conn) {
  93. byte buf[1024] = {0};
  94. int v0;
  95. std::string _dataCount = std::to_string(dataConut);
  96. sscanf(_dataCount.c_str(), "%2X", &v0);
  97. byte loginData[12] = {0};
  98. loginData[0] = 0x82;
  99. loginData[1] = 0x0A;
  100. loginData[2] = 0x01;
  101. loginData[3] = v0;
  102. loginData[4] = 0x03;
  103. for (int i = 0; i < sn.size(); i += 2) {
  104. std::string snStr = sn.substr(i, 2);
  105. sscanf(snStr.c_str(), "%2X", &v0);
  106. loginData[5 + i/2] = v0;
  107. }
  108. loginData[11] = CheckXor((char*) loginData, 11);
  109. TcpClient5084::instance()->sendMsg(loginData, 12);
  110. isWhile = true;
  111. dataConut += 1;
  112. while (isWhile && !exitPending()) {
  113. //读取,超时1000毫秒
  114. int n = conn->Read(buf, sizeof(buf) - 1, 1000);
  115. if (n > 0) {
  116. buf[n] = 0;
  117. char _buf[1024];
  118. if (buf[4] == 0x03) {
  119. code[0] = buf[5];
  120. code[1] = buf[6];
  121. code[2] = buf[7];
  122. code[3] = buf[8];
  123. isLogin = true;
  124. }
  125. } else if (n == 0) {
  126. LOGD("连接正常断开");
  127. isLogin = false;
  128. isWhile = false;
  129. break;
  130. } else if (n == net::E_TIMEOUT) {
  131. // LOGD("读取超时");
  132. } else {
  133. LOGD("出错");
  134. isLogin = false;
  135. isWhile = false;
  136. break;
  137. }
  138. }
  139. //关闭连接
  140. conn->Close();
  141. //释放内存
  142. delete conn;
  143. conn = NULL;
  144. }
  145. //休眠5s
  146. usleep(5000 * 1000);
  147. //返回真,继续下次线程循环
  148. return isLogin;
  149. }
  150. };
  151. static SleepThread sleep_thread;
  152. void TcpClient5084::dataParse(std::string data) {
  153. // LOGD("data =================> %s", data.c_str());
  154. if (data.substr(2, 2) == "0f") {
  155. // cd0f0076000004643320504168100064e401a601e301a501df01a501e601a601e101a501e101a401e401a601e301a401e401a301e401a401e101a601e201a501e301a301e401a401e101a401df01a401e301a501e301a501e201a401e401a401e101a401e101a501e201a301e201a501e201a3015631303032dc
  156. sn = data.substr(16, 12);
  157. if (!isLogin) {
  158. TcpClient5084::instance()->startTcp();
  159. }
  160. if (!isLogin) {
  161. return;
  162. }
  163. // int v0;
  164. // std::string _dataCount = std::to_string(dataConut);
  165. // sscanf(_dataCount.c_str(), "%2X", &v0);
  166. byte secondData[2048] = {0};
  167. secondData[0] = 0x82;
  168. secondData[1] = 0x16;
  169. secondData[2] = 0x01;
  170. secondData[3] = ByteUtil::stringToByte(std::to_string(dataConut));
  171. secondData[4] = 0x0E;
  172. time_t date = time(NULL);
  173. std::string dateStr = ByteUtil::timestampToHex(date - (8 * 3600));
  174. for (int i = 0; i < dateStr.size(); i += 2) {
  175. std::string _dateStr = dateStr.substr(i, 2);
  176. secondData[5 + i/2] = ByteUtil::stringToByte(_dateStr);
  177. }
  178. std::string healthyData = "";
  179. // 心率
  180. healthyData = data.substr(8, 2);
  181. secondData[9] = ByteUtil::stringToByte(healthyData);
  182. // 呼吸
  183. healthyData = data.substr(10, 2);
  184. secondData[10] = ByteUtil::stringToByte(healthyData);
  185. // 状态
  186. healthyData = data.substr(12, 2);
  187. secondData[11] = ByteUtil::stringToByte(healthyData);
  188. // 电量
  189. healthyData = data.substr(14, 2);
  190. secondData[12] = ByteUtil::stringToByte(healthyData);
  191. for (int i = 0; i < sn.size(); i += 2) {
  192. std::string snStr = sn.substr(i, 2);
  193. secondData[13 + i/2] = ByteUtil::stringToByte(snStr);
  194. }
  195. secondData[19] = code[0];
  196. secondData[20] = code[1];
  197. secondData[21] = code[2];
  198. secondData[22] = code[3];
  199. secondData[23] = CheckXor((char*) secondData, 23);
  200. TcpClient5084::instance()->sendMsg(secondData, 24);
  201. }
  202. else if (data.substr(2, 2) == "3e") {
  203. //cd3e000c620b0664013320504168108f
  204. // LOGD("=========================> 每分钟传输");
  205. sn = data.substr(18, 12);
  206. if (!isLogin) {
  207. TcpClient5084::instance()->startTcp();
  208. }
  209. if (!isLogin) {
  210. return;
  211. }
  212. byte secondData[2048] = {0};
  213. secondData[0] = 0x82;
  214. secondData[1] = 0x17;
  215. secondData[2] = 0x01;
  216. secondData[3] = ByteUtil::stringToByte(std::to_string(dataConut));
  217. secondData[4] = 0x4E;
  218. time_t date = time(NULL);
  219. std::string dateStr = ByteUtil::timestampToHex(date - (8 * 3600));
  220. for (int i = 0; i < dateStr.size(); i += 2) {
  221. std::string _dateStr = dateStr.substr(i, 2);
  222. secondData[5 + i/2] = ByteUtil::stringToByte(_dateStr);
  223. }
  224. std::string healthyData = "";
  225. // 心率
  226. healthyData = data.substr(8, 2);
  227. secondData[9] = ByteUtil::stringToByte(healthyData);
  228. // 呼吸
  229. healthyData = data.substr(10, 2);
  230. secondData[10] = ByteUtil::stringToByte(healthyData);
  231. // 状态
  232. healthyData = data.substr(12, 2);
  233. secondData[11] = ByteUtil::stringToByte(healthyData);
  234. // 电量
  235. healthyData = data.substr(14, 2);
  236. secondData[12] = ByteUtil::stringToByte(healthyData);
  237. // 睡眠分层
  238. healthyData = data.substr(16, 2);
  239. secondData[13] = ByteUtil::stringToByte(healthyData);
  240. for (int i = 0; i < sn.size(); i += 2) {
  241. std::string snStr = sn.substr(i, 2);
  242. secondData[14 + i/2] = ByteUtil::stringToByte(snStr);
  243. }
  244. secondData[20] = code[0];
  245. secondData[21] = code[1];
  246. secondData[22] = code[2];
  247. secondData[23] = code[3];
  248. secondData[24] = CheckXor((char*) secondData, 24);
  249. TcpClient5084::instance()->sendMsg(secondData, 25);
  250. }
  251. dataConut += 1;
  252. if (dataConut >= 255) {
  253. dataConut = 0;
  254. }
  255. // std::string sn = data.substr(16, 29);
  256. }
  257. void TcpClient5084::startTcp(){
  258. //调用线程类的run函数启动线程, 参数为线程名,可以任意指定。
  259. sleep_thread.run("tcp thread");
  260. }
  261. void TcpClient5084::closeTcp() {
  262. bool result = sleep_thread.isRunning();
  263. if (result) {
  264. sleep_thread.requestExitAndWait();
  265. LOGD("my_thread已关闭");
  266. }
  267. // my_thread.requestExit();
  268. }
  269. bool TcpClient5084::busy() {
  270. return instance()->busy_;
  271. }
  272. bool TcpClient5084::connected() {
  273. return conn != NULL;
  274. }