Node *Parser::parse(uHTTP::URL *url) {
  LogInfo("parse,TID(0x%04x): %s\n", GetCurrentThreadId(), url->getSting()); 
  HTTPRequest httpReq;
  HTTPResponse *httpRes = NULL;
  {
      FUNCTION_BLOCK_NAME_TRACE("post", 100);

      const char *host = url->getHost();
      int port = url->getPort();
      std::string target = url->getTarget();

      httpReq.setMethod(HTTP::GET);
      httpReq.setURI(target);
      httpRes = httpReq.post(host, port);
      if (httpRes->isSuccessful() == false){
          return NULL;
      }
  }

  Node* pNode = NULL;
  {
      FUNCTION_BLOCK_NAME_TRACE("ParseContent", 100);
     const char *contents = httpRes->getContent();
     pNode = parse(contents);
  }
  
  return pNode;
}
/**
 * DBInterface::writeToDataBase
 * @brief writeToDataBase writes content of dataBuffer_ to database
 * @param dataBuffer_ data which is written to database
 */
void DBInterface::writeToDataBase(DataBuffer& dataBuffer_) {
    if (readStatusOK()) {

        stringstream httpRequestUrl;

        httpRequestUrl << URL_OF_DATABASE << "/write?db=" << NAME_OF_DATBASE << "&precision=s";
        stringstream httpRequestPostFields;
        if (!dataBuffer_.useDataSource) {
            log << SLevel(ERROR) << "Aborted writing to database because there was either no DataSource specified" <<
             " or the useDataSource-flag was not set to true." << endl;
        } else {
            string dataSource = cleanString(dataBuffer_.dataSource);
            httpRequestPostFields << "point,DataSource=" << dataSource << " ";

            bool printComma = false;
            typedef std::map<string, double>::iterator it_type;
            for(it_type iterator = dataBuffer_.data.begin(); iterator != dataBuffer_.data.end(); iterator++) {
                if (printComma) {
                    httpRequestPostFields << ",";
                } else {
                    printComma = true;
                }
                string name = cleanString(iterator->first);
                double value = cutValueToInfluxDBRange(iterator->second);
                httpRequestPostFields << name << "=" << value;
            }
            // create datetime-string
            if (dataBuffer_.useDateTimes) {
                if (dataBuffer_.startDateTime.tm_year <= 1971) {
                    log << SLevel(ERROR) << "Aborted writing to database because of invalid datetime. " <<
                    "Please use only years bigger than 1971." << endl;
                    setDBFailure(true);
                    return;
                } else {
                    string startDateTime = cTimeToString(dataBuffer_.startDateTime,true);
                    httpRequestPostFields << " " << startDateTime;
                }
            } else {
                // if no date-time is specified use local time (cut down to current hour)
                int currentLocalDateTime = getCurrentDateTimeAsUnixTime();
                string startDateTime = to_string(currentLocalDateTime);
                httpRequestPostFields << " " << startDateTime;
            }
            HTTPRequest req;
            bool noFailure = req.post(httpRequestUrl.str(),httpRequestPostFields.str());
            setDBFailure(!noFailure);
        }
    } else {
        log << SLevel(ERROR) << "Aborted writing to database because of status not OK" << endl;
    }
}
Exemple #3
0
Node *Parser::parse(uHTTP::URL *url)
{
  const char *host = url->getHost();
  int port = url->getPort();
  const char *uri = url->getPath();
  HTTPRequest httpReq;
  httpReq.setMethod(HTTP::GET);
  httpReq.setURI(uri);
  HTTPResponse *httpRes = httpReq.post(host, port);
  if (httpRes->isSuccessful() == false)
    return NULL;
  const char *contents = httpRes->getContent();
  return parse(contents);
}
/**
 * DBInterface::writeStatusOK
 * @brief writes the boolean value statusOK_ to database
 * @param statusOK_ boolean value which is wirtten to the database
 */
void DBInterface::writeStatusOK(bool statusOK_) {
    stringstream httpRequestUrl;
    stringstream httpRequestPostFields;
    HTTPRequest req;

    // try to delete old value from influxDB
    // (post writes an error to log, if there is nothing to delete)
    httpRequestUrl << URL_OF_DATABASE << "/query?db=" << NAME_OF_DATBASE;
    httpRequestPostFields << "q=drop measurement statusOK";
    req.post(httpRequestUrl.str(),httpRequestPostFields.str());

    // write new value to influxDB
    stringstream httpRequestUrl2;
    stringstream httpRequestPostFields2;
    httpRequestUrl2 << URL_OF_DATABASE << "/write?db=" << NAME_OF_DATBASE << "&precision=s";

    httpRequestPostFields2 << "statusOK value=" << statusOK_;

    bool noFailure = req.post(httpRequestUrl2.str(),httpRequestPostFields2.str());
    setDBFailure(!noFailure);
    if (!noFailure) {
        log << SLevel(ERROR) << "Aborted writing statusOK to database because http-post was not possible." << endl;
    }
}
bool StateVariable::performQueryListener(QueryRequest *queryReq)
{
  QueryListener *listener = getQueryListener();
  if (listener == NULL)
    return false;
  QueryResponse queryRes;
  StateVariable retVar;
  retVar.set(this);
  retVar.setValue("");
  retVar.setStatus(UPnP::INVALID_VAR);
  if (listener->queryControlReceived(&retVar) == true) {
    queryRes.setResponse(&retVar);
  }
  else {
    UPnPStatus *upnpStatus = retVar.getStatus();
    queryRes.setFaultResponse(upnpStatus->getCode(), upnpStatus->getDescription());
  }
  HTTPRequest *httpReq = queryReq;
  httpReq->post(&queryRes);
  return true;
}
/**
 * DBInterface::createIfNotCreatedDataBase
 * @brief creates database defined by NAME_OF_DATBASE in config.h if not already created
 */
void DBInterface::createIfNotCreatedDataBase() {
    HTTPRequest request;
    request.post(URL_OF_DATABASE + "/query?q=create+database+"+NAME_OF_DATBASE+"&db=_internal","");
}