EgoClient.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <windows.h>
  3. #include <json/json.h>
  4. #include <fstream>
  5. #include "api.h"
  6. /*#include "../common/types.h"*/
  7. #include "PCANBasic.h"
  8. #include "include/EgoInterface.h"
  9. #include "EgoWindow.h"
  10. #include "../common/iobuffer.h"
  11. #include "../common/sensor_socket.h"
  12. #include "../common/peer_connection.h"
  13. #include "car_sim.h"
  14. #include "null_sim.h"
  15. #include "control_sensor.h"
  16. #include "radar_sim.h"
  17. #include "out_sim.h"
  18. #include "lidar_sim.h"
  19. #include "EgoClient.h"
  20. CEgoClient::CEgoClient(IEgoNotify* n) :_notify(n)
  21. {
  22. _carpeer = -1;
  23. }
  24. void CEgoClient::Start(std::array<IRender*, RenderPosition::ALL>& ar)
  25. {
  26. _connected = false;
  27. Json::Value root;
  28. Json::Reader jsonReader;
  29. std::ifstream ifile("Config.json");
  30. std::string ip;
  31. std::string motion_ip;
  32. int32_t motion_port;
  33. std::string host;
  34. int32_t Tcphost_port;
  35. int32_t Tcpremote_port;
  36. //int32_t can_port = 0, host_port = 0;
  37. //std::string can_ip;
  38. if (jsonReader.parse(ifile, root))
  39. {
  40. _accountText = root["account"].asString();
  41. _passText = root["password"].asString();
  42. _name = root["name"].asString();
  43. //_autoclose = root["auto_close"].asBool();
  44. ip = root["server"].asString();
  45. host = root["host_in"].asString();
  46. motion_ip = root["motion_ip"].asString();
  47. motion_port = root["motion_port"].asInt();
  48. Tcphost_port = root["Tcphost_port"].asInt();
  49. Tcpremote_port = root["Tcpremote_port"].asInt();
  50. //can_port = root["can_port"].asInt();
  51. //host_port = root["host_port"].asInt();
  52. //can_ip = root["can_ip"].asString();
  53. }
  54. _client = std::make_unique<SocketClient>(this);
  55. //_client->Start(ip.c_str());
  56. _client->Start(ip.c_str(), Tcpremote_port, Tcphost_port);
  57. for (int i = 0; i < RenderPosition::ALL; i++)
  58. {
  59. auto p = std::make_unique<CEgoWindow>(this,ar[i], (RenderPosition)i);
  60. p->Start();
  61. _WindowArray.push_back(std::move(p));
  62. }
  63. //CManipulationSensor* sensor = new CManipulationSensor(_WindowArray[RenderPosition::FRONT_BACK].get());
  64. _can = std::make_unique<CControlSensor>(_WindowArray[RenderPosition::FRONT].get());
  65. _can->Start();
  66. _carsim = std::make_unique<CCarSim>(this);
  67. _carsim->Start();
  68. _outsim = std::make_unique<COutSim>(this, motion_ip.c_str(), motion_port);
  69. _outsim->Start();
  70. _radarsim = std::make_unique<CRadarSim>(this);
  71. _radarsim->Start();
  72. // _lidarsim = std::make_unique<CLidarSim>(this);
  73. // _lidarsim->Start();
  74. OnRadarData(0, 0, 0, 0, 0, 0, 0, 0);
  75. OnImuData(0.0, 0.0);
  76. }
  77. int32_t CEgoClient::GetSteerAngle()
  78. {
  79. return _steer_angle;
  80. }
  81. void CEgoClient::SetReady(bool b)
  82. {
  83. for (auto i = 0; i < RenderPosition::ALL; i++)
  84. _WindowArray[i]->SetReady(b);
  85. }
  86. void CEgoClient::Login(std::string& account, std::string& pass)
  87. {
  88. _account = account;
  89. _pass = pass;
  90. if(_connected)
  91. _client->WriteSign(_account.c_str(), _pass.c_str());
  92. }
  93. //传递所有车辆信息到Ego类中
  94. void CEgoClient::OnRobot(const RemoNet::Robot& robot)
  95. {
  96. auto users = std::make_unique<UserCamera>();
  97. users->uid = robot.rid();
  98. users->name = robot.name();
  99. users->type = static_cast<EgoType>(robot.type());
  100. users->state = static_cast<UserState>(robot.state());
  101. users->carType=static_cast<CarType>(robot.cartype());
  102. //调用Ego.h中的OnRobot
  103. _notify->OnRobot(users);
  104. _userInfo.push_back(std::move(users));
  105. }
  106. void CEgoClient::OnNotifyState(int32_t rid, UserState state)
  107. {
  108. for (auto& node : _userInfo)
  109. {
  110. if (node->uid == rid)
  111. {
  112. node->state = state;
  113. _notify->OnNotifyState(rid, state);
  114. break;
  115. }
  116. }
  117. }
  118. void CEgoClient::OnConnected(bool bRet)
  119. {
  120. if (bRet)
  121. {
  122. _connected = true;
  123. if (!_account.empty())
  124. _client->WriteSign(_account.c_str(),_pass.c_str());
  125. _updateThread.start(_client.get());
  126. }
  127. else
  128. {
  129. if (_connected)
  130. {
  131. _updateThread.stop();
  132. }
  133. _connected = false;
  134. if (_carpeer != -1)
  135. {
  136. for (int32_t i = RenderPosition::FRONT; i < RenderPosition::ALL; i++)
  137. {
  138. _WindowArray[i]->SetReady(false);
  139. _WindowArray[i]->OnNotifyLeave();
  140. }
  141. _carpeer = -1;
  142. }
  143. _userInfo.clear();
  144. }
  145. }
  146. void CEgoClient::OnVideoRep(bool ok,int32_t index, int32_t peer)
  147. {
  148. if (ok == false)
  149. {
  150. _carpeer = -1;
  151. _notify->OnNotifyVideoFail(peer);
  152. return;
  153. }
  154. if (peer != _carpeer) return;
  155. for (auto& node : _userInfo)
  156. {
  157. if (node->uid == peer)
  158. {
  159. /* if (index == RenderPosition::FRONT)
  160. _notify->OpenFullView(node->viewurl.c_str());
  161. */
  162. _WindowArray[index]->PostMessage(WM_NOTIFY_REP, (int64_t)peer);
  163. }
  164. }
  165. }
  166. void CEgoClient::OnVideoOffer(int32_t index, const char* type, const char* sdp)
  167. {
  168. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__,__LINE__);
  169. OfferDesc* desc = (OfferDesc*)pBuffer->Buffer;
  170. strcpy_s(desc->type, type);
  171. strcpy_s(desc->sdp, sdp);
  172. _WindowArray[index]->PostMessage(WM_NOTIFY_OFFER, (int64_t)pBuffer);
  173. }
  174. void CEgoClient::OnVideoAnswer(int32_t index, const char* type, const char* sdp)
  175. {
  176. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  177. AnswerDesc* p = (AnswerDesc*)(pBuffer->Buffer);
  178. strcpy_s(p->type, type);
  179. strcpy_s(p->sdp, sdp);
  180. _WindowArray[index]->PostMessage(WM_NOTIFY_ANSWER, (int64_t)pBuffer);
  181. // _WindowArray[index]->DelayDataChannel();
  182. index++;
  183. if (index < RenderPosition::ALL)
  184. {
  185. _WindowArray[index]->PostMessage(WM_ASK_VIDEOREQ);// DelayNextVideoReq();
  186. }
  187. else
  188. {
  189. _WindowArray[RenderPosition::FRONT]->PostMessage(WM_ASK_PING);// DelayStartPing();
  190. }
  191. }
  192. void CEgoClient::OnVideoCandidate(int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid)
  193. {
  194. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  195. CandidateDesc* desc = (CandidateDesc*)(pBuffer->Buffer);
  196. strcpy_s(desc->candidate, candidate);
  197. strcpy_s(desc->sdp_mid, sdp_mid);
  198. desc->sdp_mline_index = sdp_mline_index;
  199. _WindowArray[index]->PostMessage(WM_NOTIFY_CANDIDATE, (int64_t)pBuffer);
  200. }
  201. void CEgoClient::OnVideoLeave(int32_t peer, EgoType type)
  202. {
  203. if (type == EgoType::Car)
  204. {
  205. _WindowArray[ChannelType::CHANNEL_CAR]->SetControlState(ControlState::Check);
  206. for (int32_t i = RenderPosition::FRONT; i < RenderPosition::ALL; i++)
  207. {
  208. _WindowArray[i]->SetReady(false);
  209. _WindowArray[i]->OnNotifyLeave();
  210. }
  211. _carpeer = -1;
  212. _notify->OnNotifyLeave(peer);
  213. }
  214. }
  215. void CEgoClient::OnNotifyKick()
  216. {
  217. _notify->OnNotifyKickOff();
  218. }
  219. void CEgoClient::OnNDTPos(Position* pos)
  220. {
  221. _notify->OnNDTPos(pos);
  222. }
  223. void CEgoClient::OnMessageFrameNotify(ChannelType type, int16_t cmd, int16_t length, const void* data)
  224. {
  225. switch (type)
  226. {
  227. case ChannelType::CHANNEL_CAR:
  228. {
  229. _carsim->OnPeerMessage(cmd, length, data);
  230. }
  231. break;
  232. case ChannelType::CHANNEL_IMU:
  233. _outsim->OnPeerMessage(cmd, length, data);
  234. break;
  235. case ChannelType::CHANNEL_RADAR:
  236. _radarsim->OnPeerMessage(cmd, length, data);
  237. break;
  238. //case ChannelType::CHANNEL_LIDAR:
  239. // _lidarsim->OnPeerMessage(cmd, length, data);
  240. // break;
  241. }
  242. }
  243. void CEgoClient::OnSigin(int32_t uid,bool bRet,std::string user_uuid)
  244. {
  245. _uid = uid;
  246. _notify->OnSigin(bRet);
  247. if (bRet)
  248. {
  249. _client->WriteRobotReq();
  250. }
  251. CControlSensor::setSCSignUuid(user_uuid);
  252. }
  253. void CEgoClient::OnFeedPage(FeedData& data)
  254. {
  255. _steer_angle = data.steer_angle;
  256. _can->SetEngineRPM(data.engine_rpm);
  257. _notify->OnNotifyFeed(data);
  258. }
  259. void CEgoClient::OnNotifyDel(int32_t peer, EgoType type)
  260. {
  261. for (auto it = _userInfo.begin(); it != _userInfo.end(); ++it)
  262. {
  263. if ((*it)->uid == peer)
  264. {
  265. _userInfo.erase(it);
  266. break;
  267. }
  268. }
  269. _notify->OnNotifyDel(peer);
  270. }
  271. //车辆连接成功后发送摄像头视频请求
  272. void CEgoClient::OnCarConnect(int32_t peer)
  273. {
  274. CControlSensor::setCarID(peer);
  275. mrsWebrtcCreateFactory(false);
  276. for (int i = 0; i < RenderPosition::ALL; i++)
  277. {
  278. _WindowArray[i]->SetPeer(peer);
  279. }
  280. for (auto& node : _userInfo)
  281. {
  282. if (node->uid == peer)
  283. {
  284. _WindowArray[RenderPosition::FRONT]->OnAskVideoReq();
  285. _carpeer = peer;
  286. break;
  287. }
  288. }
  289. }
  290. void CEgoClient::OnCarLeave()
  291. {
  292. /*
  293. RemoNet::SensorStop Req;
  294. CIOBuffer Buffer;
  295. MessageHead Head;
  296. Head.Command = RemoNet::CC_SensorStop;
  297. Head.Length = Req.ByteSizeLong();
  298. Head.Serialize(Buffer.Buffer);
  299. auto ptr = Buffer.Buffer + MessageHead::Size();
  300. Req.SerializeToArray(ptr, Head.Length);
  301. Buffer.Length = Head.Length + MessageHead::Size();
  302. _WindowArray[ChannelType::CHANNEL_CAR]->StopPing();
  303. _WindowArray[ChannelType::CHANNEL_CAR]->SendData(&Buffer);
  304. */
  305. // _notify->CloseFullView();
  306. _WindowArray[ChannelType::CHANNEL_CAR]->SetControlState(ControlState::Check);
  307. _WindowArray[ChannelType::CHANNEL_CAR]->StopPing();
  308. _client->WriteVideoLeave(EgoType::Car, _carpeer);
  309. for (int32_t i = 0; i < RenderPosition::ALL; i++)
  310. {
  311. _WindowArray[i]->OnNotifyLeave();
  312. }
  313. _carpeer = -1;
  314. }
  315. void CEgoClient::OnStopAck()
  316. {
  317. _client->WriteVideoLeave(EgoType::Car, _carpeer);
  318. for (int32_t i = 0; i < RenderPosition::ALL; i++)
  319. {
  320. _WindowArray[i]->SetReady(false);
  321. _WindowArray[i]->OnNotifyLeave();// PostMessage(WM_NOTIFY_LEAVE);
  322. }
  323. _carpeer = -1;
  324. }
  325. void CEgoClient::OnRadarData(int32_t r0, int32_t r1, int32_t r2, int32_t r3, int32_t r4, int32_t r5, int32_t r6, int32_t r7)
  326. {
  327. _notify->OnNotifyRadar(r0, r1, r2, r3, r4, r5,r6,r7);
  328. }
  329. void CEgoClient::OnImuData(float x, float y)
  330. {
  331. printf("================IMU==================");
  332. printf("%s %d == %f,%f", __FUNCTION__, __LINE__, x, y);
  333. _notify->OnNotifyImu(x, y);
  334. }
  335. void CEgoClient::SendCanDate(TPCANMsg* CANMsg)
  336. {
  337. _can->SendCANMessage(CANMsg);
  338. }
  339. void CEgoClient::OnEncodeData(int32_t left, int32_t right)
  340. {
  341. _notify->OnNotifyEncode(left, right);
  342. }
  343. void CEgoClient::ReqCarList()
  344. {
  345. _client->WriteRobotReq();
  346. }
  347. void CEgoClient::OnPingValue(int32_t value,double temp)
  348. {
  349. _notify->OnNotifyPing(value,temp);
  350. }
  351. /*
  352. void CEgoClient::OnLidarData(bool isLeft, bool isDense, int32_t seq, PointXYZI* data, int32_t length)
  353. {
  354. _notify->OnLidarData(isLeft, isDense, seq, data, length);
  355. }
  356. */
  357. void CEgoClient::OnMoveBegin(int32_t rid, WorkArea area, int32_t no)
  358. {
  359. RemoNet::CSMoveBegin Req;
  360. Req.set_peer(rid);
  361. Req.set_area(area);
  362. Req.set_no(no);
  363. MessageHead Head;
  364. CIOBuffer pBuffer;
  365. Head.Command = RemoNet::CS_MoveBegin;
  366. Head.Length = Req.ByteSizeLong();
  367. Head.Serialize(pBuffer.Buffer);
  368. auto ptr = pBuffer.Buffer + MessageHead::Size();
  369. Req.SerializeToArray(ptr, Head.Length);
  370. pBuffer.Length = MessageHead::Size() + Head.Length;
  371. _client->Write(&pBuffer);
  372. }
  373. /*
  374. void CEgoClient::SwitchDriver(int32_t id)
  375. {
  376. RemoNet::SwitchDriver Req;
  377. Req.set_peer(id);
  378. MessageHead Head;
  379. CIOBuffer pBuffer;
  380. Head.Command = RemoNet::CS_SwitchDriver;
  381. Head.Length = Req.ByteSizeLong();
  382. Head.Serialize(pBuffer.Buffer);
  383. auto ptr = pBuffer.Buffer + MessageHead::Size();
  384. Req.SerializeToArray(ptr, Head.Length);
  385. pBuffer.Length = MessageHead::Size() + Head.Length;
  386. _client->Write(&pBuffer);
  387. }
  388. */
  389. /*void CEgoClient::ChangeState(UserState state)
  390. {
  391. RemoNet::CSState Req;
  392. Req.set_state((RemoNet::UserState)state);
  393. Req.set_uid(_uid);
  394. MessageHead Head;
  395. CIOBuffer pBuffer;
  396. Head.Command = RemoNet::CS_State;
  397. Head.Length = Req.ByteSizeLong();
  398. Head.Serialize(pBuffer.Buffer);
  399. auto ptr = pBuffer.Buffer + MessageHead::Size();
  400. Req.SerializeToArray(ptr, Head.Length);
  401. pBuffer.Length = MessageHead::Size() + Head.Length;
  402. _client->Write(&pBuffer);
  403. }*/
  404. void CEgoClient::ChangeState(int carID,UserState state)
  405. {
  406. RemoNet::CSState Req;
  407. Req.set_state((RemoNet::UserState)state);
  408. Req.set_uid(carID);
  409. MessageHead Head;
  410. CIOBuffer pBuffer;
  411. Head.Command = RemoNet::CS_State;
  412. Head.Length = Req.ByteSizeLong();
  413. Head.Serialize(pBuffer.Buffer);
  414. auto ptr = pBuffer.Buffer + MessageHead::Size();
  415. Req.SerializeToArray(ptr, Head.Length);
  416. pBuffer.Length = MessageHead::Size() + Head.Length;
  417. _client->Write(&pBuffer);
  418. }
  419. void CEgoClient::OnMoveEnd(int32_t rid, WorkArea area, int32_t no)
  420. {
  421. _notify->OnNotifyMoveEnd(rid, area, no);
  422. }
  423. void CEgoClient::OnNotifyMoveRet(MoveDesc desc)
  424. {
  425. _notify->OnNotifyMoveRet(desc);
  426. }
  427. ControlStatus CEgoClient::CheckStatus()
  428. {
  429. return _can->CheckStatus();
  430. }
  431. /*
  432. void CEgoClient::OnEmergency(bool enable, bool motor, bool gear, bool turnl, bool turnR)
  433. {
  434. _notify->OnEmergency(enable, motor, gear, turnl, turnR);
  435. }
  436. void CEgoClient::OnAccel(int16_t accel , bool onoff)
  437. {
  438. _notify->OnAccel(accel, onoff);
  439. }
  440. void CEgoClient::OnSteer(uint64_t steer)
  441. {
  442. _notify->OnSteer(steer);
  443. }
  444. void CEgoClient::OnArm(int16_t flip, int16_t armL, int16_t armR, bool onoff)
  445. {
  446. _notify->OnArm(flip, armL, armR, onoff);
  447. }
  448. */
  449. SocketClient* CEgoClient::GetSocketClient()
  450. {
  451. return _client.get();
  452. }
  453. std::string CEgoClient::GetAccount()
  454. {
  455. return _accountText;
  456. }
  457. std::string CEgoClient::GetPassword()
  458. {
  459. return _passText;
  460. }
  461. std::string CEgoClient::GetName()
  462. {
  463. return _name;
  464. }
  465. /*
  466. bool CEgoClient::IsAutoClose()
  467. {
  468. return _autoclose;
  469. }
  470. void CEgoClient::SetCurrentPage(PageProp prop)
  471. {
  472. _currentpage = prop;
  473. }
  474. PageProp CEgoClient::GetCurrentPage()
  475. {
  476. return _currentpage;
  477. }
  478. */
  479. //add-wfg
  480. void CEgoClient::OnCautionLight(bool cautionLight)
  481. {
  482. _notify->OnCautionLight(cautionLight);
  483. }
  484. void CEgoClient::OnLightL(bool lightL)
  485. {
  486. _notify->OnLightL(lightL);
  487. }
  488. void CEgoClient::OnLightR(bool lightR)
  489. {
  490. _notify->OnLightR(lightR);
  491. }
  492. void CEgoClient::OnGearF(bool gearF)
  493. {
  494. _notify->OnGearF(gearF);
  495. }
  496. void CEgoClient::OnGearR(bool gearR)
  497. {
  498. _notify->OnGearR(gearR);
  499. }
  500. void CEgoClient::OnCarPark(bool carPark)
  501. {
  502. _notify->OnCarPark(carPark);
  503. }
  504. void CEgoClient::OnBuzzerPlay(bool buzzerPlay)
  505. {
  506. _notify->OnBuzzerPlay(buzzerPlay);
  507. }
  508. void CEgoClient::OnLightFront(bool lightFront)
  509. {
  510. _notify->OnLightFront(lightFront);
  511. }
  512. void CEgoClient::OnLightWork(bool lightWork)
  513. {
  514. _notify->OnLightWork(lightWork);
  515. }
  516. void CEgoClient::OnEmergency(bool emergency)
  517. {
  518. _notify->OnEmergency(emergency);
  519. }
  520. void CEgoClient::OnMqttData(cannet_frame* frames)
  521. {
  522. _notify->OnMqttData(frames);
  523. }