// Part of BNC, a utility for retrieving decoding and
// converting GNSS data streams from NTRIP broadcasters.
//
// Copyright (C) 2007
// German Federal Agency for Cartography and Geodesy (BKG)
// http://bkg.bund.de
// Czech Technical University Prague, Department of Geodesy
// http://www.fsv.cvut.cz
//
// Email: euref-ip@bkg.bund.de
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

/* -------------------------------------------------------------------------
 * BKG NTRIP Client
 * -------------------------------------------------------------------------
 *
 * Class:      t_bncCore
 *
 * Purpose:    This class implements the main application
 *
 * Author:     L. Mervart
 *
 * Created:    29-Aug-2006
 *
 * Changes:
 *
 * -----------------------------------------------------------------------*/

#include <iostream>
#include <sstream>
#include <QMessageBox>
#include <cmath>

#include "bnccore.h"
#include "ssrQc.h"
#include "bncutils.h"
#include "bncrinex.h"
#include "bncsettings.h"
#include "bncversion.h"
#include "ephemeris.h"
#include "rinex/rnxobsfile.h"
#include "rinex/rnxnavfile.h"
#include "pppMain.h"
#include "combination/bnccomb.h"

using namespace std;

// Singleton
////////////////////////////////////////////////////////////////////////////
t_bncCore* t_bncCore::instance() {
  static t_bncCore _bncCore;
  return &_bncCore;
}

// Constructor
////////////////////////////////////////////////////////////////////////////
t_bncCore::t_bncCore() : _ephUser(false) {
  _logFileFlag = 0;
  _logFile     = 0;
  _logStream   = 0;
  _logFileStructured  = 0;
  _logFormatStructured = t_logRecord::json;
  _logLevelStructured  = t_logRecord::debug;
  _rawFile     = 0;
  _caster      = 0;

  // Eph file(s)
  // -----------
  _rinexVers         = 0;
  _ephFilePerStation = false;

  _portEph    = 0;
  _serverEph  = 0;
  _socketsEph = 0;

  _portCorr    = 0;
  _serverCorr  = 0;
  _socketsCorr = 0;

  _pgmName  = QString(BNCPGMNAME).leftJustified(20, ' ', true);
#ifdef WIN32
  _userName = QString("${USERNAME}");
#else
  _userName = QString("${USER}");
#endif
  expandEnvVar(_userName);

  _userName       = _userName.leftJustified(20, ' ', true);
  _dateAndTimeGPS = 0;
  _mainWindow     = 0;

  _pppMain = new BNC_PPP::t_pppMain();
  _ssrQc   = 0;
  qRegisterMetaType< QVector<double> >      ("QVector<double>");
  qRegisterMetaType<bncTime>                ("bncTime");
  qRegisterMetaType<t_logRecord>            ("t_logRecord");
  qRegisterMetaType<t_ephGPS>               ("t_ephGPS");
  qRegisterMetaType<t_ephGlo>               ("t_ephGlo");
  qRegisterMetaType<t_ephGal>               ("t_ephGal");
  qRegisterMetaType<t_ephSBAS>              ("t_ephSBAS");
  qRegisterMetaType<t_ephBDS>               ("t_ephBDS");
  qRegisterMetaType<QList<t_orbCorr> >      ("QList<t_orbCorr>");
  qRegisterMetaType<QList<t_clkCorr> >      ("QList<t_clkCorr>");
  qRegisterMetaType<QList<t_satCodeBias> >  ("QList<t_satCodeBias>");
  qRegisterMetaType<QList<t_satPhaseBias> > ("QList<t_satPhaseBias>");
  qRegisterMetaType<t_vTec>                 ("t_vTec");
  qRegisterMetaType<t_metaData>              ("t_metaData");
  qRegisterMetaType<QList<t_satAntenna> >   ("QList<t_satAntenna>");
}

