123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- #include <json/json.h>
- #include <fstream>
- #include <random>
- #include <time.h>
- #include "EgoClient.h"
- #include "api.h"
-
- #include "include/EgoInterface.h"
- #include "EgoWindow.h"
- #include "control_sensor.h"
- #include "../common/iobuffer.h"
- #include "../common/sensor_socket.h"
- #include "../common/peer_connection.h"
- #include "car_sim.h"
- #include "null_sim.h"
- #include "out_sim.h"
- #include "radar_sim.h"
- #ifdef LIDAR_SENSOR
- #include "lidar_sim.h"
- #endif
- CEgoClient::CEgoClient(IEgoNotify* n) :_notify(n),_u(100, 500)
- {
- }
- void CEgoClient::Start(std::array<IRender*, RenderPosition::ALL>& ar)
- {
- _carpeer = -1;
- _e.seed(time(NULL));
- Json::Value root;
- Json::Reader jsonReader;
- std::ifstream ifile("Config.json");
- std::string ip;
- int32_t can_port = 0, host_port = 0;
- std::string can_ip;
- std::string motion_ip;
- int32_t motion_port;
-
-
- std::string host;
- if (jsonReader.parse(ifile, root))
- {
- ip = root["server"].asString();
- can_port = root["can_port"].asInt();
- host_port = root["host_port"].asInt();
- can_ip = root["can_ip"].asString();
- host = root["host_in"].asString();
- motion_ip = root["motion_ip"].asString();
- motion_port = root["motion_port"].asInt();
- }
- _client = std::make_unique<SocketClient>(this);
- _client->Start(ip.c_str());
- for (int i = 0; i < RenderPosition::ALL; i++)
- {
- auto p = std::make_unique<CEgoWindow>(ar[i], (RenderPosition)i, _client.get());
- p->Start();
- _WindowArray.push_back(std::move(p));
- }
-
- //CManipulationSensor* sensor = new CManipulationSensor(_WindowArray[RenderPosition::FRONT_BACK].get());
- //_control = std::make_unique<SensorSocket<CControlSensor>>(_WindowArray[RenderPosition::FRONT_BACK].get(), can_ip, can_port, host_port);
- _control = std::make_unique<SensorSocket<CControlSensor>>(_WindowArray[RenderPosition::FRONT].get(), can_ip, can_port, host_port);
- _control->Start(host.c_str());
- _carsim = std::make_unique<CCarSim>(this);
- _carsim->Start();
- _outsim = std::make_unique<COutSim>(motion_ip.c_str(),motion_port);
- _outsim->Start();
- _radarsim = std::make_unique<CRadarSim>(this);
- _radarsim->Start();
- OnRadarData(0, 0, 0, 0, 0, 0);
- #ifdef LIDAR_SENSOR
- _lidarsim = std::make_unique<CLidarSim>(this);
- _lidarsim->Start();
- #endif
-
-
- }
- void CEgoClient::Login(std::string account, std::string pass)
- {
- _account = account;
- _pass = pass;
- if(_connected)
- _client->WriteSign(_account.c_str(), _pass.c_str());
- }
- void CEgoClient::OnRobot(const RemoNet::Robot& robot)
- {
- auto users = std::make_unique<UserCamera>();
- users->uid = robot.rid();
- users->name = robot.name();
- users->type = static_cast<EgoType>(robot.type());
- users->state = static_cast<UserState>(robot.state());
-
- _notify->OnRobot(users);
- _userInfo.push_back(std::move(users));
- }
- void CEgoClient::OnConnected(bool bRet)
- {
- if (bRet)
- {
- _connected = true;
- if (!_account.empty())
- _client->WriteSign(_account.c_str(),_pass.c_str());
- _updateThread.start(_client.get());
- }
- else
- {
- _connected = false;
- if (_carpeer != -1)
- {
- for (int32_t i = RenderPosition::FRONT; i < RenderPosition::ALL; i++)
- {
- _WindowArray[i]->PostMessage(WM_NOTIFY_LEAVE);
- }
- _carpeer = -1;
- }
- _updateThread.stop();
- _userInfo.clear();
- }
- }
- void CEgoClient::OnVideoRep(int32_t index, int32_t peer)
- {
- if (peer != _carpeer) return;
- for (auto& node : _userInfo)
- {
- if (node->uid == peer)
- {
-
- _WindowArray[index]->PostMessage(WM_NOTIFY_REP, (int64_t)peer);
-
-
- }
- }
- }
- void CEgoClient::OnVideoOffer(int32_t index, const char* type, const char* sdp)
- {
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- OfferDesc* desc = (OfferDesc*)pBuffer->Buffer;
- strcpy_s(desc->type, type);
- strcpy_s(desc->sdp, sdp);
- _WindowArray[index]->PostMessage(WM_NOTIFY_OFFER, (int64_t)pBuffer);
- }
- void CEgoClient::OnVideoAnswer(int32_t index, const char* type, const char* sdp)
- {
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- AnswerDesc* p = (AnswerDesc*)(pBuffer->Buffer);
- strcpy_s(p->type, type);
- strcpy_s(p->sdp, sdp);
- _WindowArray[index]->PostMessage(WM_NOTIFY_ANSWER, (int64_t)pBuffer);
- _WindowArray[index]->DelayDataChannel();
- index++;
- if (index < RenderPosition::ALL)
- {
- _WindowArray[index]->DelayNextVideoReq();
- }
- else
- {
- _WindowArray[ChannelType::CHANNEL_CAR]->DelayStartPing();
- }
-
- }
- void CEgoClient::OnVideoCandidate(int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid)
- {
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- CandidateDesc* desc = (CandidateDesc*)(pBuffer->Buffer);
- strcpy_s(desc->candidate, candidate);
- strcpy_s(desc->sdp_mid, sdp_mid);
- desc->sdp_mline_index = sdp_mline_index;
- _WindowArray[index]->PostMessage(WM_NOTIFY_CANDIDATE, (int64_t)pBuffer);
- }
- void CEgoClient::OnVideoLeave(int32_t peer, EgoType type)
- {
- if (type == EgoType::Car)
- {
- _WindowArray[ChannelType::CHANNEL_CAR]->SetControlState(ControlState::Check);
- for (int32_t i = RenderPosition::FRONT; i < RenderPosition::ALL; i++)
- {
- _WindowArray[i]->PostMessage(WM_NOTIFY_LEAVE);
- }
- _carpeer = -1;
- }
- }
- void CEgoClient::OnNotifyKick()
- {
- _notify->OnNotifyKickOff();
- }
- void CEgoClient::OnMessageFrameNotify(ChannelType type, int16_t cmd, int16_t length, const void* data)
- {
-
- switch (type)
- {
- case ChannelType::CHANNEL_CAR:
- {
- _carsim->OnPeerMessage(cmd, length, data);
- }
- break;
- case ChannelType::CHANNEL_IMU:
- _outsim->OnPeerMessage(cmd, length, data);
- break;
- case ChannelType::CHANNEL_RADAR:
- _radarsim->OnPeerMessage(cmd, length, data);
- break;
- #ifdef LIDAR_SENSOR
- case ChannelType::CHANNEL_LEFT_LIDAR:
- case ChannelType::CHANNEL_RIGHT_LIDAR:
- _lidarsim->OnPeerMessage(cmd, length, data);
- break;
- #endif
- }
- /*
- if (type == ChannelType)
- {
- RemoNet::TestTextReq Req;
-
- Req.ParseFromArray(data, length);
- std::cout << Req.text() << std::endl;
- }
- */
- }
-
- void CEgoClient::OnSigin(bool bRet)
- {
- _notify->OnSigin(bRet);
- if (bRet)
- {
- _client->WriteRobotReq();
- }
- }
- void CEgoClient::OnNotifyDel(int32_t peer, EgoType type)
- {
- for (auto it = _userInfo.begin(); it != _userInfo.end(); ++it)
- {
- if ((*it)->uid == peer)
- {
- _userInfo.erase(it);
- break;
- }
- }
- _notify->OnNotifyDel(peer);
- }
- void CEgoClient::OnCarConnect(int32_t peer)
- {
- mrsWebrtcCreateFactory(false);
- for (int i = 0; i < RenderPosition::ALL; i++)
- {
- _WindowArray[i]->SetPeer(peer);
- }
- for (auto& node : _userInfo)
- {
- if (node->uid == peer)
- {
- _WindowArray[RenderPosition::FRONT]->CreateVideoReq();
- _carpeer = peer;
- break;
- }
- }
- }
- void CEgoClient::OnCarLeave()
- {
- /*RemoNet::SensorStop Req;
- CIOBuffer Buffer;
- MessageHead Head;
- Head.Command = RemoNet::CC_SensorStop;
- Head.Length = Req.ByteSizeLong();
- Head.Serialize(Buffer.Buffer);
- auto ptr = Buffer.Buffer + MessageHead::Size();
- Req.SerializeToArray(ptr, Head.Length);
- Buffer.Length = Head.Length + MessageHead::Size();
- */
- _WindowArray[ChannelType::CHANNEL_CAR]->SetControlState(ControlState::Check);
- _WindowArray[ChannelType::CHANNEL_CAR]->StopPing();
- _client->WriteVideoLeave(EgoType::Car, _carpeer);
- for (int32_t i = 0; i < RenderPosition::ALL; i++)
- {
- _WindowArray[i]->PostMessage(WM_NOTIFY_LEAVE);
- }
- _carpeer = -1;
- //_WindowArray[ChannelType::CHANNEL_CAR]->SendData(&Buffer);
- }
- void CEgoClient::OnStopAck()
- {
- }
- void CEgoClient::OnBootstrapd(bool ret)
- {
- _control->Get()->OnBootstrapd(ret);
- }
- void CEgoClient::OnStartupd(bool ret)
- {
- _control->Get()->OnStartupd(ret);
- }
- void CEgoClient::OnRadarData(int32_t r0, int32_t r1, int32_t r2, int32_t r3, int32_t r4, int32_t r5)
- {
- _notify->OnNotifyRadar(r0, r1, r2, r3, r4, r5);
- }
- void CEgoClient::OnImuData(int32_t x, int32_t y)
- {
- _notify->OnNotifyImu(x, y);
- }
- void CEgoClient::ReqCarList()
- {
- _client->WriteRobotReq();
- }
-
- void CEgoClient::OnPingValue(int32_t value)
- {
- _notify->OnNotifyPing(value);
- //OnRadarData(_u(_e), _u(_e), _u(_e), _u(_e), _u(_e), _u(_e));
- }
- #ifdef LIDAR_SENSOR
- void CEgoClient::OnLidarData(bool isLeft, bool isDense, int32_t seq, PointXYZI* data, int32_t length)
- {
- _notify->OnLidarData(isLeft, isDense, seq, data, length);
- }
- #endif
- ControlStatus CEgoClient::CheckStatus()
- {
- return _control->Get()->CheckStatus();
- }
|