//******************************************************************************************************
// QueryPacketBase.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:
// ----------------------------------------------------------------------------------------------------
// 07/16/2007 - Pinal C. Patel
// Generated original version of code based on DatAWare system specifications by Brian B. Fox, GSF.
// 04/21/2009 - Pinal C. Patel
// Converted to C#.
// 09/15/2009 - Stephen C. Wills
// Added new header and license agreement.
// 10/11/2010 - Mihir Brahmbhatt
// Updated header and license agreement.
// 11/30/2011 - J. Ritchie Carroll
// Modified to support buffer optimized ISupportBinaryImage.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
namespace GSF.Historian.Packets
{
///
/// Base class for a packet to be used for requesting information from a historian.
///
public abstract class QueryPacketBase : PacketBase
{
// **************************************************************************************************
// * Binary Structure *
// **************************************************************************************************
// * # Of Bytes Byte Index Data Type Property Name *
// * ---------- ---------- ---------- --------------------------------------------------------------*
// * 2 0-1 Int16 TypeID (packet identifier) *
// * 4 2-5 Int32 RequestIDs.Count *
// * 4 6-9 Int32 RequestIDs[0] *
// * 4 n1-n2 Int32 RequestIDs[RequestIDs.Count -1 ] *
// **************************************************************************************************
#region [ Members ]
// Fields
private readonly List m_requestIDs;
#endregion
#region [ Constructors ]
///
/// Initializes a new instance of the query packet.
///
/// Numeric identifier for the packet type.
protected QueryPacketBase(short packetID)
: base(packetID)
{
m_requestIDs = new List();
}
#endregion
#region [ Properties ]
///
/// Gets a list of historian identifiers whose information is being requested.
///
/// A singe entry with ID of -1 can be used to request information for all defined historian identifiers.
public IList RequestIDs
{
get
{
return m_requestIDs;
}
}
///
/// Gets the length of the .
///
public override int BinaryLength
{
get
{
return (2 + 4 + (m_requestIDs.Count * 4));
}
}
#endregion
#region [ Methods ]
///
/// Initializes the query packet from the specified .
///
/// Binary image to be used for initializing the query packet.
/// 0-based starting index of initialization data in the .
/// Valid number of bytes in from .
/// Number of bytes used from the for initializing the query packet.
public override int ParseBinaryImage(byte[] buffer, int startIndex, int length)
{
if (length >= 6)
{
// Binary image has sufficient data.
short packetID = LittleEndian.ToInt16(buffer, startIndex);
if (packetID != TypeID)
throw new ArgumentException(string.Format("Unexpected packet id '{0}' (expected '{1}')", packetID, TypeID));
// Ensure that the binary image is complete
int requestIDCount = LittleEndian.ToInt32(buffer, startIndex + 2);
if (length < 6 + requestIDCount * 4)
return 0;
// We have a binary image with the correct packet id.
m_requestIDs.Clear();
for (int i = 0; i < requestIDCount; i++)
{
m_requestIDs.Add(LittleEndian.ToInt32(buffer, startIndex + 6 + (i * 4)));
}
return BinaryLength;
}
else
{
// Binary image does not have sufficient data.
return 0;
}
}
///
/// Generates binary image of the and copies it into the given buffer, for bytes.
///
/// Buffer used to hold generated binary image of the source object.
/// 0-based starting index in the to start writing.
/// The number of bytes written to the .
/// is null.
///
/// or is less than 0 -or-
/// and will exceed length.
///
public override int GenerateBinaryImage(byte[] buffer, int startIndex)
{
int length = BinaryLength;
buffer.ValidateParameters(startIndex, length);
Buffer.BlockCopy(LittleEndian.GetBytes(TypeID), 0, buffer, startIndex, 2);
Buffer.BlockCopy(LittleEndian.GetBytes(m_requestIDs.Count), 0, buffer, startIndex + 2, 4);
for (int i = 0; i < m_requestIDs.Count; i++)
{
Buffer.BlockCopy(LittleEndian.GetBytes(m_requestIDs[i]), 0, buffer, startIndex + 6 + (i * 4), 4);
}
return length;
}
///
/// Extracts time-series data from the query packet type.
///
/// A null reference.
public override IEnumerable ExtractTimeSeriesData()
{
return null;
}
#endregion
}
}