176 lines
4.5 KiB
C++
Executable File
176 lines
4.5 KiB
C++
Executable File
/**
|
|
* Handle one wind sensor
|
|
* Requests for wind sensor data in an given interval.
|
|
* All data will be send by signal/slot connection in case of data change.
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by hjl, SLSE, 2016-09-22
|
|
*/
|
|
|
|
#include "tcpwind.h"
|
|
#include "settings.h"
|
|
#include "logger.h"
|
|
|
|
namespace MeteoDataManagement
|
|
{
|
|
|
|
TcpWind::TcpWind(QObject *parent)
|
|
: TcpSocket("TcpWind", parent)
|
|
, m_requestTimer(new QTimer(this))
|
|
, m_requestTimeoutTimer(new QTimer(this))
|
|
, m_direction(0)
|
|
, m_speed(0)
|
|
, m_dataChanged(false)
|
|
{
|
|
// m_logger geerbt aus TcpSocket
|
|
INFO_LOG(m_logger) << "CTOR TcpWind... hat TcpSocket fuer connects initialisiert";
|
|
|
|
connect(m_requestTimer, &QTimer::timeout, this, &TcpWind::onSensorDataRequest);
|
|
connect(m_requestTimeoutTimer, &QTimer::timeout, this, &TcpWind::onRequestTimeout);
|
|
|
|
}
|
|
//-------------------------------------------------------------------
|
|
|
|
void TcpWind::start(const QString &converterIp, quint16 windPort)
|
|
{
|
|
INFO_LOG(m_logger) << "startTcpWind() ... call function TcpSocket::start()";
|
|
|
|
TcpSocket::start(converterIp, windPort); // connect...
|
|
m_requestTimer->start(Settings::getRequstIntervalWindSensor());
|
|
}
|
|
//-------------------------------------------------------------------
|
|
|
|
void TcpWind::init()
|
|
{
|
|
// pure virtual function aus TcpSocket
|
|
|
|
// WS-425 Einstellungen: NEMA-Extended, PollMode, 9600,8,N,1
|
|
// Hier sind dann keine weiteren Einstellungen erforderlich.
|
|
// Nach PowerOn ist der WS betriebsbereit.
|
|
}
|
|
//-------------------------------------------------------------------
|
|
|
|
void TcpWind::dataAvailable(const QByteArray &line)
|
|
{
|
|
// pure virtual function aus TcpSocket
|
|
|
|
// Nur lines mit Wetterdaten behandeln
|
|
if (line.contains("PAMWV"))
|
|
{
|
|
m_requestTimeoutTimer->stop();
|
|
|
|
bool windOk = extractDataFromLine(line);
|
|
|
|
if (m_dataChanged)
|
|
{
|
|
m_dataChanged = false;
|
|
emit sigWindDataChanged(m_direction, m_speed, windOk);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DEBUG_LOG(m_logger) << "Other wind sensor data available line: " << line ;
|
|
// ggf. andere Meldungen behandeln
|
|
}
|
|
}
|
|
//-------------------------------------------------------------------
|
|
|
|
void TcpWind::onSensorDataRequest()
|
|
{
|
|
if (m_requestTimeoutTimer->isActive())
|
|
return;
|
|
|
|
INFO_LOG(m_logger) << "onSensorDataRequest() ...";
|
|
|
|
if (!sendCommand("$WIPAQ,*72\r\n"))
|
|
return;
|
|
|
|
m_requestTimeoutTimer->start(Settings::getRequstTimeoutWindSensor());
|
|
}
|
|
//-------------------------------------------------------------------
|
|
|
|
void TcpWind::onRequestTimeout()
|
|
{
|
|
ERROR_LOG(m_logger) << "Timeout for wind sensor data request."
|
|
<< " RS232-LAN Converter: " << m_hostName
|
|
<< " Port: " << m_hostPort;
|
|
|
|
emit sigWindDataChanged(0, 0, false);
|
|
m_requestTimeoutTimer->stop();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
|
|
bool TcpWind::extractDataFromLine(const QByteArray &line)
|
|
{
|
|
bool bOk = true;
|
|
DEBUG_LOG(m_logger) << "extractDataFromLine line: " << line;
|
|
|
|
QList<QByteArray> list = line.split(',');
|
|
|
|
if (list.size() != 6)
|
|
{
|
|
WARNING_LOG(m_logger) << "Invalid number of data in line: " << line << " cnt: " << list.size();
|
|
return false;
|
|
}
|
|
|
|
// NMEA-Extended line data format:
|
|
// [0] $PAWMV fixed Remark: 'A' discribes the sensor-id
|
|
// [1] float direction in degree, or empty
|
|
// [2] char R = relativ, T = true
|
|
// [3] float speed, or empty
|
|
// [4] char M = m/s, K = Km/h, N = Knoten
|
|
// [5] String "<State> * <checksum>"
|
|
// State: A = Data valid, V = Data invalid
|
|
// * fixed
|
|
// checksum 8bit XOR, excluding $ and *)
|
|
|
|
if (list.at(5).contains('V'))
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid data from windsensor. "
|
|
<< " RS232-LAN Converter: " << m_hostName
|
|
<< " Port: " << m_hostPort
|
|
<< " Line: " << line;
|
|
return false;
|
|
}
|
|
|
|
float value;
|
|
if (list.at(1).size() > 0)
|
|
{
|
|
value = list.at(1).toFloat(&bOk);
|
|
if (bOk && !qFuzzyCompare(value, m_direction))
|
|
{
|
|
m_direction = value;
|
|
m_dataChanged = true;
|
|
}
|
|
}
|
|
|
|
if (list.at(3).size() > 0)
|
|
{
|
|
value = list.at(3).toFloat(&bOk);
|
|
if (bOk && !qFuzzyCompare(value, m_speed))
|
|
{
|
|
m_speed = value;
|
|
m_dataChanged = true;
|
|
}
|
|
}
|
|
|
|
if (list.at(4).contains('K'))
|
|
{
|
|
// km/h -> m/s
|
|
m_speed = m_speed * 0.2778f;
|
|
}
|
|
|
|
if (list.at(4).contains('N'))
|
|
{
|
|
// Kn -> m/s
|
|
m_speed = m_speed * 0.5144f;
|
|
}
|
|
|
|
return bOk;
|
|
}
|
|
//-------------------------------------------------------------------
|
|
|
|
|
|
} // namespace MeteoDataManagement
|