//******************************************************************************************************
// FlatlineService.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:
// ----------------------------------------------------------------------------------------------------
// 12/10/2009 - Stephen C. Wills
// Generated original version of source code.
// 12/11/2009 - Pinal C. Patel
// Added error checking to TryGetMeasurementInfo().
// 12/16/2009 - Stephen C. Wills
// Replaced TryGetMeasurementInfo() with SerializableMeasurement.SetDeviceAndSignalType().
// 11/07/2010 - Pinal C. Patel
// Modified to fix breaking changes made to SelfHostingService.
// 12/13/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.ServiceModel;
using GSF;
using GSF.ServiceModel;
using GSF.TimeSeries;
namespace DataQualityMonitoring.Services
{
///
/// Represents a REST web service for flatlined measurements.
///
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class FlatlineService : SelfHostingService, IFlatlineService
{
#region [ Members ]
// Fields
private FlatlineTest m_test;
#endregion
#region [ Constructors ]
///
/// Initializes a new instance of the class.
///
/// The test to be used by this .
public FlatlineService(FlatlineTest test)
{
m_test = test;
PublishMetadata = true;
PersistSettings = true;
Endpoints = "http.rest://localhost:6100/flatlinetest";
}
#endregion
#region [ Properties ]
///
/// Gets or sets the used by the web service for its data.
///
public FlatlineTest Test
{
get
{
return m_test;
}
set
{
m_test = value;
}
}
#endregion
#region [ Methods ]
///
/// Reads all flatlined measurements from the and sends it in format.
///
/// A object.
public SerializableFlatlineTest ReadAllFlatlinedMeasurementsAsXml()
{
return ReadFlatlinedMeasurements();
}
///
/// Reads all flatlined measurements from the and sends it in format.
///
/// A object.
public SerializableFlatlineTest ReadAllFlatlinedMeasurementsAsJson()
{
return ReadFlatlinedMeasurements();
}
///
/// Reads all flatlined measurements from the specified device and sends it in format.
///
/// The name of the device to check for flatlined measurements.
/// A object.
public SerializableFlatlineTest ReadFlatlinedMeasurementsFromDeviceAsXml(string device)
{
return ReadFlatlinedMeasurements(device);
}
///
/// Reads all flatlined measurements from the specified device and sends it in format.
///
/// The name of the device to check for flatlined measurements.
/// A object.
public SerializableFlatlineTest ReadFlatlinedMeasurementsFromDeviceAsJson(string device)
{
return ReadFlatlinedMeasurements(device);
}
// Reads all flatlined measurements.
private SerializableFlatlineTest ReadFlatlinedMeasurements()
{
SerializableFlatlineTest serializableTest = new SerializableFlatlineTest();
ICollection flatlinedMeasurements = m_test.GetFlatlinedMeasurements();
List serializableFlatlinedMeasurements = new List();
foreach (IMeasurement measurement in flatlinedMeasurements)
{
SerializableFlatlinedMeasurement serializableFlatlinedMeasurement = CreateSerializableFlatlinedMeasurement(measurement);
serializableFlatlinedMeasurements.Add(serializableFlatlinedMeasurement);
}
serializableTest.FlatlinedMeasurements = serializableFlatlinedMeasurements.ToArray();
return serializableTest;
}
// Reads all flatlined measurements associated with a particular device.
private SerializableFlatlineTest ReadFlatlinedMeasurements(string device)
{
SerializableFlatlineTest serializableTest = new SerializableFlatlineTest();
ICollection flatlinedMeasurements = m_test.GetFlatlinedMeasurements();
List serializableFlatlinedMeasurements = new List();
foreach (IMeasurement measurement in flatlinedMeasurements)
{
SerializableFlatlinedMeasurement serializableFlatlinedMeasurement = CreateSerializableFlatlinedMeasurement(measurement);
if (serializableFlatlinedMeasurement.Device == device)
serializableFlatlinedMeasurements.Add(serializableFlatlinedMeasurement);
}
serializableTest.FlatlinedMeasurements = serializableFlatlinedMeasurements.ToArray();
return serializableTest;
}
// Properly creates a SerializableFlatlinedMeasurement by sending in TimeSinceLastChange, attaching to the exception event, and setting device and signal type.
private SerializableFlatlinedMeasurement CreateSerializableFlatlinedMeasurement(IMeasurement measurement)
{
SerializableFlatlinedMeasurement serializableMeasurement = new SerializableFlatlinedMeasurement(measurement, m_test.RealTime - measurement.Timestamp);
serializableMeasurement.ProcessException += serializableMeasurement_ProcessException;
serializableMeasurement.SetDeviceAndSignalType(m_test.DataSource);
return serializableMeasurement;
}
// Exceptions from flatlined measurements get forwarded to the ServiceProcessException event.
private void serializableMeasurement_ProcessException(object sender, EventArgs e)
{
OnServiceProcessException(e.Argument);
}
#endregion
}
}