// Destructor
////////////////////////////////////////////////////////////////////////////
t_bncCore::~t_bncCore() {
  delete _logStream; _logStream = 0;
  delete _logFile;   _logFile   = 0;
  delete _logFileStructured; _logFileStructured = 0;
  qDeleteAll(_ephFiles);
  delete _serverEph;
  delete _socketsEph;
  delete _serverCorr;
  delete _socketsCorr;
  delete _dateAndTimeGPS;
  if (_rawFile) {
    delete _rawFile;
  }

  bncComb::destruct();
  delete _ssrQc;

  delete _pppMain;
}

// Write a Program Message
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotMessage(QByteArray msg, bool showOnScreen) {
  if (msg.isEmpty()) {
    return;
  }
  slotLogRecord(t_logRecord::fromMessage(msg), showOnScreen);
}

// Write a Program Message of a component with an explicit level
////////////////////////////////////////////////////////////////////////////
void t_bncCore::message(const QByteArray& msg, bool showOnScreen, const QByteArray& component,
                        t_logRecord::e_level level) {
  if (msg.isEmpty()) {
    return;
  }
  slotLogRecord(t_logRecord::message(level, msg, component), showOnScreen);
}

// Write a Program Message of a component
////////////////////////////////////////////////////////////////////////////
void t_bncCore::message(const QByteArray& msg, bool showOnScreen, const QByteArray& component) {
  if (msg.isEmpty()) {
    return;
  }
  slotLogRecord(t_logRecord::fromMessage(msg, component), showOnScreen);
}

// Write a Program Message with structured fields (structured logfile);
// the classic logfile and the screen get the text rec._msg
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotLogRecord(t_logRecord rec, bool showOnScreen) {
  if (rec._msg.isEmpty()) {
    return;
  }
  QMutexLocker locker(&_mutexMessage);

  messagePrivate(rec);
  emit newMessage(rec._msg, showOnScreen);
}

// Write a Program Message (private, no lock)
////////////////////////////////////////////////////////////////////////////
void t_bncCore::messagePrivate(const t_logRecord& rec) {

  // First time resolve the log file names
  // -------------------------------------
  QDateTime currDateTime = currentDateAndTimeGPS();
  QDate currDate = currDateTime.date();
  if (_logFileFlag == 0 || _fileDate != currDate) {
    delete _logStream; _logStream = 0;
    delete _logFile;   _logFile   = 0;
    delete _logFileStructured; _logFileStructured = 0;
    _logFileFlag = 1;
    _fileDate = currDate;
    bncSettings settings;
    bool append = (Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked);
    QString logFileName = settings.value("logFile").toString();
    if ( !logFileName.isEmpty() ) {
      expandEnvVar(logFileName);
      _logFile = new QFile(logFileName + "_" +
                          currDate.toString("yyMMdd").toLatin1().data());
      if (append) {
        _logFile->open(QIODevice::WriteOnly | QIODevice::Append);
      }
      else {
        _logFile->open(QIODevice::WriteOnly);
      }
      _logStream = new QTextStream();
      _logStream->setDevice(_logFile);
    }
    QString structName = settings.value("logFileStructured").toString();
    if ( !structName.isEmpty() ) {
      expandEnvVar(structName);
      _logFormatStructured = t_logRecord::formatFromString(
                               settings.value("logFormatStructured").toString());
      _logLevelStructured  = t_logRecord::levelFromString(
                               settings.value("logLevelStructured").toString());
      _logFileStructured = new QFile(structName + "_" + currDate.toString("yyMMdd"));
      if (!_logFileStructured->open(append ? (QIODevice::WriteOnly | QIODevice::Append)
                                           : QIODevice::WriteOnly)) {
        delete _logFileStructured; _logFileStructured = 0;
      }
    }
  }

  if (_logStream) {
    QByteArray msgLocal = rec._msg;
    if (rec._msg.indexOf('\n') == 0) {
      *_logStream << "\r\n";
      msgLocal = rec._msg.mid(1);
    }
    *_logStream << currDateTime.toString("yy-MM-dd hh:mm:ss ").toLatin1().data();
    *_logStream << msgLocal.data() << "\r\n";
    _logStream->flush();
    _logFile->flush();
  }

  if (_logFileStructured && rec._level >= _logLevelStructured) {
    _logFileStructured->write(rec.encode(_logFormatStructured, currDateTime) + "\n");
    _logFileStructured->flush();
  }
}

