#define WIN32_LEAN_AND_MEAN #include #include #include #include "api/video/video_frame.h" #include "api.h" /*#include "../common/types.h"*/ #include "PCANBasic.h" #include "include/EgoInterface.h" #include "EgoWindow.h" #include "../common/iobuffer.h" #include "../common/sensor_socket.h" #include "../common/peer_connection.h" #include "car_sim.h" #include "null_sim.h" #include "control_sensor.h" #include "radar_sim.h" #include "lidar_sim.h" #include "EgoClient.h" #include "remote.pb.h" #include "remote_notify.h" CEgoClient::CEgoClient(IEgoNotify* n) :_notify(n) { _carpeer = -1; } void CEgoClient::Start(std::array& ar) { _connected = false; 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; if (jsonReader.parse(ifile, root)) { _accountText = root["account"].asString(); _passText = root["password"].asString(); _name = root["name"].asString(); //_autoclose = root["auto_close"].asBool(); ip = root["server"].asString(); //can_port = root["can_port"].asInt(); //host_port = root["host_port"].asInt(); //can_ip = root["can_ip"].asString(); } _client = std::make_unique(this); _client->Start(ip.c_str()); for (int i = 0; i < RenderPosition::ALL; i++) { auto p = std::make_unique(this,ar[i], (RenderPosition)i); p->Start(); _WindowArray.push_back(std::move(p)); } //CManipulationSensor* sensor = new CManipulationSensor(_WindowArray[RenderPosition::FRONT_BACK].get()); _can = std::make_unique(_WindowArray[RenderPosition::FRONT].get()); _can->Start(); _carsim = std::make_unique(this); _carsim->Start(); _radarsim = std::make_unique(this); _radarsim->Start(); _ctrl = GetRemoteController(nullptr); std::array as{ nullptr }; _ctrl->Start(EgoType::User, as); // _lidarsim = std::make_unique(this); // _lidarsim->Start(); } int32_t CEgoClient::GetSteerAngle() { return _steer_angle; } void CEgoClient::SetReady(bool b) { for (auto i = 0; i < RenderPosition::ALL; i++) _WindowArray[i]->SetReady(b); } void CEgoClient::Login(std::string& account, std::string& pass) { _account = account; _pass = pass; if(_connected) _client->WriteSign(_account.c_str(), _pass.c_str()); _ctrl->Login(_ctrl->GetAccount(), _ctrl->GetPassword()); } void CEgoClient::OnRobot(const RemoNet::Robot& robot) { auto users = std::make_unique(); users->uid = robot.rid(); users->name = robot.name(); users->type = static_cast(robot.type()); users->state = static_cast(robot.state()); users->carType=static_cast(robot.cartype()); _notify->OnRobot(users); _userInfo.push_back(std::move(users)); } void CEgoClient::OnNotifyState(int32_t rid, UserState state) { for (auto& node : _userInfo) { if (node->uid == rid) { node->state = state; _notify->OnNotifyState(rid, state); break; } } } 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 { if (_connected) { _updateThread.stop(); } _connected = false; if (_carpeer != -1) { for (int32_t i = RenderPosition::FRONT; i < RenderPosition::ALL; i++) { _WindowArray[i]->SetReady(false); _WindowArray[i]->OnNotifyLeave(); } _carpeer = -1; } _userInfo.clear(); } } void CEgoClient::OnVideoRep(bool ok,int32_t index, int32_t peer) { if (ok == false) { _carpeer = -1; _notify->OnNotifyVideoFail(peer); return; } if (peer != _carpeer) return; for (auto& node : _userInfo) { if (node->uid == peer) { /* if (index == RenderPosition::FRONT) _notify->OpenFullView(node->viewurl.c_str()); */ _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(__FILE__,__LINE__); 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(__FILE__, __LINE__); 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]->PostMessage(WM_ASK_VIDEOREQ);// DelayNextVideoReq(); } else { _WindowArray[RenderPosition::FRONT]->PostMessage(WM_ASK_PING);// DelayStartPing(); } } void CEgoClient::OnVideoCandidate(int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid) { CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__); 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]->SetReady(false); _WindowArray[i]->OnNotifyLeave(); } _carpeer = -1; _notify->OnNotifyLeave(peer); } } void CEgoClient::OnNotifyKick() { _notify->OnNotifyKickOff(); } void CEgoClient::OnNDTPos(Position* pos) { _notify->OnNDTPos(pos); } 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_RADAR: _radarsim->OnPeerMessage(cmd, length, data); break; //case ChannelType::CHANNEL_LIDAR: // _lidarsim->OnPeerMessage(cmd, length, data); // break; } } void CEgoClient::OnSigin(int32_t uid,bool bRet) { _uid = uid; _notify->OnSigin(bRet); if (bRet) { _client->WriteRobotReq(); } } void CEgoClient::OnFeedPage(FeedData& data) { _steer_angle = data.steer_angle; _can->SetEngineRPM(data.engine_rpm); _notify->OnNotifyFeed(data); } 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(true,false); for (int i = 0; i < RenderPosition::ALL; i++) { _WindowArray[i]->SetPeer(peer); } for (auto& node : _userInfo) { if (node->uid == peer) { _WindowArray[RenderPosition::FRONT]->OnAskVideoReq(); _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]->StopPing(); _WindowArray[ChannelType::CHANNEL_CAR]->SendData(&Buffer); */ // _notify->CloseFullView(); _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]->OnNotifyLeave(); } _carpeer = -1; } void CEgoClient::OnStopAck() { _client->WriteVideoLeave(EgoType::Car, _carpeer); for (int32_t i = 0; i < RenderPosition::ALL; i++) { _WindowArray[i]->SetReady(false); _WindowArray[i]->OnNotifyLeave();// PostMessage(WM_NOTIFY_LEAVE); } _carpeer = -1; } void CEgoClient::OnRadarData(int32_t r0, int32_t r1, int32_t r2, int32_t r3, int32_t r4, int32_t r5, int32_t r6) { _notify->OnNotifyRadar(r0, r1, r2, r3, r4, r5,r6); } void CEgoClient::OnEncodeData(int32_t left, int32_t right) { _notify->OnNotifyEncode(left, right); } void CEgoClient::ReqCarList() { _client->WriteRobotReq(); } void CEgoClient::OnPingValue(int32_t value,double temp) { _notify->OnNotifyPing(value,temp); } /* void CEgoClient::OnLidarData(bool isLeft, bool isDense, int32_t seq, PointXYZI* data, int32_t length) { _notify->OnLidarData(isLeft, isDense, seq, data, length); } */ void CEgoClient::OnMoveBegin(int32_t rid, WorkArea area, int32_t no) { RemoNet::CSMoveBegin Req; Req.set_peer(rid); Req.set_area(area); Req.set_no(no); MessageHead Head; CIOBuffer pBuffer; Head.Command = RemoNet::CS_MoveBegin; Head.Length = Req.ByteSizeLong(); Head.Serialize(pBuffer.Buffer); auto ptr = pBuffer.Buffer + MessageHead::Size(); Req.SerializeToArray(ptr, Head.Length); pBuffer.Length = MessageHead::Size() + Head.Length; _client->Write(&pBuffer); } /* void CEgoClient::SwitchDriver(int32_t id) { RemoNet::SwitchDriver Req; Req.set_peer(id); MessageHead Head; CIOBuffer pBuffer; Head.Command = RemoNet::CS_SwitchDriver; Head.Length = Req.ByteSizeLong(); Head.Serialize(pBuffer.Buffer); auto ptr = pBuffer.Buffer + MessageHead::Size(); Req.SerializeToArray(ptr, Head.Length); pBuffer.Length = MessageHead::Size() + Head.Length; _client->Write(&pBuffer); } */ void CEgoClient::ChangeState(UserState state) { RemoNet::CSState Req; Req.set_state((RemoNet::UserState)state); Req.set_uid(_uid); MessageHead Head; CIOBuffer pBuffer; Head.Command = RemoNet::CS_State; Head.Length = Req.ByteSizeLong(); Head.Serialize(pBuffer.Buffer); auto ptr = pBuffer.Buffer + MessageHead::Size(); Req.SerializeToArray(ptr, Head.Length); pBuffer.Length = MessageHead::Size() + Head.Length; _client->Write(&pBuffer); } void CEgoClient::OnMoveEnd(int32_t rid, WorkArea area, int32_t no) { _notify->OnNotifyMoveEnd(rid, area, no); } void CEgoClient::OnNotifyMoveRet(MoveDesc desc) { _notify->OnNotifyMoveRet(desc); } ControlStatus CEgoClient::CheckStatus() { return _can->CheckStatus(); } /* void CEgoClient::OnEmergency(bool enable, bool motor, bool gear, bool turnl, bool turnR) { _notify->OnEmergency(enable, motor, gear, turnl, turnR); } void CEgoClient::OnAccel(int16_t accel , bool onoff) { _notify->OnAccel(accel, onoff); } void CEgoClient::OnSteer(uint64_t steer) { _notify->OnSteer(steer); } void CEgoClient::OnArm(int16_t flip, int16_t armL, int16_t armR, bool onoff) { _notify->OnArm(flip, armL, armR, onoff); } */ void CEgoClient::OverlayVideo(RenderPosition pos, const webrtc::VideoFrame& frame) { _ctrl->OverlayVideo(pos, frame); } SocketClient* CEgoClient::GetSocketClient() { return _client.get(); } std::string CEgoClient::GetAccount() { return _accountText; } std::string CEgoClient::GetPassword() { return _passText; } std::string CEgoClient::GetName() { return _name; } /* bool CEgoClient::IsAutoClose() { return _autoclose; } void CEgoClient::SetCurrentPage(PageProp prop) { _currentpage = prop; } PageProp CEgoClient::GetCurrentPage() { return _currentpage; } */