EgoClient.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <windows.h>
  3. #include <json/json.h>
  4. #include <fstream>
  5. #include "api/video/video_frame.h"
  6. #include "api.h"
  7. /*#include "../common/types.h"*/
  8. #include "PCANBasic.h"
  9. #include "include/EgoInterface.h"
  10. #include "EgoWindow.h"
  11. #include "../common/iobuffer.h"
  12. #include "../common/sensor_socket.h"
  13. #include "../common/peer_connection.h"
  14. #include "car_sim.h"
  15. #include "null_sim.h"
  16. #include "control_sensor.h"
  17. #include "radar_sim.h"
  18. #include "lidar_sim.h"
  19. #include "EgoClient.h"
  20. #include "remote.pb.h"
  21. #include "remote_notify.h"
  22. CEgoClient::CEgoClient(IEgoNotify* n) :_notify(n)
  23. {
  24. _carpeer = -1;
  25. }
  26. void CEgoClient::Start(std::array<IRender*, RenderPosition::ALL>& ar)
  27. {
  28. _connected = false;
  29. Json::Value root;
  30. Json::Reader jsonReader;
  31. std::ifstream ifile("Config.json");
  32. std::string ip;
  33. //int32_t can_port = 0, host_port = 0;
  34. //std::string can_ip;
  35. if (jsonReader.parse(ifile, root))
  36. {
  37. _accountText = root["account"].asString();
  38. _passText = root["password"].asString();
  39. _name = root["name"].asString();
  40. //_autoclose = root["auto_close"].asBool();
  41. ip = root["server"].asString();
  42. //can_port = root["can_port"].asInt();
  43. //host_port = root["host_port"].asInt();
  44. //can_ip = root["can_ip"].asString();
  45. }
  46. _client = std::make_unique<SocketClient>(this);
  47. _client->Start(ip.c_str());
  48. for (int i = 0; i < RenderPosition::ALL; i++)
  49. {
  50. auto p = std::make_unique<CEgoWindow>(this,ar[i], (RenderPosition)i);
  51. p->Start();
  52. _WindowArray.push_back(std::move(p));
  53. }
  54. //CManipulationSensor* sensor = new CManipulationSensor(_WindowArray[RenderPosition::FRONT_BACK].get());
  55. _can = std::make_unique<CControlSensor>(_WindowArray[RenderPosition::FRONT].get());
  56. _can->Start();
  57. _carsim = std::make_unique<CCarSim>(this);
  58. _carsim->Start();
  59. _radarsim = std::make_unique<CRadarSim>(this);
  60. _radarsim->Start();
  61. _ctrl = GetRemoteController(nullptr);
  62. std::array<IRender*, RenderPosition::ALL> as{ nullptr };
  63. _ctrl->Start(EgoType::User, as);
  64. // _lidarsim = std::make_unique<CLidarSim>(this);
  65. // _lidarsim->Start();
  66. }
  67. int32_t CEgoClient::GetSteerAngle()
  68. {
  69. return _steer_angle;
  70. }
  71. void CEgoClient::SetReady(bool b)
  72. {
  73. for (auto i = 0; i < RenderPosition::ALL; i++)
  74. _WindowArray[i]->SetReady(b);
  75. }
  76. void CEgoClient::Login(std::string& account, std::string& pass)
  77. {
  78. _account = account;
  79. _pass = pass;
  80. if(_connected)
  81. _client->WriteSign(_account.c_str(), _pass.c_str());
  82. _ctrl->Login(_ctrl->GetAccount(), _ctrl->GetPassword());
  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(true,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. }
  298. void CEgoClient::OnStopAck()
  299. {
  300. _client->WriteVideoLeave(EgoType::Car, _carpeer);
  301. for (int32_t i = 0; i < RenderPosition::ALL; i++)
  302. {
  303. _WindowArray[i]->SetReady(false);
  304. _WindowArray[i]->OnNotifyLeave();// PostMessage(WM_NOTIFY_LEAVE);
  305. }
  306. _carpeer = -1;
  307. }
  308. void CEgoClient::OnRadarData(int32_t r0, int32_t r1, int32_t r2, int32_t r3, int32_t r4, int32_t r5, int32_t r6)
  309. {
  310. _notify->OnNotifyRadar(r0, r1, r2, r3, r4, r5,r6);
  311. }
  312. void CEgoClient::OnEncodeData(int32_t left, int32_t right)
  313. {
  314. _notify->OnNotifyEncode(left, right);
  315. }
  316. void CEgoClient::ReqCarList()
  317. {
  318. _client->WriteRobotReq();
  319. }
  320. void CEgoClient::OnPingValue(int32_t value,double temp)
  321. {
  322. _notify->OnNotifyPing(value,temp);
  323. }
  324. /*
  325. void CEgoClient::OnLidarData(bool isLeft, bool isDense, int32_t seq, PointXYZI* data, int32_t length)
  326. {
  327. _notify->OnLidarData(isLeft, isDense, seq, data, length);
  328. }
  329. */
  330. void CEgoClient::OnMoveBegin(int32_t rid, WorkArea area, int32_t no)
  331. {
  332. RemoNet::CSMoveBegin Req;
  333. Req.set_peer(rid);
  334. Req.set_area(area);
  335. Req.set_no(no);
  336. MessageHead Head;
  337. CIOBuffer pBuffer;
  338. Head.Command = RemoNet::CS_MoveBegin;
  339. Head.Length = Req.ByteSizeLong();
  340. Head.Serialize(pBuffer.Buffer);
  341. auto ptr = pBuffer.Buffer + MessageHead::Size();
  342. Req.SerializeToArray(ptr, Head.Length);
  343. pBuffer.Length = MessageHead::Size() + Head.Length;
  344. _client->Write(&pBuffer);
  345. }
  346. /*
  347. void CEgoClient::SwitchDriver(int32_t id)
  348. {
  349. RemoNet::SwitchDriver Req;
  350. Req.set_peer(id);
  351. MessageHead Head;
  352. CIOBuffer pBuffer;
  353. Head.Command = RemoNet::CS_SwitchDriver;
  354. Head.Length = Req.ByteSizeLong();
  355. Head.Serialize(pBuffer.Buffer);
  356. auto ptr = pBuffer.Buffer + MessageHead::Size();
  357. Req.SerializeToArray(ptr, Head.Length);
  358. pBuffer.Length = MessageHead::Size() + Head.Length;
  359. _client->Write(&pBuffer);
  360. }
  361. */
  362. void CEgoClient::ChangeState(UserState state)
  363. {
  364. RemoNet::CSState Req;
  365. Req.set_state((RemoNet::UserState)state);
  366. Req.set_uid(_uid);
  367. MessageHead Head;
  368. CIOBuffer pBuffer;
  369. Head.Command = RemoNet::CS_State;
  370. Head.Length = Req.ByteSizeLong();
  371. Head.Serialize(pBuffer.Buffer);
  372. auto ptr = pBuffer.Buffer + MessageHead::Size();
  373. Req.SerializeToArray(ptr, Head.Length);
  374. pBuffer.Length = MessageHead::Size() + Head.Length;
  375. _client->Write(&pBuffer);
  376. }
  377. void CEgoClient::OnMoveEnd(int32_t rid, WorkArea area, int32_t no)
  378. {
  379. _notify->OnNotifyMoveEnd(rid, area, no);
  380. }
  381. void CEgoClient::OnNotifyMoveRet(MoveDesc desc)
  382. {
  383. _notify->OnNotifyMoveRet(desc);
  384. }
  385. ControlStatus CEgoClient::CheckStatus()
  386. {
  387. return _can->CheckStatus();
  388. }
  389. /*
  390. void CEgoClient::OnEmergency(bool enable, bool motor, bool gear, bool turnl, bool turnR)
  391. {
  392. _notify->OnEmergency(enable, motor, gear, turnl, turnR);
  393. }
  394. void CEgoClient::OnAccel(int16_t accel , bool onoff)
  395. {
  396. _notify->OnAccel(accel, onoff);
  397. }
  398. void CEgoClient::OnSteer(uint64_t steer)
  399. {
  400. _notify->OnSteer(steer);
  401. }
  402. void CEgoClient::OnArm(int16_t flip, int16_t armL, int16_t armR, bool onoff)
  403. {
  404. _notify->OnArm(flip, armL, armR, onoff);
  405. }
  406. */
  407. void CEgoClient::OverlayVideo(RenderPosition pos, const webrtc::VideoFrame& frame)
  408. {
  409. _ctrl->OverlayVideo(pos, frame);
  410. }
  411. SocketClient* CEgoClient::GetSocketClient()
  412. {
  413. return _client.get();
  414. }
  415. std::string CEgoClient::GetAccount()
  416. {
  417. return _accountText;
  418. }
  419. std::string CEgoClient::GetPassword()
  420. {
  421. return _passText;
  422. }
  423. std::string CEgoClient::GetName()
  424. {
  425. return _name;
  426. }
  427. /*
  428. bool CEgoClient::IsAutoClose()
  429. {
  430. return _autoclose;
  431. }
  432. void CEgoClient::SetCurrentPage(PageProp prop)
  433. {
  434. _currentpage = prop;
  435. }
  436. PageProp CEgoClient::GetCurrentPage()
  437. {
  438. return _currentpage;
  439. }
  440. */