//
////////////////////////////////////////////////////////////////////////////
t_irc t_bncCore::checkPrintEph(t_eph* eph) {
  QMutexLocker locker(&_mutex);
  t_irc ircPut = _ephUser.putNewEph(eph, true);
#ifdef BNC_DEBUG_BCE
  if (eph->checkState() == t_eph::unhealthy) {
    messagePrivate(t_logRecord::debugMessage("bce", QString("%1: UNHEALTHY %2:%3")
                   .arg(eph->receptStaID())
                   .arg(eph->typeStr(eph->type(), eph->prn(), 99.0))
                   .arg(eph->rinexDateStr(eph->TOC(), eph->prn(), 99.0)).toLatin1()));
  }
  if (eph->checkState() == t_eph::bad) {
    messagePrivate(t_logRecord::debugMessage("bce", QString("%1: WRONG %2:%3")
                   .arg(eph->receptStaID())
                   .arg(eph->typeStr(eph->type(), eph->prn(), 99.0))
                   .arg(eph->rinexDateStr(eph->TOC(), eph->prn(), 99.0)).toLatin1()));
  }
  if (eph->checkState() == t_eph::outdated) {
    messagePrivate(t_logRecord::debugMessage("bce", QString("%1: OUTDATED %2:%3")
                   .arg(eph->receptStaID())
                   .arg(eph->typeStr(eph->type(), eph->prn(), 99.0))
                   .arg(eph->rinexDateStr(eph->TOC(), eph->prn(), 99.0)).toLatin1()));
  }
#endif
  t_ephFile* ephFile = printEphHeader(eph->receptStaID());
  bool printFile = (ircPut == success);
  if (ephFile && ephFile->_ephUser) {
    // file per stream: new ephemerides of this stream, independent of other streams;
    // the check state of the emitted ephemeris stays that of the common check
    t_eph::e_checkState checkState = eph->checkState();
    printFile = (ephFile->_ephUser->putNewEph(eph, true) == success);
    eph->setCheckState(checkState);
  }
  printEph(ephFile, *eph, printFile);
  return success;
}

// New GPS Ephemeris
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewGPSEph(t_ephGPS eph) {
  if (checkPrintEph(&eph) == success) {
    emit newGPSEph(eph);
  }
}

// New Glonass Ephemeris
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewGlonassEph(t_ephGlo eph) {
  if (checkPrintEph(&eph) == success) {
    emit newGlonassEph(eph);
  }
}

// New Galileo Ephemeris
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewGalileoEph(t_ephGal eph) {
  if (checkPrintEph(&eph) == success) {
    emit newGalileoEph(eph);
  }
}

// New SBAS Ephemeris
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewSBASEph(t_ephSBAS eph) {
  if (checkPrintEph(&eph) == success) {
    emit newSBASEph(eph);
  }
}

// New BDS Ephemeris
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewBDSEph(t_ephBDS eph) {
  if (checkPrintEph(&eph) == success) {
    emit newBDSEph(eph);
  }
}

// Close RINEX Navigation File(s)
////////////////////////////////////////////////////////////////////////////
void t_bncCore::t_ephFile::close() {
  delete _streamN; _streamN = 0;
  delete _fileN;   _fileN   = 0;
  delete _streamG; _streamG = 0;
  delete _fileG;   _fileG   = 0;
}

// Destructor
////////////////////////////////////////////////////////////////////////////
t_bncCore::t_ephFile::~t_ephFile() {
  close();
  delete _ephUser;
}

// Station part of a RINEX navigation file name: "BRDC" (+ "00WRD" in RINEX 3/4)
// for the single file, otherwise derived from the stream like the observation files
////////////////////////////////////////////////////////////////////////////
QString t_bncCore::ephFileNameBase(const QString& staID, const QString& country) const {
  QString ID = staID.isEmpty() ? QString("BRDC") : staID.left(4).toUpper();
  if (_rinexVers <= 2) {
    return ID;
  }
  if (staID.size() >= 9 &&
      isdigit((unsigned char) staID[4].toLatin1()) &&
      isdigit((unsigned char) staID[5].toLatin1())) {
    return staID.left(9).toUpper();   // e.g. BCEP00BKG0 -> BCEP00BKG
  }
  return ID + "00" + ((!staID.isEmpty() && country.size() >= 3) ? country.left(3).toUpper()
                                                                : QString("WRD")); // WORLD
}

