287 lines
8.6 KiB
C++
Executable File
287 lines
8.6 KiB
C++
Executable File
/**
|
|
* Exercises Handler - checks the avilable exercises and creates subhandlers fort each of them
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by Christian Ehrlicher, SLSE, 2016-04-14
|
|
*/
|
|
|
|
#include "exercises_handler.h"
|
|
#include "exercise_participant_handler.h"
|
|
|
|
namespace WarehouseManagement
|
|
{
|
|
|
|
ExercisesHandler::ExercisesHandler(const std::shared_ptr<DdsHandler> &ddsHandler)
|
|
: m_ddsHandler(ddsHandler)
|
|
, m_logger(getLogger("ExercisesHandler"))
|
|
{}
|
|
|
|
ExercisesHandler::~ExercisesHandler()
|
|
{
|
|
qDeleteAll(m_exerciseHandler);
|
|
}
|
|
|
|
bool ExercisesHandler::init()
|
|
{
|
|
connect(m_ddsHandler->exmDtc().get(), &ExerciseManagement::Interface::Dtc::exerciseChanged,
|
|
this, &ExercisesHandler::onExerciseChanged);
|
|
|
|
auto cb = [this](const ExerciseManagement::Exercise &exercise)
|
|
{
|
|
exerciseAdded(exercise.id());
|
|
};
|
|
m_ddsHandler->exmDtc()->exercises(cb);
|
|
|
|
return true;
|
|
}
|
|
|
|
QMap<qint32, ExerciseManagement::Exercise> ExercisesHandler::getExercises() const
|
|
{
|
|
INFO_LOG(m_logger) << "IN getExercises";
|
|
m_ddsHandler->exmDtc()->waitForHistoricalDataExercise();
|
|
return m_ddsHandler->exmDtc()->exercises();
|
|
}
|
|
|
|
ParticipantManagement::Type ExercisesHandler::participantType(int typeId) const
|
|
{
|
|
m_ddsHandler->ppmDtc()->waitForHistoricalDataType();
|
|
return m_ddsHandler->ppmDtc()->type(typeId);
|
|
}
|
|
|
|
ParticipantManagement::TypeClass ExercisesHandler::typeClassIdFromEquipmentTypeId(int equipTypeId) const
|
|
{
|
|
m_ddsHandler->ppmDtc()->waitForHistoricalDataType();
|
|
ParticipantManagement::TypeClass ret = ParticipantManagement::TypeClass::Unknown;
|
|
std::function<bool(const ParticipantManagement::Type &data)> cb =
|
|
[equipTypeId, &ret](const ParticipantManagement::Type &t) -> bool
|
|
{
|
|
if (t.equipmentTypeId() != equipTypeId)
|
|
return false;
|
|
ret = t.typeClass();
|
|
return true;
|
|
};
|
|
m_ddsHandler->ppmDtc()->types(cb);
|
|
return ret;
|
|
}
|
|
|
|
|
|
ParticipantManagement::Party ExercisesHandler::getParty(int partyId, int exerciseId) const
|
|
{
|
|
DEBUG_LOG(m_logger) << "getParty: " << partyId << ", exId: " << exerciseId;
|
|
auto it = m_exerciseHandler.find(exerciseId);
|
|
if (it == m_exerciseHandler.end())
|
|
return ParticipantManagement::Party();
|
|
|
|
return it.value()->getParty(partyId);
|
|
}
|
|
|
|
QString ExercisesHandler::getHierarchyFromParticipant(int participantId, int exerciseId, const QMap<int, ParticipantManagement::Participant> &allParticipants) const
|
|
{
|
|
auto it = m_exerciseHandler.find(exerciseId);
|
|
if (it == m_exerciseHandler.end())
|
|
return QString();
|
|
|
|
return it.value()->getHierarchyFromParticipant(participantId, allParticipants);
|
|
}
|
|
ParticipantManagement::Participant ExercisesHandler::getParticipant(int participantId, int exerciseId) const
|
|
{
|
|
auto it = m_exerciseHandler.find(exerciseId);
|
|
if (it == m_exerciseHandler.end())
|
|
return ParticipantManagement::Participant();
|
|
|
|
return it.value()->getParticipant(participantId);
|
|
}
|
|
|
|
QMap<qint32, ParticipantManagement::Participant> ExercisesHandler::getParticipants(int exerciseId) const
|
|
{
|
|
auto it = m_exerciseHandler.find(exerciseId);
|
|
if (it == m_exerciseHandler.end())
|
|
return QMap<qint32, ParticipantManagement::Participant>();
|
|
|
|
return it.value()->getParticipants();
|
|
}
|
|
|
|
bool ExercisesHandler::registerParticipant(int exerciseId, qint32 participantId,
|
|
ParticipantManagement::ParticipantAssignState assignState,
|
|
const dds::core::optional<int> &equipNo,
|
|
const dds::core::optional<QString> &identification,
|
|
const dds::core::optional<QString> &info)
|
|
{
|
|
auto it = m_exerciseHandler.find(exerciseId);
|
|
if (it == m_exerciseHandler.end())
|
|
return false;
|
|
it.value()->registerParticipant(participantId, assignState, equipNo, identification, info);
|
|
return true;
|
|
}
|
|
|
|
bool ExercisesHandler::unregisterParticipant(int exerciseId, qint32 equipNo)
|
|
{
|
|
if (!m_exerciseHandler.contains(exerciseId))
|
|
return false;
|
|
|
|
if (!m_participants.contains(equipNo))
|
|
return false;
|
|
|
|
const QList<ParticipantInfoStruct> listPis = m_participants.values(equipNo);
|
|
|
|
qint32 participantId = -1;
|
|
for(ParticipantInfoStruct pis : listPis)
|
|
{
|
|
if (pis.exerciseId != exerciseId)
|
|
continue;
|
|
|
|
participantId = pis.participantId;
|
|
}
|
|
|
|
if (participantId == -1)
|
|
return false;
|
|
|
|
// NotAssigned with equipNo 0
|
|
ParticipantManagement::ParticipantAssignState assignState = ParticipantManagement::ParticipantAssignState_def::NotAssigned;
|
|
|
|
m_exerciseHandler.value(exerciseId)->registerParticipant(participantId, assignState, 0, {}, {});
|
|
return true;
|
|
}
|
|
|
|
bool ExercisesHandler::modifyEquipmentTypes(const std::vector<ParticipantManagement::EquipmentType> &vectorEquipTypes)
|
|
{
|
|
ParticipantManagement::ModifyEquipmentTypeDataRequest req;
|
|
|
|
req.data(vectorEquipTypes);
|
|
req.deleteAll(true);
|
|
return m_ddsHandler->ppmFtc()->modifyEquipmentTypeDataRequest(req, this);
|
|
}
|
|
|
|
void ExercisesHandler::onModifyEquipmentTypeDataReply(const Core::FunctionReplyObject &replyObject)
|
|
{
|
|
if (replyObject.error())
|
|
ERROR_LOG(m_logger) << replyObject();
|
|
}
|
|
|
|
bool ExercisesHandler::addParticipant(const ParticipantInfoStruct &pis)
|
|
{
|
|
m_participants.insert(pis.equipmentNumber, pis);
|
|
return true;
|
|
}
|
|
|
|
bool ExercisesHandler::modifyParticipant(const ParticipantInfoStruct &pis)
|
|
{
|
|
auto it = findParticipant(pis);
|
|
if (it == m_participants.end())
|
|
{
|
|
ERROR_LOG(m_logger) << "Trying to modify a participant which is not in our map. mod: "
|
|
<< pis;
|
|
return false;
|
|
}
|
|
it.value() = pis;
|
|
return true;
|
|
}
|
|
|
|
bool ExercisesHandler::removeParticipant(const ParticipantInfoStruct &pis)
|
|
{
|
|
auto it = findParticipant(pis);
|
|
if (it == m_participants.end())
|
|
{
|
|
ERROR_LOG(m_logger) << "Trying to remove a participant which is not in our map. mod: "
|
|
<< pis;
|
|
return false;
|
|
}
|
|
m_participants.erase(it);
|
|
return true;
|
|
}
|
|
|
|
QVector<ParticipantInfoStruct> ExercisesHandler::infoStructForEquipment(int equipmentNumber) const
|
|
{
|
|
QVector<ParticipantInfoStruct> ret;
|
|
|
|
auto it = m_participants.find(equipmentNumber);
|
|
if (it == m_participants.end()) {
|
|
// TODO: create temporary participant entry for ex_0 etc.
|
|
}
|
|
while (it == m_participants.end() && it.value().equipmentNumber == equipmentNumber) {
|
|
ret += it.value();
|
|
++it;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
void ExercisesHandler::exerciseAdded(int exerciseId)
|
|
{
|
|
INFO_LOG(m_logger) << "Exercise with id " << exerciseId << " created";
|
|
auto it = m_exerciseHandler.find(exerciseId);
|
|
if (it != m_exerciseHandler.end())
|
|
{
|
|
WARNING_LOG(m_logger) << "Exercise with id " << exerciseId
|
|
<< " added but it was already in our list - recreating handler";
|
|
delete it.value();
|
|
m_exerciseHandler.erase(it);
|
|
}
|
|
ExerciseParticipantHandler *exH = new ExerciseParticipantHandler(m_ddsHandler->getDomainParticipant(), exerciseId, this);
|
|
if (!exH->init())
|
|
{
|
|
ERROR_LOG(m_logger) << "Error initializing ExerciseHandler for exercise " << exerciseId;
|
|
delete exH;
|
|
return;
|
|
}
|
|
connect(exH, &ExerciseParticipantHandler::registrationReturnValue,
|
|
this, &ExercisesHandler::registrationReturnValue);
|
|
connect(exH, &ExerciseParticipantHandler::participantChanged,
|
|
this, &ExercisesHandler::participantChanged);
|
|
INFO_LOG(m_logger) << "Successfully created exercise handler for exercise " << exerciseId;
|
|
m_exerciseHandler[exerciseId] = exH;
|
|
}
|
|
|
|
void ExercisesHandler::exerciseRemoved(int exerciseId)
|
|
{
|
|
INFO_LOG(m_logger) << "Exercise with id " << exerciseId << " removed";
|
|
auto it = m_exerciseHandler.find(exerciseId);
|
|
if (it == m_exerciseHandler.end())
|
|
{
|
|
WARNING_LOG(m_logger) << "Exercise removed but it was not found in our internal map";
|
|
return;
|
|
}
|
|
delete it.value();
|
|
m_exerciseHandler.erase(it);
|
|
}
|
|
|
|
QMultiHash<int, ParticipantInfoStruct>::iterator ExercisesHandler::findParticipant(const ParticipantInfoStruct &pis)
|
|
{
|
|
auto it = m_participants.find(pis.equipmentNumber);
|
|
while (it != m_participants.end() && it.value().equipmentNumber == pis.equipmentNumber)
|
|
{
|
|
if (it.value().exerciseId == pis.exerciseId)
|
|
return it;
|
|
++it;
|
|
}
|
|
return it;
|
|
}
|
|
|
|
void ExercisesHandler::onExerciseChanged(const ExerciseManagement::Exercise &data, DataModification modType)
|
|
{
|
|
if (!data.sendToWMS())
|
|
{
|
|
if (m_exerciseHandler.contains(data.id()))
|
|
exerciseRemoved(data.id());
|
|
|
|
emit exerciseChanged(data, DataModification::eRemoved);
|
|
}
|
|
else
|
|
{
|
|
if (!m_exerciseHandler.contains(data.id()))
|
|
{
|
|
modType = DataModification::eAdded;
|
|
exerciseAdded(data.id());
|
|
}
|
|
else
|
|
{
|
|
modType = DataModification::eModified;
|
|
}
|
|
emit exerciseChanged(data, modType);
|
|
}
|
|
}
|
|
|
|
|
|
} // namespace WarehouseManagement
|