/** * @RDE_HEADER@ * The ClientMessage class is an abstract base class for concrete ClientMessage objects * $Id: client_message.cpp 58958 2023-02-13 11:26:20Z fritsche_h $ * $HeadURL: svn://slssvn/TDS/branches/product/stable/guez2/Lts/Services/WarehouseGuez/WsBiteInterface/Messages/client_message.cpp $ * $Author: fritsche_h $ * Created by Hannes Fritsche, SLSE, 2017-09-07 */ #include "ws_client_message.h" #include "ws_reply_message.h" #include "WsBiteInterface/bite_data_manager.h" #include "WsBiteInterface/Messages/message_transmitter.h" #include "WsBiteInterface/json_keys.h" #include #include #include /* *python testing: import websocket import json import time ws = websocket.create_connection("ws://127.0.0.1:2081") d = { 'type' : 'getEssentials', 'uuid': 'blub' } ws.send(json.dumps(d)) d = { 'type' : 'getConfigCNR', 'uuid': 'blub' } ws.send(json.dumps(d)) d = { 'type' : 'getPositionsCNR', 'uuid': 'blub', 'data': {'repeat': 10}} ws.send(json.dumps(d)) while True: ws.recv() */ namespace WarehouseGuez { namespace WsBiteInterface { using namespace JsonKeys; namespace Messages { // ####### ClientMessage begin ####### ClientMessage *ClientMessage::create(const QString &message, NGL::INGLLogger &logger) { ClientMessage *result = nullptr; QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8(), &error); if (error.error != QJsonParseError::NoError) { ERROR_LOG(logger) << "Got message which is not JSON conform: " << message; return new InvalidMessage(QString(), QStringLiteral("Got message which is not JSON conform")); } QJsonObject obj = doc.object(); QJsonObject dataPart = obj.value(DATA()).toObject(); QString type = obj.value(TYPE()).toString(); QString uuid = obj.value(UUID_()).toString(); if (type.isEmpty()) { ERROR_LOG(logger) << "Got message without type: " << message; result = new InvalidMessage(uuid, QStringLiteral("Message has no type")); } else if (type == ERROR_() && dataPart.contains(ERROR_MESSAGE()) && dataPart.contains(ERROR_CLASS()) && dataPart.contains(UUID_())) { ERROR_LOG(logger) << "Got error message: " << QJsonDocument(dataPart).toJson(); return nullptr; } else if (uuid.isEmpty()) { ERROR_LOG(logger) << "Got message without uuid: " << message; result = new InvalidMessage(QString(), QStringLiteral("Got message without uuid")); } else if (type == GET_BITE_DATA()) result = new GetBiteDataMessage(type, uuid, dataPart); if (result == nullptr) { ERROR_LOG(logger) << "Got message with invalid type: " << message; result = new InvalidMessage(uuid, QStringLiteral("Invalid type")); } if (!result->hasValidData()) { ERROR_LOG(logger) << "Got message with invalid data part: " << message; delete result; result = new InvalidMessage(uuid, QStringLiteral("Has invalid data part")); } return result; } ClientMessage::ClientMessage(const QString &type, const QString &uuid, const QJsonObject &data) : m_type(type) , m_uuid(uuid) , m_data(data) { } // ####### ClientMessage end ####### InvalidMessage::InvalidMessage(const QString &uuid, const QString &errorText) : ClientMessage(QString(), uuid) , m_errorText(errorText) {} // ####### InvalidMessage begin ####### void InvalidMessage::process(DataManager *dataManager, MessageTransmitter *transmitter) { Q_UNUSED(dataManager); ErrorMessage::ErrorData errorData; errorData.errorClass = 1; // ?????????? errorData.errorMessage = m_errorText; errorData.uuid = m_uuid; transmitter->addMessage(ErrorMessage(errorData)); } bool InvalidMessage::hasValidData() const { // always true because there is no data part return true; } // ####### InvalidMessage end ####### // ####### GetBiteDataMessage begin ####### void GetBiteDataMessage::process(DataManager *dataManager, MessageTransmitter *transmitter) { using namespace Messages; int equipNo = m_data.value(EQUIP_NO()).toInt(); int boxId = m_data.value(BOX()).toInt(); dataManager->setEquipNoBoxId(equipNo, boxId, transmitter, m_uuid); OkMessage::OkData okMsg; okMsg.messageType = GET_BITE_DATA(); okMsg.uuid = m_uuid; okMsg.info = QStringLiteral("Received"); OkMessage msg(okMsg); transmitter->addMessage(msg); } // ####### GetBiteDataMessage end ####### } // namespace Messages } // namespace WsBiteInterface } // namespace WarehouseGuez