// Print Header of the output File(s), returns the file of the stream
////////////////////////////////////////////////////////////////////////////
t_bncCore::t_ephFile* t_bncCore::printEphHeader(QString receptStaID) {

  bncSettings settings;

  // Initialization
  // --------------
  if (_rinexVers == 0) {

    _rinexVers = settings.value("ephVersion").toInt();

    _ephFilePerStation = Qt::CheckState(settings.value("ephFilePerStation").toInt()) == Qt::Checked;

    _ephPath = settings.value("ephPath").toString();

    if ( !_ephPath.isEmpty() ) {
      if ( _ephPath[_ephPath.length()-1] != QDir::separator() ) {
        _ephPath += QDir::separator();
      }
      expandEnvVar(_ephPath);
    }
  }

  if (_ephPath.isEmpty()) {
    return 0;
  }

  QString key = _ephFilePerStation ? receptStaID : QString();

  // Source comments and station part of the file name
  // --------------------------------------------------
  QStringList comments;
  QMap<QString, QString> nameBase;  // staID -> station part of its file name
  QListIterator<QString> it(settings.value("mountPoints").toStringList());
  while (it.hasNext()) {
    QStringList hlp = it.next().split(" ");
    if (hlp.size() < 7)
      continue;
    QUrl url(hlp[0]);
    QString decoder = hlp[1];
    QString staID   = url.path().mid(1);
    if (key.isEmpty() || staID == key) {
      comments.append("Source: " + decoder +
                      " " + QUrl::toAce(url.host()) +
                      "/" + url.path().mid(1).toLatin1());
    }
    nameBase[staID] = ephFileNameBase(staID, hlp[2]);
  }

  QString base = (!key.isEmpty() && nameBase.contains(key)) ? nameBase[key]
                                                           : ephFileNameBase(key, QString());
  QString distStr;
  if (!key.isEmpty()) {
    QMapIterator<QString, QString> im(nameBase);
    while (im.hasNext()) {
      im.next();
      if (im.key() != key && im.value() == base) { // name conflict with another stream
        distStr = "_" + key;
        break;
      }
    }
  }

  t_ephFile*& ephFile = _ephFiles[key];
  if (!ephFile) {
    ephFile = new t_ephFile;
    if (_ephFilePerStation) {
      ephFile->_ephUser = new bncEphUser(false);
    }
  }

  // (Re-)Open output File(s)
  // ------------------------
  QDateTime datTim = currentDateAndTimeGPS();

  QString hlpStr = bncRinex::nextEpochStr(datTim,
                       settings.value("ephIntr").toString(), _rinexVers);

  QString ephFileNameGPS = _ephPath + base;

  if (_rinexVers > 2) {
    ephFileNameGPS += "_S_" +     // stream
                      QString("%1").arg(datTim.date().year()) +
                      QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
                      hlpStr +   // HM_period
                      "_MN" +    // mixed BRDC
                      distStr + ".rnx";
  }
  else { // RNX v2.11
    ephFileNameGPS += QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
                      hlpStr + distStr + datTim.toString(".yyN");
  }

  if (ephFile->_name == ephFileNameGPS) {
    return ephFile;
  }
  ephFile->_name = ephFileNameGPS;
  ephFile->close();

  QFlags<QIODevice::OpenModeFlag> appendFlagGPS;
  QFlags<QIODevice::OpenModeFlag> appendFlagGlonass;

  if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
       QFile::exists(ephFileNameGPS) ) {
    appendFlagGPS = QIODevice::Append;
  }

  ephFile->_fileN = new QFile(ephFileNameGPS);
  ephFile->_fileN->open(QIODevice::WriteOnly | appendFlagGPS);
  ephFile->_streamN = new QTextStream();
  ephFile->_streamN->setDevice(ephFile->_fileN);
  QTextStream* streamGPS = ephFile->_streamN;

  if (_rinexVers == 2) {
    QString ephFileNameGlonass = _ephPath + base +
        QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
        hlpStr + distStr + datTim.toString(".yyG");

    if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
         QFile::exists(ephFileNameGlonass) ) {
      appendFlagGlonass = QIODevice::Append;
    }

    ephFile->_fileG = new QFile(ephFileNameGlonass);
    ephFile->_fileG->open(QIODevice::WriteOnly | appendFlagGlonass);
    ephFile->_streamG = new QTextStream();
    ephFile->_streamG->setDevice(ephFile->_fileG);
  }
  QTextStream* streamGlonass = ephFile->_streamG;

  // Header - RINEX Version 3
  // ------------------------
  if (_rinexVers > 2) {
    if ( ! (appendFlagGPS & QIODevice::Append)) {
      double rinexVersion = defaultRnxNavVersion4;
      if (_rinexVers < 4.0) {
        rinexVersion = defaultRnxNavVersion3;
      }
      QString line = QString().asprintf("%9.2f%11sN: GNSS NAV DATA    M: Mixed%12sRINEX VERSION / TYPE\n",
          rinexVersion, "", "");
      *streamGPS << line;
      line.clear();

      QString hlp = currentDateAndTimeGPS().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
      *streamGPS << _pgmName.toLatin1().data()
                 << _userName.toLatin1().data()
                 << hlp.toLatin1().data()
                 << "PGM / RUN BY / DATE\n";

      QStringListIterator it(comments);
      while (it.hasNext()) {
        *streamGPS << it.next().trimmed().left(60).leftJustified(60) << "COMMENT\n";
      }

      if (_rinexVers == 4) {
        int leapSecs = gnumleap(datTim.date().year(), datTim.date().month(), datTim.date().day());
        *streamGPS << QString("%1").arg(leapSecs, 6, 10, QLatin1Char(' ')).left(60).leftJustified(60)
                   << "LEAP SECONDS\n";
      }

      line = QString().asprintf("%60sEND OF HEADER\n", "");
      *streamGPS << line;

      streamGPS->flush();
    }
  }

  // Headers - RINEX Version 2
  // -------------------------
  else {
    if (! (appendFlagGPS & QIODevice::Append)) {
      QString line = QString().asprintf("%9.2f%11sN: GPS NAV DATA%25sRINEX VERSION / TYPE\n",
                   defaultRnxNavVersion2, "", "");
      *streamGPS << line;
      line.clear();

      QString hlp = currentDateAndTimeGPS().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
      *streamGPS << _pgmName.toLatin1().data()
                 << _userName.toLatin1().data()
                 << hlp.toLatin1().data()
                 << "PGM / RUN BY / DATE\n";

      QStringListIterator it(comments);
      while (it.hasNext()) {
        *streamGPS << it.next().trimmed().left(60).leftJustified(60) << "COMMENT\n";
      }

      line = QString().asprintf("%60sEND OF HEADER\n", "");
      *streamGPS << line;

      streamGPS->flush();
    }
    if (! (appendFlagGlonass & QIODevice::Append)) {
      QString line = QString().asprintf("%9.2f%11sG: GLONASS NAV DATA%21sRINEX VERSION / TYPE\n",
                   defaultRnxNavVersion2, "", "");
      *streamGlonass << line;
      line.clear();

      QString hlp = currentDateAndTimeGPS().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
      *streamGlonass << _pgmName.toLatin1().data()
                     << _userName.toLatin1().data()
                     << hlp.toLatin1().data()
                     << "PGM / RUN BY / DATE\n";

      QStringListIterator it(comments);
      while (it.hasNext()) {
        *streamGlonass << it.next().trimmed().left(60).leftJustified(60) << "COMMENT\n";
      }

      line = QString().asprintf("%60sEND OF HEADER\n", "");
      *streamGlonass << line;

      streamGlonass->flush();
    }
  }

  return ephFile;
}

