appmodel.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #include <stdint.h>
  2. #include <memory>
  3. #include "../common/comm.h"
  4. #include "appmodel.h"
  5. #include "Ego.h"
  6. #define WIN23_LEAN_AND_MEAN
  7. #include <Windows.h>
  8. //#include "FishEyeWindow.h"
  9. // extern std::array<IRender *, 3> ar;
  10. CarData::CarData(QObject *parent) : QObject(parent) {}
  11. CarData::CarData(const CarData &other)
  12. : QObject(nullptr), m_uid(other.m_uid), m_name(other.m_name),
  13. m_type(other.m_type), m_state(other.m_state) {}
  14. int32_t CarData::uid() const { return m_uid; }
  15. QString CarData::name() const { return m_name; }
  16. QString CarData::type() const { return m_type; }
  17. QString CarData::state() const { return m_state; }
  18. void CarData::setUid(const int32_t &value) {
  19. m_uid = value;
  20. emit dataChanged();
  21. }
  22. void CarData::setCarType(const int32_t& value)
  23. {
  24. m_carType = value;
  25. }
  26. void CarData::setName(const QString &value) {
  27. m_name = value;
  28. emit dataChanged();
  29. }
  30. void CarData::setState(const QString &value) {
  31. m_state = value;
  32. emit dataChanged();
  33. }
  34. void CarData::setType(const QString &value) {
  35. m_type = value;
  36. emit dataChanged();
  37. }
  38. class AppModelPrivate {
  39. public:
  40. QList<CarData *> car;
  41. QQmlListProperty<CarData> *carProp = nullptr;
  42. };
  43. static void carAppend(QQmlListProperty<CarData> *prop, CarData *val) {
  44. Q_UNUSED(val);
  45. Q_UNUSED(prop);
  46. }
  47. static CarData *carAt(QQmlListProperty<CarData> *prop, qsizetype index) {
  48. AppModelPrivate *d = static_cast<AppModelPrivate *>(prop->data);
  49. return d->car.at(index);
  50. }
  51. static qsizetype carCount(QQmlListProperty<CarData> *prop) {
  52. AppModelPrivate *d = static_cast<AppModelPrivate *>(prop->data);
  53. return d->car.size();
  54. }
  55. static void carClear(QQmlListProperty<CarData> *prop) {
  56. static_cast<AppModelPrivate *>(prop->data)->car.clear();
  57. }
  58. AppModel::AppModel(IMonitroNotify*n, IRemoteCtrl *c, QObject* qmlobj, QObject *parent)
  59. : _notify(n), _control(c), QObject(parent), d(new AppModelPrivate),m_peer(-1),m_qmlObj(qmlobj) {
  60. m_account = QString::fromStdString(_control->GetAccount());
  61. m_password = QString::fromStdString(_control->GetPassword());
  62. m_driveName = QString::fromStdString(_control->GetName());
  63. d->carProp = new QQmlListProperty<CarData>(this, d, carAppend, carCount,
  64. carAt, carClear);
  65. m_ready = false;
  66. // _window = std::make_unique<CFishEyeWindow>();
  67. //_window->Start("192.168.1.64");
  68. connect(dynamic_cast<QObject *>(_notify), SIGNAL(egoLoginResult(bool)), this,
  69. SLOT(onLoginRst(bool)));
  70. connect(dynamic_cast<QObject *>(_notify), SIGNAL(egoCarAppend(UserDriver)),
  71. this, SLOT(onCarAppend(UserDriver)));
  72. connect(dynamic_cast<QObject*>(_notify), SIGNAL(egoNotifyLeave(int32_t)), this,
  73. SLOT(onNotifyLeave(int32_t)));
  74. connect(dynamic_cast<QObject*>(_notify), SIGNAL(egoNotifyFail(int32_t)), this,
  75. SLOT(onNotifyFail(int32_t)));
  76. connect(dynamic_cast<QObject *>(_notify), SIGNAL(egoNotifyDel(int32_t)), this,
  77. SLOT(onNotifyDel(int32_t)));
  78. connect(dynamic_cast<QObject *>(_notify), SIGNAL(egoKickoff()), this,
  79. SLOT(onKickoff()));
  80. connect(dynamic_cast<QObject*>(_notify), SIGNAL(egoNotifyState(int32_t, UserState)), this,
  81. SLOT(onNotifyState(int32_t, UserState)));
  82. // connect(dynamic_cast<QObject *>(_notify),SIGNAL(ego)
  83. }
  84. AppModel::~AppModel() {
  85. if (d->carProp)
  86. delete d->carProp;
  87. if (d)
  88. delete d;
  89. }
  90. //int AppModel::ModelX() const { return m_modelx; }
  91. //int AppModel::ModelY() const { return m_modely; }
  92. //int AppModel::ModelZ() const { return m_modelz; }
  93. bool AppModel::ready() const
  94. {
  95. return m_ready;
  96. }
  97. QString AppModel::Account() const { return m_account; }
  98. QString AppModel::Password() const { return m_password; }
  99. QString AppModel::CarName() const { return m_carName; }
  100. QString AppModel::DriveName() const { return m_driveName; }
  101. void AppModel::setReady(const bool value) {
  102. m_ready = value;
  103. emit readyChanged();
  104. }
  105. void AppModel::setAccount(const QString& value)
  106. {
  107. m_account = value;
  108. emit accountChanged();
  109. }
  110. /*
  111. void AppModel::setModelX(const int32_t value)
  112. {
  113. m_modelx = value;
  114. emit modelXChanged();
  115. }
  116. void AppModel::setModelZ(const int32_t value)
  117. {
  118. m_modelz = value+200;
  119. emit modelZChanged();
  120. }
  121. void AppModel::setModelY(const int32_t value)
  122. {
  123. m_modely = value;
  124. emit modelYChanged();
  125. }
  126. */
  127. void AppModel::setPassword(const QString& password)
  128. {
  129. m_password = password;
  130. emit passwordChanged();
  131. }
  132. void AppModel::setCarName(const QString& carname)
  133. {
  134. m_carName = carname;
  135. emit carNameChanged();
  136. }
  137. void AppModel::setDriveName(const QString& drivename)
  138. {
  139. m_driveName = drivename;
  140. emit driveNameChanged();
  141. }
  142. /*
  143. void AppModel::SetGear(const int32_t value)
  144. {
  145. m_gear = value;
  146. emit gearChanged();
  147. }
  148. */
  149. /*
  150. void AppModel::setRadar6(const int32_t& value)
  151. {
  152. m_radar6 = value;
  153. emit radar6Changed();
  154. }
  155. void AppModel::setRadar7(const int32_t& value)
  156. {
  157. m_radar7 = value;
  158. emit radar7Changed();
  159. }
  160. */
  161. /*
  162. void AppModel::setRadar6(const int32_t &value) {
  163. m_radar6 = value;
  164. emit radar6Changed();
  165. }
  166. */
  167. void AppModel::onKickoff() { emit closeWin(); }
  168. void AppModel::onNotifyLeave(int32_t peer) { m_peer = -1; }
  169. void AppModel::onNotifyFail(int32_t peer)
  170. {
  171. m_peer = -1;
  172. }
  173. QQmlListProperty<CarData> AppModel::car() const { return *(d->carProp); }
  174. void AppModel::userLogin(QString useName, QString password) {
  175. _control->Login(useName.toStdString(), password.toStdString());
  176. }
  177. void AppModel::leaveCar() {
  178. if (m_ready == false) return;
  179. m_ready = false;
  180. m_peer = -1;
  181. _control->OnLeave();
  182. }
  183. bool AppModel::idle(int id)
  184. {
  185. for (auto& node : _users)
  186. {
  187. if (node.uid == id &&((id==m_peer)|| node.state == UserState::Idle))
  188. return true;
  189. }
  190. return false;
  191. }
  192. void AppModel::connectCar(int id) {
  193. for (auto& node : _users)
  194. {
  195. if (node.uid == id&&node.state==UserState::Idle)
  196. {
  197. if (m_ready == true) return;
  198. _control->OnConnect(id);
  199. m_peer = id;
  200. setReady(true);
  201. this->setCarName(QString::fromStdString(node.name));
  202. break;
  203. }
  204. }
  205. }
  206. void AppModel::controlCar(int id)
  207. {
  208. connectCar(id);
  209. }
  210. /*
  211. void AppModel::showTrackDlg()
  212. {
  213. }
  214. void AppModel::unTrack()
  215. {
  216. m_ready = false;
  217. _control->SetReady(false);
  218. _control->OnCarLeave();
  219. }
  220. bool AppModel::autoClose()
  221. {
  222. return _control->IsAutoClose();
  223. }
  224. */
  225. void AppModel::onLoginRst(bool rst) {
  226. qDebug() << "AppModel::loginResult >>";
  227. // if (rst) {
  228. // delete previous carList
  229. // foreach (CarData *inf, d->car)
  230. // delete inf;
  231. // d->car.clear();
  232. // req new list
  233. //_control->ReqCarList();
  234. // } else {
  235. // qDebug() << "AppModel::loginResult >>" << rst;
  236. // }
  237. }
  238. void AppModel::onNotifyDel(int32_t peer) {
  239. for (int32_t i = 0; i < d->car.count(); i++)
  240. {
  241. if (d->car.at(i)->uid() == peer)
  242. {
  243. delete d->car.at(i);
  244. d->car.removeAt(i);
  245. break;
  246. }
  247. }
  248. emit carChanged();
  249. for (auto it = _users.begin(); it != _users.end(); ++it)
  250. {
  251. if ((*it).uid == peer)
  252. {
  253. _users.erase(it);
  254. break;
  255. }
  256. }
  257. // d->car.clear();
  258. }
  259. void AppModel::onCarAppend(const UserDriver&info) {
  260. bool bFind = false;
  261. for (auto& node : d->car)
  262. {
  263. if (node->uid() == info.uid)
  264. {
  265. node->setType(QString::number(info.type));
  266. node->setName(QString::fromStdString(info.name));
  267. node->setState(QString::number(info.state));
  268. bFind = true;
  269. return;
  270. }
  271. }
  272. if (!bFind)
  273. {
  274. CarData* users = new CarData;// = (CarData*)malloc;
  275. users->setUid(info.uid);
  276. users->setType(QString::number(info.type));
  277. users->setName(QString::fromStdString(info.name));
  278. users->setState(QString::number(info.state));
  279. d->car.append(users);
  280. }
  281. bFind = false;
  282. for (auto& node : _users)
  283. {
  284. if (node.uid == info.uid)
  285. {
  286. bFind = true;
  287. }
  288. }
  289. if(!bFind)
  290. _users.push_back(info);
  291. emit carChanged();
  292. }
  293. void AppModel::onNotifyState(int32_t uid, UserState state)
  294. {
  295. for (auto& node : d->car)
  296. {
  297. if (node->uid() == uid)
  298. {
  299. node->setState(QString::number(state));
  300. break;
  301. }
  302. }
  303. emit carChanged();
  304. for (auto& node : _users)
  305. {
  306. if (node.uid == uid)
  307. {
  308. node.state = state;
  309. break;
  310. }
  311. }
  312. }
  313. void AppModel::logout() {
  314. d->car.clear();
  315. m_ready = false;
  316. }
  317. /*
  318. void AppModel::switchDriver()
  319. {
  320. if (m_peer == -1) return;
  321. _control->SwitchDriver(m_peer);
  322. }
  323. */
  324. /*
  325. void AppModel::areaChanged(int32_t area)
  326. {
  327. m_area = static_cast<WorkArea>(area);
  328. }
  329. */
  330. void AppModel::OnClose()
  331. {
  332. if (m_ready)
  333. {
  334. leaveCar();
  335. Sleep(5000);
  336. }
  337. ::ExitProcess(0);
  338. }
  339. /*
  340. void AppModel::OnPosition(float x, float y, float z)
  341. {
  342. setModelX(x * 100);
  343. setModelY(y * 100);
  344. setModelZ(z * 100);
  345. }
  346. */