MainDlg.cpp 14 KB

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