// Print One Ephemeris
////////////////////////////////////////////////////////////////////////////
void t_bncCore::printEph(t_ephFile* ephFile, const t_eph& eph, bool printFile) {

  QString strV2 = eph.toString(defaultRnxNavVersion2);
  QString strV3 = eph.toString(defaultRnxNavVersion3);
  QString strV4 = eph.toString(defaultRnxNavVersion4);

  QTextStream* streamN = ephFile ? ephFile->_streamN : 0;
  QTextStream* streamG = ephFile ? ephFile->_streamG : 0;

  if      (_rinexVers == 2 && eph.system() == t_eph::GLONASS) {
    printOutputEph(printFile, streamG, strV2, strV3, strV4);
  }
  else if (_rinexVers == 2 && eph.system() == t_eph::GPS) {
    printOutputEph(printFile, streamN, strV2, strV3, strV4);
  }
  else if (_rinexVers >= 3) {
    printOutputEph(printFile, streamN, strV2, strV3, strV4);
  }
}

// Output
////////////////////////////////////////////////////////////////////////////
void t_bncCore::printOutputEph(bool printFile, QTextStream* stream,
                               const QString& strV2, const QString& strV3,
                               const QString& strV4) {
  QString strVx;
  if     (_rinexVers == 2) {
    strVx = strV2;
  }
  else if (_rinexVers == 3) {
    strVx = strV3;
  }
  else if (_rinexVers == 4) {
    strVx = strV4;
  }

  // Output into file
  // ----------------
  if (printFile && stream) {
    *stream << strVx.toLatin1();
    stream->flush();
  }

  // Output into the socket
  // ----------------------
  if (_socketsEph) {
    QMutableListIterator<QTcpSocket*> is(*_socketsEph);
    while (is.hasNext()) {
      QTcpSocket* sock = is.next();
      if (sock->state() == QAbstractSocket::ConnectedState) {
        if (sock->write(strVx.toLatin1()) == -1) {
          delete sock;
          is.remove();
        }
      }
      else if (sock->state() != QAbstractSocket::ConnectingState) {
        delete sock;
        is.remove();
      }
    }
  }
}

