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