EgoClient.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include <json/json.h>
  2. #include <fstream>
  3. #include <random>
  4. #include <time.h>
  5. #include "EgoClient.h"
  6. #include "api.h"
  7. #include "include/EgoInterface.h"
  8. #include "EgoWindow.h"
  9. #include "control_sensor.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 "out_sim.h"
  16. #include "radar_sim.h"
  17. #ifdef LIDAR_SENSOR
  18. #include "lidar_sim.h"
  19. #endif
  20. CEgoClient::CEgoClient(IEgoNotify* n) :_notify(n),_u(100, 500)
  21. {
  22. }
  23. void CEgoClient::Start(std::array<IRender*, RenderPosition::ALL>& ar)
  24. {
  25. _carpeer = -1;
  26. _e.seed(time(NULL));
  27. Json::Value root;
  28. Json::Reader jsonReader;
  29. std::ifstream ifile("Config.json");
  30. std::string ip;
  31. int32_t can_port = 0, host_port = 0;
  32. std::string can_ip;
  33. std::string motion_ip;
  34. int32_t motion_port;
  35. std::string host;
  36. if (jsonReader.parse(ifile, root))
  37. {
  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. host = root["host_in"].asString();
  43. motion_ip = root["motion_ip"].asString();
  44. motion_port = root["motion_port"].asInt();
  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>(ar[i], (RenderPosition)i, _client.get());
  51. p->Start();
  52. _WindowArray.push_back(std::move(p));
  53. }
  54. //CManipulationSensor* sensor = new CManipulationSensor(_WindowArray[RenderPosition::FRONT_BACK].get());
  55. //_control = std::make_unique<SensorSocket<CControlSensor>>(_WindowArray[RenderPosition::FRONT_BACK].get(), can_ip, can_port, host_port);
  56. _control = std::make_unique<SensorSocket<CControlSensor>>(_WindowArray[RenderPosition::FRONT].get(), can_ip, can_port, host_port);
  57. _control->Start(host.c_str());
  58. _carsim = std::make_unique<CCarSim>(this);
  59. _carsim->Start();
  60. _outsim = std::make_unique<COutSim>(motion_ip.c_str(),motion_port);
  61. _outsim->Start();
  62. _radarsim = std::make_unique<CRadarSim>(this);
  63. _radarsim->Start();
  64. OnRadarData(0, 0, 0, 0, 0, 0);
  65. #ifdef LIDAR_SENSOR
  66. _lidarsim = std::make_unique<CLidarSim>(this);
  67. _lidarsim->Start();
  68. #endif
  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. _notify->OnRobot(users);
  85. _userInfo.push_back(std::move(users));
  86. }
  87. void CEgoClient::OnConnected(bool bRet)
  88. {
  89. if (bRet)
  90. {
  91. _connected = true;
  92. if (!_account.empty())
  93. _client->WriteSign(_account.c_str(),_pass.c_str());
  94. _updateThread.start(_client.get());
  95. }
  96. else
  97. {
  98. _connected = false;
  99. if (_carpeer != -1)
  100. {
  101. for (int32_t i = RenderPosition::FRONT; i < RenderPosition::ALL; i++)
  102. {
  103. _WindowArray[i]->PostMessage(WM_NOTIFY_LEAVE);
  104. }
  105. _carpeer = -1;
  106. }
  107. _updateThread.stop();
  108. _userInfo.clear();
  109. }
  110. }
  111. void CEgoClient::OnVideoRep(int32_t index, int32_t peer)
  112. {
  113. if (peer != _carpeer) return;
  114. for (auto& node : _userInfo)
  115. {
  116. if (node->uid == peer)
  117. {
  118. _WindowArray[index]->PostMessage(WM_NOTIFY_REP, (int64_t)peer);
  119. }
  120. }
  121. }
  122. void CEgoClient::OnVideoOffer(int32_t index, const char* type, const char* sdp)
  123. {
  124. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  125. OfferDesc* desc = (OfferDesc*)pBuffer->Buffer;
  126. strcpy_s(desc->type, type);
  127. strcpy_s(desc->sdp, sdp);
  128. _WindowArray[index]->PostMessage(WM_NOTIFY_OFFER, (int64_t)pBuffer);
  129. }
  130. void CEgoClient::OnVideoAnswer(int32_t index, const char* type, const char* sdp)
  131. {
  132. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  133. AnswerDesc* p = (AnswerDesc*)(pBuffer->Buffer);
  134. strcpy_s(p->type, type);
  135. strcpy_s(p->sdp, sdp);
  136. _WindowArray[index]->PostMessage(WM_NOTIFY_ANSWER, (int64_t)pBuffer);
  137. _WindowArray[index]->DelayDataChannel();
  138. index++;
  139. if (index < RenderPosition::ALL)
  140. {
  141. _WindowArray[index]->DelayNextVideoReq();
  142. }
  143. else
  144. {
  145. _WindowArray[ChannelType::CHANNEL_CAR]->DelayStartPing();
  146. }
  147. }
  148. void CEgoClient::OnVideoCandidate(int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid)
  149. {
  150. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  151. CandidateDesc* desc = (CandidateDesc*)(pBuffer->Buffer);
  152. strcpy_s(desc->candidate, candidate);
  153. strcpy_s(desc->sdp_mid, sdp_mid);
  154. desc->sdp_mline_index = sdp_mline_index;
  155. _WindowArray[index]->PostMessage(WM_NOTIFY_CANDIDATE, (int64_t)pBuffer);
  156. }
  157. void CEgoClient::OnVideoLeave(int32_t peer, EgoType type)
  158. {
  159. if (type == EgoType::Car)
  160. {
  161. _WindowArray[ChannelType::CHANNEL_CAR]->SetControlState(ControlState::Check);
  162. for (int32_t i = RenderPosition::FRONT; i < RenderPosition::ALL; i++)
  163. {
  164. _WindowArray[i]->PostMessage(WM_NOTIFY_LEAVE);
  165. }
  166. _carpeer = -1;
  167. }
  168. }
  169. void CEgoClient::OnNotifyKick()
  170. {
  171. _notify->OnNotifyKickOff();
  172. }
  173. void CEgoClient::OnMessageFrameNotify(ChannelType type, int16_t cmd, int16_t length, const void* data)
  174. {
  175. switch (type)
  176. {
  177. case ChannelType::CHANNEL_CAR:
  178. {
  179. _carsim->OnPeerMessage(cmd, length, data);
  180. }
  181. break;
  182. case ChannelType::CHANNEL_IMU:
  183. _outsim->OnPeerMessage(cmd, length, data);
  184. break;
  185. case ChannelType::CHANNEL_RADAR:
  186. _radarsim->OnPeerMessage(cmd, length, data);
  187. break;
  188. #ifdef LIDAR_SENSOR
  189. case ChannelType::CHANNEL_LEFT_LIDAR:
  190. case ChannelType::CHANNEL_RIGHT_LIDAR:
  191. _lidarsim->OnPeerMessage(cmd, length, data);
  192. break;
  193. #endif
  194. }
  195. /*
  196. if (type == ChannelType)
  197. {
  198. RemoNet::TestTextReq Req;
  199. Req.ParseFromArray(data, length);
  200. std::cout << Req.text() << std::endl;
  201. }
  202. */
  203. }
  204. void CEgoClient::OnSigin(bool bRet)
  205. {
  206. _notify->OnSigin(bRet);
  207. if (bRet)
  208. {
  209. _client->WriteRobotReq();
  210. }
  211. }
  212. void CEgoClient::OnNotifyDel(int32_t peer, EgoType type)
  213. {
  214. for (auto it = _userInfo.begin(); it != _userInfo.end(); ++it)
  215. {
  216. if ((*it)->uid == peer)
  217. {
  218. _userInfo.erase(it);
  219. break;
  220. }
  221. }
  222. _notify->OnNotifyDel(peer);
  223. }
  224. void CEgoClient::OnCarConnect(int32_t peer)
  225. {
  226. mrsWebrtcCreateFactory(false);
  227. for (int i = 0; i < RenderPosition::ALL; i++)
  228. {
  229. _WindowArray[i]->SetPeer(peer);
  230. }
  231. for (auto& node : _userInfo)
  232. {
  233. if (node->uid == peer)
  234. {
  235. _WindowArray[RenderPosition::FRONT]->CreateVideoReq();
  236. _carpeer = peer;
  237. break;
  238. }
  239. }
  240. }
  241. void CEgoClient::OnCarLeave()
  242. {
  243. /*RemoNet::SensorStop Req;
  244. CIOBuffer Buffer;
  245. MessageHead Head;
  246. Head.Command = RemoNet::CC_SensorStop;
  247. Head.Length = Req.ByteSizeLong();
  248. Head.Serialize(Buffer.Buffer);
  249. auto ptr = Buffer.Buffer + MessageHead::Size();
  250. Req.SerializeToArray(ptr, Head.Length);
  251. Buffer.Length = Head.Length + MessageHead::Size();
  252. */
  253. _WindowArray[ChannelType::CHANNEL_CAR]->SetControlState(ControlState::Check);
  254. _WindowArray[ChannelType::CHANNEL_CAR]->StopPing();
  255. _client->WriteVideoLeave(EgoType::Car, _carpeer);
  256. for (int32_t i = 0; i < RenderPosition::ALL; i++)
  257. {
  258. _WindowArray[i]->PostMessage(WM_NOTIFY_LEAVE);
  259. }
  260. _carpeer = -1;
  261. //_WindowArray[ChannelType::CHANNEL_CAR]->SendData(&Buffer);
  262. }
  263. void CEgoClient::OnStopAck()
  264. {
  265. }
  266. void CEgoClient::OnBootstrapd(bool ret)
  267. {
  268. _control->Get()->OnBootstrapd(ret);
  269. }
  270. void CEgoClient::OnStartupd(bool ret)
  271. {
  272. _control->Get()->OnStartupd(ret);
  273. }
  274. void CEgoClient::OnRadarData(int32_t r0, int32_t r1, int32_t r2, int32_t r3, int32_t r4, int32_t r5)
  275. {
  276. _notify->OnNotifyRadar(r0, r1, r2, r3, r4, r5);
  277. }
  278. void CEgoClient::OnImuData(int32_t x, int32_t y)
  279. {
  280. _notify->OnNotifyImu(x, y);
  281. }
  282. void CEgoClient::ReqCarList()
  283. {
  284. _client->WriteRobotReq();
  285. }
  286. void CEgoClient::OnPingValue(int32_t value)
  287. {
  288. _notify->OnNotifyPing(value);
  289. //OnRadarData(_u(_e), _u(_e), _u(_e), _u(_e), _u(_e), _u(_e));
  290. }
  291. #ifdef LIDAR_SENSOR
  292. void CEgoClient::OnLidarData(bool isLeft, bool isDense, int32_t seq, PointXYZI* data, int32_t length)
  293. {
  294. _notify->OnLidarData(isLeft, isDense, seq, data, length);
  295. }
  296. #endif
  297. ControlStatus CEgoClient::CheckStatus()
  298. {
  299. return _control->Get()->CheckStatus();
  300. }