1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
#include "orbitalelementsdatabase.hh"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QStandardPaths>
#include <QFileInfo>
#include <QNetworkReply>
#include <QDir>
#include "logger.hh"
/* ********************************************************************************************* *
* Implementation of OrbitalElement::Epoch
* ********************************************************************************************* */
OrbitalElement::Epoch::Epoch()
: year(0), month(0), day(0), hour(0), minute(0), second(0), microsecond(0)
{
// pass...
}
OrbitalElement::Epoch::Epoch(unsigned int pyear, unsigned int pmonth, unsigned int pday,
unsigned int phour, unsigned int pminute, unsigned int psecond,
unsigned int pmicrosecond)
: year(pyear), month(pmonth), day(pday), hour(phour), minute(pminute), second(psecond),
microsecond(pmicrosecond)
{
// pass...
}
OrbitalElement::Epoch
OrbitalElement::Epoch::parse(const QString &datetime) {
QRegularExpression pattern("([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{6})");
QRegularExpressionMatch match = pattern.match(datetime);
if (! match.hasMatch())
return Epoch();
return Epoch{
match.captured(1).toUInt(),
match.captured(2).toUInt(),
match.captured(3).toUInt(),
match.captured(4).toUInt(),
match.captured(5).toUInt(),
match.captured(6).toUInt(),
match.captured(7).toUInt()
};
}
double
OrbitalElement::Epoch::toEpoch() const {
QDate date(year, month, day);
double res = microsecond;
res = (res / 1e6) + second;
res = (res / 60) + minute;
res = (res / 60) + hour;
res = (res / 24) + date.dayOfYear();
return res;
}
QString
OrbitalElement::Epoch::toString() const {
return QString("%1-%2-%3T%4:%5:%6.%7")
.arg(year, 4, 10, QChar('0'))
.arg(month, 2, 10, QChar('0'))
.arg(day, 2, 10, QChar('0'))
.arg(hour, 2, 10, QChar('0'))
.arg(minute, 2, 10, QChar('0'))
.arg(second, 2, 10, QChar('0'))
.arg(microsecond, 6, 10, QChar('0'));
}
/* ********************************************************************************************* *
* Implementation of OrbitalElement
* ********************************************************************************************* */
OrbitalElement::OrbitalElement()
: _id(0), _name(), _epoch(), _meanMotion(0.0), _meanMotionDerivative(0.0),
_inclination(0.0), _ascension(0.0), _eccentricity(0.0), _perigee(0.0), _meanAnomaly(0.0),
_revolutionNumber(0)
{
// pass...
}
OrbitalElement::OrbitalElement(unsigned int id)
: _id(id), _name(), _epoch(), _meanMotion(0.0), _meanMotionDerivative(0.0),
_inclination(0.0), _ascension(0.0), _eccentricity(0.0), _perigee(0.0), _meanAnomaly(0.0),
_revolutionNumber(0)
{
// pass...
}
bool
OrbitalElement::isValid() const {
return 0 != _id;
}
unsigned int
OrbitalElement::id() const {
return _id;
}
const QString &
OrbitalElement::name() const {
return _name;
}
const OrbitalElement::Epoch &
OrbitalElement::epoch() const {
return _epoch;
}
double
OrbitalElement::meanMotion() const {
return _meanMotion;
}
double
OrbitalElement::meanMotionDerivative() const {
return _meanMotionDerivative;
}
double
OrbitalElement::inclination() const {
return _inclination;
}
double
OrbitalElement::ascension() const {
return _ascension;
}
double
OrbitalElement::eccentricity() const {
return _eccentricity;
}
double
OrbitalElement::perigee() const {
return _perigee;
}
double
OrbitalElement::meanAnomaly() const {
return _meanAnomaly;
}
unsigned int
OrbitalElement::revolutionNumber() const {
return _revolutionNumber;
}
OrbitalElement
OrbitalElement::fromCelesTrak(const QJsonObject &obj) {
OrbitalElement el;
el._name = obj.value("OBJECT_NAME").toString();
el._epoch = Epoch::parse(obj.value("EPOCH").toString());
el._meanMotion = obj.value("MEAN_MOTION").toDouble();
el._meanMotionDerivative = obj.value("MEAN_MOTION_DOT").toDouble();
el._inclination = obj.value("INCLINATION").toDouble();
el._ascension = obj.value("RA_OF_ASC_NODE").toDouble();
el._eccentricity = obj.value("ECCENTRICITY").toDouble();
el._perigee = obj.value("ARG_OF_PERICENTER").toDouble();
el._meanAnomaly = obj.value("MEAN_ANOMALY").toDouble();
el._revolutionNumber = obj.value("REV_AT_EPOCH").toInt();
el._id = obj.value("NORAD_CAT_ID").toInt();
return el;
}
/* ********************************************************************************************* *
* Implementation of OrbitalElementsDatabase
* ********************************************************************************************* */
OrbitalElementsDatabase::OrbitalElementsDatabase(bool autoLoad, unsigned int updatePeriod, QObject *parent)
: QAbstractTableModel{parent}, _updatePeriod(updatePeriod), _elements(), _network()
{
connect(&_network, SIGNAL(finished(QNetworkReply*)),
this, SLOT(downloadFinished(QNetworkReply*)));
if (autoLoad) load();
}
bool
OrbitalElementsDatabase::contains(unsigned int id) const {
return _idIndexMap.contains(id);
}
OrbitalElement
OrbitalElementsDatabase::getById(unsigned int id) const {
return _elements.at(_idIndexMap.value(id));
}
const OrbitalElement &
OrbitalElementsDatabase::getAt(unsigned int idx) const {
return _elements[idx];
}
OrbitalElement &
OrbitalElementsDatabase::getAt(unsigned int idx) {
return _elements[idx];
}
int
OrbitalElementsDatabase::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return _elements.size();
}
int
OrbitalElementsDatabase::columnCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return 3;
}
QVariant
OrbitalElementsDatabase::data(const QModelIndex &index, int role) const {
if (index.row() >= _elements.size())
return QVariant();
if (Qt::DisplayRole == role) {
if (0 == index.column())
return _elements.at(index.row()).id();
if (1 == index.column())
return _elements.at(index.row()).name();
if (2 == index.column())
return _elements.at(index.row()).epoch().toString();
}
return QVariant();
}
QVariant
OrbitalElementsDatabase::headerData(int section, Qt::Orientation orientation,
int role) const {
if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
return QVariant();
switch (section) {
case 0: return QStringLiteral("NORAD");
case 1: return QStringLiteral("Name");
case 2: return QStringLiteral("Epoch");
}
return QVariant();
}
unsigned
OrbitalElementsDatabase::dbAge() const {
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/elements.json";
QFileInfo info(path);
if (! info.exists())
return -1;
return info.lastModified().daysTo(QDateTime::currentDateTime());
}
void
OrbitalElementsDatabase::load() {
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/elements.json";
if ((! load(path)) || (_updatePeriod < dbAge()))
download();
}
bool
OrbitalElementsDatabase::load(const QString &filename) {
QFile file(filename);
if (! file.open(QIODevice::ReadOnly)) {
QString msg = QString("Cannot open orbital elements '%1': %2").arg(filename).arg(file.errorString());
logError() << msg;
emit error(msg);
return false;
}
QByteArray data = file.readAll();
file.close();
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(data, &err);
if (doc.isEmpty()) {
QString msg = "Failed to load orbital elements: " + err.errorString();
logError() << msg;
emit error(msg);
return false;
}
if (! doc.isArray()) {
QString msg = "Failed to load orbital elements: JSON document is not an array!";
logError() << msg;
emit error(msg);
return false;
}
beginResetModel();
QJsonArray array = doc.array();
_elements.clear();
_elements.reserve(array.size());
for (int i=0; i<array.size(); i++) {
OrbitalElement element = OrbitalElement::fromCelesTrak(array.at(i).toObject());
if (element.isValid()) {
_elements.append(element);
}
}
// Sort repeater w.r.t. their IDs
std::stable_sort(_elements.begin(), _elements.end(),
[](const OrbitalElement &a, const OrbitalElement &b){ return a.id() < b.id(); });
_idIndexMap.clear();
for (unsigned int i=0; i<(unsigned int)_elements.size(); i++)
_idIndexMap.insert(_elements.at(i).id(), i);
// Done.
endResetModel();
logDebug() << "Loaded orbital elements with " << _elements.size() << " entries from " << filename << ".";
emit loaded();
return true;
}
void
OrbitalElementsDatabase::download() {
QUrl url("https://celestrak.org/NORAD/elements/gp.php?GROUP=amateur&FORMAT=json");
QNetworkRequest request(url);
_network.get(request);
}
void
OrbitalElementsDatabase::downloadFinished(QNetworkReply *reply) {
if (reply->error()) {
QString msg = QString("Cannot download orbital elements: %1").arg(reply->errorString());
logError() << msg;
emit error(msg);
return;
}
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QFile file(path+"/elements.json");
QDir directory;
if ((! directory.exists(path)) && (!directory.mkpath(path))) {
QString msg = QString("Cannot create path '%1'.").arg(path);
logError() << msg;
emit error(msg);
return;
}
if (! file.open(QIODevice::WriteOnly)) {
QString msg = QString("Cannot save orbital elements at '%1'.").arg(file.fileName());
logError() << msg;
emit error(msg);
return;
}
file.write(reply->readAll());
file.flush();
file.close();
load();
reply->deleteLater();
}
|