Files
qt5-legatus-legacy/Lts/Services/SoundManagement/handler_sounds.h
T
2026-08-13 18:33:10 +02:00

138 lines
4.9 KiB
C++
Executable File

/**
* Handler for table-io message data request
* $Id$
* $HeadURL$
* $Author$
* Created by hjl, SLSE, 2016-10-12
*/
#pragma once
#include <QObject>
#include "dds_handler.h"
#include "logger.h"
#include <som_db.h>
#include <ngldb/db_service_handler.h>
#include "main_handler.h"
#include <SoundManagement/som_fts.h>
#include <SoundManagement/som_dts.h>
#include <InfrastructureManagement/ism_dtc.h>
#include <LtsCoordTrans/lts_coord_trans.h>
// HTTP Request
#include <QNetworkAccessManager>
#include <QTimer>
#include <QEventLoop>
namespace SoundManagement
{
template <typename Func1>
void await(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal)
{
QEventLoop x;
QObject::connect(sender, signal, &x, &QEventLoop::quit);
x.exec();
}
using DbHandler = NGLDB::DbServiceHandler<DatabaseInterface>;
inline QPointF geographic2Utm(const Core::GeoPosition2D &geoPos, int *zone = nullptr)
{
const int zoneToUse = zone ? *zone : -1;
auto utm = LtsCoordTrans::UtmCoordinate::fromGeographic(geoPos.lon(), geoPos.lat(), zoneToUse);
if (zone)
*zone = utm.zone;
return {utm.x, utm.y};
}
class HandlerSounds final
: public QObject
, protected Interface::Fts::GetActiveSoundsRequestHandler
, protected Interface::Fts::StopAllSoundsRequestHandler
, protected Interface::Fts::GetSoundsRequestHandler
, protected Interface::Fts::ControlSoundRequestHandler
{
Q_OBJECT
public:
explicit HandlerSounds(const std::shared_ptr<DdsHandler> &ddsHandler,
const std::shared_ptr<DbHandler> &dbHandler,
QObject *parent = nullptr);
~HandlerSounds() = default;
bool init();
private Q_SLOTS:
void onInstrumentationAssetChanged(const InfrastructureManagement::InstrumentationAsset &data, DataModification modType);
void onMountingPointAssetChanged(const InfrastructureManagement::MountingPointAsset &data, DataModification modType);
void onSoundStateChanged(const SoundManagement::SoundState &data, DataModification modType);
void httpFinished();
void onStopSingleSound(const int egcuId, const SoundManagement::SoundCategory soundCategory, const int soundId);
private: // methods
QString httpFinishedBlocking(QNetworkReply *reply);
void onGetActiveSoundsRequest(const rti::core::SampleIdentity &cookie,
const GetActiveSoundsRequest &request) override;
void onStopAllSoundsRequest(const rti::core::SampleIdentity &cookie,
const StopAllSoundsRequest &request) override;
void onGetSoundsRequest(const rti::core::SampleIdentity &cookie,
const GetSoundsRequest &request) override;
void onControlSoundRequest(const rti::core::SampleIdentity &cookie,
const ControlSoundRequest &request) override;
void onTimerTimeout();
QString stopAllSounds(const int egcuId); // BLOCKING!
void getActiveSounds();
void getActiveSound(const int egcuId);
QString playBackgroundSound(const QString &user,
const QString &passwd,
const QString &host,
const QString &category,
const int sound,
const int volume,
const int control);
void startRequest(const QUrl &requestedUrl);
QString startRequestBlocking(const QUrl &requestedUrl);
QString enumToString(const SoundManagement::SoundCategory &category);
SoundManagement::SoundCategory stringToEnum(const QString &category);
void handleSoundReport(const QString &json, const QString &host);
int getAssetId(const QString &hostName);
int getDuration(const SoundCategory &category, const int soundId);
int getSoundIndex(const SoundCategory &category, const QString &name);
private: // attributes
struct SoundStr
{
int index = 0;
int soundId = 0;
SoundManagement::SoundCategory category = SoundManagement::SoundCategory::Unknown;
QString name = QString();
QString displayName = QString();
int duration = 0;
bool isRepeatable = false;
};
std::shared_ptr<DdsHandler> m_ddsHandler; ///< Pointer to DDS handler
std::shared_ptr<DbHandler> m_dbHandler; ///< Pointer to DB handler
QHash<int, QString> m_hostnames; // key = assetId, value = hostname
QHash<QString, QString> m_activeSounds; // key = hostname
QNetworkAccessManager m_qnam;
QTimer *m_updateActiveSoundsTimer{new QTimer(this)};
bool m_httpRequestAborted{false};
QVector<SoundStr> m_soundNames{QVector<SoundStr>()};
int m_indexActiveSound{0};
QVector<SoundState> m_soundStates{QVector<SoundManagement::SoundState>()};
int m_topicId{0};
QHash <int, int> m_mountingPointAsset; // Key= structureElementId, value= assetId
NGL::INGLLogger &m_logger;
};
} // namespace SoundManagement