//******************************************************************************************************
// TsfDataAdapter.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 10/05/2012 - Adam Crain
// Generated original version of source code.
// 12/13/2012 - Starlynn Danyelle Gilliam
// Modified Header.
// 03/02/2014 - Adam Crain
// Migrated to new ISOEHandler interface
// 09/18/2020 - J. Ritchie Carroll
// Updated to accommodate recent changes in ISOEHandler interface
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using Automatak.DNP3.Interface;
using GSF.TimeSeries;
namespace DNP3Adapters
{
///
/// This is the data adapter that converts data from the dnp3 world to the Time Series Framework.
///
internal class TimeSeriesSOEHandler : ISOEHandler
{
public delegate void OnNewMeasurements(ICollection measurements);
public event OnNewMeasurements NewMeasurements;
private readonly MeasurementLookup m_lookup;
private readonly List m_measurements = new List();
public TimeSeriesSOEHandler(MeasurementLookup lookup)
{
TimestampDifferentiation = TimeSpan.FromMilliseconds(1.0D);
m_lookup = lookup;
}
public TimeSpan TimestampDifferentiation { get; set; }
private void PutTimestamps(IEnumerable> values) where T : MeasurementBase
{
DateTime now = DateTime.UtcNow;
foreach (IGrouping> grouping in values.GroupBy(indexedValue => indexedValue.Index))
{
int count = grouping.Count();
foreach (IndexedValue indexedValue in grouping)
{
TimeSpan offset = TimeSpan.FromTicks(count * TimestampDifferentiation.Ticks);
indexedValue.Value.Timestamp = new DNPTime(now - offset);
count--;
}
}
}
void ISOEHandler.BeginFragment(ResponseInfo info)
{
m_measurements.Clear();
}
void ISOEHandler.EndFragment(ResponseInfo info)
{
if (m_measurements.Count > 0 && NewMeasurements != null)
{
NewMeasurements(m_measurements);
}
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
IEnumerable> indexedValues = values.ToArray();
PutTimestamps(indexedValues);
foreach (IndexedValue indexedValue in indexedValues)
m_lookup.Lookup(indexedValue.Value, indexedValue.Index, m_measurements.Add);
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
IEnumerable> indexedValues = values.ToArray();
PutTimestamps(indexedValues);
foreach (IndexedValue indexedValue in indexedValues)
m_lookup.Lookup(indexedValue.Value, indexedValue.Index, m_measurements.Add);
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
IEnumerable> indexedValues = values.ToArray();
PutTimestamps(indexedValues);
foreach (IndexedValue indexedValue in indexedValues)
m_lookup.Lookup(indexedValue.Value, indexedValue.Index, m_measurements.Add);
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
IEnumerable> indexedValues = values.ToArray();
PutTimestamps(indexedValues);
foreach (IndexedValue indexedValue in indexedValues)
m_lookup.Lookup(indexedValue.Value, indexedValue.Index, m_measurements.Add);
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
IEnumerable> indexedValues = values.ToArray();
PutTimestamps(indexedValues);
foreach (IndexedValue indexedValue in indexedValues)
m_lookup.Lookup(indexedValue.Value, indexedValue.Index, m_measurements.Add);
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
IEnumerable> indexedValues = values.ToArray();
PutTimestamps(indexedValues);
foreach (IndexedValue indexedValue in indexedValues)
m_lookup.Lookup(indexedValue.Value, indexedValue.Index, m_measurements.Add);
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
IEnumerable> indexedValues = values.ToArray();
PutTimestamps(indexedValues);
foreach (IndexedValue indexedValue in indexedValues)
m_lookup.Lookup(indexedValue.Value, indexedValue.Index, m_measurements.Add);
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
}
void ISOEHandler.Process(HeaderInfo info, IEnumerable> values)
{
}
}
}