private static void WriteCharacteristics(Utf8JsonWriter writer, RSM012Fragments fragments)
 {
     writer.WriteString("Product", fragments.IncludedProductCharacteristic !.Identification);
     writer.WriteString("QuantityMeasurementUnit_Name", fragments.IncludedProductCharacteristic.UnitType);
     writer.WriteString("MarketEvaluationPointType", fragments.DetailMeasurementMeteringPointCharacteristic !.TypeOfMeteringPoint);
     writer.WriteString("SettlementMethod", fragments.DetailMeasurementMeteringPointCharacteristic.SettlementMethod);
 }
        private static void WriteTransaction(Utf8JsonWriter writer, RSM012Fragments fragments)
        {
            writer.WriteStartObject("Transaction");

            writer.WriteString("mRID", fragments.PayloadEnergyTimeSeriesIdentification);

            writer.WriteEndObject();
        }
        private static void WriteTimeInterval(Utf8JsonWriter writer, RSM012Fragments fragments)
        {
            writer.WriteStartObject("TimeInterval");

            writer.WriteString("Start", fragments.ObservationTimeSeriesPeriod !.Start.ToString());
            writer.WriteString("End", fragments.ObservationTimeSeriesPeriod !.End.ToString());

            writer.WriteEndObject();
        }
        private static void WritePeriodUpToPoints(Utf8JsonWriter writer, RSM012Fragments fragments)
        {
            writer.WriteStartObject("Period");

            writer.WriteString("Resolution", fragments.ObservationTimeSeriesPeriod !.ResolutionDuration);

            WriteTimeInterval(writer, fragments);

            WriteStartOfPoints(writer);
        }
        private static void WriteTimeSeriesPoint(
            Utf8JsonWriter writer, TimeSeriesPoint point, RSM012Fragments fragments)
        {
            writer.WriteStartObject();

            writer.WriteNumber("Position", point.Position);
            writer.WriteNumber("Quantity", point.GetCimEnergyQuantity());
            writer.WriteString("Quality", point.GetCimQuantityQuality());
            writer.WriteString(
                "Time",
                Iso8601Duration.GetObservationTime(
                    fragments.ObservationTimeSeriesPeriod?.Start,
                    fragments.ObservationTimeSeriesPeriod?.ResolutionDuration,
                    point.Position).ToString());

            writer.WriteEndObject();
        }
        private static async ValueTask ProcessPayloadEnergyTimeSeriesAsync(
            XmlReader reader,
            RsmHeader header,
            Utf8JsonWriter writer,
            string correlationId)
        {
            var fragments = new RSM012Fragments();

            do
            {
                if (reader.NodeType != XmlNodeType.Element && reader.NodeType != XmlNodeType.EndElement)
                {
                    continue;
                }

                if (reader.Is("PayloadEnergyTimeSeries", B2BNamespace))
                {
                    writer.WriteStartObject();
                    writer.WriteString("CorrelationId", correlationId);
                }
                else if (reader.Is("Identification", B2BNamespace))
                {
                    fragments.PayloadEnergyTimeSeriesIdentification = await reader.ReadElementContentAsStringAsync()
                                                                      .ConfigureAwait(false);
                }
                else if (reader.Is("Function", B2BNamespace))
                {
                    fragments.Function = await reader.ReadElementContentAsStringAsync()
                                         .ConfigureAwait(false);
                }
                else if (reader.Is("ObservationTimeSeriesPeriod", B2BNamespace))
                {
                    fragments.ObservationTimeSeriesPeriod = await ProcessObservationTimeSeriesPeriodAsync(reader)
                                                            .ConfigureAwait(false);
                }
                else if (reader.Is("IncludedProductCharacteristic", B2BNamespace))
                {
                    fragments.IncludedProductCharacteristic = await ProcessIncludedProductCharacteristicAsync(reader)
                                                              .ConfigureAwait(false);
                }
                else if (reader.Is("DetailMeasurementMeteringPointCharacteristic", B2BNamespace))
                {
                    fragments.DetailMeasurementMeteringPointCharacteristic = await ProcessDetailMeasurementMeteringPointCharacteristicAsync(reader)
                                                                             .ConfigureAwait(false);
                }
                else if (reader.Is("MeteringPointDomainLocation", B2BNamespace))
                {
                    fragments.MeteringPointDomainLocationIdentification = await GetChildIdentificationAsync(reader)
                                                                          .ConfigureAwait(false);

                    /* 2020-12-22 - XMDJE - At this point we have all the necessary information to write everything to the stream up until points.
                     *  Points are specifically written immediately to the stream in the effort to save memory.
                     *  This means we will write everything up to that segment to the stream to be ready */
                    WriteTimeSeriesUpUntilIndividualPoints(writer, fragments, header);
                }
                else if (reader.Is("IntervalEnergyObservation", B2BNamespace))
                {
                    // Notice: We write each point to the stream immediately, unlike the rest of the parsing
                    var point = await ProcessPointsAsync(reader)
                                .ConfigureAwait(false);

                    WriteTimeSeriesPoint(writer, point, fragments);
                }
                else if (reader.Is("PayloadEnergyTimeSeries", B2BNamespace, XmlNodeType.EndElement))
                {
                    // We have now parsed all the points and can write the remaining parts of the document
                    WriteTimeSeriesFromAfterPoints(writer, fragments);
                    writer.WriteEndObject();
                }
            }while (await reader.ReadAsync().ConfigureAwait(false));
        }
 private static void WriteTimeSeriesFromAfterPoints(Utf8JsonWriter writer, RSM012Fragments fragments)
 {
     WriteEndOfPoints(writer);
     WriteTransaction(writer, fragments);
     WriteRequestDate(writer);
 }
 private static void WriteMarketEvaluationPointMRID(Utf8JsonWriter writer, RSM012Fragments fragments)
 {
     writer.WriteString("MarketEvaluationPoint_mRID", fragments.MeteringPointDomainLocationIdentification);
 }
 private static void WriteActivityRecordStatus(Utf8JsonWriter writer, RSM012Fragments fragments)
 {
     writer.WriteString("MktActivityRecord_Status", fragments.Function);
 }
 private static void WriteIdentifications(Utf8JsonWriter writer, RsmHeader header, RSM012Fragments fragments)
 {
     writer.WriteString("mRID", fragments.PayloadEnergyTimeSeriesIdentification);
     writer.WriteString("MessageReference", header.MessageReference);
 }
 private static void WriteTimeSeriesUpUntilIndividualPoints(Utf8JsonWriter writer, RSM012Fragments fragments, RsmHeader header)
 {
     WriteIdentifications(writer, header, fragments);
     WriteMarketDocument(writer, header);
     WriteActivityRecordStatus(writer, fragments);
     WriteCharacteristics(writer, fragments);
     WriteMarketEvaluationPointMRID(writer, fragments);
     WritePeriodUpToPoints(writer, fragments);
 }