MainDlg.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. // MainDlg.cpp : implementation of the CMainDlg class
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "resource.h"
  6. #include <memory>
  7. #include <mutex>
  8. #include "api.h"
  9. #include "Protocol.pb.h"
  10. #include "../common/peer_connection.h"
  11. #include "VideoRenderer.h"
  12. #include "WindowThread.h"
  13. #include "../common/sensor_socket.h"
  14. #include "ControlSensor.h"
  15. #include "MainDlg.h"
  16. #include <fstream>
  17. #include <json/json.h>
  18. #include "../common/iobuffer.h"
  19. BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
  20. {
  21. return CWindow::IsDialogMessage(pMsg);
  22. }
  23. BOOL CMainDlg::OnIdle()
  24. {
  25. UIUpdateChildWindows();
  26. return FALSE;
  27. }
  28. LRESULT CMainDlg::OnInitDialog(HWND, LPARAM)
  29. {
  30. // center the dialog on the screen
  31. CenterWindow();
  32. // set icons
  33. HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON));
  34. SetIcon(hIcon, TRUE);
  35. HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON));
  36. SetIcon(hIconSmall, FALSE);
  37. // register object for message filtering and idle updates
  38. CMessageLoop* pLoop = _Module.GetMessageLoop();
  39. ATLASSERT(pLoop != NULL);
  40. pLoop->AddMessageFilter(this);
  41. pLoop->AddIdleHandler(this);
  42. UIAddChildWindowContainer(m_hWnd);
  43. Json::Value root;
  44. Json::Reader jsonReader;
  45. std::ifstream ifile("Config.json");
  46. std::string ip;
  47. int32_t can_port = 0, host_port = 0;
  48. std::string can_ip;
  49. if (jsonReader.parse(ifile, root))
  50. {
  51. _name = root["name"].asString();
  52. _account = root["account"].asString();
  53. _password = root["password"].asString();
  54. ip = root["ip"].asString();
  55. can_port = root["can_port"].asInt();
  56. host_port = root["host_port"].asInt();
  57. can_ip = root["can_ip"].asString();
  58. }
  59. _client = std::make_unique<SocketClient>(this);
  60. _client->Start(ip.c_str());
  61. for (int i = 0; i < RenderPosition::ALL; i++)
  62. {
  63. auto p = std::make_unique<CThreadWindow>((RenderPosition)i, _client.get());
  64. p->Start();
  65. _WindowArray.push_back(std::move(p));
  66. }
  67. _control = std::make_unique <SensorSocket<CControlSensor>>(_WindowArray[ChannelType::CHANNEL_CAR].get(), can_ip, can_port, host_port);
  68. _control->Start();
  69. //_can = std::make_unique<SocketCan>(_WindowArray[ChannelType::CHANNEL_CAR].get());
  70. //_can->Start(can_ip, can_port, host_port);
  71. // for (int i = 0; i < RadarPosition::RADAR_ALL; i++)
  72. // {
  73. // auto p = std::make_unique<CRadarWindow>((RadarPosition)i);
  74. // p->Start();
  75. // _RadarArray.push_back(std::move(p));
  76. // }
  77. // _monitor = std::make_unique<MonitorThread>(this);
  78. // _monitor->Start(monitor_ip.c_str(), mon_remote, mon_local);
  79. _carpeer = -1;
  80. SetTimer(IDC_STATETIMER, 300); //Radar Bmp refresh
  81. return TRUE;
  82. }
  83. void CMainDlg::OnDestroy(void)
  84. {
  85. // unregister message filtering and idle updates
  86. CMessageLoop* pLoop = _Module.GetMessageLoop();
  87. ATLASSERT(pLoop != NULL);
  88. pLoop->RemoveMessageFilter(this);
  89. pLoop->RemoveIdleHandler(this);
  90. }
  91. void CMainDlg::OnFront(WORD, WORD, HWND)
  92. {
  93. RemoNet::CCSwitch Req;
  94. Req.set_front(true);
  95. CIOBuffer Buffer;
  96. MessageHead Head;
  97. Head.Command = RemoNet::CC_Switch;
  98. Head.Length = Req.ByteSize();
  99. Head.Serialize(Buffer.Buffer);
  100. auto ptr = Buffer.Buffer + MessageHead::Size();
  101. Req.SerializeToArray(ptr, Head.Length);
  102. Buffer.Length = Head.Length + MessageHead::Size();
  103. _WindowArray[ChannelType::CHANNEL_CAR]->SendData(&Buffer);
  104. }
  105. void CMainDlg::OnBack(WORD, WORD, HWND)
  106. {
  107. RemoNet::CCSwitch Req;
  108. Req.set_front(false);
  109. CIOBuffer Buffer;
  110. MessageHead Head;
  111. Head.Command = RemoNet::CC_Switch;
  112. Head.Length = Req.ByteSize();
  113. Head.Serialize(Buffer.Buffer);
  114. auto ptr = Buffer.Buffer + MessageHead::Size();
  115. Req.SerializeToArray(ptr, Head.Length);
  116. Buffer.Length = Head.Length + MessageHead::Size();
  117. _WindowArray[ChannelType::CHANNEL_CAR]->SendData(&Buffer);
  118. }
  119. void CMainDlg::OnSend(WORD, WORD, HWND)
  120. {
  121. char text[1024];
  122. GetDlgItemText(IDC_EDIT, text, sizeof(text));
  123. RemoNet::TestTextReq Req;
  124. Req.set_text(text);
  125. CIOBuffer Buffer;
  126. MessageHead Head;
  127. Head.Command = RemoNet::CC_Text;
  128. Head.Length = Req.ByteSize();
  129. Head.Serialize(Buffer.Buffer);
  130. auto ptr = Buffer.Buffer + MessageHead::Size();
  131. Req.SerializeToArray(ptr, Head.Length);
  132. Buffer.Length = Head.Length + MessageHead::Size();
  133. _WindowArray[ChannelType::CHANNEL_CAR]->SendData(&Buffer);
  134. }
  135. void CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/)
  136. {
  137. _client->Stop();
  138. //_can->Stop();
  139. //_monitor->Stop();
  140. for (int i = 0; i < RenderPosition::ALL; i++)
  141. {
  142. _WindowArray[i]->Stop();
  143. _WindowArray[i].reset();
  144. }
  145. CloseDialog(wID);
  146. }
  147. void CMainDlg::CloseDialog(int nVal)
  148. {
  149. DestroyWindow();
  150. ::PostQuitMessage(nVal);
  151. }
  152. void CMainDlg::OnMessageFrameNotify(ChannelType type, int16_t cmd, int16_t length, const void* data)
  153. {
  154. if (cmd == RemoNet::CC_Text)
  155. {
  156. RemoNet::TestTextReq Req;
  157. Req.ParseFromArray(data,length);
  158. std::cout << Req.text() << std::endl;
  159. }
  160. if (cmd == RemoNet::CC_StopACK)
  161. {
  162. PostMessage(WM_STOP_ACK);
  163. }
  164. if (cmd == RemoNet::CC_BootRep)
  165. {
  166. RemoNet::CCBooStrapRep Rep;
  167. Rep.ParseFromArray(data, length);
  168. _control->Get()->OnBootstrapd(Rep.ret());
  169. }
  170. if (cmd == RemoNet::CC_StartupRep)
  171. {
  172. RemoNet::CCStartupRep Rep;
  173. Rep.ParseFromArray(data, length);
  174. _control->Get()->OnStartupd(Rep.ret());
  175. }
  176. }
  177. void CMainDlg::OnVideoOffer(int32_t index, const char* type, const char* sdp)
  178. {
  179. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  180. OfferDesc* desc = (OfferDesc*)pBuffer->Buffer;
  181. strcpy_s(desc->type, type);
  182. strcpy_s(desc->sdp, sdp);
  183. _WindowArray[index]->PostMessage(WM_NOTIFY_OFFER, (WPARAM)pBuffer);
  184. }
  185. void CMainDlg::OnVideoAnswer(int32_t index, const char* type, const char* sdp)
  186. {
  187. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  188. AnswerDesc* p = (AnswerDesc*)(pBuffer->Buffer);
  189. strcpy_s(p->type, type);
  190. strcpy_s(p->sdp, sdp);
  191. _WindowArray[index]->PostMessage(WM_NOTIFY_ANSWER, (WPARAM)pBuffer);
  192. _WindowArray[index]->DelayDataChannel();
  193. index++;
  194. if (index < RenderPosition::ALL)
  195. {
  196. _WindowArray[index]->DelayNextVideoReq();
  197. }
  198. else
  199. {
  200. _WindowArray[ChannelType::CHANNEL_CAR]->DelayControlStart();
  201. }
  202. }
  203. void CMainDlg::OnVideoCandidate(int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid)
  204. {
  205. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  206. CandidateDesc* desc = (CandidateDesc*)(pBuffer->Buffer);
  207. strcpy_s(desc->candidate, candidate);
  208. strcpy_s(desc->sdp_mid, sdp_mid);
  209. desc->sdp_mline_index = sdp_mline_index;
  210. _WindowArray[index]->PostMessage(WM_NOTIFY_CANDIDATE, (WPARAM)pBuffer);
  211. }
  212. void CMainDlg::OnSigin(bool bRet)
  213. {
  214. PostMessage(WM_NOTIFYSIGIN, (WPARAM)bRet);
  215. }
  216. void CMainDlg::OnNotifySigin(bool bRet)
  217. {
  218. if (bRet)
  219. {
  220. _client->WriteRobotReq();
  221. }
  222. }
  223. void CMainDlg::OnNotifyDel(int32_t peer, EgoType type)
  224. {
  225. CListViewCtrl box;
  226. box.Attach(GetDlgItem(IDC_CARLIST));
  227. int32_t Count = box.GetItemCount();
  228. for (int32_t i = 0; i < Count; i++)
  229. {
  230. auto id = box.GetItemData(i);
  231. if (id == peer)
  232. {
  233. box.DeleteItem(i);
  234. break;
  235. }
  236. }
  237. for (auto it = _userInfo.begin(); it != _userInfo.end(); ++it)
  238. {
  239. if ((*it)->uid == peer)
  240. {
  241. _userInfo.erase(it);
  242. break;
  243. }
  244. }
  245. }
  246. void CMainDlg::OnRobot(const RemoNet::Robot& robot)
  247. {
  248. CListViewCtrl box;
  249. box.Attach(GetDlgItem(IDC_CARLIST));
  250. std::string info = robot.name();
  251. switch (robot.state())
  252. {
  253. case RemoNet::Robot_RobotState::Robot_RobotState_Busy:
  254. info += " (busy)";
  255. break;
  256. case RemoNet::Robot_RobotState::Robot_RobotState_Offline:
  257. info += " (offline)";
  258. break;
  259. case RemoNet::Robot_RobotState::Robot_RobotState_Online:
  260. info += " (online)";
  261. break;
  262. }
  263. int32_t index = box.AddItem(0, 0, info.c_str());
  264. //box.SetItemText(index, 1, robot.type() == EgoType::Car ? "Car" : "Cave");
  265. auto users = std::make_unique<UserCamera>();
  266. users->uid = robot.rid();
  267. users->name = info;
  268. users->type = static_cast<EgoType>(robot.type());
  269. users->state = static_cast<UserState>(robot.state());
  270. // for (int32_t i = 0; i < robot.info().size(); i++)
  271. // {
  272. // auto& node = robot.info(i);
  273. // CameraInfo camera;
  274. // camera.index = static_cast<RenderPosition>(node.pos());
  275. // camera.label = node.label();
  276. // camera.solution = static_cast<DisplayResolution>(node.solution());
  277. // users->info.push_back(camera);
  278. // }
  279. box.SetItemData(index, users->uid);
  280. _userInfo.push_back(std::move(users));
  281. }
  282. /*
  283. void CMainDlg::OnNotifyConnect(int32_t uid)
  284. {
  285. for (auto& node : _userInfo)
  286. {
  287. if (node->uid == uid)
  288. {
  289. if (node->type == EgoType::Car)
  290. {
  291. if (_carpeer != -1) return;
  292. mrsWebrtcCreateFactory(false);
  293. _carpeer = uid;
  294. for (int i = 0; i < RenderPosition::ALL; i++)
  295. {
  296. _WindowArray[i]->SetPeer(_carpeer);
  297. }
  298. for (auto& node : _userInfo)
  299. {
  300. if (node->uid == _carpeer)
  301. {
  302. _WindowArray[RenderPosition::FRONT_BACK]->CreateVideoReq();
  303. break;;
  304. }
  305. }
  306. }
  307. break;
  308. }
  309. }
  310. }
  311. */
  312. void CMainDlg::OnCarConnect(WORD, WORD, HWND)
  313. {
  314. if (_carpeer != -1) return;
  315. CListViewCtrl box;
  316. box.Attach(GetDlgItem(IDC_CARLIST));
  317. int32_t cursel = box.GetSelectedIndex();
  318. if (cursel == -1) return;
  319. mrsWebrtcCreateFactory(false);
  320. _carpeer = box.GetItemData(cursel);
  321. for (int i = 0; i < RenderPosition::ALL; i++)
  322. {
  323. _WindowArray[i]->SetPeer(_carpeer);
  324. }
  325. for (auto& node : _userInfo)
  326. {
  327. if (node->uid == _carpeer)
  328. {
  329. _WindowArray[RenderPosition::FRONT_BACK]->CreateVideoReq();
  330. break;;
  331. }
  332. }
  333. }
  334. /*
  335. void CMainDlg::OnNotifyDisconnect(int32_t uid)
  336. {
  337. for (auto& node : _userInfo)
  338. {
  339. if (node->uid == uid)
  340. {
  341. if (node->type == EgoType::Car)
  342. {
  343. if (_carpeer == -1) return;
  344. _client->WriteVideoLeave(EgoType::Car, _carpeer);
  345. for (int32_t i = 0; i < RenderPosition::ALL; i++)
  346. {
  347. _WindowArray[i]->PostMessage(WM_NOTIFY_LEAVE);
  348. }
  349. _carpeer = -1;
  350. }
  351. break;
  352. }
  353. }
  354. }
  355. */
  356. void CMainDlg::OnCarLeave(WORD, WORD, HWND)
  357. {
  358. RemoNet::SensorStop Req;
  359. CIOBuffer Buffer;
  360. MessageHead Head;
  361. Head.Command = RemoNet::CC_SensorStop;
  362. Head.Length = Req.ByteSize();
  363. Head.Serialize(Buffer.Buffer);
  364. auto ptr = Buffer.Buffer + MessageHead::Size();
  365. Req.SerializeToArray(ptr, Head.Length);
  366. Buffer.Length = Head.Length + MessageHead::Size();
  367. _WindowArray[ChannelType::CHANNEL_CAR]->SendData(&Buffer);
  368. }
  369. void CMainDlg::OnStopAck()
  370. {
  371. _client->WriteVideoLeave(EgoType::Car, _carpeer);
  372. for (int32_t i = 0; i < RenderPosition::ALL; i++)
  373. {
  374. _WindowArray[i]->PostMessage(WM_NOTIFY_LEAVE);
  375. }
  376. _carpeer = -1;
  377. }
  378. void CMainDlg::OnConnected(bool bRet)
  379. {
  380. PostMessage(WM_NOTIFYCONNECTED, (WPARAM)bRet);
  381. }
  382. void CMainDlg::OnNotifyConnected(bool bRet)
  383. {
  384. if (bRet)
  385. {
  386. if (!_account.empty())
  387. _client->WriteSign(_account.c_str(), _password.c_str());
  388. _updateThread.start(_client.get());
  389. }
  390. else
  391. {
  392. if (_carpeer != -1)
  393. {
  394. for (int32_t i = RenderPosition::FRONT_BACK; i < RenderPosition::ALL; i++)
  395. {
  396. _WindowArray[i]->PostMessage(WM_NOTIFY_LEAVE);
  397. }
  398. _carpeer = -1;
  399. }
  400. _updateThread.stop();
  401. CListViewCtrl car;
  402. car.Attach(GetDlgItem(IDC_CARLIST));
  403. car.DeleteAllItems();
  404. _userInfo.clear();
  405. }
  406. }
  407. void CMainDlg::OnTimer(UINT id)
  408. {
  409. // FOR TEST ONLY
  410. //float x = rand() % 10;
  411. //float y = rand() % 10;
  412. //Coord p;
  413. //p.x = x;
  414. //p.y = y;
  415. //_ptArray[0].push_back(p);
  416. //_RadarArray[0]->SetRadarPoint(_ptArray[0]);
  417. //_ptArray[0].clear();
  418. //_RadarArray[0]->Invalidate();
  419. // FOR TEST ONLY
  420. if (!bUpdated) return;
  421. bUpdated = false;
  422. /*
  423. int32_t temp = feedback.feedBackData.feedback.water_temp_alarm;
  424. SetDlgItemText(IDC_WATER_TEMP_ALARM, warnMsg(temp).c_str());
  425. temp = feedback.feedBackData.feedback.oil_pressure_low_alarm;
  426. SetDlgItemText(IDC_OIL_PRESSURE_LOW_ALARM, warnMsg(temp).c_str());
  427. temp = feedback.feedBackData.feedback.air_filter_alarm;
  428. SetDlgItemText(IDC_AIR_FILTER_ALARM, warnMsg(temp).c_str());
  429. temp = feedback.feedBackData.feedback.left_oil_filter_alarm;
  430. SetDlgItemText(IDC_LEFT_OIL_FILTER_ALARM, warnMsg(temp).c_str());
  431. temp = feedback.feedBackData.feedback.right_oil_filter_alarm;
  432. SetDlgItemText(IDC_RIGHT_OIL_FILTER_ALARM, warnMsg(temp).c_str());
  433. temp = feedback.feedBackData.feedback.oil_loop_filter_alarm;
  434. SetDlgItemText(IDC_OIL_LOOP_FILTER_ALARM, warnMsg(temp).c_str());
  435. temp = feedback.feedBackData.feedback.hydr_pressure_low_alarm;
  436. SetDlgItemText(IDC_HYDR_PRESSURE_LOW_ALARM, warnMsg(temp).c_str());
  437. temp = feedback.feedBackData.feedback.engine_rpm;
  438. SetDlgItemInt(IDC_ENGINE_RPM, temp);
  439. temp = feedback.feedBackData.feedback.oil_pressure;
  440. SetDlgItemInt(IDC_OIL_PRESSURE, temp);
  441. temp = feedback.feedBackData.feedback.water_temperature;
  442. SetDlgItemInt(IDC_WATER_TEMPERATURE, temp);
  443. temp = feedback.feedBackData.feedback.left_MA_pressure;
  444. SetDlgItemInt(IDC_LEFT_MA_PRESSURE, temp);
  445. temp = feedback.feedBackData.feedback.left_MB_pressure;
  446. SetDlgItemInt(IDC_LEFT_MB_PRESSURE, temp);
  447. temp = feedback.feedBackData.feedback.right_MA_pressure;
  448. SetDlgItemInt(IDC_RIGHT_MA_PRESSURE, temp);
  449. temp = feedback.feedBackData.feedback.right_MB_pressure;
  450. SetDlgItemInt(IDC_RIGHT_MB_PRESSURE, temp);
  451. temp = feedback.feedBackData.feedback.pump_pressure;
  452. SetDlgItemInt(IDC_PUMP_PRESSURE, temp);
  453. temp = feedback.feedBackData.feedback.left_motor_rpm;
  454. SetDlgItemInt(IDC_LEFT_MOTOR_RPM, temp);
  455. temp = feedback.feedBackData.feedback.right_motor_rpm;
  456. SetDlgItemInt(IDC_RIGHT_MOTOR_RPM, temp);
  457. temp = feedback.feedBackData.feedback.hydr_oil_height;
  458. SetDlgItemInt(IDC_HYDR_OIL_HEIGHT, temp);
  459. temp = feedback.feedBackData.feedback.disel_oil_height;
  460. SetDlgItemInt(IDC_DISEL_OIL_HEIGHT, temp);
  461. temp = feedback.feedBackData.feedback.up_time;
  462. SetDlgItemInt(IDC_UP_TIME, temp);
  463. temp = feedback.feedBackData.feedback.votage;
  464. SetDlgItemInt(IDC_VOTAGE, temp);
  465. */
  466. }
  467. void CMainDlg::OnVideoRep(int32_t index, int32_t peer)
  468. {
  469. if ( peer != _carpeer) return;
  470. for (auto& node : _userInfo)
  471. {
  472. if (node->uid == peer)
  473. {
  474. _WindowArray[index]->PostMessage(WM_NOTIFY_REP, (WPARAM)peer, (LPARAM)index);
  475. return;
  476. }
  477. }
  478. }
  479. void CMainDlg::OnNotifyKick()
  480. {
  481. }
  482. void CMainDlg::OnVideoLeave(int32_t peer, EgoType type)
  483. {
  484. if (type == EgoType::Car)
  485. {
  486. for (int32_t i = RenderPosition::FRONT_BACK; i < RenderPosition::ALL; i++)
  487. {
  488. _WindowArray[i]->PostMessage(WM_NOTIFY_LEAVE);
  489. }
  490. _carpeer = -1;
  491. }
  492. }
  493. /*
  494. void CMainDlg::OnNotifyReady()
  495. {
  496. if (bNotifyReady) return;
  497. CIOBuffer Buffer;
  498. HM_CarList* l = reinterpret_cast<HM_CarList*>(Buffer.Buffer);
  499. /// HM_CarList * list+-
  500. l->Head.Command = HM_CARLIST;
  501. l->count = _userInfo.size();
  502. Buffer.Length = offsetof(HM_CarList, carArray);
  503. int32_t index = 0;
  504. for (auto& node : _userInfo)
  505. {
  506. strcpy_s(l->carArray[index].name, node->name.c_str());
  507. l->carArray[index].state = node->state;
  508. l->carArray[index].type = node->type;
  509. l->carArray[index].uid = node->uid;
  510. index++;
  511. Buffer.Length += sizeof(CarState);
  512. }
  513. l->Head.Length = Buffer.Length;
  514. //_monitor->Write(&Buffer);
  515. bNotifyReady = true;
  516. }
  517. void CMainDlg::OnNotifyStop()
  518. {
  519. }
  520. */