146 lines
4.8 KiB
C++
Executable File
146 lines
4.8 KiB
C++
Executable File
/**
|
|
* Handler for one meteo data unit
|
|
* Manages the connection to a MDU and sends data via DDS as soon as they have been changed
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by hjl, SLSE, 2016-09-27
|
|
*/
|
|
|
|
#include "handler_meteo_data_unit.h"
|
|
#include "handler_meteo_data_units.h"
|
|
#include "handler_infrastructure_data.h"
|
|
#include "tcpweatherstation.h"
|
|
#include "main_handler.h"
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
|
|
using namespace MeteoDataManagement;
|
|
|
|
HandlerMeteoDataUnit::HandlerMeteoDataUnit(MainHandler *mainHandler,
|
|
const InfrastructureManagement::InstrumentationAsset &asset,
|
|
QObject *parent)
|
|
: QObject(parent)
|
|
, m_mainHandler(mainHandler)
|
|
, m_asset(asset)
|
|
, m_tcpWS(nullptr)
|
|
, m_logger(getLogger("HandlerMeteoDataUnit"))
|
|
{
|
|
init();
|
|
} // end of ctor()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
HandlerMeteoDataUnit::~HandlerMeteoDataUnit()
|
|
{
|
|
delete m_tcpWS;
|
|
} // end of dtor()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool HandlerMeteoDataUnit::init()
|
|
{
|
|
delete m_tcpWS;
|
|
QJsonParseError jError;
|
|
QJsonDocument doc(QJsonDocument::fromJson(m_asset.properties.c_str(), &jError));
|
|
if (jError.error != QJsonParseError::NoError ||
|
|
!doc.isObject())
|
|
{
|
|
ERROR_LOG(m_logger) << "Can not initialize meteo data unit - properties are not parsable: "
|
|
<< jError.errorString() << " - '" << m_asset.properties << "'";
|
|
return false;
|
|
}
|
|
QJsonObject obj(doc.object());
|
|
quint16 climatePort = obj[QStringLiteral("climatePort")].toInt();
|
|
quint16 windPort = obj[QStringLiteral("windPort")].toInt();
|
|
if (climatePort == 0 || windPort == 0)
|
|
{
|
|
ERROR_LOG(m_logger) << "Can not parse climate and/or wind port from properties: '" <<
|
|
m_asset.properties << "'";
|
|
return false;
|
|
}
|
|
|
|
m_tcpWS = new TcpWeatherStation(m_asset.id,
|
|
QString::fromStdString(m_asset.hostname),
|
|
climatePort,
|
|
windPort);
|
|
|
|
// init local MeteoData Values for compare
|
|
m_newMeteoData.id = m_asset.id;
|
|
m_newMeteoData.weatherStationId = m_asset.id;
|
|
m_newMeteoData.note = m_asset.name;
|
|
m_newMeteoData.isStaticMeteoData = false;
|
|
|
|
// Position der MDU eintragen (GeoPosition2D)
|
|
if (m_mainHandler->ism())
|
|
{
|
|
const Core::GeoPosition geoPos = m_mainHandler->ism()->getStructureElementPosition(m_asset.id);
|
|
//GeoPos->GeoPos2D
|
|
m_newMeteoData.pos = Core::GeoPosition2D(geoPos.lon, geoPos.lat);
|
|
}
|
|
|
|
|
|
m_newMeteoData.pressure = 800.0;
|
|
m_newMeteoData.humidity = 0.0;
|
|
m_newMeteoData.temperature = 0.0;
|
|
m_newMeteoData.windSpeed = 0.0;
|
|
m_newMeteoData.windDirection = 0.0;
|
|
|
|
m_lastMeteoData = m_newMeteoData;
|
|
|
|
connect(m_tcpWS, &TcpWeatherStation::sigWeatherDataChanged,
|
|
this, &HandlerMeteoDataUnit::onMeteoDataChanged);
|
|
|
|
return true;
|
|
|
|
} // end of init()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void HandlerMeteoDataUnit::onMeteoDataChanged(int id,
|
|
float pressure, float temperature, float humidity,
|
|
float direction, float speed, bool climateOk, bool windOk)
|
|
{
|
|
m_newMeteoData.id = id;
|
|
m_newMeteoData.pressure = pressure; // in hPa
|
|
m_newMeteoData.temperature = temperature; // in Grad Celsius
|
|
m_newMeteoData.humidity = humidity; // % rel. Feuchte
|
|
m_newMeteoData.windDirection = direction; // Grad, Nordbezogen mathem. negativ
|
|
m_newMeteoData.windSpeed = speed; // in m/s
|
|
m_newMeteoData.climateOk = climateOk;
|
|
m_newMeteoData.windOk = windOk;
|
|
|
|
// Position der MDU eintragen (falls sie noch nicht beim init() ermittelt werden konnte)
|
|
if (m_lastMeteoData.pos == Core::GeoPosition2D())
|
|
{
|
|
if (m_mainHandler->ism())
|
|
{
|
|
const Core::GeoPosition geoPos = m_mainHandler->ism()->getStructureElementPosition(m_asset.id);
|
|
m_newMeteoData.pos = Core::GeoPosition2D(geoPos.lon, geoPos.lat);
|
|
}
|
|
}
|
|
|
|
if (m_lastMeteoData == m_newMeteoData)
|
|
return;
|
|
|
|
// publish newMeteoData
|
|
onPublishData();
|
|
|
|
// safe for next compare
|
|
m_lastMeteoData = m_newMeteoData;
|
|
|
|
} // end of onMeteoDataChanged()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void HandlerMeteoDataUnit::onPublishData()
|
|
{
|
|
MeteoDataManagement::MeteoData ddsData;
|
|
|
|
ddsData = m_newMeteoData;
|
|
ddsData.timeStamp = m_mainHandler->dds()->currentPartitionTime();
|
|
|
|
INFO_LOG(m_logger) << "Publish dynamic MeteoData to dds: " << ddsData;
|
|
|
|
if (!m_mainHandler->dds()->dts()->publish(ddsData))
|
|
ERROR_LOG(m_logger) << "Error publishing MdmMeteoData to dds";
|
|
} // end of onPublishData()
|
|
//-----------------------------------------------------------------------------
|
|
|