1 #include "usermanagement.h"
2 #include <QApplication>
4 #include <QtCore/QJsonDocument>
6 UserManagement::UserManagement(QObject *root) : QObject()
8 home = root->findChild<QObject *>("Home");
9 logo = root->findChild<QObject *>("Logo_colour");
10 shortcutArea = root->findChild<QObject *>("ShortcutArea");
11 statusArea = root->findChild<QObject *>("StatusArea");
12 this->appModel = home->findChild<ApplicationModel *>("ApplicationModel");
18 connect(&timerTest, SIGNAL(timeout()), this, SLOT(slot_timerTest()));
19 timerTest.setSingleShot(false);
20 timerTest.start(5000);
24 void UserManagement::setUser(const User &user)
26 appModel->changeLanguage(user.graphPreferredLanguage);
27 QMetaObject::invokeMethod(logo, "setImage", Q_ARG(QVariant, "./images/Utility_Logo_Colour-01.png"));
28 QMetaObject::invokeMethod(home, "languageChanged");
29 QMetaObject::invokeMethod(shortcutArea, "languageChanged", Q_ARG(QVariant, user.graphPreferredLanguage));
30 QMetaObject::invokeMethod(statusArea, "languageChanged", Q_ARG(QVariant, user.graphPreferredLanguage));
31 QMetaObject::invokeMethod(home, "showSign90", Q_ARG(QVariant, !user.graphActions.contains("Exceed 100 Kph")));
32 if(user.ccNumberMasked.isEmpty())
33 QMetaObject::invokeMethod(home, "showVisa", Q_ARG(QVariant, false), Q_ARG(QVariant, ""));
35 QMetaObject::invokeMethod(home, "showVisa", Q_ARG(QVariant, true), Q_ARG(QVariant, user.ccNumberMasked));
36 const QString welcome = QString("%1").arg(user.graphPreferredLanguage == "fr" ? "Bonjour " : "Hello") + " ";
37 QMetaObject::invokeMethod(home, "showHello", Q_ARG(QVariant, welcome + user.first_name));
39 void UserManagement::connectWebsockets()
42 const QUrl url(REAL_SERVER);
44 const QUrl url(QStringLiteral("ws://localhost:1234"));
46 QSslConfiguration config = QSslConfiguration::defaultConfiguration();
47 config.setProtocol(QSsl::SecureProtocols);
48 webSocket.setSslConfiguration(config);
49 connect(&webSocket, &QWebSocket::connected, this, &UserManagement::onConnected);
50 connect(&webSocket, &QWebSocket::disconnected, this, &UserManagement::onClosed);
51 if(!connect(&webSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onWebSocketError(QAbstractSocket::SocketError)))) {
52 qWarning() << "Failed to connect to QWebSocket::error";
54 webSocket.open(QUrl(url));
56 void UserManagement::onWebSocketError(QAbstractSocket::SocketError)
58 qWarning()<<"Websocket error:" << webSocket.errorString();
61 void UserManagement::onConnected()
63 connect(&webSocket, &QWebSocket::textMessageReceived,
64 this, &UserManagement::onTextMessageReceived);
67 list << 2 << QString().setNum(++sequence) << "agl-identity-agent/subscribe" << true;
68 listToJson(list, &json);
69 webSocket.sendTextMessage(QString(json));
71 list << 2 << QString().setNum(++sequence) << "agl-identity-agent/scan" << true;
72 listToJson(list, &json);
73 webSocket.sendTextMessage(QString(json));
75 void UserManagement::onTextMessageReceived(QString message)
78 const bool ok = jsonToList(message.toUtf8(), &list);
79 if(!ok || list.size() < 3) {
80 qWarning()<<"error 1 decoding json"<<list.size()<<message;
83 QVariantMap map = list.at(2).toMap();
84 if(list.first().toInt() == 5) {
85 QMetaObject::invokeMethod(logo, "setImage", Q_ARG(QVariant, "./images/Utility_Logo_Red-01.png"));
86 map = map["data"].toMap();
87 if(map["eventName"].toString() == "login") {
88 //qWarning()<<"login received in client";
90 list << 2 << QString().setNum(++sequence) << "agl-identity-agent/get" << true;
91 listToJson(list, &data);
92 QTimer::singleShot(300, this, SLOT(slot_sendData()));
96 if(list.first().toInt() == 3) {
97 if(!map.contains("response")) {
100 map = map["response"].toMap();
102 user.postal_address = map["postal_address"].toString();
103 QStringList temp = map["loc"].toString().split(",");
104 if(temp.size() == 2) {
105 user.loc.setX(temp.at(0).toDouble());
106 user.loc.setY(temp.at(1).toDouble());
108 user.graphActions = map["graphActions"].toString().split(",");
109 user.country = map["country"].toString();
110 user.mail = map["mail"].toString();
111 user.city = map["city"].toString();
112 user.graphEmail = map["graphEmail"].toString();
113 user.graphPreferredLanguage = map["graphPreferredLanguage"].toString();
114 user.ccNumberMasked = map["ccNumberMasked"].toString();
115 user.ccExpYear = map["ccExpYear"].toString();
116 user.ccExpMonth = map["ccExpMonth"].toString();
117 user.description = map["description"].toString();
118 user.groups = map["groups"].toStringList();
119 user.last_name = map["last_name"].toString();
120 user.ccNumber = map["ccNumber"].toString();
121 user.house_identifier = map["house_identifier"].toString();
122 user.phone = map["phone"].toString();
123 user.name = map["name"].toString();
124 user.state = map["state"].toString();
125 user.common_name = map["common_name"].toString();
126 user.fax = map["fax"].toString();
127 user.postal_code = map["postal_code"].toString();
128 user.first_name = map["first_name"].toString();
129 user.keytoken = map["keytoken"].toString();
133 void UserManagement::slot_sendData()
135 webSocket.sendTextMessage(QString(data));
138 void UserManagement::onClosed()
140 qWarning()<<"webSocket closed";
142 bool UserManagement::listToJson(const QList<QVariant> &list, QByteArray *json) const
145 *json = QJsonDocument::fromVariant(v).toJson(QJsonDocument::Compact);
148 bool UserManagement::jsonToList(const QByteArray &buf, QList<QVariant> *list) const
153 QVariant v = QJsonDocument::fromJson(buf, &err).toVariant();
155 qWarning() << "Error parsing json data" << err.errorString() << buf;
156 *list = QList<QVariant>();
162 bool UserManagement::mapToJson( const QVariantMap &map, QByteArray *json) const
167 *json = QJsonDocument::fromVariant(v).toJson(QJsonDocument::Compact);
170 bool UserManagement::jsonToMap(const QByteArray &buf, QVariantMap *map) const
175 QVariant v = QJsonDocument::fromJson(buf, &err).toVariant();
177 qWarning() << "Error parsing json data" << err.errorString() << buf;
178 *map = QVariantMap();
185 void UserManagement::launchServer()
187 webSocketServer = new QWebSocketServer(QStringLiteral("My Server"),
188 QWebSocketServer::NonSecureMode, this);
189 if(webSocketServer->listen(QHostAddress::Any, 1234)) {
190 connect(webSocketServer, &QWebSocketServer::newConnection,
191 this, &UserManagement::onServerNewConnection);
192 connect(webSocketServer, &QWebSocketServer::closed, this, &UserManagement::onServerClosed);
193 QTimer::singleShot(100, this, SLOT(connectWebsockets()));
195 qWarning()<<"unable to launch webSocket server";
198 void UserManagement::onServerNewConnection()
200 pSocket = webSocketServer->nextPendingConnection();
201 connect(pSocket, &QWebSocket::textMessageReceived, this, &UserManagement::processTextMessage, Qt::UniqueConnection);
202 connect(pSocket, &QWebSocket::binaryMessageReceived, this, &UserManagement::processBinaryMessage, Qt::UniqueConnection);
203 connect(pSocket, &QWebSocket::disconnected, this, &UserManagement::serverSocketDisconnected, Qt::UniqueConnection);
205 void UserManagement::processTextMessage(QString message)
207 QString clientDetails_1 = "{\"postal_address\":\"201 Mission Street\",\"loc\":\"37.7914374,-122.3950694\",\"graphActions\":\"Install App,Update Software,Exceed 100 Kph,Open Trunk,View Online\""
208 ",\"country\":\"USA\",\"mail\":\"bjensen@example.com\",\"city\":\"San Francisco\",\"graphEmail\":"
209 "\"bjensen@example.com\",\"graphPreferredLanguage\":\"en\",\"ccNumberMasked\":\"-111\",\"ccExpYear\""
210 ":\"19\",\"ccExpMonth\":\"01\",\"description\":\"Original description\",\"groups\":[],\"last_name\":\""
211 "Jensen\",\"ccNumber\":\"111-2343-1121-111\",\"house_identifier\":\"ForgeRock\",\"phone\":\""
212 "+1 408 555 1862\",\"name\":\"bjensen\",\"state\":\"CA\",\"common_name\":\"Barbara Jensen\",\"fax\":\""
213 "+1 408 555 1862\",\"postal_code\":\"94105\",\"first_name\":\"Barbara\",\"keytoken\":\"a123456\"}";
214 QString clientDetails_2 = "{\"postal_address\":\"201 Mission Street\",\"loc\":\"37.7914374,-122.3950694\""
215 ",\"country\":\"USA\",\"mail\":\"bjensen@example.com\",\"city\":\"San Francisco\",\"graphEmail\":"
216 "\"bjensen@example.com\",\"graphPreferredLanguage\":\"fr\",\"ccNumberMasked\":\"-222\",\"ccExpYear\""
217 ":\"19\",\"ccExpMonth\":\"01\",\"description\":\"Original description\",\"groups\":[],\"last_name\":\""
218 "Jensen\",\"ccNumber\":\"111-2343-1121-111\",\"house_identifier\":\"ForgeRock\",\"phone\":\""
219 "+1 408 555 1862\",\"name\":\"bjensen\",\"state\":\"CA\",\"common_name\":\"Barbara Jensen\",\"fax\":\""
220 "+1 408 555 1862\",\"postal_code\":\"94105\",\"first_name\":\"José\",\"keytoken\":\"a123456\"}";
221 QString clientDetails = clientDetails_1;
222 if(sequence % 2 == 1)
223 clientDetails = clientDetails_2;
224 QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
225 //qDebug() << "message received in server:" << message;
229 if(!jsonToList(message.toUtf8(), &list))
233 const int messType = list.at(0).toInt();
234 const QString messId = list.at(1).toString();
235 const QString cmd = list.at(2).toString();
240 if(cmd == "agl-identity-agent/subscribe") {
241 reply = "[3,\"999maitai999\",{\"jtype\":\"afb-reply\",\"request\":{\"status\":\"success\",\"uuid\":\"1f2f7678-6f2e-4f54-b7b5-d0d4dcbf2e41\"}}]";
242 } else if (cmd == "agl-identity-agent/get") {
243 reply = "[3,\"999maitai99\",{\"jtype\":\"afb-reply\",\"request\":{\"status\":\"success\"},\"response\":....}]";
244 reply = reply.replace("....", clientDetails);
246 qWarning()<<"invalid cmd received:"<<cmd;
251 qWarning()<<"invalid message type"<<messType;
255 reply = reply.replace("999maitai999", messId);
256 pClient->sendTextMessage(reply);
258 void UserManagement::processBinaryMessage(QByteArray message)
260 QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
261 qDebug() << "Binary Message received ????:" << message;
263 // pClient->sendBinaryMessage(message);
266 void UserManagement::serverSocketDisconnected()
268 QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
269 qDebug() << "socketDisconnected:" << pClient;
271 pClient->deleteLater();
274 void UserManagement::slot_timerTest()
278 pSocket->sendTextMessage("[5,\"agl-identity-agent/event\",{\"event\":\"agl-identity-agent\/event\",\"data\":{\"eventName\":\"incoming\",\"accountid\":\"D2:D4:71:0D:B5:F1\",\"nickname\":\"D2:D4:71:0D:B5:F1\"},\"jtype\":\"afb-event\"}]");
279 pSocket->sendTextMessage("[5,\"agl-identity-agent/event\",{\"event\":\"agl-identity-agent\/event\",\"data\":{\"eventName\":\"login\",\"accountid\":\"null\"},\"jtype\":\"afb-event\"}]");
281 void UserManagement::onServerClosed()
283 qWarning()<<"websocket server closed";