// Set Port Number
////////////////////////////////////////////////////////////////////////////
void t_bncCore::setPortEph(int port) {
  _portEph = port;
  if (_portEph != 0) {
    delete _serverEph;
    _serverEph = new QTcpServer;
    _serverEph->setProxy(QNetworkProxy::NoProxy);
    if ( !_serverEph->listen(QHostAddress::LocalHost, _portEph) ) {
      QString message = "t_bncCore: Cannot listen on ephemeris port "
                      + QByteArray::number(_portEph) + ": "
                      + _serverEph->errorString();
      slotLogRecord(t_logRecord::message(t_logRecord::error, message.toLatin1(), "bnc"), true);
    }
    connect(_serverEph, SIGNAL(newConnection()), this, SLOT(slotNewConnectionEph()));
    delete _socketsEph;
    _socketsEph = new QList<QTcpSocket*>;
  }
}

// Set Port Number
////////////////////////////////////////////////////////////////////////////
void t_bncCore::setPortCorr(int port) {
  _portCorr = port;
  if (_portCorr != 0) {
    delete _serverCorr;
    _serverCorr = new QTcpServer;
    _serverCorr->setProxy(QNetworkProxy::NoProxy);
    if ( !_serverCorr->listen(QHostAddress::LocalHost, _portCorr) ) {
      QString message = "t_bncCore: Cannot listen on correction port "
                      + QByteArray::number(_portCorr) + ": "
                      + _serverCorr->errorString();
      slotLogRecord(t_logRecord::message(t_logRecord::error, message.toLatin1(), "bnc"), true);
    }
    connect(_serverCorr, SIGNAL(newConnection()), this, SLOT(slotNewConnectionCorr()));
    delete _socketsCorr;
    _socketsCorr = new QList<QTcpSocket*>;
  }
}

// New Connection
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewConnectionEph() {
  _socketsEph->push_back( _serverEph->nextPendingConnection() );
}

