// Sort by: Device, Component, Category, DataItem
bool DataItem::operator<(DataItem &aOther) 
{
  Device *dev = mComponent->getDevice();
  if (dev->getId() < aOther.getComponent()->getDevice()->getId())
    return true;
  else if (dev->getId() > aOther.getComponent()->getDevice()->getId())
    return false;
  else if (mComponent->getId() < aOther.getComponent()->getId())
    return true;
  else if (mComponent->getId() > aOther.getComponent()->getId())
    return false;
  else if (mCategory < aOther.mCategory)
    return true;
  else if (mCategory == aOther.mCategory)
    return mId < aOther.mId;
  else
    return false;
}
Exemplo n.º 2
0
string XmlPrinter::printSample(const unsigned int instanceId,
                               const unsigned int bufferSize,
                               const uint64_t nextSeq,
                               const uint64_t firstSeq,
                               const uint64_t lastSeq,
                               ComponentEventPtrArray& results
  )
{
  xmlTextWriterPtr writer;
  xmlBufferPtr buf;
  string ret;
  
  try {
    THROW_IF_XML2_NULL(buf = xmlBufferCreate());
    THROW_IF_XML2_NULL(writer = xmlNewTextWriterMemory(buf, 0));
    THROW_IF_XML2_ERROR(xmlTextWriterSetIndent(writer, 1));
    THROW_IF_XML2_ERROR(xmlTextWriterSetIndentString(writer, BAD_CAST "  "));
    
    initXmlDoc(writer, eSTREAMS,
               instanceId,
               bufferSize,
               0, 0,
               nextSeq,
               firstSeq,
               lastSeq);
        
    THROW_IF_XML2_ERROR(xmlTextWriterStartElement(writer, BAD_CAST "Streams"));
    
    // Sort the vector by category.
    if (results.size() > 1)
      dlib::qsort_array<ComponentEventPtrArray,EventComparer>(results, 0, results.size() - 1, EventCompare);

    Device *lastDevice = NULL;
    Component *lastComponent = NULL;
    int lastCategory = -1;
    
    for (unsigned int i = 0; i < results.size(); i++)
    {
      ComponentEventPtr result = results[i];
      DataItem *dataItem = result->getDataItem();
      Component *component = dataItem->getComponent();
      Device *device = component->getDevice();
      
      if (device != lastDevice)
      {
        if (lastDevice != NULL)
          THROW_IF_XML2_ERROR(xmlTextWriterEndElement(writer)); // DeviceStream
        lastDevice = device;
        if (lastComponent != NULL)
          THROW_IF_XML2_ERROR(xmlTextWriterEndElement(writer)); // ComponentStream
        lastComponent = NULL;
        if (lastCategory != -1)
          THROW_IF_XML2_ERROR(xmlTextWriterEndElement(writer)); // Category
        lastCategory = -1;
        addDeviceStream(writer, device);
      }
      
      if (component != lastComponent)
      {
        if (lastComponent != NULL)
          THROW_IF_XML2_ERROR(xmlTextWriterEndElement(writer)); // ComponentStream
        lastComponent = component;
        if (lastCategory != -1)
          THROW_IF_XML2_ERROR(xmlTextWriterEndElement(writer)); // Category
        lastCategory = -1;
        addComponentStream(writer, component);        
      }
      
      if (lastCategory != dataItem->getCategory())
      {
        if (lastCategory != -1)
          THROW_IF_XML2_ERROR(xmlTextWriterEndElement(writer)); // Category
        lastCategory = dataItem->getCategory();
        addCategory(writer, dataItem->getCategory());
      }
      
      addEvent(writer, result);
    }
    
    if (lastCategory != -1)
      THROW_IF_XML2_ERROR(xmlTextWriterEndElement(writer)); // Category
    if (lastDevice != NULL)
      THROW_IF_XML2_ERROR(xmlTextWriterEndElement(writer)); // DeviceStream
    if (lastComponent != NULL)
      THROW_IF_XML2_ERROR(xmlTextWriterEndElement(writer)); // ComponentStream
    
    THROW_IF_XML2_ERROR(xmlTextWriterEndElement(writer)); // Streams    
    THROW_IF_XML2_ERROR(xmlTextWriterEndElement(writer)); // MTConnectStreams
    THROW_IF_XML2_ERROR(xmlTextWriterEndDocument(writer));
    
    xmlFreeTextWriter(writer);
    ret = (string) ((char*) buf->content);
    xmlBufferFree(buf);    
  }
  catch (string error) {
    if (buf != NULL)
      xmlBufferFree(buf);
    if (writer != NULL)
      xmlFreeTextWriter(writer);
    sLogger << dlib::LERROR << "printProbe: " << error;
  }
  catch (...) {
    if (buf != NULL)
      xmlBufferFree(buf);
    if (writer != NULL)
      xmlFreeTextWriter(writer);
    sLogger << dlib::LERROR << "printProbe: unknown error";
  }
  
  return ret;
}