324 lines
13 KiB
C++
Executable File
324 lines
13 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* ISM Handler Instrumentation Assets
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by Iuliana Ojog, SLSE, 2016-11-26
|
|
*/
|
|
|
|
#include "handler_instrumentation_asset.h"
|
|
#include "handler_mounting_point_asset.h"
|
|
#include "object_instrumentation_asset.h"
|
|
|
|
#include "main_handler.h"
|
|
|
|
using namespace InfrastructureManagement;
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
HandlerInstrumentationAsset::HandlerInstrumentationAsset(MainHandler *mainHandler)
|
|
: Base(mainHandler, getLogger("HandlerInstrumentationAsset"))
|
|
, Interface::Fts::InstrumentationAssetModifyRequestHandler(mainHandler->dds()->fts())
|
|
, Interface::Fts::InstrumentationAssetRemoveRequestHandler(mainHandler->dds()->fts())
|
|
, Interface::Fts::InstrumentationAssetSetStateRequestHandler(mainHandler->dds()->fts())
|
|
, Interface::Fts::InstrumentationAssetUnmountRequestHandler(mainHandler->dds()->fts())
|
|
{
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
HandlerInstrumentationAsset::~HandlerInstrumentationAsset()
|
|
{
|
|
qDeleteAll(m_ObjectInstrumentationAssetMap);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
bool HandlerInstrumentationAsset::init()
|
|
{
|
|
bool bRet = true;
|
|
auto cb = [this, &bRet] (const Database::IsmAsset &data)
|
|
{
|
|
InstrumentationAsset ddsData = ObjectInstrumentationAsset::toDdsData(data);
|
|
ObjectInstrumentationAsset *ifs(new ObjectInstrumentationAsset(m_mainHandler));
|
|
Core::FunctionReplyObject ret = ifs->createObject(ddsData);
|
|
if (ret.error())
|
|
{
|
|
delete ifs;
|
|
ERROR_LOG(m_logger) << ret();
|
|
bRet = false;
|
|
return;
|
|
}
|
|
m_ObjectInstrumentationAssetMap.insert(ifs->getId(), ifs);
|
|
DEBUG_LOG(m_logger) << "asset id=" << ifs->getId()
|
|
<< " id=" << ifs->getData().id
|
|
<< " mountingPointTypeId=" << ifs->getData().mountingPointTypeId
|
|
<< " name=" << ifs->getData().name;
|
|
};
|
|
|
|
// create Asset objects
|
|
if (!db()->select(cb) || !bRet)
|
|
{
|
|
ERROR_LOG(m_logger) << "Error reading data from database!";
|
|
return false;
|
|
}
|
|
|
|
DEBUG_LOG(m_logger) << "m_ObjectInstrumentationAssetMap.size() = " << m_ObjectInstrumentationAssetMap.size();
|
|
|
|
return true;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
ObjectInstrumentationAsset *HandlerInstrumentationAsset::createInstrumentationAssetForMse(int buildingId, int structureElementId)
|
|
{
|
|
int assetId = 0;
|
|
InstrumentationAssetModifyRequest request;
|
|
request.name = QStringLiteral("Building_%1_SE_%2").arg(buildingId).arg(structureElementId).toStdString();
|
|
request.mountingPointTypeId = InfrastructureManagement::MountingPointType::BuildingControlUnit;
|
|
instrumentationAssetCreate(request, &assetId);
|
|
return m_ObjectInstrumentationAssetMap.value(assetId);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
bool HandlerInstrumentationAsset::removeInstrumentationAssetForMse(int assetId)
|
|
{
|
|
Core::RemoveElementRequest request;
|
|
request.id = assetId;
|
|
const Core::FunctionReplyObject ret = onInstrumentationAssetRemove(request);
|
|
if (ret.error())
|
|
WARNING_LOG(m_logger) << ret();
|
|
return !ret.error();
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
bool HandlerInstrumentationAsset::setAssetState(int assetId, InstrumentationAssetState state)
|
|
{
|
|
ObjectInstrumentationAsset *obj = m_ObjectInstrumentationAssetMap.value(assetId);
|
|
if (!obj)
|
|
return false;
|
|
|
|
InstrumentationAsset data = obj->getData();
|
|
data.assetStateId = state;
|
|
return !obj->updateObject(data).error();
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
bool HandlerInstrumentationAsset::setAssetAvailableInExercise(int32_t assetId, const std::vector<int32_t> &exercises)
|
|
{
|
|
ObjectInstrumentationAsset *obj = m_ObjectInstrumentationAssetMap.value(assetId);
|
|
if (!obj)
|
|
return false;
|
|
|
|
InstrumentationAsset data = obj->getData();
|
|
data.availableInExercise = exercises;
|
|
return !obj->updateObject(data).error();
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
ObjectInstrumentationAsset *HandlerInstrumentationAsset::getAssetFromBarcode(const QString &barcode) const
|
|
{
|
|
const std::string barcodeStd = barcode.toStdString();
|
|
for (auto it = m_ObjectInstrumentationAssetMap.begin(); it != m_ObjectInstrumentationAssetMap.end(); ++it)
|
|
{
|
|
if (it.value()->getData().barcode == barcodeStd)
|
|
return it.value();
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
ObjectInstrumentationAsset *HandlerInstrumentationAsset::getAssetFromId(int32_t assetId) const
|
|
{
|
|
return m_ObjectInstrumentationAssetMap.value(assetId);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
ObjectInstrumentationAsset *HandlerInstrumentationAsset::getAssetFromBarcodeAndId(const QString &barcode, int32_t assetId) const
|
|
{
|
|
ObjectInstrumentationAsset *result = m_ObjectInstrumentationAssetMap.value(assetId);
|
|
|
|
if (result == nullptr)
|
|
return nullptr;
|
|
if (QString::fromStdString(result->getData().barcode) != barcode)
|
|
return nullptr;
|
|
return result;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
void HandlerInstrumentationAsset::onInstrumentationAssetModifyRequest(const rti::core::SampleIdentity &cookie,
|
|
const InstrumentationAssetModifyRequest &request)
|
|
{
|
|
DEBUG_LOG(m_logger) << "onInstrumentationAssetModifyRequest: " << request;
|
|
const Core::FunctionReplyObject ret = request.id > 0 ? instrumentationAssetModify(request) : instrumentationAssetCreate(request);
|
|
if (ret.error())
|
|
WARNING_LOG(m_logger) << ret();
|
|
m_mainHandler->dds()->fts()->instrumentationAssetModifyReply(cookie, ret);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
static InstrumentationAsset convert(const InstrumentationAssetModifyRequest &request,
|
|
const InstrumentationAssetState &state = InstrumentationAssetState::Unmounted)
|
|
{
|
|
InstrumentationAsset newData;
|
|
newData.id = request.id;
|
|
newData.name = request.name;
|
|
newData.mountingPointTypeId = request.mountingPointTypeId;
|
|
newData.barcode = request.barcode;
|
|
newData.hostname = request.hostname;
|
|
newData.properties = request.properties;
|
|
newData.assetStateId = state;
|
|
newData.lastUpdated = QDateTime::currentMSecsSinceEpoch();
|
|
newData.availableInExercise = request.availableInExercise;
|
|
|
|
return newData;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
Core::FunctionReplyObject HandlerInstrumentationAsset::instrumentationAssetCreate(const InstrumentationAssetModifyRequest &request, int *assetId)
|
|
{
|
|
if (assetId)
|
|
*assetId = 0;
|
|
|
|
ObjectInstrumentationAsset *ifs(new ObjectInstrumentationAsset(m_mainHandler));
|
|
Core::FunctionReplyObject ret = ifs->createObject(convert(request));
|
|
if (ret.error())
|
|
{
|
|
delete ifs;
|
|
return ret;
|
|
}
|
|
if (assetId)
|
|
*assetId = ifs->getId();
|
|
m_ObjectInstrumentationAssetMap.insert(ifs->getId(), ifs);
|
|
return ret;
|
|
}
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
Core::FunctionReplyObject HandlerInstrumentationAsset::instrumentationAssetModify(const InstrumentationAssetModifyRequest &request)
|
|
{
|
|
ObjectInstrumentationAsset *ifs = m_ObjectInstrumentationAssetMap.value(request.id);
|
|
if (ifs == nullptr)
|
|
return Core::FunctionReplyObject(request.id, tr_lts("dds", "Asset with id %1 not found")).arg(request.id);
|
|
|
|
return ifs->updateObject(convert(request, ifs->getData().assetStateId));
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
void HandlerInstrumentationAsset::onInstrumentationAssetRemoveRequest(const rti::core::SampleIdentity &cookie,
|
|
const Core::RemoveElementRequest &request)
|
|
{
|
|
const Core::FunctionReplyObject ret = onInstrumentationAssetRemove(request);
|
|
if (ret.error())
|
|
WARNING_LOG(m_logger) << ret();
|
|
m_mainHandler->dds()->fts()->instrumentationAssetRemoveReply(cookie, ret);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
Core::FunctionReplyObject HandlerInstrumentationAsset::onInstrumentationAssetRemove(const Core::RemoveElementRequest &request)
|
|
{
|
|
ObjectInstrumentationAsset *ifs = m_ObjectInstrumentationAssetMap.value(request.id);
|
|
if (ifs == nullptr)
|
|
return Core::FunctionReplyObject(request.id, tr_lts("dds", "Asset with id %1 not found")).arg(request.id);
|
|
|
|
if (m_mainHandler->dds()->topicLockMaster()->isLocked(ifs->getData(), m_mainHandler->exerciseId()))
|
|
return Core::FunctionReplyObject(request.id, tr_lts("dds", "Asset '%1' is used by another user!")).arg(ifs->getName());
|
|
|
|
Core::FunctionReplyObject ret = ifs->removeObject();
|
|
DEBUG_LOG(m_logger) << "Deleted Asset id = " << request.id;
|
|
|
|
if (ret.error())
|
|
return ret;
|
|
|
|
// delete ObjectInstrumentationAsset (including Map)
|
|
delete ifs;
|
|
m_ObjectInstrumentationAssetMap.remove(request.id);
|
|
m_mainHandler->getHandlerMountingPointAsset()->unmountAsset(request.id);
|
|
return ret;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
void HandlerInstrumentationAsset::onInstrumentationAssetSetStateRequest(const rti::core::SampleIdentity &cookie,
|
|
const InstrumentationAssetSetStateRequest &request)
|
|
{
|
|
DEBUG_LOG(m_logger) << "onInstrumentationAssetSetStateRequest: " << request;
|
|
const Core::FunctionReplyObject ret = instrumentationAssetSetState(request);
|
|
if (ret.error())
|
|
WARNING_LOG(m_logger) << ret();
|
|
m_mainHandler->dds()->fts()->instrumentationAssetSetStateReply(cookie, ret);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
Core::FunctionReplyObject HandlerInstrumentationAsset::instrumentationAssetSetState(const InstrumentationAssetSetStateRequest &request)
|
|
{
|
|
Core::FunctionReplyObject ret;
|
|
|
|
if (request.id > 0)
|
|
{
|
|
ObjectInstrumentationAsset *ifs = m_ObjectInstrumentationAssetMap.value(request.id);
|
|
if (ifs == nullptr)
|
|
return Core::FunctionReplyObject(request.id, tr_lts("dds", "Asset with id %1 not found")).arg(request.id);
|
|
|
|
if (m_mainHandler->dds()->topicLockMaster()->isLocked(ifs->getData(), m_mainHandler->exerciseId()))
|
|
return Core::FunctionReplyObject(request.id, tr_lts("dds", "Asset '%1' is used by another user!")).arg(ifs->getName());
|
|
|
|
// we cannot mount here
|
|
if (request.instrumentationAssetStateId == InstrumentationAssetState::Mounted)
|
|
{
|
|
return Core::FunctionReplyObject(request.id, tr_lts("dds", "Asset '%1' cannot be mounted by this request")).arg(ifs->getName());
|
|
}
|
|
|
|
if(request.instrumentationAssetStateId == InstrumentationAssetState::Defect)
|
|
{
|
|
bool retUnmount = m_mainHandler->getHandlerMountingPointAsset()->unmountAsset(request.id);
|
|
WARNING_LOG(m_logger)<<"unmount Asset id ( "<< request.id<<" )"<<" return ? "<<retUnmount;
|
|
}
|
|
// set new state (unmounted or defect)
|
|
if (!setAssetState(request.id, request.instrumentationAssetStateId))
|
|
{
|
|
return Core::FunctionReplyObject(request.id, tr_lts("dds", "Could not update state for asset with id %1")).arg(request.id);
|
|
}
|
|
|
|
InstrumentationAsset data = ifs->getData();
|
|
data.assetStateId = request.instrumentationAssetStateId;
|
|
data.lastUpdated = QDateTime::currentMSecsSinceEpoch();
|
|
ret = ifs->updateObject(data);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
void HandlerInstrumentationAsset::onInstrumentationAssetUnmountRequest(const rti::core::SampleIdentity &cookie, const InstrumentationAssetUnmountRequest &request)
|
|
{
|
|
DEBUG_LOG(m_logger) << "onInstrumentationAssetUnmountRequest: " << request;
|
|
const Core::FunctionReplyObject ret = instrumentationAssetUnmount(request);
|
|
if (ret.error())
|
|
WARNING_LOG(m_logger) << ret();
|
|
m_mainHandler->dds()->fts()->instrumentationAssetUnmountReply(cookie, ret);
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
Core::FunctionReplyObject HandlerInstrumentationAsset::instrumentationAssetUnmount(const InstrumentationAssetUnmountRequest &request)
|
|
{
|
|
Core::FunctionReplyObject ret;
|
|
if(request.id > 0)
|
|
{
|
|
ObjectInstrumentationAsset *ifs = m_ObjectInstrumentationAssetMap.value(request.id);
|
|
if (ifs == nullptr)
|
|
{
|
|
return Core::FunctionReplyObject(request.id, tr_lts("dds", "Asset with id %1 not found")).arg(request.id);
|
|
}
|
|
|
|
if (m_mainHandler->dds()->topicLockMaster()->isLocked(ifs->getData(), m_mainHandler->exerciseId()))
|
|
{
|
|
return Core::FunctionReplyObject(request.id, tr_lts("dds", "Asset '%1' is used by another user!")).arg(ifs->getName());
|
|
}
|
|
|
|
if (!m_mainHandler->getHandlerMountingPointAsset()->unmountAsset(request.id))
|
|
{
|
|
return Core::FunctionReplyObject(request.id, tr_lts("dds", "Error unmounting instrumentation asset with id %1")).arg(request.id);
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|