EgoClient.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 "lidar_sim.h"
  18. #include "EgoClient.h"
  19. CEgoClient::CEgoClient(IEgoNotify* n) :_notify(n)
  20. {
  21. _carpeer = -1;
  22. }
  23. void CEgoClient::Start(std::array<IRender*, RenderPosition::ALL>& ar)
  24. {
  25. _connected = false;
  26. Json::Value root;
  27. Json::Reader jsonReader;
  28. std::ifstream ifile("Config.json");
  29. std::string ip;
  30. int32_t can_port = 0, host_port = 0;
  31. std::string can_ip;
  32. int32_t Tcphost_port;
  33. int32_t Tcpremote_port;
  34. if (jsonReader.parse(ifile, root))
  35. {
  36. _accountText = root["account"].asString();
  37. _passText = root["password"].asString();
  38. _name = root["name"].asString();
  39. //_autoclose = root["auto_close"].asBool();
  40. ip = root["server"].asString();
  41. Tcphost_port = root["Tcphost_port"].asInt();
  42. Tcpremote_port = root["Tcpremote_port"].asInt();
  43. can_port = root["can_port"].asInt();
  44. host_port = root["host_port"].asInt();
  45. can_ip = root["can_ip"].asString();
  46. }
  47. _client = std::make_unique<SocketClient>(this);
  48. //_client->Start(ip.c_str());
  49. _client->Start(ip.c_str(), Tcpremote_port, Tcphost_port);
  50. for (int i = 0; i < RenderPosition::ALL; i++)
  51. {
  52. auto p = std::make_unique<CEgoWindow>(this,ar[i], (RenderPosition)i);
  53. p->Start();
  54. _WindowArray.push_back(std::move(p));
  55. }
  56. //CManipulationSensor* sensor = new CManipulationSensor(_WindowArray[RenderPosition::FRONT_BACK].get());
  57. _can = std::make_unique<CControlSensor>(_WindowArray[RenderPosition::FRONT].get());
  58. _can->Start();
  59. _udpcan = std::make_unique<SensorSocket<CUdpSensor>>(_WindowArray[RenderPosition::FRONT].get(), can_ip, can_port, host_port);
  60. _udpcan->Start();
  61. _carsim = std::make_unique<CCarSim>(this);
  62. _carsim->Start();
  63. _radarsim = std::make_unique<CRadarSim>(this);
  64. _radarsim->Start();
  65. // _lidarsim = std::make_unique<CLidarSim>(this);
  66. // _lidarsim->Start();
  67. }
  68. int32_t CEgoClient::GetSteerAngle()
  69. {
  70. return _steer_angle;
  71. }
  72. void CEgoClient::SetReady(bool b)
  73. {
  74. for (auto i = 0; i < RenderPosition::ALL; i++)
  75. _WindowArray[i]->SetReady(b);
  76. }
  77. void CEgoClient::Login(std::string& account, std::string& pass)
  78. {
  79. _account = account;
  80. _pass = pass;
  81. if(_connected)
  82. _client->WriteSign(_account.c_str(), _pass.c_str());
  83. }
  84. void CEgoClient::OnRobot(const RemoNet::Robot& robot)
  85. {
  86. auto users = std::make_unique<UserCamera>();
  87. users->uid = robot.rid();
  88. users->name = robot.name();
  89. users->type = static_cast<EgoType>(robot.type());
  90. users->state = static_cast<UserState>(robot.state());
  91. users->carType=static_cast<CarType>(robot.cartype());
  92. _notify->OnRobot(users);
  93. _userInfo.push_back(std::move(users));
  94. }
  95. void CEgoClient::OnNotifyState(int32_t rid, UserState state)
  96. {
  97. for (auto& node : _userInfo)
  98. {
  99. if (node->uid == rid)
  100. {
  101. node->state = state;
  102. _notify->OnNotifyState(rid, state);
  103. break;
  104. }
  105. }
  106. }
  107. void CEgoClient::OnConnected(bool bRet)
  108. {
  109. if (bRet)
  110. {
  111. _connected = true;
  112. if (!_account.empty())
  113. _client->WriteSign(_account.c_str(),_pass.c_str());
  114. _updateThread.start(_client.get());
  115. }
  116. else
  117. {
  118. if (_connected)
  119. {
  120. _updateThread.stop();
  121. }
  122. _connected = false;
  123. if (_carpeer != -1)
  124. {
  125. for (int32_t i = RenderPosition::FRONT; i < RenderPosition::ALL; i++)
  126. {
  127. _WindowArray[i]->SetReady(false);
  128. _WindowArray[i]->OnNotifyLeave();
  129. }
  130. _carpeer = -1;
  131. }
  132. _userInfo.clear();
  133. }
  134. }
  135. void CEgoClient::OnVideoRep(bool ok,int32_t index, int32_t peer)
  136. {
  137. if (ok == false)
  138. {
  139. _carpeer = -1;
  140. _notify->OnNotifyVideoFail(peer);
  141. return;
  142. }
  143. if (peer != _carpeer) return;
  144. for (auto& node : _userInfo)
  145. {
  146. if (node->uid == peer)
  147. {
  148. /* if (index == RenderPosition::FRONT)
  149. _notify->OpenFullView(node->viewurl.c_str());
  150. */
  151. _WindowArray[index]->PostMessage(WM_NOTIFY_REP, (int64_t)peer);
  152. }
  153. }
  154. }
  155. void CEgoClient::OnVideoOffer(int32_t index, const char* type, const char* sdp)
  156. {
  157. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__,__LINE__);
  158. OfferDesc* desc = (OfferDesc*)pBuffer->Buffer;
  159. strcpy_s(desc->type, type);
  160. strcpy_s(desc->sdp, sdp);
  161. _WindowArray[index]->PostMessage(WM_NOTIFY_OFFER, (int64_t)pBuffer);
  162. }
  163. void CEgoClient::OnVideoAnswer(int32_t index, const char* type, const char* sdp)
  164. {
  165. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  166. AnswerDesc* p = (AnswerDesc*)(pBuffer->Buffer);
  167. strcpy_s(p->type, type);
  168. strcpy_s(p->sdp, sdp);
  169. _WindowArray[index]->PostMessage(WM_NOTIFY_ANSWER, (int64_t)pBuffer);
  170. // _WindowArray[index]->DelayDataChannel();
  171. index++;
  172. if (index < RenderPosition::ALL)
  173. {
  174. _WindowArray[index]->PostMessage(WM_ASK_VIDEOREQ);// DelayNextVideoReq();
  175. }
  176. else
  177. {
  178. _WindowArray[RenderPosition::FRONT]->PostMessage(WM_ASK_PING);// DelayStartPing();
  179. }
  180. }
  181. void CEgoClient::OnVideoCandidate(int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid)
  182. {
  183. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  184. CandidateDesc* desc = (CandidateDesc*)(pBuffer->Buffer);
  185. strcpy_s(desc->candidate, candidate);
  186. strcpy_s(desc->sdp_mid, sdp_mid);
  187. desc->sdp_mline_index = sdp_mline_index;
  188. _WindowArray[index]->PostMessage(WM_NOTIFY_CANDIDATE, (int64_t)pBuffer);
  189. }
  190. void CEgoClient::OnVideoLeave(int32_t peer, EgoType type)
  191. {
  192. if (type == EgoType::Car)
  193. {
  194. _WindowArray[ChannelType::CHANNEL_CAR]->SetControlState(ControlState::Check);
  195. for (int32_t i = RenderPosition::FRONT; i < RenderPosition::ALL; i++)
  196. {
  197. _WindowArray[i]->SetReady(false);
  198. _WindowArray[i]->OnNotifyLeave();
  199. }
  200. _carpeer = -1;
  201. _notify->OnNotifyLeave(peer);
  202. }
  203. }
  204. void CEgoClient::OnNotifyKick()
  205. {
  206. _notify->OnNotifyKickOff();
  207. }
  208. void CEgoClient::OnNDTPos(Position* pos)
  209. {
  210. _notify->OnNDTPos(pos);
  211. }
  212. void CEgoClient::OnMessageFrameNotify(ChannelType type, int16_t cmd, int16_t length, const void* data)
  213. {
  214. switch (type)
  215. {
  216. case ChannelType::CHANNEL_CAR:
  217. {
  218. _carsim->OnPeerMessage(cmd, length, data);
  219. }
  220. break;
  221. case ChannelType::CHANNEL_RADAR:
  222. _radarsim->OnPeerMessage(cmd, length, data);
  223. break;
  224. //case ChannelType::CHANNEL_LIDAR:
  225. // _lidarsim->OnPeerMessage(cmd, length, data);
  226. // break;
  227. }
  228. }
  229. void CEgoClient::OnSigin(int32_t uid,bool bRet)
  230. {
  231. _uid = uid;
  232. _notify->OnSigin(bRet);
  233. if (bRet)
  234. {
  235. _client->WriteRobotReq();
  236. }
  237. }
  238. void CEgoClient::OnFeedPage(FeedData& data)
  239. {
  240. _steer_angle = data.steer_angle;
  241. _can->SetEngineRPM(data.engine_rpm);
  242. _notify->OnNotifyFeed(data);
  243. }
  244. void CEgoClient::OnNotifyDel(int32_t peer, EgoType type)
  245. {
  246. for (auto it = _userInfo.begin(); it != _userInfo.end(); ++it)
  247. {
  248. if ((*it)->uid == peer)
  249. {
  250. _userInfo.erase(it);
  251. break;
  252. }
  253. }
  254. _notify->OnNotifyDel(peer);
  255. }
  256. void CEgoClient::OnCarConnect(int32_t peer)
  257. {
  258. mrsWebrtcCreateFactory(false);
  259. for (int i = 0; i < RenderPosition::ALL; i++)
  260. {
  261. _WindowArray[i]->SetPeer(peer);
  262. }
  263. for (auto& node : _userInfo)
  264. {
  265. if (node->uid == peer)
  266. {
  267. _WindowArray[RenderPosition::FRONT]->OnAskVideoReq();
  268. _carpeer = peer;
  269. break;
  270. }
  271. }
  272. }
  273. void CEgoClient::OnCarLeave()
  274. {
  275. /*
  276. RemoNet::SensorStop Req;
  277. CIOBuffer Buffer;
  278. MessageHead Head;
  279. Head.Command = RemoNet::CC_SensorStop;
  280. Head.Length = Req.ByteSizeLong();
  281. Head.Serialize(Buffer.Buffer);
  282. auto ptr = Buffer.Buffer + MessageHead::Size();
  283. Req.SerializeToArray(ptr, Head.Length);
  284. Buffer.Length = Head.Length + MessageHead::Size();
  285. _WindowArray[ChannelType::CHANNEL_CAR]->StopPing();
  286. _WindowArray[ChannelType::CHANNEL_CAR]->SendData(&Buffer);
  287. */
  288. // _notify->CloseFullView();
  289. _WindowArray[ChannelType::CHANNEL_CAR]->SetControlState(ControlState::Check);
  290. _WindowArray[ChannelType::CHANNEL_CAR]->StopPing();
  291. _client->WriteVideoLeave(EgoType::Car, _carpeer);
  292. for (int32_t i = 0; i < RenderPosition::ALL; i++)
  293. {
  294. _WindowArray[i]->OnNotifyLeave();
  295. }
  296. _carpeer = -1;
  297. //_can->Stop();
  298. }
  299. void CEgoClient::OnStopAck()
  300. {
  301. _client->WriteVideoLeave(EgoType::Car, _carpeer);
  302. for (int32_t i = 0; i < RenderPosition::ALL; i++)
  303. {
  304. _WindowArray[i]->SetReady(false);
  305. _WindowArray[i]->OnNotifyLeave();// PostMessage(WM_NOTIFY_LEAVE);
  306. }
  307. _carpeer = -1;
  308. }
  309. void CEgoClient::SendCanDate(TPCANMsg* CANMsg)
  310. {
  311. _can->SendCANMessage(CANMsg);
  312. }
  313. 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,int32_t r8)
  314. {
  315. _notify->OnNotifyRadar(r0, r1, r2, r3, r4, r5,r6,r7,r8);
  316. }
  317. void CEgoClient::OnEncodeData(int32_t left, int32_t right)
  318. {
  319. _notify->OnNotifyEncode(left, right);
  320. }
  321. void CEgoClient::ReqCarList()
  322. {
  323. _client->WriteRobotReq();
  324. }
  325. void CEgoClient::OnPingValue(int32_t value,double temp)
  326. {
  327. _notify->OnNotifyPing(value,temp);
  328. }
  329. /*
  330. void CEgoClient::OnLidarData(bool isLeft, bool isDense, int32_t seq, PointXYZI* data, int32_t length)
  331. {
  332. _notify->OnLidarData(isLeft, isDense, seq, data, length);
  333. }
  334. */
  335. void CEgoClient::OnMoveBegin(int32_t rid, WorkArea area, int32_t no)
  336. {
  337. RemoNet::CSMoveBegin Req;
  338. Req.set_peer(rid);
  339. Req.set_area(area);
  340. Req.set_no(no);
  341. MessageHead Head;
  342. CIOBuffer pBuffer;
  343. Head.Command = RemoNet::CS_MoveBegin;
  344. Head.Length = Req.ByteSizeLong();
  345. Head.Serialize(pBuffer.Buffer);
  346. auto ptr = pBuffer.Buffer + MessageHead::Size();
  347. Req.SerializeToArray(ptr, Head.Length);
  348. pBuffer.Length = MessageHead::Size() + Head.Length;
  349. _client->Write(&pBuffer);
  350. }
  351. /*
  352. void CEgoClient::SwitchDriver(int32_t id)
  353. {
  354. RemoNet::SwitchDriver Req;
  355. Req.set_peer(id);
  356. MessageHead Head;
  357. CIOBuffer pBuffer;
  358. Head.Command = RemoNet::CS_SwitchDriver;
  359. Head.Length = Req.ByteSizeLong();
  360. Head.Serialize(pBuffer.Buffer);
  361. auto ptr = pBuffer.Buffer + MessageHead::Size();
  362. Req.SerializeToArray(ptr, Head.Length);
  363. pBuffer.Length = MessageHead::Size() + Head.Length;
  364. _client->Write(&pBuffer);
  365. }
  366. */
  367. void CEgoClient::ChangeState(UserState state)
  368. {
  369. RemoNet::CSState Req;
  370. Req.set_state((RemoNet::UserState)state);
  371. Req.set_uid(_uid);
  372. MessageHead Head;
  373. CIOBuffer pBuffer;
  374. Head.Command = RemoNet::CS_State;
  375. Head.Length = Req.ByteSizeLong();
  376. Head.Serialize(pBuffer.Buffer);
  377. auto ptr = pBuffer.Buffer + MessageHead::Size();
  378. Req.SerializeToArray(ptr, Head.Length);
  379. pBuffer.Length = MessageHead::Size() + Head.Length;
  380. _client->Write(&pBuffer);
  381. }
  382. void CEgoClient::OnMoveEnd(int32_t rid, WorkArea area, int32_t no)
  383. {
  384. _notify->OnNotifyMoveEnd(rid, area, no);
  385. }
  386. void CEgoClient::OnNotifyMoveRet(MoveDesc desc)
  387. {
  388. _notify->OnNotifyMoveRet(desc);
  389. }
  390. ControlStatus CEgoClient::CheckStatus()
  391. {
  392. return _can->CheckStatus();
  393. }
  394. /*
  395. void CEgoClient::OnEmergency(bool enable, bool motor, bool gear, bool turnl, bool turnR)
  396. {
  397. _notify->OnEmergency(enable, motor, gear, turnl, turnR);
  398. }
  399. void CEgoClient::OnAccel(int16_t accel , bool onoff)
  400. {
  401. _notify->OnAccel(accel, onoff);
  402. }
  403. void CEgoClient::OnSteer(uint64_t steer)
  404. {
  405. _notify->OnSteer(steer);
  406. }
  407. void CEgoClient::OnArm(int16_t flip, int16_t armL, int16_t armR, bool onoff)
  408. {
  409. _notify->OnArm(flip, armL, armR, onoff);
  410. }
  411. */
  412. SocketClient* CEgoClient::GetSocketClient()
  413. {
  414. return _client.get();
  415. }
  416. std::string CEgoClient::GetAccount()
  417. {
  418. return _accountText;
  419. }
  420. std::string CEgoClient::GetPassword()
  421. {
  422. return _passText;
  423. }
  424. std::string CEgoClient::GetName()
  425. {
  426. return _name;
  427. }
  428. /*
  429. bool CEgoClient::IsAutoClose()
  430. {
  431. return _autoclose;
  432. }
  433. void CEgoClient::SetCurrentPage(PageProp prop)
  434. {
  435. _currentpage = prop;
  436. }
  437. PageProp CEgoClient::GetCurrentPage()
  438. {
  439. return _currentpage;
  440. }
  441. */