TelescopeClientJsonRts2.cpp   TelescopeClientJsonRts2.cpp 
skipping to change at line 31 skipping to change at line 31
#include "StelUtils.hpp" #include "StelUtils.hpp"
#include "StelModuleMgr.hpp" #include "StelModuleMgr.hpp"
#include "StelTranslator.hpp" #include "StelTranslator.hpp"
#include "QByteArray" #include "QByteArray"
#include "QJsonArray" #include "QJsonArray"
#include "QJsonDocument" #include "QJsonDocument"
#include "QJsonParseError" #include "QJsonParseError"
#include "QJsonObject" #include "QJsonObject"
#include "QUrlQuery" #include "QUrlQuery"
#include "QTimer"
#include "QTimerEvent"
#include "TelescopeClientJsonRts2.hpp" #include "TelescopeClientJsonRts2.hpp"
TelescopeClientJsonRts2::TelescopeClientJsonRts2(const QString &name, const QString &params, Equinox eq) TelescopeClientJsonRts2::TelescopeClientJsonRts2(const QString &name, const QString &params, Equinox eq)
: TelescopeClient(name) : TelescopeClient(name)
, networkManager(new QNetworkAccessManager) , networkManager(new QNetworkAccessManager)
, equinox(eq) , equinox(eq)
, baseurl("http://localhost:8889/") , baseurl("http://localhost:8889/")
, telName("") , telName("")
, telReadonly(false) , telReadonly(false)
, telLatitude(NAN) , telLatitude(NAN)
, telLongitude(NAN) , telLongitude(NAN)
, telAltitude(NAN) , telAltitude(NAN)
, telTargetDist(NAN) , telTargetDist(NAN)
, time_delay(500) , time_delay(50)
, reconnectTimer(-1)
, refresh_delay(500)
, server_micros(0)
{ {
telescopeManager = GETSTELMODULE(TelescopeControl); telescopeManager = GETSTELMODULE(TelescopeControl);
// Example params: // Example params:
// petr:test@localhost:8889/tel // 1000:test:1234@localhost:8889/tel
qDebug() << "TelescopeRTS2(" << name << ") paramaters: " << params; QRegExp paramRx("^(\\d+):(.*)$");
QString url;
if (paramRx.exactMatch(params))
{
refresh_delay = paramRx.capturedTexts().at(1).toInt() / 1000
; // convert microseconds to milliseconds
url = paramRx.capturedTexts().at(2).trimmed();
}
else
{
qWarning() << "ERROR creating TelescopeClientJsonRts2: inval
id parameters.";
return;
}
baseurl.setUrl(params); qDebug() << "TelescopeRTS2(" << name << ") URL, refresh timeout: " <
< url << "," << refresh_delay;
baseurl.setUrl(url);
if (!baseurl.isValid()) if (!baseurl.isValid())
{ {
qWarning() << "TelescopeRTS2(" << name << ") invalid URL"; qWarning() << "TelescopeRTS2(" << name << ") invalid URL: " << url;
return; return;
} }
QUrl rurl(baseurl); QUrl rurl(baseurl);
rurl.setPath(baseurl.path() + "/api/devbytype"); rurl.setPath(baseurl.path() + "/api/devbytype");
QUrlQuery query; QUrlQuery query;
query.addQueryItem("t", "2"); query.addQueryItem("t", "2");
rurl.setQuery(query); rurl.setQuery(query);
QNetworkRequest cfgRequest;
cfgRequest.setUrl(rurl); cfgRequest.setUrl(rurl);
qDebug() << "TelescopeRTS2(" << name << ")::TelescopeRTS2: request u rl:" << rurl.toString(); qDebug() << "TelescopeRTS2(" << name << ")::TelescopeRTS2: request u rl:" << rurl.toString();
connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLO T(replyFinished(QNetworkReply*))); connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLO T(replyFinished(QNetworkReply*)));
networkManager.get(cfgRequest); networkManager.get(cfgRequest);
} }
TelescopeClientJsonRts2::~TelescopeClientJsonRts2(void) TelescopeClientJsonRts2::~TelescopeClientJsonRts2(void)
{ {
} }
void TelescopeClientJsonRts2::refreshTimer()
{
server_micros = getNow();
networkManager.get(request);
}
void TelescopeClientJsonRts2::replyFinished(QNetworkReply *reply) void TelescopeClientJsonRts2::replyFinished(QNetworkReply *reply)
{ {
if (reply->error() != QNetworkReply::NoError) if (reply->error() != QNetworkReply::NoError)
{ {
if (reply->url().path().endsWith("/api/cmd") && reply->error () == QNetworkReply::ProtocolInvalidOperationError) if (reply->url().path().endsWith("/api/cmd") && reply->error () == QNetworkReply::ProtocolInvalidOperationError)
{ {
setReadOnly(true); setReadOnly(true);
return; return;
} }
qDebug() << "TelescopeRTS2(" << name << ")::replyFinished: e rror " << reply->error() << " url: " << reply->url().toString(); qDebug() << "TelescopeRTS2(" << name << ")::replyFinished: e rror " << reply->error() << " url: " << reply->url().toString();
telName = ""; telName = "";
if (reconnectTimer < 0)
reconnectTimer = startTimer(15000);
return; return;
} }
if (reconnectTimer > 0)
{
killTimer(reconnectTimer);
reconnectTimer = -1;
}
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
//qDebug() << "TelescopeRTS2(" << name << ")::replyFinished: data " << (QString) data; //qDebug() << "TelescopeRTS2(" << name << ")::replyFinished: data " << (QString) data;
QJsonDocument doc; QJsonDocument doc;
QJsonParseError jsonError; QJsonParseError jsonError;
doc = QJsonDocument::fromJson(data, &jsonError); doc = QJsonDocument::fromJson(data, &jsonError);
if (reply->url().path().endsWith("/api/devbytype")) if (reply->url().path().endsWith("/api/devbytype"))
{ {
QJsonArray arr = doc.array(); QJsonArray arr = doc.array();
telName = arr[0].toString(); telName = arr[0].toString();
QUrl diurl(baseurl); getReadOnly();
diurl.setPath(baseurl.path() + "/api/deviceinfo");
QUrlQuery query;
query.addQueryItem("d", telName);
diurl.setQuery(query);
request.setUrl(diurl);
qDebug() << "TelescopeRTS2(" << name << ")::replyFinished: r
equest url:" << diurl.toString();
networkManager.get(request);
} }
else if (reply->url().path().endsWith("/api/deviceinfo")) else if (reply->url().path().endsWith("/api/deviceinfo"))
{ {
QJsonObject docObject = doc.object(); QJsonObject docObject = doc.object();
setReadOnly(docObject["readonly"].toBool()); setReadOnly(docObject["readonly"].toBool());
QUrl rurl(baseurl); QUrl rurl(baseurl);
rurl.setPath(baseurl.path() + "/api/get"); rurl.setPath(baseurl.path() + "/api/get");
QUrlQuery query; QUrlQuery query;
query.addQueryItem("d", telName); query.addQueryItem("d", telName);
rurl.setQuery(query); rurl.setQuery(query);
request.setUrl(rurl); request.setUrl(rurl);
qDebug() << "TelescopeRTS2(" << name << ")::replyFinished: r equest url:" << rurl.toString(); qDebug() << "TelescopeRTS2(" << name << ")::replyFinished: r equest url:" << rurl.toString();
networkManager.get(request); refreshTimer();
} }
else if (reply->url().path().endsWith("/api/get")) else if (reply->url().path().endsWith("/api/get"))
{ {
QJsonObject docObject = doc.object(); QJsonObject docObject = doc.object();
QJsonObject dObject = docObject["d"].toObject(); QJsonObject dObject = docObject["d"].toObject();
QJsonObject telObject = dObject["TEL"].toObject(); QJsonObject telObject = dObject["TEL"].toObject();
telLongitude = dObject["LONGITUD"].toDouble() * M_PI / 180.0 ; telLongitude = dObject["LONGITUD"].toDouble() * M_PI / 180.0 ;
telLatitude = dObject["LATITUDE"].toDouble() * M_PI / 180.0; telLatitude = dObject["LATITUDE"].toDouble() * M_PI / 180.0;
telAltitude = dObject["ALTITUDE"].toDouble(); telAltitude = dObject["ALTITUDE"].toDouble();
telTargetDist = dObject["target_distance"].toDouble() * M_PI / 180.0; telTargetDist = dObject["target_distance"].toDouble() * M_PI / 180.0;
const double ra = telObject["ra"].toDouble() * M_PI / 180.0; const double ra = telObject["ra"].toDouble() * M_PI / 180.0;
const double dec = telObject["dec"].toDouble() * M_PI / 180. 0; const double dec = telObject["dec"].toDouble() * M_PI / 180. 0;
const double cdec = cos(dec); const double cdec = cos(dec);
qDebug() << "TelescopeRTS2(" << name << ")::replyFinished: R ADEC" << ra << dec; qDebug() << "TelescopeRTS2(" << name << ")::replyFinished: R ADEC" << ra << dec;
Vec3d pos(cos(ra)*cdec, sin(ra)*cdec, sin(dec)); lastPos.set(cos(ra)*cdec, sin(ra)*cdec, sin(dec));
interpolatedPosition.add(pos, getNow(), getNow(), 0); interpolatedPosition.add(lastPos, getNow(), server_micros, 0
);
qDebug() << "TelescopeRTS2(" << name << ")::replyFinished: r QTimer::singleShot(refresh_delay, this, SLOT(refreshTimer())
equest url:" << request.url().toString(); );
}
networkManager.get(request); else if (reply->url().path().endsWith("/api/cmd"))
{
QJsonObject docObject = doc.object();
int cmdRet = docObject["ret"].toInt();
qDebug() << "Move command finished: " << cmdRet;
if (cmdRet == 0)
getReadOnly();
else
setReadOnly(true);
} }
else else
{ {
qWarning() << "TelescopeRTS2(" << name << ")::replyFinished: unhandled reply: " << reply->url().toString(); qWarning() << "TelescopeRTS2(" << name << ")::replyFinished: unhandled reply: " << reply->url().toString();
} }
reply->deleteLater(); reply->deleteLater();
} }
bool TelescopeClientJsonRts2::isConnected(void) const bool TelescopeClientJsonRts2::isConnected(void) const
{ {
return telName.isEmpty() == false; return telName.isEmpty() == false;
} }
Vec3d TelescopeClientJsonRts2::getJ2000EquatorialPos(const StelCore* core) const Vec3d TelescopeClientJsonRts2::getJ2000EquatorialPos(const StelCore*) const
{ {
Q_UNUSED(core);
const qint64 now = getNow() - time_delay; const qint64 now = getNow() - time_delay;
return interpolatedPosition.get(now); return interpolatedPosition.get(now);
} }
void TelescopeClientJsonRts2::telescopeGoto(const Vec3d &j2000Pos, StelObje ctP selectObject) void TelescopeClientJsonRts2::telescopeGoto(const Vec3d &j2000Pos, StelObje ctP selectObject)
{ {
if (!isConnected()) if (!isConnected())
return; return;
QUrl set(baseurl); QUrl set(baseurl);
skipping to change at line 232 skipping to change at line 258
qDebug() << "TelescopeRTS2(" << name << ")::telescopeGoto: request: " << set.toString(); qDebug() << "TelescopeRTS2(" << name << ")::telescopeGoto: request: " << set.toString();
networkManager.get(setR); networkManager.get(setR);
} }
bool TelescopeClientJsonRts2::hasKnownPosition(void) const bool TelescopeClientJsonRts2::hasKnownPosition(void) const
{ {
return interpolatedPosition.isKnown(); return interpolatedPosition.isKnown();
} }
void TelescopeClientJsonRts2::timerEvent(QTimerEvent *event)
{
if (event->timerId() == reconnectTimer)
{
qDebug() << "Telescope reconnect";
networkManager.get(cfgRequest);
}
}
QString TelescopeClientJsonRts2::getTelescopeInfoString(const StelCore* cor e, const InfoStringGroup& flags) const QString TelescopeClientJsonRts2::getTelescopeInfoString(const StelCore* cor e, const InfoStringGroup& flags) const
{ {
Q_UNUSED(core); Q_UNUSED(core);
Q_UNUSED(flags); Q_UNUSED(flags);
QString str; QString str;
QTextStream oss(&str); QTextStream oss(&str);
if (telReadonly) if (telReadonly)
{ {
oss << q_("Read-only telescope") << "<br /"; oss << q_("Read-only telescope") << "<br /";
} }
oss << q_("Telescope position: ") << QString("%1 %2 %3m").arg(StelUt ils::radToDmsStr(telLongitude,true), StelUtils::radToDmsStr(telLatitude,tru e)).arg(telAltitude) << "<br />"; oss << q_("Telescope position: ") << QString("%1 %2 %3m").arg(StelUt ils::radToDmsStr(telLongitude,true), StelUtils::radToDmsStr(telLatitude,tru e)).arg(telAltitude) << "<br />";
oss << q_("Distance to target position: ") << StelUtils::radToDmsStr (telTargetDist,true) << "<br />"; oss << q_("Distance to target position: ") << StelUtils::radToDmsStr (telTargetDist,true) << "<br />";
return str; return str;
} }
void TelescopeClientJsonRts2::getReadOnly()
{
QUrl diurl(baseurl);
diurl.setPath(baseurl.path() + "/api/deviceinfo");
QUrlQuery query;
query.addQueryItem("d", telName);
diurl.setQuery(query);
request.setUrl(diurl);
qDebug() << "TelescopeRTS2(" << name << ")::replyFinished: request u
rl:" << diurl.toString();
networkManager.get(request);
}
void TelescopeClientJsonRts2::setReadOnly(bool readonly) void TelescopeClientJsonRts2::setReadOnly(bool readonly)
{ {
telReadonly = readonly; telReadonly = readonly;
QSettings* settings = StelApp::getInstance().getSettings(); QSettings* settings = StelApp::getInstance().getSettings();
Q_ASSERT(settings); Q_ASSERT(settings);
if (telescopeManager) if (telescopeManager)
{ {
if (telReadonly) if (telReadonly)
{ {
 End of changes. 18 change blocks. 
30 lines changed or deleted 86 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/