/// <summary> /// Resets the parser to the beginning of the PQDIF file. /// </summary> public void Reset() { m_currentDataSourceRecord = null; m_currentMonitorSettingsRecord = null; m_nextObservationRecord = null; m_physicalParser.Reset(); m_physicalParser.NextRecord(); // skip container record }
/// <summary> /// Writes the given observation record, as well as its data source /// and monitor settings records if necessary, to the file stream. /// </summary> /// <param name="observationRecord">The observation record to be written to the PQDIF file.</param> /// <param name="lastRecord">Indicates whether this observation record is the last record in the file.</param> public void Write(ObservationRecord observationRecord, bool lastRecord = false) { if ((object)observationRecord == null) { throw new ArgumentNullException("observationRecord"); } if (m_disposed) { throw new ObjectDisposedException(GetType().Name); } if ((object)m_containerRecord == null) { throw new InvalidOperationException("Container record must be the first record in a PQDIF file."); } if ((object)observationRecord.DataSource == null) { throw new InvalidOperationException("An observation record must have a data source."); } if (!ReferenceEquals(m_currentDataSource, observationRecord.DataSource)) { ValidateDataSourceRecord(observationRecord.DataSource); } if ((object)observationRecord.Settings != null && !ReferenceEquals(m_currentMonitorSettings, observationRecord.Settings)) { ValidateMonitorSettingsRecord(observationRecord.Settings); } ValidateObservationRecord(observationRecord); if (!ReferenceEquals(m_currentDataSource, observationRecord.DataSource)) { m_currentMonitorSettings = null; m_currentDataSource = observationRecord.DataSource; m_physicalWriter.WriteRecord(observationRecord.DataSource.PhysicalRecord); } if ((object)observationRecord.Settings != null && !ReferenceEquals(m_currentMonitorSettings, observationRecord.Settings)) { m_currentMonitorSettings = observationRecord.Settings; m_physicalWriter.WriteRecord(observationRecord.Settings.PhysicalRecord); } m_physicalWriter.WriteRecord(observationRecord.PhysicalRecord, lastRecord); }
// Static Methods /// <summary> /// Creates a new channel definition belonging to the given data source record. /// </summary> /// <param name="dataSourceRecord">The data source record that the new channel definition belongs to.</param> /// <returns>The new channel definition.</returns> public static ChannelDefinition CreateChannelDefinition(DataSourceRecord dataSourceRecord) { ChannelDefinition channelDefinition = dataSourceRecord.AddNewChannelDefinition(); channelDefinition.Phase = Phase.None; channelDefinition.QuantityMeasured = QuantityMeasured.None; CollectionElement physicalStructure = channelDefinition.PhysicalStructure; physicalStructure.AddElement(new CollectionElement() { TagOfElement = SeriesDefinitionsTag }); return(channelDefinition); }
/// <summary> /// Determines whether there are any more /// <see cref="ObservationRecord"/>s to be /// read from the PQDIF file. /// </summary> /// <returns>true if there is another observation record to be read from PQDIF file; false otherwise</returns> public bool HasNextObservationRecord() { Record physicalRecord; RecordType recordType; // Read records from the file until we encounter an observation record or end of file while ((object)m_nextObservationRecord == null && m_physicalParser.HasNextRecord()) { physicalRecord = m_physicalParser.NextRecord(); recordType = physicalRecord.Header.TypeOfRecord; switch (recordType) { case RecordType.DataSource: // Keep track of the latest data source record in order to associate it with observation records m_currentDataSourceRecord = DataSourceRecord.CreateDataSourceRecord(physicalRecord); m_allDataSourceRecords.Add(m_currentDataSourceRecord); break; case RecordType.MonitorSettings: // Keep track of the latest monitor settings record in order to associate it with observation records m_currentMonitorSettingsRecord = MonitorSettingsRecord.CreateMonitorSettingsRecord(physicalRecord); break; case RecordType.Observation: // Found an observation record! m_nextObservationRecord = ObservationRecord.CreateObservationRecord(physicalRecord, m_currentDataSourceRecord, m_currentMonitorSettingsRecord); break; case RecordType.Container: // The container record is parsed when the file is opened; it should never be encountered here throw new InvalidOperationException("Found more than one container record in PQDIF file."); default: // Ignore unrecognized record types as the rest of the file might be valid. //throw new ArgumentOutOfRangeException(string.Format("Unknown record type: {0}", physicalRecord.Header.RecordTypeTag)); break; } } return((object)m_nextObservationRecord != null); }
// Static Methods /// <summary> /// Creates a new data source record from scratch. /// </summary> /// <param name="dataSourceName">The name of the data source to be created.</param> /// <returns>The new data source record.</returns> public static DataSourceRecord CreateDataSourceRecord(string dataSourceName) { Guid recordTypeTag = Record.GetTypeAsTag(RecordType.DataSource); Record physicalRecord = new Record(recordTypeTag); DataSourceRecord dataSourceRecord = new DataSourceRecord(physicalRecord); DateTime now = DateTime.UtcNow; dataSourceRecord.DataSourceTypeID = DataSourceType.Simulate; dataSourceRecord.DataSourceName = dataSourceName; dataSourceRecord.Effective = now; CollectionElement bodyElement = physicalRecord.Body.Collection; bodyElement.AddElement(new CollectionElement() { TagOfElement = ChannelDefinitionsTag }); return(dataSourceRecord); }
// Static Methods /// <summary> /// Creates a new observation record from scratch with the given data source and settings. /// </summary> /// <param name="dataSource">The data source record that defines the channels in this observation record.</param> /// <param name="settings">The monitor settings to be applied to this observation record.</param> /// <returns>The new observation record.</returns> public static ObservationRecord CreateObservationRecord(DataSourceRecord dataSource, MonitorSettingsRecord settings) { Guid recordTypeTag = Record.GetTypeAsTag(RecordType.Observation); Record physicalRecord = new Record(recordTypeTag); ObservationRecord observationRecord = new ObservationRecord(physicalRecord, dataSource, settings); DateTime now = DateTime.UtcNow; observationRecord.Name = now.ToString(); observationRecord.CreateTime = now; observationRecord.StartTime = now; observationRecord.TriggerMethod = TriggerMethod.None; CollectionElement bodyElement = physicalRecord.Body.Collection; bodyElement.AddElement(new CollectionElement() { TagOfElement = ChannelInstancesTag }); return(observationRecord); }
// Static Methods private static bool AreEquivalent(DataSourceRecord dataSource1, DataSourceRecord dataSource2) { if (ReferenceEquals(dataSource1, dataSource2)) return true; if ((object)dataSource1 == null) return false; if ((object)dataSource2 == null) return false; return dataSource1.DataSourceName == dataSource2.DataSourceName && dataSource1.VendorID == dataSource2.VendorID && dataSource1.EquipmentID == dataSource2.EquipmentID; }
/// <summary> /// Creates a new instance of the <see cref="ObservationRecord"/> class. /// </summary> /// <param name="physicalRecord">The physical structure of the observation record.</param> /// <param name="dataSource">The data source record that defines the channels in this observation record.</param> /// <param name="settings">The monitor settings to be applied to this observation record.</param> private ObservationRecord(Record physicalRecord, DataSourceRecord dataSource, MonitorSettingsRecord settings) { m_physicalRecord = physicalRecord; m_dataSource = dataSource; m_settings = settings; }
/// <summary> /// Creates a new observation record from the given physical record /// if the physical record is of type observation. Returns null if /// it is not. /// </summary> /// <param name="physicalRecord">The physical record used to create the observation record.</param> /// <param name="dataSource">The data source record that defines the channels in this observation record.</param> /// <param name="settings">The monitor settings to be applied to this observation record.</param> /// <returns>The new observation record, or null if the physical record does not define a observation record.</returns> public static ObservationRecord CreateObservationRecord(Record physicalRecord, DataSourceRecord dataSource, MonitorSettingsRecord settings) { bool isValidObservationRecord = physicalRecord.Header.TypeOfRecord == RecordType.Observation; return(isValidObservationRecord ? new ObservationRecord(physicalRecord, dataSource, settings) : null); }
/// <summary> /// Determines whether there are any more /// <see cref="ObservationRecord"/>s to be /// read from the PQDIF file. /// </summary> /// <returns>true if there is another observation record to be read from PQDIF file; false otherwise</returns> public bool HasNextObservationRecord() { Record physicalRecord; RecordType recordType; // Read records from the file until we encounter an observation record or end of file while ((object)m_nextObservationRecord == null && m_physicalParser.HasNextRecord()) { physicalRecord = m_physicalParser.NextRecord(); recordType = physicalRecord.Header.TypeOfRecord; switch (recordType) { case RecordType.DataSource: // Keep track of the latest data source record in order to associate it with observation records m_currentDataSourceRecord = DataSourceRecord.CreateDataSourceRecord(physicalRecord); break; case RecordType.MonitorSettings: // Keep track of the latest monitor settings record in order to associate it with observation records m_currentMonitorSettingsRecord = MonitorSettingsRecord.CreateMonitorSettingsRecord(physicalRecord); break; case RecordType.Observation: // Found an observation record! m_nextObservationRecord = ObservationRecord.CreateObservationRecord(physicalRecord, m_currentDataSourceRecord, m_currentMonitorSettingsRecord); break; case RecordType.Container: // The container record is parsed when the file is opened; it should never be encountered here throw new InvalidOperationException("Found more than one container record in PQDIF file."); default: // Ignore unrecognized record types as the rest of the file might be valid. //throw new ArgumentOutOfRangeException(string.Format("Unknown record type: {0}", physicalRecord.Header.RecordTypeTag)); break; } } return (object)m_nextObservationRecord != null; }
/// <summary> /// Resets the parser to the beginning of the PQDIF file. /// </summary> public void Reset() { m_currentDataSourceRecord = null; m_currentMonitorSettingsRecord = null; m_nextObservationRecord = null; m_physicalParser.Reset(); m_physicalParser.NextRecord(); // skip container record }
/// <summary> /// Creates a new instance of the <see cref="ObservationRecord"/> class. /// </summary> /// <param name="physicalRecord">The physical structure of the observation record.</param> /// <param name="dataSource">The data source record that defines the channels in this observation record.</param> /// <param name="settings">The monitor settings to be applied to this observation record.</param> private ObservationRecord(Record physicalRecord, DataSourceRecord dataSource, MonitorSettingsRecord settings) { m_physicalRecord = physicalRecord; m_dataSource = dataSource; m_settings = settings; }
/// <summary> /// Writes the given observation record, as well as its data source /// and monitor settings records if necessary, to the file stream. /// </summary> /// <param name="observationRecord">The observation record to be written to the PQDIF file.</param> /// <param name="lastRecord">Indicates whether this observation record is the last record in the file.</param> public void Write(ObservationRecord observationRecord, bool lastRecord = false) { if ((object)observationRecord == null) throw new ArgumentNullException("observationRecord"); if (m_disposed) throw new ObjectDisposedException(GetType().Name); if ((object)m_containerRecord == null) throw new InvalidOperationException("Container record must be the first record in a PQDIF file."); if ((object)observationRecord.DataSource == null) throw new InvalidOperationException("An observation record must have a data source."); if (!ReferenceEquals(m_currentDataSource, observationRecord.DataSource)) ValidateDataSourceRecord(observationRecord.DataSource); if ((object)observationRecord.Settings != null && !ReferenceEquals(m_currentMonitorSettings, observationRecord.Settings)) ValidateMonitorSettingsRecord(observationRecord.Settings); ValidateObservationRecord(observationRecord); if (!ReferenceEquals(m_currentDataSource, observationRecord.DataSource)) { m_currentMonitorSettings = null; m_currentDataSource = observationRecord.DataSource; m_physicalWriter.WriteRecord(observationRecord.DataSource.PhysicalRecord); } if ((object)observationRecord.Settings != null && !ReferenceEquals(m_currentMonitorSettings, observationRecord.Settings)) { m_currentMonitorSettings = observationRecord.Settings; m_physicalWriter.WriteRecord(observationRecord.Settings.PhysicalRecord); } m_physicalWriter.WriteRecord(observationRecord.PhysicalRecord, lastRecord); }
private void ValidateDataSourceRecord(DataSourceRecord dataSourceRecord) { if (!dataSourceRecord.PhysicalRecord.Body.Collection.GetElementsByTag(DataSourceRecord.DataSourceTypeTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "DataSourceTypeTag", DataSourceRecord.DataSourceTypeTag)); } if (!dataSourceRecord.PhysicalRecord.Body.Collection.GetElementsByTag(DataSourceRecord.DataSourceNameTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "DataSourceNameTag", DataSourceRecord.DataSourceNameTag)); } if (!dataSourceRecord.PhysicalRecord.Body.Collection.GetElementsByTag(DataSourceRecord.EffectiveTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "EffectiveTag", DataSourceRecord.EffectiveTag)); } if (!dataSourceRecord.PhysicalRecord.Body.Collection.GetElementsByTag(DataSourceRecord.ChannelDefinitionsTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "ChannelDefinitionsTag", DataSourceRecord.ChannelDefinitionsTag)); } if (dataSourceRecord.ChannelDefinitions.Count == 0) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "OneChannelDefinitionTag", DataSourceRecord.OneChannelDefinitionTag)); } foreach (ChannelDefinition channelDefinition in dataSourceRecord.ChannelDefinitions) { if (!channelDefinition.PhysicalStructure.GetElementsByTag(ChannelDefinition.PhaseIDTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "PhaseIDTag", ChannelDefinition.PhaseIDTag)); } if (!channelDefinition.PhysicalStructure.GetElementsByTag(ChannelDefinition.QuantityTypeIDTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "QuantityTypeIDTag", ChannelDefinition.QuantityTypeIDTag)); } if (!channelDefinition.PhysicalStructure.GetElementsByTag(ChannelDefinition.QuantityMeasuredIDTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "QuantityMeasuredIDTag", ChannelDefinition.QuantityMeasuredIDTag)); } if (!channelDefinition.PhysicalStructure.GetElementsByTag(ChannelDefinition.SeriesDefinitionsTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "SeriesDefinitionsTag", ChannelDefinition.SeriesDefinitionsTag)); } if (channelDefinition.SeriesDefinitions.Count == 0) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "OneSeriesDefinitionTag", ChannelDefinition.OneSeriesDefinitionTag)); } foreach (SeriesDefinition seriesDefinition in channelDefinition.SeriesDefinitions) { if (!seriesDefinition.PhysicalStructure.GetElementsByTag(SeriesDefinition.ValueTypeIDTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "ValueTypeIDTag", SeriesDefinition.ValueTypeIDTag)); } if (!seriesDefinition.PhysicalStructure.GetElementsByTag(SeriesDefinition.QuantityUnitsIDTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "QuantityUnitsIDTag", SeriesDefinition.QuantityUnitsIDTag)); } if (!seriesDefinition.PhysicalStructure.GetElementsByTag(SeriesDefinition.QuantityCharacteristicIDTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "QuantityCharacteristicIDTag", SeriesDefinition.QuantityCharacteristicIDTag)); } if (!seriesDefinition.PhysicalStructure.GetElementsByTag(SeriesDefinition.StorageMethodIDTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.DataSource, "StorageMethodIDTag", SeriesDefinition.StorageMethodIDTag)); } } } }
/// <summary> /// Creates a new instance of the <see cref="ChannelDefinition"/> class. /// </summary> /// <param name="physicalStructure">The collection element which is the physical structure of the channel definition.</param> /// <param name="dataSource">The data source in which the channel definition resides.</param> public ChannelDefinition(CollectionElement physicalStructure, DataSourceRecord dataSource) { m_dataSource = dataSource; m_physicalStructure = physicalStructure; }
private static Meter ParseDataSource(DataSourceRecord dataSource) { Meter meter; MeterLocation meterLocation; string name = dataSource.DataSourceName; Guid vendorID = dataSource.VendorID; Guid equipmentID = dataSource.EquipmentID; meter = new Meter(); meter.Name = name; meter.AssetKey = name; meter.ShortName = name.Substring(0, Math.Min(name.Length, 50)); meterLocation = new MeterLocation(); meterLocation.AssetKey = meter.Name; meterLocation.Name = string.Format("{0} location", meter.Name); meterLocation.ShortName = meterLocation.Name.Substring(0, Math.Min(meterLocation.Name.Length, 50)); meterLocation.Description = meterLocation.Name; if (vendorID != Vendor.None) meter.Make = Vendor.ToString(vendorID); if (equipmentID != Guid.Empty) meter.Model = Equipment.ToString(equipmentID); return meter; }
private void ValidateDataSourceRecord(DataSourceRecord dataSourceRecord) { if (!dataSourceRecord.PhysicalRecord.Body.Collection.GetElementsByTag(DataSourceRecord.DataSourceTypeTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "DataSourceTypeTag", DataSourceRecord.DataSourceTypeTag)); if (!dataSourceRecord.PhysicalRecord.Body.Collection.GetElementsByTag(DataSourceRecord.DataSourceNameTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "DataSourceNameTag", DataSourceRecord.DataSourceNameTag)); if (!dataSourceRecord.PhysicalRecord.Body.Collection.GetElementsByTag(DataSourceRecord.EffectiveTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "EffectiveTag", DataSourceRecord.EffectiveTag)); if (!dataSourceRecord.PhysicalRecord.Body.Collection.GetElementsByTag(DataSourceRecord.ChannelDefinitionsTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "ChannelDefinitionsTag", DataSourceRecord.ChannelDefinitionsTag)); if (dataSourceRecord.ChannelDefinitions.Count == 0) m_missingTags.Add(new MissingTag(RecordType.DataSource, "OneChannelDefinitionTag", DataSourceRecord.OneChannelDefinitionTag)); foreach (ChannelDefinition channelDefinition in dataSourceRecord.ChannelDefinitions) { if (!channelDefinition.PhysicalStructure.GetElementsByTag(ChannelDefinition.PhaseIDTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "PhaseIDTag", ChannelDefinition.PhaseIDTag)); if (!channelDefinition.PhysicalStructure.GetElementsByTag(ChannelDefinition.QuantityTypeIDTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "QuantityTypeIDTag", ChannelDefinition.QuantityTypeIDTag)); if (!channelDefinition.PhysicalStructure.GetElementsByTag(ChannelDefinition.QuantityMeasuredIDTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "QuantityMeasuredIDTag", ChannelDefinition.QuantityMeasuredIDTag)); if (!channelDefinition.PhysicalStructure.GetElementsByTag(ChannelDefinition.SeriesDefinitionsTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "SeriesDefinitionsTag", ChannelDefinition.SeriesDefinitionsTag)); if (channelDefinition.SeriesDefinitions.Count == 0) m_missingTags.Add(new MissingTag(RecordType.DataSource, "OneSeriesDefinitionTag", ChannelDefinition.OneSeriesDefinitionTag)); foreach (SeriesDefinition seriesDefinition in channelDefinition.SeriesDefinitions) { if (!seriesDefinition.PhysicalStructure.GetElementsByTag(SeriesDefinition.ValueTypeIDTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "ValueTypeIDTag", SeriesDefinition.ValueTypeIDTag)); if (!seriesDefinition.PhysicalStructure.GetElementsByTag(SeriesDefinition.QuantityUnitsIDTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "QuantityUnitsIDTag", SeriesDefinition.QuantityUnitsIDTag)); if (!seriesDefinition.PhysicalStructure.GetElementsByTag(SeriesDefinition.QuantityCharacteristicIDTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "QuantityCharacteristicIDTag", SeriesDefinition.QuantityCharacteristicIDTag)); if (!seriesDefinition.PhysicalStructure.GetElementsByTag(SeriesDefinition.StorageMethodIDTag).Any()) m_missingTags.Add(new MissingTag(RecordType.DataSource, "StorageMethodIDTag", SeriesDefinition.StorageMethodIDTag)); } } }
// Static Methods /// <summary> /// Creates a new observation record from the given physical record /// if the physical record is of type observation. Returns null if /// it is not. /// </summary> /// <param name="physicalRecord">The physical record used to create the observation record.</param> /// <param name="dataSource">The data source record that defines the channels in this observation record.</param> /// <param name="settings">The monitor settings to be applied to this observation record.</param> /// <returns>The new observation record, or null if the physical record does not define a observation record.</returns> public static ObservationRecord CreateObservationRecord(Record physicalRecord, DataSourceRecord dataSource, MonitorSettingsRecord settings) { bool isValidObservationRecord = physicalRecord.Header.TypeOfRecord == RecordType.Observation; return isValidObservationRecord ? new ObservationRecord(physicalRecord, dataSource, settings) : null; }