// New Connection
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewConnectionCorr() {
  _socketsCorr->push_back( _serverCorr->nextPendingConnection() );
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotQuit() {
  delete _caster; _caster = 0;
  qApp->quit();
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewOrbCorrections(QList<t_orbCorr> orbCorrections) {
  QMutexLocker locker(&_mutex);
  emit newOrbCorrections(orbCorrections);
  if (_socketsCorr) {
    ostringstream out;
    t_orbCorr::writeEpoch(&out, orbCorrections);
    QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
    while (is.hasNext()) {
      QTcpSocket* sock = is.next();
      if (sock->state() == QAbstractSocket::ConnectedState) {
        if (sock->write(out.str().c_str()) == -1) {
          delete sock;
          is.remove();
        }
      }
      else if (sock->state() != QAbstractSocket::ConnectingState) {
        delete sock;
        is.remove();
      }
    }
  }
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewClkCorrections(QList<t_clkCorr> clkCorrections) {
  QMutexLocker locker(&_mutex);
  emit newClkCorrections(clkCorrections);
  if (_socketsCorr) {
    ostringstream out;
    t_clkCorr::writeEpoch(&out, clkCorrections);
    QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
    while (is.hasNext()) {
      QTcpSocket* sock = is.next();
      if (sock->state() == QAbstractSocket::ConnectedState) {
        if (sock->write(out.str().c_str()) == -1) {
          delete sock;
          is.remove();
        }
      }
      else if (sock->state() != QAbstractSocket::ConnectingState) {
        delete sock;
        is.remove();
      }
    }
  }
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewCodeBiases(QList<t_satCodeBias> codeBiases) {
  QMutexLocker locker(&_mutex);
  emit newCodeBiases(codeBiases);
  if (_socketsCorr) {
    ostringstream out;
    t_satCodeBias::writeEpoch(&out, codeBiases);
    QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
    while (is.hasNext()) {
      QTcpSocket* sock = is.next();
      if (sock->state() == QAbstractSocket::ConnectedState) {
        if (sock->write(out.str().c_str()) == -1) {
          delete sock;
          is.remove();
        }
      }
      else if (sock->state() != QAbstractSocket::ConnectingState) {
        delete sock;
        is.remove();
      }
    }
  }
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewPhaseBiases(QList<t_satPhaseBias> phaseBiases) {
  QMutexLocker locker(&_mutex);
  emit newPhaseBiases(phaseBiases);
  if (_socketsCorr) {
    ostringstream out;
    t_satPhaseBias::writeEpoch(&out, phaseBiases);
    QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
    while (is.hasNext()) {
      QTcpSocket* sock = is.next();
      if (sock->state() == QAbstractSocket::ConnectedState) {
        if (sock->write(out.str().c_str()) == -1) {
          delete sock;
          is.remove();
        }
      }
      else if (sock->state() != QAbstractSocket::ConnectingState) {
        delete sock;
        is.remove();
      }
    }
  }
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewTec(t_vTec vTec) {
  QMutexLocker locker(&_mutex);
  emit newTec(vTec);
  if (_socketsCorr) {
    ostringstream out;
    t_vTec::write(&out, vTec);
    QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
    while (is.hasNext()) {
      QTcpSocket* sock = is.next();
      if (sock->state() == QAbstractSocket::ConnectedState) {
        if (sock->write(out.str().c_str()) == -1) {
          delete sock;
          is.remove();
        }
      }
      else if (sock->state() != QAbstractSocket::ConnectingState) {
        delete sock;
        is.remove();
      }
    }
  }
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewMetaData(t_metaData metaData) {
  QMutexLocker locker(&_mutex);
  emit newMetaData(metaData);
  if (_socketsCorr) {
    ostringstream out;
    t_metaData::write(&out, metaData);
    QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
    while (is.hasNext()) {
      QTcpSocket* sock = is.next();
      if (sock->state() == QAbstractSocket::ConnectedState) {
        if (sock->write(out.str().c_str()) == -1) {
          delete sock;
          is.remove();
        }
      }
      else if (sock->state() != QAbstractSocket::ConnectingState) {
        delete sock;
        is.remove();
      }
    }
  }
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::slotNewSatAntennas(QList<t_satAntenna> satAntennas) {
  QMutexLocker locker(&_mutex);
  emit newSatAntennas(satAntennas);
  if (_socketsCorr) {
    ostringstream out;
    t_satAntenna::writeEpoch(&out, satAntennas);
    QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
    while (is.hasNext()) {
      QTcpSocket* sock = is.next();
      if (sock->state() == QAbstractSocket::ConnectedState) {
        if (sock->write(out.str().c_str()) == -1) {
          delete sock;
          is.remove();
        }
      }
      else if (sock->state() != QAbstractSocket::ConnectingState) {
        delete sock;
        is.remove();
      }
    }
  }
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::setConfFileName(const QString& confFileName) {
  if (confFileName.isEmpty()) {
    _confFileName = QDir::homePath() + QDir::separator()
                  + ".config" + QDir::separator()
                  + qApp->organizationName() + QDir::separator()
                  + qApp->applicationName() + ".bnc";
  }
  else {
    _confFileName = confFileName;
  }
}

// Raw Output
////////////////////////////////////////////////////////////////////////////
void t_bncCore::writeRawData(const QByteArray& data, const QByteArray& staID,
                          const QByteArray& format) {

  QMutexLocker locker(&_mutex);

  if (!_rawFile) {
    bncSettings settings;
    QByteArray fileName = settings.value("rawOutFile").toByteArray();
    if (!fileName.isEmpty()) {
      _rawFile = new bncRawFile(fileName, staID, bncRawFile::output);
    }
  }

  if (_rawFile) {
    _rawFile->writeRawData(data, staID, format);
  }
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::initCombination() {
  bncComb::getInstance();
  if (BNC_CMB->nStreams() < 1) {
    bncComb::destruct();
  }
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::stopCombination() {
  bncComb::destruct();
}

// SSR QC of the decoded correction streams (optional)
////////////////////////////////////////////////////////////////////////////
void t_bncCore::initSsrQc() {
  delete _ssrQc; _ssrQc = 0;
  bncSettings settings;
  QString dirName = settings.value("ssrQcPath").toString();
  if (dirName.isEmpty()) {
    return;
  }
  expandEnvVar(dirName);
  QStringList streams = settings.value("ssrQcStreams").toString().split(QRegularExpression("[ ,;]+"), Qt::SkipEmptyParts);
  if (streams.isEmpty()) {
    streams << "ALL";
  }
  QStringList hlp = settings.value("ssrQcIntr").toString().split(' ', Qt::SkipEmptyParts);
  double interval = 3600.0;
  if (hlp.size() == 2) {
    double fac = (hlp[1] == "min") ? 60.0 : (hlp[1] == "hour") ? 3600.0 : (hlp[1] == "day") ? 86400.0 : 0.0;
    if (hlp[0].toDouble() * fac > 0.0) {
      interval = hlp[0].toDouble() * fac;
    }
  }
  _ssrQc = new t_ssrQc(dirName, interval, streams, settings.value("ssrQcRef").toString().trimmed());
}

// Writes the final SSR QC report
////////////////////////////////////////////////////////////////////////////
void t_bncCore::stopSsrQc() {
  delete _ssrQc; _ssrQc = 0;
}

//
////////////////////////////////////////////////////////////////////////////
bool t_bncCore::dateAndTimeGPSSet() const {
  QMutexLocker locker(&_mutexDateAndTimeGPS);
  if (_dateAndTimeGPS) {
    return true;
  }
  else {
    return false;
  }
}

//
////////////////////////////////////////////////////////////////////////////
QDateTime t_bncCore::dateAndTimeGPS() const {
  QMutexLocker locker(&_mutexDateAndTimeGPS);
  if (_dateAndTimeGPS) {
    return *_dateAndTimeGPS;
  }
  else {
    return QDateTime();
  }
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::setDateAndTimeGPS(QDateTime dateTime) {
  QMutexLocker locker(&_mutexDateAndTimeGPS);
  delete _dateAndTimeGPS;
  _dateAndTimeGPS = new QDateTime(dateTime);
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::startPPP() {
  _pppMain->start();
}

//
////////////////////////////////////////////////////////////////////////////
void t_bncCore::stopPPP() {
  _pppMain->stop();
  emit stopRinexPPP();
}

