EgoClient.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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)
  244. {
  245. _uid = uid;
  246. _notify->OnSigin(bRet);
  247. if (bRet)
  248. {
  249. _client->WriteRobotReq();
  250. }
  251. }
  252. void CEgoClient::OnFeedPage(FeedData& data)
  253. {
  254. _steer_angle = data.steer_angle;
  255. _can->SetEngineRPM(data.engine_rpm);
  256. _notify->OnNotifyFeed(data);
  257. }
  258. void CEgoClient::OnNotifyDel(int32_t peer, EgoType type)
  259. {
  260. for (auto it = _userInfo.begin(); it != _userInfo.end(); ++it)
  261. {
  262. if ((*it)->uid == peer)
  263. {
  264. _userInfo.erase(it);
  265. break;
  266. }
  267. }
  268. _notify->OnNotifyDel(peer);
  269. }
  270. //车辆连接成功后发送摄像头视频请求
  271. void CEgoClient::OnCarConnect(int32_t peer)
  272. {
  273. mrsWebrtcCreateFactory(false);
  274. for (int i = 0; i < RenderPosition::ALL; i++)
  275. {
  276. _WindowArray[i]->SetPeer(peer);
  277. }
  278. for (auto& node : _userInfo)
  279. {
  280. if (node->uid == peer)
  281. {
  282. _WindowArray[RenderPosition::FRONT]->OnAskVideoReq();
  283. _carpeer = peer;
  284. break;
  285. }
  286. }
  287. }
  288. void CEgoClient::OnCarLeave()
  289. {
  290. /*
  291. RemoNet::SensorStop Req;
  292. CIOBuffer Buffer;
  293. MessageHead Head;
  294. Head.Command = RemoNet::CC_SensorStop;
  295. Head.Length = Req.ByteSizeLong();
  296. Head.Serialize(Buffer.Buffer);
  297. auto ptr = Buffer.Buffer + MessageHead::Size();
  298. Req.SerializeToArray(ptr, Head.Length);
  299. Buffer.Length = Head.Length + MessageHead::Size();
  300. _WindowArray[ChannelType::CHANNEL_CAR]->StopPing();
  301. _WindowArray[ChannelType::CHANNEL_CAR]->SendData(&Buffer);
  302. */
  303. // _notify->CloseFullView();
  304. _WindowArray[ChannelType::CHANNEL_CAR]->SetControlState(ControlState::Check);
  305. _WindowArray[ChannelType::CHANNEL_CAR]->StopPing();
  306. _client->WriteVideoLeave(EgoType::Car, _carpeer);
  307. for (int32_t i = 0; i < RenderPosition::ALL; i++)
  308. {
  309. _WindowArray[i]->OnNotifyLeave();
  310. }
  311. _carpeer = -1;
  312. }
  313. void CEgoClient::OnStopAck()
  314. {
  315. _client->WriteVideoLeave(EgoType::Car, _carpeer);
  316. for (int32_t i = 0; i < RenderPosition::ALL; i++)
  317. {
  318. _WindowArray[i]->SetReady(false);
  319. _WindowArray[i]->OnNotifyLeave();// PostMessage(WM_NOTIFY_LEAVE);
  320. }
  321. _carpeer = -1;
  322. }
  323. 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)
  324. {
  325. _notify->OnNotifyRadar(r0, r1, r2, r3, r4, r5,r6,r7);
  326. }
  327. void CEgoClient::OnImuData(float x, float y)
  328. {
  329. printf("================IMU==================");
  330. printf("%s %d == %f,%f", __FUNCTION__, __LINE__, x, y);
  331. _notify->OnNotifyImu(x, y);
  332. }
  333. void CEgoClient::SendCanDate(TPCANMsg* CANMsg)
  334. {
  335. _can->SendCANMessage(CANMsg);
  336. }
  337. void CEgoClient::OnEncodeData(int32_t left, int32_t right)
  338. {
  339. _notify->OnNotifyEncode(left, right);
  340. }
  341. void CEgoClient::ReqCarList()
  342. {
  343. _client->WriteRobotReq();
  344. }
  345. void CEgoClient::OnPingValue(int32_t value,double temp)
  346. {
  347. _notify->OnNotifyPing(value,temp);
  348. }
  349. /*
  350. void CEgoClient::OnLidarData(bool isLeft, bool isDense, int32_t seq, PointXYZI* data, int32_t length)
  351. {
  352. _notify->OnLidarData(isLeft, isDense, seq, data, length);
  353. }
  354. */
  355. void CEgoClient::OnMoveBegin(int32_t rid, WorkArea area, int32_t no)
  356. {
  357. RemoNet::CSMoveBegin Req;
  358. Req.set_peer(rid);
  359. Req.set_area(area);
  360. Req.set_no(no);
  361. MessageHead Head;
  362. CIOBuffer pBuffer;
  363. Head.Command = RemoNet::CS_MoveBegin;
  364. Head.Length = Req.ByteSizeLong();
  365. Head.Serialize(pBuffer.Buffer);
  366. auto ptr = pBuffer.Buffer + MessageHead::Size();
  367. Req.SerializeToArray(ptr, Head.Length);
  368. pBuffer.Length = MessageHead::Size() + Head.Length;
  369. _client->Write(&pBuffer);
  370. }
  371. /*
  372. void CEgoClient::SwitchDriver(int32_t id)
  373. {
  374. RemoNet::SwitchDriver Req;
  375. Req.set_peer(id);
  376. MessageHead Head;
  377. CIOBuffer pBuffer;
  378. Head.Command = RemoNet::CS_SwitchDriver;
  379. Head.Length = Req.ByteSizeLong();
  380. Head.Serialize(pBuffer.Buffer);
  381. auto ptr = pBuffer.Buffer + MessageHead::Size();
  382. Req.SerializeToArray(ptr, Head.Length);
  383. pBuffer.Length = MessageHead::Size() + Head.Length;
  384. _client->Write(&pBuffer);
  385. }
  386. */
  387. void CEgoClient::ChangeState(UserState state)
  388. {
  389. RemoNet::CSState Req;
  390. Req.set_state((RemoNet::UserState)state);
  391. Req.set_uid(_uid);
  392. MessageHead Head;
  393. CIOBuffer pBuffer;
  394. Head.Command = RemoNet::CS_State;
  395. Head.Length = Req.ByteSizeLong();
  396. Head.Serialize(pBuffer.Buffer);
  397. auto ptr = pBuffer.Buffer + MessageHead::Size();
  398. Req.SerializeToArray(ptr, Head.Length);
  399. pBuffer.Length = MessageHead::Size() + Head.Length;
  400. _client->Write(&pBuffer);
  401. }
  402. void CEgoClient::OnMoveEnd(int32_t rid, WorkArea area, int32_t no)
  403. {
  404. _notify->OnNotifyMoveEnd(rid, area, no);
  405. }
  406. void CEgoClient::OnNotifyMoveRet(MoveDesc desc)
  407. {
  408. _notify->OnNotifyMoveRet(desc);
  409. }
  410. ControlStatus CEgoClient::CheckStatus()
  411. {
  412. return _can->CheckStatus();
  413. }
  414. /*
  415. void CEgoClient::OnEmergency(bool enable, bool motor, bool gear, bool turnl, bool turnR)
  416. {
  417. _notify->OnEmergency(enable, motor, gear, turnl, turnR);
  418. }
  419. void CEgoClient::OnAccel(int16_t accel , bool onoff)
  420. {
  421. _notify->OnAccel(accel, onoff);
  422. }
  423. void CEgoClient::OnSteer(uint64_t steer)
  424. {
  425. _notify->OnSteer(steer);
  426. }
  427. void CEgoClient::OnArm(int16_t flip, int16_t armL, int16_t armR, bool onoff)
  428. {
  429. _notify->OnArm(flip, armL, armR, onoff);
  430. }
  431. */
  432. SocketClient* CEgoClient::GetSocketClient()
  433. {
  434. return _client.get();
  435. }
  436. std::string CEgoClient::GetAccount()
  437. {
  438. return _accountText;
  439. }
  440. std::string CEgoClient::GetPassword()
  441. {
  442. return _passText;
  443. }
  444. std::string CEgoClient::GetName()
  445. {
  446. return _name;
  447. }
  448. /*
  449. bool CEgoClient::IsAutoClose()
  450. {
  451. return _autoclose;
  452. }
  453. void CEgoClient::SetCurrentPage(PageProp prop)
  454. {
  455. _currentpage = prop;
  456. }
  457. PageProp CEgoClient::GetCurrentPage()
  458. {
  459. return _currentpage;
  460. }
  461. */
  462. //add-wfg
  463. void CEgoClient::OnCautionLight(bool cautionLight)
  464. {
  465. _notify->OnCautionLight(cautionLight);
  466. }
  467. void CEgoClient::OnLightL(bool lightL)
  468. {
  469. _notify->OnLightL(lightL);
  470. }
  471. void CEgoClient::OnLightR(bool lightR)
  472. {
  473. _notify->OnLightR(lightR);
  474. }
  475. void CEgoClient::OnGearF(bool gearF)
  476. {
  477. _notify->OnGearF(gearF);
  478. }
  479. void CEgoClient::OnGearR(bool gearR)
  480. {
  481. _notify->OnGearR(gearR);
  482. }
  483. void CEgoClient::OnCarPark(bool carPark)
  484. {
  485. _notify->OnCarPark(carPark);
  486. }
  487. void CEgoClient::OnBuzzerPlay(bool buzzerPlay)
  488. {
  489. _notify->OnBuzzerPlay(buzzerPlay);
  490. }
  491. void CEgoClient::OnLightFront(bool lightFront)
  492. {
  493. _notify->OnLightFront(lightFront);
  494. }
  495. void CEgoClient::OnLightWork(bool lightWork)
  496. {
  497. _notify->OnLightWork(lightWork);
  498. }
  499. void CEgoClient::OnEmergency(bool emergency)
  500. {
  501. _notify->OnEmergency(emergency);
  502. }