Commit f5b19539 authored by Summer Stapleton's avatar Summer Stapleton Committed by Makayla Shepherd
Browse files

Cleaned up the code a bit

parent 8dc3265f
Loading
Loading
Loading
Loading
+5 −157
Original line number Diff line number Diff line
@@ -56,12 +56,11 @@ namespace Isis {
    p_rawResponse = NULL;
    p_error = NULL;

    QString version = Application::Version();
    QByteArray isisVersionRaw(version.toLatin1());
    // QString version = Application::Version();
    // QByteArray isisVersionRaw(version.toLatin1());

    stringstream labelStream;
    labelStream << cubeLabel;
    //QString labelText = QString( QByteArray( labelStream.str().c_str() ).toHex().constData() );
    QString labelText = QString::fromStdString(labelStream.str());

    QJsonObject properties {
@@ -78,24 +77,14 @@ namespace Isis {
      {"endPad", endPad}
    };

    //I can't see any reason for this, so commenting out to make json file simpler
    //QJsonObject object;
    //object.insert("Properties", properties);

    p_jsonDocument = new QJsonDocument(properties);

    // QFile finalOutput("output.txt");
    // finalOutput.open(QIODevice::WriteOnly);
    // finalOutput.write( p_jsonDocument->toJson() );
    // finalOutput.close();

    p_request = new QNetworkRequest();

    QUrl qurl(url);

    qurl.setPort(port);
    p_request->setUrl(qurl);
    //p_request->setRawHeader("User-Agent", "SpiceInit 1.0");
    p_request->setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    moveToThread(this);
@@ -156,26 +145,14 @@ namespace Isis {
  void SpiceClient::replyFinished(QNetworkReply *reply) {
    QByteArray p_rawResponseByteArray = reply->readAll();

    //qDebug() << "p_rawResponseByteArray:" << p_rawResponseByteArray;

    // Decode the response
    // p_response = new QString();
    p_response = new QJsonObject();

    try {
      // *p_response = QString(
      //     QByteArray::fromHex(QByteArray(p_rawResponse->toLatin1())).constData());

      // QJsonDocument document;

      QJsonDocument doc = QJsonDocument::fromJson(p_rawResponseByteArray);

      *p_response = doc.object();





       QFile finalOutput("output.txt");
       finalOutput.open(QIODevice::WriteOnly);
       finalOutput.write( doc.toJson() );
@@ -365,17 +342,6 @@ namespace Isis {
  }



// QString SpiceClient::jsonToTable(QJsonValue val) {
//   QFile tableFile(file);
//   tableFile.open(QIODevice::ReadOnly);
//   QByteArray data = tableFile.readAll();
//   tableFile.close();
//
//   return QJsonValue::toVariant(data.toHex().constData());
// }


  /**
   * Called if the server requires a authentication
   */
@@ -424,66 +390,6 @@ namespace Isis {
  }


  /**
   * This returns the root of the returned XML, throws an
   * appropriate error if the XML is wrong or missing.
   *
   * @return QDomElement
   */
  // QDomElement SpiceClient::rootXMLElement() {
  //   if(!p_response || !p_rawResponse) {
  //     QString error = "No server response available";
  //     throw IException(IException::Io, error, _FILEINFO_);
  //   }
  //
  //   QDomDocument document;
  //   QString errorMsg;
  //   int errorLine, errorCol;
  //
  //   if(!p_response->isEmpty() &&
  //       document.setContent(QString(p_response->toLatin1()),
  //                           &errorMsg, &errorLine, &errorCol)) {
  //     return document.firstChild().toElement();
  //   }
  //   else if (p_response->isEmpty()){
  //     QString msg = "Empty response returned from server.";
  //     throw IException(IException::Io, msg, _FILEINFO_);
  //   }
  //   else {
  //     QString msg = "Unexpected response from spice server [";
  //     msg += *p_rawResponse;
  //     msg += "]";
  //     throw IException(IException::Io, msg, _FILEINFO_);
  //   }
  // }


  /**
   * This finds a tag (i.e. \<some_tag>) inside the current
   * element.
   *
   * @param currentElement
   * @param name
   *
   * @return QDomElement
   */
  QDomElement SpiceClient::findTag(QDomElement currentElement, QString name) {
    QString qtName = name;
    for(QDomNode node = currentElement.firstChild();
        !node .isNull();
        node = node.nextSibling()) {
      QDomElement element = node.toElement();

      if(element.tagName() == qtName) {
        return element;
      }
    }

    QString msg = "Server response missing XML Tag [" + name + "]";
    throw IException(IException::Io, msg, _FILEINFO_);
  }


  /**
   * This returns the spiceinit'd PvlGroup from the server.
   *
@@ -492,12 +398,6 @@ namespace Isis {
  PvlGroup SpiceClient::kernelsGroup() {
    checkErrors();

    // QDomElement root = rootXMLElement();
    // QDomElement kernelsLabel = findTag(root, "kernels_label");
    // QString kernelsLabels = elementContents(kernelsLabel);
    //
    // QString unencoded(QByteArray::fromHex(kernelsLabels.toLatin1()).constData());

    QString kernVal = p_response->value("Kernels Label").toVariant().toString();
    QString unencoded(QByteArray::fromHex(kernVal.toLatin1()).constData());

@@ -521,38 +421,8 @@ namespace Isis {

    PvlGroup group("group");
    return group;

    // QDomElement root = rootXMLElement();
    // QDomElement logLabel = findTag(root, "application_log");
    // QString logLabels = elementContents(logLabel);
    //
    // QString unencoded(QByteArray::fromHex(logLabels.toLatin1()).constData());
    //
    // stringstream pvlStream;
    // pvlStream << unencoded;
    //
    // Pvl labels;
    // pvlStream >> labels;
    //
    // return labels.findGroup("Kernels", Pvl::Traverse);
  }


  /**
   * This returns the contents of the current element as a string.
   *   \<element>
   *     Contents
   *   \</element>
   *
   * @param element
   *
   * @return QString
   */
  QString SpiceClient::elementContents(QDomElement element) {
    return element.firstChild().toText().data();
  }


  /**
   * This returns the table given by the server. The ownership of the table is
   *   given to the caller.
@@ -587,7 +457,8 @@ namespace Isis {


  /**
   * This returns the table given by the server.
   * This returns the table given by the server. The ownership of the table is
   *   given to the caller.
   */
  Table *SpiceClient::sunPositionTable() {
    return readTable("Sun Position", "SunPosition");
@@ -624,41 +495,18 @@ namespace Isis {
  }


  /**
   * Converts a boolean to "yes" or "no"
   *
   * @param boolVal
   *
   * @return QString
   */
  QString SpiceClient::yesNo(bool boolVal) {
    if(boolVal)
      return "yes";
    else
      return "no";
  }


  Table *SpiceClient::readTable(QString jsonName, QString tableName) {
    checkErrors();

    QString value = p_response->value(jsonName).toString();

    QByteArray decoded = QByteArray::fromHex(value.toUtf8().constData());

    QDataStream decodedStream;

    decodedStream << decoded;

    qDebug() << decoded;

    QFile finalOutput(tableName + ".txt");
    finalOutput.open(QIODevice::WriteOnly);
    finalOutput.write(decoded);
    finalOutput.close();

    Table *table = new Table(tableName,tableName+
                             ".txt");
    Table *table = new Table(tableName,tableName + ".txt");

    return table;
  }
+1 −5
Original line number Diff line number Diff line
@@ -59,11 +59,7 @@ namespace Isis {
      void sslErrors(QNetworkReply *, const QList<QSslError> &);

    private:
      static QString yesNo(bool boolVal);
      Table *readTable(QString xmlName, QString tableName);
      // QDomElement rootXMLElement();
      QDomElement findTag(QDomElement currentElement, QString name);
      QString elementContents(QDomElement element);
      void checkErrors();

    private:
@@ -71,7 +67,7 @@ namespace Isis {
      QJsonDocument *p_jsonDocument; //!< Json file to send to server
      QString *p_rawResponse;  //!< Server raw response
      QJsonObject *p_response;  //!< The QJsonObject constructed from the server response
      QNetworkAccessManager *p_networkMgr; //!< Network manager does request
      QNetworkAccessManager *p_networkMgr; //!< Network manager that makes request
      QNetworkRequest *p_request; //!< Network request sent
  };

+6 −29
Original line number Diff line number Diff line
@@ -87,9 +87,7 @@ void IsisMain() {
  QString mission = missionXlater.Translate("MissionName");

  if (ui.GetBoolean("WEB")) {
    std::cout << "Before Request" << std::endl;
    requestSpice(icube, *icube->label(), mission);
    std::cout << "After Request" << std::endl;
  }
  else {
    // Get system base kernels
@@ -574,16 +572,13 @@ void requestSpice(Cube *icube, Pvl &labels, QString missionName) {
  client.blockUntilComplete();
  connectionProgress.CheckStatus();

  PvlGroup kernelsGroup = client.kernelsGroup(); //
  qDebug() << "SHAPOOPY!!!";


  PvlGroup logGrp = client.applicationLog(); //
  PvlGroup kernelsGroup = client.kernelsGroup();
  PvlGroup logGrp = client.applicationLog();
  PvlObject naifKeywords = client.naifKeywordsObject();
  Table *pointingTable = client.pointingTable(); //
  Table *positionTable = client.positionTable(); //
  Table *bodyTable = client.bodyRotationTable(); //
  Table *sunPosTable = client.sunPositionTable(); //
  Table *pointingTable = client.pointingTable();
  Table *positionTable = client.positionTable();
  Table *bodyTable = client.bodyRotationTable();
  Table *sunPosTable = client.sunPositionTable();

  // Verify everything in the kernels group exists, if not then our kernels are
  //   out of date.
@@ -602,30 +597,12 @@ void requestSpice(Cube *icube, Pvl &labels, QString missionName) {
        curKeyword.name() == "TargetAttitudeShape") {
      continue;
    }

    /*for (int valueIndex = 0; valueIndex < curKeyword.Size(); valueIndex ++) {
      IString value = curKeyword[valueIndex];
      if (value == "Table" || value == "Null")
        continue;

      IString fullFileName = FileName(value).expanded();
      QFile testFile(fullFileName.ToQt());

      if (!testFile.exists()) {
        IString msg = "The spice server says you need the kernel [" +
          value + "] which is not present. " +
          "Please update your spice kernels";
        throw iException::Message(iException::Spice, msg, _FILEINFO_);
      }
    }*/
  }

  Application::Log(logGrp);

  icube->putGroup(kernelsGroup);
  icube->label()->addObject(naifKeywords);
  qDebug() << "YAYA!    " << pointingTable->Records();

  icube->write(*pointingTable);
  icube->write(*positionTable);
  icube->write(*bodyTable);
+9 −55
Original line number Diff line number Diff line
@@ -304,20 +304,19 @@ void RequestHandler::service(HttpRequest& request, HttpResponse& response)
      }
      catch (...) {

        //qDebug() << "Uncaught exception here.";
        // We failed at something, delete the temp files...
//        QString outFile = ui.GetFileName("TO");

        QFile pointingFile("kernels.pointing");
        //if ( pointingFile.exists() ) pointingFile.remove();
        if ( pointingFile.exists() ) pointingFile.remove();

        QFile positionFile("kernels.position");
        //if ( positionFile.exists() ) positionFile.remove();
        if ( positionFile.exists() ) positionFile.remove();

        QFile bodyRotFile("kernels.bodyrot");
        //if ( bodyRotFile.exists() ) bodyRotFile.remove();
        if ( bodyRotFile.exists() ) bodyRotFile.remove();

        QFile sunFile("kernels.sun");
        //if ( sunFile.exists() ) sunFile.remove();
        if ( sunFile.exists() ) sunFile.remove();

        throw;
      }
@@ -542,35 +541,12 @@ QJsonValue tableToJson(QString file) {
  QFile tableFile(file);
  tableFile.open(QIODevice::ReadOnly);
  QByteArray data = tableFile.readAll();

  //xml += QString( data.toHex().constData() ) + "\n";
  tableFile.close();

  return QJsonValue(data.toHex().constData());
}


QString tableToXml(QString tableName, QString file) {
  QString xml;
  xml += "    <" + tableName + ">\n";

  QFile tableFile(file);
  if ( !tableFile.open(QIODevice::ReadOnly) ) {
    QString msg = "Unable to read temporary file [" + file + "]";
    throw IException(IException::Io, msg, _FILEINFO_);
  }

  QByteArray data = tableFile.readAll();
  xml += QString( data.toHex().constData() ) + "\n";
  tableFile.close();
  // we should now be completely done with this temp file
  tableFile.remove();

  xml += "    </" + tableName + ">\n";
  return xml;
}


void parseParameters(QJsonObject jsonObject) {

    g_ckSmithed = jsonObject.value("cksmithed").toBool();
@@ -592,15 +568,12 @@ QByteArray packageKernels(QString toFile) {
  QJsonObject spiceData;

  QString logFile(toFile + ".print");

  Pvl logMessage(logFile);

  QFile::remove(logFile);
  stringstream logStream;

  stringstream logStream;
  logStream << logMessage;


  QString logText = QString( QByteArray( logStream.str().c_str() ).toHex().constData() );
  spiceData.insert("Application Log", QJsonValue::fromVariant(logText));

@@ -610,9 +583,6 @@ QByteArray packageKernels(QString toFile) {
  stringstream labelStream;
  labelStream << kernLabels;




  QString labelText = QString( QByteArray( labelStream.str().c_str() ).toHex().constData() );
  spiceData.insert("Kernels Label", QJsonValue::fromVariant(labelText));
  spiceData.insert("Instrument Pointing", tableToJson(toFile + ".pointing"));
@@ -620,36 +590,20 @@ QByteArray packageKernels(QString toFile) {
  spiceData.insert("Body Rotation", tableToJson(toFile + ".bodyrot"));
  spiceData.insert("Sun Position", tableToJson(toFile + ".sun"));


  QJsonValue pos = spiceData.value("Sun Position");
  QByteArray posArray= QByteArray::fromHex(pos.toString().toUtf8() );
  //qDebug() << posArray;
  QString posString(posArray.constData());
  qDebug() <<"Sun Position" <<posString;




  ///QByteArray pos = QByteArray::fromHex(spiceData.value("Sun Position").toString().constData()).toLocal8Bit();

  //qDebug() << QString(QByteArray::fromHex(spiceData.value("Sun Position").toString().constData() );

  QJsonDocument doc(spiceData);

  QByteArray jsonHexedTables( doc.toJson() );

  QFile finalOutput("finalOutput.txt");
  finalOutput.open(QIODevice::WriteOnly);
  //QByteArray decoded = QByteArray::fromHex(jsonHexedTables);
  finalOutput.write(jsonHexedTables.constData());
  finalOutput.close();
  //qDebug() << "SHAPOOPY!!!!";
  //int * sizeOfData;

  //QFile finalsOutput("toFile.txt");
  //finalsOutput.open(QIODevice::WriteOnly);
  //QString raw(doc.rawData(sizeOfData));

  //finalsOutput.write(raw.);
  //finalsOutput.close();

  return jsonHexedTables;
}