appmodel.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. #include "appmodel.h"
  2. #include "Ego.h"
  3. #define WIN23_LEAN_AND_MEAN
  4. #include <Windows.h>
  5. #include <mmsystem.h>
  6. #pragma comment (lib,"winmm.lib")
  7. //#include "FishEyeWindow.h"
  8. // extern std::array<IRender *, 3> ar;
  9. CarData::CarData(QObject *parent) : QObject(parent) {}
  10. CarData::CarData(const CarData &other)
  11. : QObject(nullptr), m_uid(other.m_uid), m_name(other.m_name),
  12. m_type(other.m_type), m_state(other.m_state) {}
  13. int32_t CarData::uid() const { return m_uid; }
  14. QString CarData::name() const { return m_name; }
  15. QString CarData::type() const { return m_type; }
  16. QString CarData::state() const { return m_state; }
  17. void CarData::setUid(const int32_t &value) {
  18. m_uid = value;
  19. emit dataChanged();
  20. }
  21. void CarData::setCarType(const int32_t& value)
  22. {
  23. m_carType = value;
  24. }
  25. void CarData::setName(const QString &value) {
  26. m_name = value;
  27. emit dataChanged();
  28. }
  29. void CarData::setState(const QString &value) {
  30. m_state = value;
  31. emit dataChanged();
  32. }
  33. void CarData::setType(const QString &value) {
  34. m_type = value;
  35. emit dataChanged();
  36. }
  37. class AppModelPrivate {
  38. public:
  39. QList<CarData *> car;
  40. QQmlListProperty<CarData> *carProp = nullptr;
  41. };
  42. static void carAppend(QQmlListProperty<CarData> *prop, CarData *val) {
  43. Q_UNUSED(val);
  44. Q_UNUSED(prop);
  45. }
  46. static CarData *carAt(QQmlListProperty<CarData> *prop, qsizetype index) {
  47. AppModelPrivate *d = static_cast<AppModelPrivate *>(prop->data);
  48. return d->car.at(index);
  49. }
  50. static qsizetype carCount(QQmlListProperty<CarData> *prop) {
  51. AppModelPrivate *d = static_cast<AppModelPrivate *>(prop->data);
  52. return d->car.size();
  53. }
  54. static void carClear(QQmlListProperty<CarData> *prop) {
  55. static_cast<AppModelPrivate *>(prop->data)->car.clear();
  56. }
  57. AppModel::AppModel(IEgoNotify *n, IEgoControl *c, QObject* qmlobj, QObject *parent)
  58. : _notify(n), _control(c), QObject(parent), d(new AppModelPrivate),m_peer(-1),m_qmlObj(qmlobj) {
  59. m_account = QString::fromStdString(_control->GetAccount());
  60. m_password = QString::fromStdString(_control->GetPassword());
  61. m_driveName = QString::fromStdString(_control->GetName());
  62. d->carProp = new QQmlListProperty<CarData>(this, d, carAppend, carCount,
  63. carAt, carClear);
  64. m_ready = false;
  65. // _window = std::make_unique<CFishEyeWindow>();
  66. //_window->Start("192.168.1.64");
  67. connect(dynamic_cast<QObject *>(_notify), SIGNAL(egoLoginResult(bool)), this,
  68. SLOT(onLoginRst(bool)));
  69. connect(dynamic_cast<QObject *>(_notify), SIGNAL(egoCarAppend(UserCamera)),
  70. this, SLOT(onCarAppend(UserCamera)));
  71. connect(
  72. dynamic_cast<QObject *>(_notify),
  73. SIGNAL(egoRadar(int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t)),
  74. this,
  75. SLOT(
  76. onRadarUpadte(int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t)));
  77. connect(
  78. dynamic_cast<QObject*>(_notify),
  79. SIGNAL(egoEncode(int32_t, int32_t)),
  80. this,
  81. SLOT(
  82. onEncodeUpdate(int32_t, int32_t)));
  83. connect(dynamic_cast<QObject*>(_notify),
  84. SIGNAL(egoFeedData(const FeedData&)),
  85. this,
  86. SLOT(onFeedData(const FeedData&)));
  87. connect(dynamic_cast<QObject *>(_notify), SIGNAL(egoPing(int32_t,double)), this,
  88. SLOT(onPing(int32_t,double)));
  89. connect(dynamic_cast<QObject*>(_notify), SIGNAL(egoPosition(float, float, float)), this,
  90. SLOT(OnPosition(float, float, float)));
  91. connect(dynamic_cast<QObject*>(_notify), SIGNAL(egoNotifyLeave(int32_t)), this,
  92. SLOT(onNotifyLeave(int32_t)));
  93. connect(dynamic_cast<QObject*>(_notify), SIGNAL(egoNotifyFail(int32_t)), this,
  94. SLOT(onNotifyFail(int32_t)));
  95. connect(dynamic_cast<QObject *>(_notify), SIGNAL(egoNotifyDel(int32_t)), this,
  96. SLOT(onNotifyDel(int32_t)));
  97. connect(dynamic_cast<QObject *>(_notify), SIGNAL(egoKickoff()), this,
  98. SLOT(onKickoff()));
  99. connect(dynamic_cast<QObject*>(_notify), SIGNAL(egoMoveEnd(int32_t, WorkArea , int32_t)), this,
  100. SLOT(onMoveEnd(int32_t, WorkArea, int32_t)));
  101. connect(dynamic_cast<QObject*>(_notify), SIGNAL(egoMoveRet(MoveDesc)), this,
  102. SLOT(onMoveRet(MoveDesc)));
  103. connect(dynamic_cast<QObject*>(_notify), SIGNAL(egoNotifyState(int32_t, UserState)), this,
  104. SLOT(onNotifyState(int32_t, UserState)));
  105. // connect(dynamic_cast<QObject *>(_notify),SIGNAL(ego)
  106. }
  107. AppModel::~AppModel() {
  108. if (d->carProp)
  109. delete d->carProp;
  110. if (d)
  111. delete d;
  112. }
  113. int AppModel::BrakePressure() const { return m_brake_pressure; }
  114. int AppModel::GearboxOilPressure()const { return m_gearbox_oil_pressure; }
  115. int AppModel::GearboxOilTemp() const { return m_gearbox_oil_temp; }
  116. int AppModel::EnginRpm()const { return m_engine_rpm; }
  117. int AppModel::Speed() const { return m_speed; }
  118. bool AppModel::Visible() const { return m_visible; }
  119. //int AppModel::Gear() const { return m_gear; }
  120. int AppModel::Cold() const { return m_cold_water; }
  121. //int AppModel::ModelX() const { return m_modelx; }
  122. //int AppModel::ModelY() const { return m_modely; }
  123. //int AppModel::ModelZ() const { return m_modelz; }
  124. int32_t AppModel::leftAngle() const { return m_leftAngle; }
  125. int32_t AppModel::rightAngle() const { return m_rightAngle; }
  126. int AppModel::EngineOilPressure() const { return m_engine_oil_pressure; }
  127. bool AppModel::ready() const
  128. {
  129. return m_ready;
  130. }
  131. QString AppModel::Account() const { return m_account; }
  132. QString AppModel::Password() const { return m_password; }
  133. QString AppModel::CarName() const { return m_carName; }
  134. QString AppModel::DriveName() const { return m_driveName; }
  135. int AppModel::radar1() const { return m_radar1; }
  136. int32_t AppModel::WorkPressure() const { return m_work_pressure; }
  137. int AppModel::radar2() const { return m_radar2; }
  138. int AppModel::radar3() const { return m_radar3; }
  139. int AppModel::radar4() const { return m_radar4; }
  140. int AppModel::radar5() const { return m_radar5; }
  141. //int AppModel::radar6() const { return m_radar6; }
  142. //int AppModel::radar7() const { return m_radar7; }
  143. int32_t AppModel::OilPos() const { return m_oil_pos; }
  144. //int AppModel::radar6() const { return m_radar6; }
  145. int AppModel::ping() const { return m_ping; }
  146. double AppModel::temp() const { return m_temp; }
  147. int AppModel::alertSound() const { return m_alert; }
  148. void AppModel::setReady(const bool value) {
  149. m_ready = value;
  150. emit readyChanged();
  151. }
  152. void AppModel::SetSpeed(const int32_t value)
  153. {
  154. m_speed = value;
  155. emit speedChanged();
  156. }
  157. void AppModel::SetVisible(const bool value)
  158. {
  159. m_visible = value;
  160. emit visibleChanged();
  161. }
  162. void AppModel::SetCold(const int32_t value)
  163. {
  164. m_cold_water = value;
  165. emit coldChanged();
  166. }
  167. void AppModel::SetGearBoxOilPressure(const int32_t value)
  168. {
  169. m_gearbox_oil_pressure = value;
  170. emit gearboxOilPressureChanged();
  171. }
  172. void AppModel::setAccount(const QString& value)
  173. {
  174. m_account = value;
  175. emit accountChanged();
  176. }
  177. void AppModel::setLeftAngle(const int32_t value)
  178. {
  179. m_leftAngle = value;
  180. emit leftAngleChanged();
  181. }
  182. void AppModel::setRightAngle(const int32_t value)
  183. {
  184. m_rightAngle = value;
  185. emit rightAngleChanged();
  186. }
  187. /*
  188. void AppModel::setModelX(const int32_t value)
  189. {
  190. m_modelx = value;
  191. emit modelXChanged();
  192. }
  193. void AppModel::setModelZ(const int32_t value)
  194. {
  195. m_modelz = value+200;
  196. emit modelZChanged();
  197. }
  198. void AppModel::setModelY(const int32_t value)
  199. {
  200. m_modely = value;
  201. emit modelYChanged();
  202. }
  203. */
  204. void AppModel::setPassword(const QString& password)
  205. {
  206. m_password = password;
  207. emit passwordChanged();
  208. }
  209. void AppModel::setCarName(const QString& carname)
  210. {
  211. m_carName = carname;
  212. emit carNameChanged();
  213. }
  214. void AppModel::setDriveName(const QString& drivename)
  215. {
  216. m_driveName = drivename;
  217. emit driveNameChanged();
  218. }
  219. void AppModel::setWorkPressure(const int32_t value)
  220. {
  221. m_work_pressure = value;
  222. emit workPressureChanged();
  223. }
  224. /*
  225. void AppModel::SetGear(const int32_t value)
  226. {
  227. m_gear = value;
  228. emit gearChanged();
  229. }
  230. */
  231. void AppModel::SetBrakePressure(int32_t value)
  232. {
  233. m_brake_pressure = value;
  234. emit brakePressureChanged();
  235. }
  236. void AppModel::SetOilPos(int32_t value)
  237. {
  238. m_oil_pos = value;
  239. emit oilPosChanged();
  240. }
  241. void AppModel::SetGearBoxOilTemp(const int32_t value)
  242. {
  243. m_gearbox_oil_temp = value;
  244. emit gearboxOilTempChanged();
  245. }
  246. void AppModel::SetEngingRpm(const int32_t value)
  247. {
  248. m_engine_rpm = value;
  249. emit engineRpmChanged();
  250. }
  251. void AppModel::SetEngineOilPressure(const int32_t value)
  252. {
  253. m_engine_oil_pressure = value;
  254. emit engineoilPressureChanged();
  255. }
  256. void AppModel::setRadar1(const int32_t &value) {
  257. m_radar1 = value;
  258. emit radar1Changed();
  259. }
  260. void AppModel::setRadar2(const int32_t &value) {
  261. m_radar2 = value;
  262. emit radar2Changed();
  263. }
  264. void AppModel::setRadar3(const int32_t &value) {
  265. m_radar3 = value;
  266. emit radar3Changed();
  267. }
  268. void AppModel::setSteerAngle(const float& value)
  269. {
  270. m_steerAngle = value;
  271. emit steerAngleChanged();
  272. }
  273. float AppModel::Steer() const
  274. {
  275. return m_steerAngle;
  276. }
  277. void AppModel::setRadar4(const int32_t &value) {
  278. m_radar4 = value;
  279. emit radar4Changed();
  280. }
  281. void AppModel::setRadar5(const int32_t &value) {
  282. m_radar5 = value;
  283. emit radar5Changed();
  284. }
  285. /*
  286. void AppModel::setRadar6(const int32_t& value)
  287. {
  288. m_radar6 = value;
  289. emit radar6Changed();
  290. }
  291. void AppModel::setRadar7(const int32_t& value)
  292. {
  293. m_radar7 = value;
  294. emit radar7Changed();
  295. }
  296. */
  297. /*
  298. void AppModel::setRadar6(const int32_t &value) {
  299. m_radar6 = value;
  300. emit radar6Changed();
  301. }
  302. */
  303. void AppModel::setPing(const int32_t &value) {
  304. m_ping = value;
  305. emit pingChanged();
  306. }
  307. void AppModel::setTemp(const double& value)
  308. {
  309. m_temp = value;
  310. emit tempChanged();
  311. }
  312. void AppModel::onKickoff() { emit closeWin(); }
  313. void AppModel::onNotifyLeave(int32_t peer) { m_peer = -1; }
  314. void AppModel::onNotifyFail(int32_t peer)
  315. {
  316. m_peer = -1;
  317. }
  318. QQmlListProperty<CarData> AppModel::car() const { return *(d->carProp); }
  319. void AppModel::userLogin(QString useName, QString password) {
  320. _control->Login(useName.toStdString(), password.toStdString());
  321. }
  322. void AppModel::leaveCar() {
  323. if (m_ready == false) return;
  324. m_ready = false;
  325. m_peer = -1;
  326. _control->SetReady(false);
  327. _control->OnCarLeave();
  328. _control->ChangeState(UserState::Idle);
  329. }
  330. bool AppModel::idle(int id)
  331. {
  332. for (auto& node : _users)
  333. {
  334. if (node.uid == id &&((id==m_peer)|| node.state == UserState::Idle))
  335. return true;
  336. }
  337. return false;
  338. }
  339. void AppModel::connectCar(int id) {
  340. for (auto& node : _users)
  341. {
  342. if (node.uid == id&&node.state==UserState::Idle)
  343. {
  344. if (m_ready == true) return;
  345. _control->ChangeState(UserState::Remote);
  346. _control->OnCarConnect(id);
  347. _control->SetReady(true);
  348. m_peer = id;
  349. SetVisible(node.carType == CarType::ZJ);
  350. setReady(true);
  351. this->setCarName(QString::fromStdString(node.name));
  352. break;
  353. }
  354. }
  355. }
  356. void AppModel::controlCar(int id)
  357. {
  358. connectCar(id);
  359. }
  360. void AppModel::cancelControl()
  361. {
  362. _control->ChangeState(UserState::Idle);
  363. }
  364. void AppModel::showBackDlg(QString content, WorkArea area, int32_t no, int32_t uid)
  365. {
  366. QVariant arg_1 = content;
  367. QVariant arg_2 = (int32_t)area;
  368. QVariant arg_3 = no;
  369. QVariant arg_4 = uid;
  370. QMetaObject::invokeMethod(m_qmlObj, "popDialog",
  371. Q_ARG(QVariant, arg_1),
  372. Q_ARG(QVariant, arg_2),
  373. Q_ARG(QVariant, arg_3),
  374. Q_ARG(QVariant, arg_4));
  375. }
  376. void AppModel::showMoveDlg(MoveDesc desc)
  377. {
  378. QVariant arg_1 = (int32_t)desc;
  379. QMetaObject::invokeMethod(m_qmlObj, "moveErrorDialog", Q_ARG(QVariant, arg_1));
  380. }
  381. /*
  382. void AppModel::showTrackDlg()
  383. {
  384. }
  385. void AppModel::unTrack()
  386. {
  387. m_ready = false;
  388. _control->SetReady(false);
  389. _control->OnCarLeave();
  390. }
  391. bool AppModel::autoClose()
  392. {
  393. return _control->IsAutoClose();
  394. }
  395. */
  396. void AppModel::onLoginRst(bool rst) {
  397. qDebug() << "AppModel::loginResult >>";
  398. // if (rst) {
  399. // delete previous carList
  400. // foreach (CarData *inf, d->car)
  401. // delete inf;
  402. // d->car.clear();
  403. // req new list
  404. //_control->ReqCarList();
  405. // } else {
  406. // qDebug() << "AppModel::loginResult >>" << rst;
  407. // }
  408. }
  409. void AppModel::onNotifyDel(int32_t peer) {
  410. for (int32_t i = 0; i < d->car.count(); i++)
  411. {
  412. if (d->car.at(i)->uid() == peer)
  413. {
  414. delete d->car.at(i);
  415. d->car.removeAt(i);
  416. break;
  417. }
  418. }
  419. emit carChanged();
  420. for (auto it = _users.begin(); it != _users.end(); ++it)
  421. {
  422. if ((*it).uid == peer)
  423. {
  424. _users.erase(it);
  425. break;
  426. }
  427. }
  428. // d->car.clear();
  429. }
  430. void AppModel::onCarAppend(const UserCamera &info) {
  431. bool bFind = false;
  432. for (auto& node : d->car)
  433. {
  434. if (node->uid() == info.uid)
  435. {
  436. node->setType(QString::number(info.type));
  437. node->setName(QString::fromStdString(info.name));
  438. node->setState(QString::number(info.state));
  439. node->setCarType(info.carType);
  440. bFind = true;
  441. return;
  442. }
  443. }
  444. if (!bFind)
  445. {
  446. CarData* users = new CarData;// = (CarData*)malloc;
  447. users->setUid(info.uid);
  448. users->setType(QString::number(info.type));
  449. users->setName(QString::fromStdString(info.name));
  450. users->setState(QString::number(info.state));
  451. users->setCarType(info.carType);
  452. d->car.append(users);
  453. }
  454. bFind = false;
  455. for (auto& node : _users)
  456. {
  457. if (node.uid == info.uid)
  458. {
  459. bFind = true;
  460. }
  461. }
  462. if(!bFind)
  463. _users.push_back(info);
  464. emit carChanged();
  465. }
  466. void AppModel::onNotifyState(int32_t uid, UserState state)
  467. {
  468. for (auto& node : d->car)
  469. {
  470. if (node->uid() == uid)
  471. {
  472. node->setState(QString::number(state));
  473. break;
  474. }
  475. }
  476. emit carChanged();
  477. for (auto& node : _users)
  478. {
  479. if (node.uid == uid)
  480. {
  481. node.state = state;
  482. break;
  483. }
  484. }
  485. }
  486. void AppModel::onRadarUpadte(int32_t r0, int32_t r1, int32_t r2, int32_t r3,
  487. int32_t r4, int32_t ,int32_t ) {
  488. if (r0 > 0)
  489. this->setRadar1(r0);
  490. if (r1 > 0)
  491. this->setRadar2(r1);
  492. if (r2 > 0)
  493. this->setRadar3(r2);
  494. if (r3 > 0)
  495. this->setRadar4(r3);
  496. if (r4 > 0)
  497. this->setRadar5(r4);
  498. /*
  499. if (r5 > 0)
  500. this->setRadar6(r5);
  501. if (r6 > 0)
  502. this->setRadar7(r6);
  503. */
  504. int32_t distance = 200;
  505. /*if ((r0 > 0 && r0 < distance)) {
  506. PlaySound(TEXT("beep-1.wav"), NULL, SND_FILENAME | SND_ASYNC);
  507. }*/
  508. }
  509. void AppModel::onFeedData(const FeedData& data)
  510. {
  511. this->SetBrakePressure(data.brake_pressure);
  512. this->SetEngineOilPressure(data.engine_pressure);
  513. this->SetEngingRpm(data.engine_rpm);
  514. //this->SetGear(data.gear);
  515. this->SetGearBoxOilPressure(data.gearbox_oil_pressure);
  516. this->SetGearBoxOilTemp(data.gearbox_oil_temp);
  517. this->SetSpeed(data.speed);
  518. this->setWorkPressure(data.work_pressure);
  519. this->SetCold(data.cold_water);
  520. this->setSteerAngle(data.steer_angle/4096.f*360.f);
  521. }
  522. void AppModel::onPing(int32_t value, double temp) { this->setPing(value); this->setTemp(temp); }
  523. void AppModel::logout() {
  524. d->car.clear();
  525. m_ready = false;
  526. }
  527. void AppModel::moveBegin(int32_t area,int32_t no)
  528. {
  529. if (m_peer == -1) return;
  530. no -= 1;
  531. // _control->OnCarLeave();
  532. _control->OnMoveBegin(m_peer, static_cast<WorkArea>(area), no);
  533. }
  534. /*
  535. void AppModel::switchDriver()
  536. {
  537. if (m_peer == -1) return;
  538. _control->SwitchDriver(m_peer);
  539. }
  540. */
  541. void AppModel::onMoveEnd(int32_t rid, WorkArea area, int32_t no)
  542. {
  543. for (auto& user:_users)
  544. {
  545. if (user.uid == rid)
  546. {
  547. //sr text[128];
  548. //sprintf(text,"%s �Ѿ�����Ŀ��ص�\n \t ����Զ�̲ٿ�", data->name().toStdString().c_str());
  549. QString str = QString::fromStdString(user.name);
  550. showBackDlg(str, area,no,rid);
  551. _control->ChangeState(UserState::AskRemote);
  552. // sprintf(text, "321321312" );
  553. }
  554. }
  555. }
  556. void AppModel::onMoveRet(MoveDesc desc)
  557. {
  558. if (desc != MoveDesc::Move_OK)
  559. {
  560. showMoveDlg(desc);
  561. }
  562. }
  563. /*
  564. void AppModel::areaChanged(int32_t area)
  565. {
  566. m_area = static_cast<WorkArea>(area);
  567. }
  568. */
  569. void AppModel::OnClose()
  570. {
  571. if (m_ready)
  572. {
  573. leaveCar();
  574. Sleep(5000);
  575. }
  576. ::ExitProcess(0);
  577. }
  578. void AppModel::onEncodeUpdate(int32_t left, int32_t right)
  579. {
  580. setLeftAngle(left);
  581. setRightAngle(right);
  582. }
  583. /*
  584. void AppModel::OnPosition(float x, float y, float z)
  585. {
  586. setModelX(x * 100);
  587. setModelY(y * 100);
  588. setModelZ(z * 100);
  589. }
  590. */