/** * @RDE_HEADER@ * The ClientMessage class is an abstract base class for concrete ClientMessage objects * $Id$ * $HeadURL$ * $Author$ * Created by Hannes Baeuerle, SLSE, 2016-08-25 */ #include "client_message.h" #include "WebSocketInterface/Messages/reply_message.h" #include "WebSocketInterface/json_keys.h" #include "WebSocketInterface/data_manager.h" #include #include #include namespace InfrastructureManagement { namespace WebSocketInterface { namespace Messages { using namespace JsonKeys; template static QJsonArray fillArray(const T &messages) { QJsonArray arr; for (const auto ¤tMsg : messages) arr += currentMsg.json().value(LtsWebSocket::JsonKeys::DATA()); return arr; } // ####### ClientMessageFactory begin ####### ClientMessageFactory::ClientMessageFactory(DataManager *dataManager) : LtsWebSocket::AbstractClientMessageFactory(getLogger("ClientMessageFactory")) , m_dataManager(dataManager) {} LtsWebSocket::AbstractClientMessage *ClientMessageFactory::createMessage(LtsWebSocket::ReplyHandler *client, const QString &message) { LtsWebSocket::AbstractClientMessage *result = checkAttributes(client, message); if (result) return result; if (m_type == OcBaseDownload::WebSocketInterface::JsonKeys::BASE_DL_REQUEST()) result = new BaseDlRequest(m_dataManager, client, m_type, m_typeId, m_uuid); else if (m_type == GET_ASSETS()) result = new GetAssetsMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); else if (m_type == GET_ASSET_STATE()) result = new GetAssetStateMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); else if (m_type == GET_BUILDING_STATE()) result = new GetBuildingStateMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); else if (m_type == GET_ESSENTIALS()) result = new GetEssentialsMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); else if (m_type == GET_OBJECT_MOUNTING()) result = new GetObjectMountingMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); else if (m_type == GET_PLANNED_MOUNTINGS()) result = new GetPlannedMountingsMessage(m_dataManager, client, m_type, m_typeId, m_uuid); else if (m_type == MOUNT()) result = new MountMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); else if (m_type == MOUNT_TAG()) result = new MountTagMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); else if (m_type == OBJECT_MOUNTING_READY()) result = new ObjectMountingReadyMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); else if (m_type == UNMOUNT_AREA()) result = new UnmountAreaMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); else if (m_type == UNMOUNT_ASSET()) result = new UnmountAssetMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); else if (m_type == UNMOUNT_MOUNTING_POINT()) result = new UnmountMountingPointMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); else if (m_type == UNMOUNT_OBJECT()) result = new UnmountObjectMessage(m_dataManager, client, m_type, m_typeId, m_uuid, m_data); return checkResult(client, message, result); } // ####### ClientMessageFactory end ####### // ####### ClientMessage begin ####### ClientMessage::ClientMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : LtsWebSocket::AbstractClientMessage(client, type, typeId, uuid, data) , m_dataManager(dataManager) {} // ####### ClientMessage end ####### // ####### GetAssetStateMessage begin ####### void GetAssetStateMessage::process() { m_client->addReply(m_dataManager->getAssetState(m_data.toObject().value(ASSET_ID()).toInt(), m_data.toObject().value(ASSET_TAG()).toString(), m_uuid)); } bool GetAssetStateMessage::hasValidData() const { // one (exclusive) value has to be set return ((!m_data.toObject().value(ASSET_ID()).isDouble() && m_data.toObject().value(ASSET_TAG()).isString()) || (m_data.toObject().value(ASSET_ID()).isDouble() && !m_data.toObject().value(ASSET_TAG()).isString())); } GetAssetStateMessage::GetAssetStateMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### GetAssetStateMessage end ####### // ####### GetAssetStateMessage begin ####### void GetAssetsMessage::process() { const QJsonValue val = m_data.toObject().value(ASSET_TYPE_ID()); QSet assetTypeIds; if (val.isArray()) { const QJsonArray ary = val.toArray(); assetTypeIds.reserve(ary.size()); for (const auto &ele : ary) assetTypeIds += ele.toInt(); } else assetTypeIds += val.toInt(); m_client->addReply(m_dataManager->getAssets(assetTypeIds, m_uuid)); } bool GetAssetsMessage::hasValidData() const { const auto val = m_data.toObject().value(ASSET_TYPE_ID()); return val.isDouble() || val.isArray(); } GetAssetsMessage::GetAssetsMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### GetAssetStateMessage end ####### // ####### GetBuildingStateMessage begin ####### void GetBuildingStateMessage::process() { m_client->addReply(m_dataManager->getBuildingState(m_data.toObject().value(BUILDING_ID()).toInt(), m_uuid)); } bool GetBuildingStateMessage::hasValidData() const { return m_data.toObject().value(BUILDING_ID()).isDouble(); } GetBuildingStateMessage::GetBuildingStateMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### GetBuildingStateMessage end ####### class GetEssentialsReply final : public ReplyMessage { public: GetEssentialsReply(const QJsonObject &obj, const QString &uuid) : ReplyMessage(QStringLiteral("essentials"), 2, uuid) // Todo: Check whether typeId is correct { m_jsonRepresentation.insert(LtsWebSocket::JsonKeys::DATA(), obj); } }; // ####### GetEssentialsMessage begin ####### void GetEssentialsMessage::process() { const DataManager::EinruestEssentials data = m_dataManager->getEssentials(m_uuid); QJsonObject obj; obj[TRAINING_AREAS()] = fillArray(data.trainingAreas); obj[AREAS()] = fillArray(data.areas); obj[OBJECTS()] = fillArray(data.objects); obj[LEVELS()] = fillArray(data.levels); obj[ROOMS()] = fillArray(data.rooms); obj[MOUNTING_POINTS()] = fillArray(data.mountingPoints); obj[ASSET_TYPES()] = fillArray(data.assetTypes); m_client->addReply(new GetEssentialsReply(obj, m_uuid)); } bool GetEssentialsMessage::hasValidData() const { return true; // no data part to validate } GetEssentialsMessage::GetEssentialsMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### GetEssentialsMessage end ####### // ####### GetPlannedMountingMessage begin ####### class PlannedMountingsReply final : public ReplyMessage { public: PlannedMountingsReply(const QJsonArray &arr, const QString &uuid) : ReplyMessage(QStringLiteral("plannedMountings"), 13, uuid) // Todo: Check whether typeId is correct { m_jsonRepresentation.insert(LtsWebSocket::JsonKeys::DATA(), arr); } }; void GetPlannedMountingsMessage::process() { const QVector plannedMountings = m_dataManager->getPlannedMounting(m_uuid); m_client->addReply(new PlannedMountingsReply(fillArray(plannedMountings), m_uuid)); } bool GetPlannedMountingsMessage::hasValidData() const { return true; // no data part to validate } GetPlannedMountingsMessage::GetPlannedMountingsMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid) : ClientMessage(dataManager, client, type, typeId, uuid) { } // ####### GetPlannedMountingMessage end ####### // ####### GetObjectMountingMessage begin ####### void GetObjectMountingMessage::process() { m_client->addReply(m_dataManager->getObjectMounting(m_data.toObject().value(PLANNED_MOUNTING_ID()).toInt(), m_data.toObject().value(OBJECT_ID()).toInt(), m_uuid)); } bool GetObjectMountingMessage::hasValidData() const { return m_data.toObject().value(PLANNED_MOUNTING_ID()).isDouble() && m_data.toObject().value(OBJECT_ID()).isDouble(); } GetObjectMountingMessage::GetObjectMountingMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### GetObjectMountingMessage end ####### // ####### MountMessage begin ####### void MountMessage::process() { m_client->addReply(m_dataManager->mount(m_data.toObject().value(MOUNTING_POINT_ID()).toInt(), m_data.toObject().value(ASSET_ID()).toInt(), m_type, m_uuid)); } bool MountMessage::hasValidData() const { return m_data.toObject().value(MOUNTING_POINT_ID()).isDouble() && m_data.toObject().value(ASSET_ID()).isDouble(); } MountMessage::MountMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### MountMessage end ####### // ####### MountTagMessage begin ####### void MountTagMessage::process() { m_client->addReply(m_dataManager->mountTag(m_data.toObject().value(MOUNTING_POINT_TAG()).toString(), m_data.toObject().value(ASSET_TAG()).toString(), m_type, m_uuid)); } bool MountTagMessage::hasValidData() const { // at least one return m_data.toObject().value(ASSET_TAG()).isString() || m_data.toObject().value(MOUNTING_POINT_TAG()).isString(); } MountTagMessage::MountTagMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### MountTagMessage end ####### // ####### ObjectMountingReadyMessage begin ####### void ObjectMountingReadyMessage::process() { m_client->addReply(m_dataManager->objectMountingReady(m_data.toObject().value(PLANNED_MOUNTING_ID()).toInt(), m_data.toObject().value(OBJECT_ID()).toInt(), m_type, m_uuid)); } bool ObjectMountingReadyMessage::hasValidData() const { return m_data.toObject().value(PLANNED_MOUNTING_ID()).isDouble() && m_data.toObject().value(OBJECT_ID()).isDouble(); } ObjectMountingReadyMessage::ObjectMountingReadyMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### ObjectMountingReadyMessage end ####### // ####### UnmountAreaMessage begin ####### void UnmountAreaMessage::process() { m_client->addReply(m_dataManager->unmountArea(m_data.toObject().value(AREA_ID()).toInt(), m_type, m_uuid)); } bool UnmountAreaMessage::hasValidData() const { return m_data.toObject().value(AREA_ID()).isDouble(); } UnmountAreaMessage::UnmountAreaMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### UnmountAreaMessage end ####### // ####### UnmountAssetMessage begin ####### void UnmountAssetMessage::process() { m_client->addReply(m_dataManager->unmountAsset(m_data.toObject().value(ASSET_ID()).toInt(), m_type, m_uuid)); } bool UnmountAssetMessage::hasValidData() const { return m_data.toObject().value(ASSET_ID()).isDouble(); } UnmountAssetMessage::UnmountAssetMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### UnmountAssetMessage end ####### // ####### UnmountMountingPointMessage begin ####### void UnmountMountingPointMessage::process() { m_client->addReply(m_dataManager->unmountMountingPoint(m_data.toObject().value(MOUNTING_POINT_ID()).toInt(), m_type, m_uuid)); } bool UnmountMountingPointMessage::hasValidData() const { return m_data.toObject().value(MOUNTING_POINT_ID()).isDouble(); } UnmountMountingPointMessage::UnmountMountingPointMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### UnmountMountingPointMessage end ####### // ####### UnmountObjectMessage begin ####### void UnmountObjectMessage::process() { m_client->addReply(m_dataManager->unmountObject(m_data.toObject().value(OBJECT_ID()).toInt(), m_type, m_uuid)); } bool UnmountObjectMessage::hasValidData() const { return m_data.toObject().value(OBJECT_ID()).isDouble(); } UnmountObjectMessage::UnmountObjectMessage(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid, const QJsonValue &data) : ClientMessage(dataManager, client, type, typeId, uuid, data) { } // ####### UnmountObjectMessage end ####### // ####### BaseDlRequest begin ####### BaseDlRequest::BaseDlRequest(DataManager *dataManager, LtsWebSocket::ReplyHandler *client, const QString &type, const int typeId, const QString &uuid) : ClientMessage(dataManager, client, type, typeId, uuid) {} void BaseDlRequest::process() { m_client->addReply(m_dataManager->assetBaseData(m_uuid)); m_client->addReply(m_dataManager->mainStructureElementBaseData(m_uuid)); m_client->addReply(m_dataManager->structureElementBaseData(m_uuid)); m_client->addReply(m_dataManager->structureElementFootprintBaseData(m_uuid)); m_client->addReply(m_dataManager->mountingPointAssetBaseData(m_uuid)); m_client->addReply(m_dataManager->mountingPointScheduleBaseData(m_uuid)); m_client->addReply(m_dataManager->mountingPointPlanBaseData(m_uuid)); } bool BaseDlRequest::hasValidData() const { return true; } // ####### BaseDlRequest end ####### } // namespace Messages } // namespace WebSocketInterface } // namespace Infrastrukture Management