//****************************************************************************************************** // TimeSeriesDataService.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: // ---------------------------------------------------------------------------------------------------- // 09/01/2009 - Pinal C. Patel // Generated original version of source code. // 09/10/2009 - Pinal C. Patel // Modified the logic in ReadCurrentTimeSeriesData() overloads to remove try-catch and check for // null reference instead. // 09/15/2009 - Stephen C. Wills // Added new header and license agreement. // 12/15/2009 - Pinal C. Patel // Changed the default port for the service from 5152 to 6152. // 03/30/2010 - Pinal C. Patel // Added check to verify that the service is enabled before processing incoming requests. // 10/11/2010 - Mihir Brahmbhatt // Updated header and license agreement. // 11/07/2010 - Pinal C. Patel // Modified to fix breaking changes made to SelfHostingService. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.ServiceModel; using System.Text; using GSF.Historian.Files; namespace GSF.Historian.DataServices { /// /// Represents a REST web service for time-series data. /// /// [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)] public class TimeSeriesDataService : DataService, ITimeSeriesDataService { #region [ Constructors ] /// /// Initializes a new instance of the class. /// public TimeSeriesDataService() { Endpoints = "http.rest://localhost:6152/historian"; } #endregion #region [ Methods ] /// /// Writes received in format to the . /// /// An object. public void WriteTimeSeriesDataAsXml(SerializableTimeSeriesData data) { WriteTimeSeriesData(data); } /// /// Writes received in format to the . /// /// An object. public void WriteTimeSeriesDataAsJson(SerializableTimeSeriesData data) { WriteTimeSeriesData(data); } /// /// Reads current time-series data from the and sends it in format. /// /// A comma or semi-colon delimited list of IDs for which current time-series data is to be read. /// An object. public SerializableTimeSeriesData ReadSelectCurrentTimeSeriesDataAsXml(string idList) { return ReadCurrentTimeSeriesData(idList); } /// /// Reads current time-series data from the and sends it in format. /// /// Starting ID in the ID range for which current time-series data is to be read. /// Ending ID in the ID range for which current time-series data is to be read. /// An object. public SerializableTimeSeriesData ReadRangeCurrentTimeSeriesDataAsXml(string fromID, string toID) { return ReadCurrentTimeSeriesData(fromID, toID); } /// /// Reads current time-series data from the and sends it in format. /// /// A comma or semi-colon delimited list of IDs for which current time-series data is to be read. /// An object. public SerializableTimeSeriesData ReadSelectCurrentTimeSeriesDataAsJson(string idList) { return ReadCurrentTimeSeriesData(idList); } /// /// Reads current time-series data from the and sends it in format. /// /// Starting ID in the ID range for which current time-series data is to be read. /// Ending ID in the ID range for which current time-series data is to be read. /// An object. public SerializableTimeSeriesData ReadRangeCurrentTimeSeriesDataAsJson(string fromID, string toID) { return ReadCurrentTimeSeriesData(fromID, toID); } /// /// Reads historic time-series data from the and sends it in format. /// /// A comma or semi-colon delimited list of IDs for which historic time-series data is to be read. /// Start time in format of the timespan for which historic time-series data is to be read. /// End time in format of the timespan for which historic time-series data is to be read. /// An object. public SerializableTimeSeriesData ReadSelectHistoricTimeSeriesDataAsXml(string idList, string startTime, string endTime) { return ReadHistoricTimeSeriesData(idList, startTime, endTime); } /// /// Reads historic time-series data from the and sends it in format. /// /// Starting ID in the ID range for which historic time-series data is to be read. /// Ending ID in the ID range for which historic time-series data is to be read. /// Start time in format of the timespan for which historic time-series data is to be read. /// End time in format of the timespan for which historic time-series data is to be read. /// An object. public SerializableTimeSeriesData ReadRangeHistoricTimeSeriesDataAsXml(string fromID, string toID, string startTime, string endTime) { return ReadHistoricTimeSeriesData(fromID, toID, startTime, endTime); } /// /// Reads historic time-series data from the and sends it in format. /// /// A comma or semi-colon delimited list of IDs for which historic time-series data is to be read. /// Start time in format of the timespan for which historic time-series data is to be read. /// End time in format of the timespan for which historic time-series data is to be read. /// An object. public SerializableTimeSeriesData ReadSelectHistoricTimeSeriesDataAsJson(string idList, string startTime, string endTime) { return ReadHistoricTimeSeriesData(idList, startTime, endTime); } /// /// Reads historic time-series data from the and sends it in format. /// /// Starting ID in the ID range for which historic time-series data is to be read. /// Ending ID in the ID range for which historic time-series data is to be read. /// Start time in format of the timespan for which historic time-series data is to be read. /// End time in format of the timespan for which historic time-series data is to be read. /// An object. public SerializableTimeSeriesData ReadRangeHistoricTimeSeriesDataAsJson(string fromID, string toID, string startTime, string endTime) { return ReadHistoricTimeSeriesData(fromID, toID, startTime, endTime); } /// /// Reads historic time-series data from the and sends it in format. /// /// JSON serialized post request. /// An object. public SerializableTimeSeriesData ReadSelectHistoricTimeSeriesDataAsJsonPost(SerializableReadRequestData data) { StringBuilder idList = new StringBuilder(); foreach (int id in data.idArray) { if (idList.Length > 0) idList.Append(','); idList.Append(id); } return ReadHistoricTimeSeriesData(idList.ToString(), data.startTime, data.endTime); } private void WriteTimeSeriesData(SerializableTimeSeriesData data) { try { // Abort if services is not enabled. if (!Enabled) return; // Ensure that data archive is available. if (Archive == null) throw new ArgumentNullException("Archive"); // Write all time-series data to the archive. foreach (SerializableTimeSeriesDataPoint point in data.TimeSeriesDataPoints) { Archive.WriteData(point.Deflate()); } } catch (Exception ex) { // Notify about the encountered processing exception. OnServiceProcessException(ex); throw; } } private SerializableTimeSeriesData ReadCurrentTimeSeriesData(string idList) { try { // Abort if services is not enabled. if (!Enabled) return null; // Ensure that data archive is available. if (Archive == null) throw new ArgumentNullException("Archive"); // Read current time-series data from the archive. int id = 0; byte[] buffer = null; StateRecord state = null; SerializableTimeSeriesData data = new SerializableTimeSeriesData(); List points = new List(); foreach (string singleID in idList.Split(',', ';')) { buffer = Archive.ReadStateData(id = int.Parse(singleID)); if ((object)buffer == null) { // ID is invalid. continue; } else { // Add to resultset. state = new StateRecord(id, buffer, 0, buffer.Length); points.Add(new SerializableTimeSeriesDataPoint(state.CurrentData)); } } data.TimeSeriesDataPoints = points.ToArray(); return data; } catch (Exception ex) { // Notify about the encountered processing exception. OnServiceProcessException(ex); throw; } } private SerializableTimeSeriesData ReadCurrentTimeSeriesData(string fromID, string toID) { try { // Abort if services is not enabled. if (!Enabled) return null; // Ensure that data archive is available. if (Archive == null) throw new ArgumentNullException("Archive"); // Read current time-series data from the archive. byte[] buffer = null; StateRecord state = null; SerializableTimeSeriesData data = new SerializableTimeSeriesData(); List points = new List(); for (int id = int.Parse(fromID); id <= int.Parse(toID); id++) { buffer = Archive.ReadStateData(id); if ((object)buffer == null) { // ID is invalid. continue; } else { // Add to resultset. state = new StateRecord(id, buffer, 0, buffer.Length); points.Add(new SerializableTimeSeriesDataPoint(state.CurrentData)); } } data.TimeSeriesDataPoints = points.ToArray(); return data; } catch (Exception ex) { // Notify about the encountered processing exception. OnServiceProcessException(ex); throw; } } private SerializableTimeSeriesData ReadHistoricTimeSeriesData(string idList, string startTime, string endTime) { try { // Abort if services is not enabled. if (!Enabled) return null; // Ensure that data archive is available. if (Archive == null) throw new ArgumentNullException("Archive"); // Read historic time-series data from the archive. int id = 0; SerializableTimeSeriesData data = new SerializableTimeSeriesData(); List points = new List(); List historianIDs = new List(); foreach (string singleID in idList.Split(',', ';')) { if (int.TryParse(singleID, out id)) historianIDs.Add(id); } foreach (IDataPoint point in Archive.ReadData(historianIDs, startTime, endTime)) { points.Add(new SerializableTimeSeriesDataPoint(point)); } data.TimeSeriesDataPoints = points.ToArray(); return data; } catch (Exception ex) { // Notify about the encountered processing exception. OnServiceProcessException(ex); throw; } } private SerializableTimeSeriesData ReadHistoricTimeSeriesData(string fromID, string toID, string startTime, string endTime) { try { // Abort if services is not enabled. if (!Enabled) return null; // Ensure that data archive is available. if (Archive == null) throw new ArgumentNullException("Archive"); // Read historic time-series data from the archive. SerializableTimeSeriesData data = new SerializableTimeSeriesData(); List points = new List(); List historianIDs = new List(); for (int id = int.Parse(fromID); id <= int.Parse(toID); id++) { historianIDs.Add(id); } foreach (IDataPoint point in Archive.ReadData(historianIDs, startTime, endTime)) { points.Add(new SerializableTimeSeriesDataPoint(point)); } data.TimeSeriesDataPoints = points.ToArray(); return data; } catch (Exception ex) { // Notify about the encountered processing exception. OnServiceProcessException(ex); throw; } } #endregion } }