EgoClient.cpp 11 KB

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