private void ValidateObservationRecord(ObservationRecord observationRecord) { if (!observationRecord.PhysicalRecord.Body.Collection.GetElementsByTag(ObservationRecord.ObservationNameTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.Observation, "ObservationNameTag", ObservationRecord.ObservationNameTag)); } if (!observationRecord.PhysicalRecord.Body.Collection.GetElementsByTag(ObservationRecord.TimeCreateTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.Observation, "TimeCreateTag", ObservationRecord.TimeCreateTag)); } if (!observationRecord.PhysicalRecord.Body.Collection.GetElementsByTag(ObservationRecord.TimeStartTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.Observation, "TimeStartTag", ObservationRecord.TimeStartTag)); } if (!observationRecord.PhysicalRecord.Body.Collection.GetElementsByTag(ObservationRecord.TriggerMethodTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.Observation, "TriggerMethodTag", ObservationRecord.TriggerMethodTag)); } if (!observationRecord.PhysicalRecord.Body.Collection.GetElementsByTag(ObservationRecord.ChannelInstancesTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.Observation, "ChannelInstancesTag", ObservationRecord.ChannelInstancesTag)); } if (observationRecord.ChannelInstances.Count == 0) { m_missingTags.Add(new MissingTag(RecordType.Observation, "OneChannelInstanceTag", ObservationRecord.OneChannelInstanceTag)); } foreach (ChannelInstance channelInstance in observationRecord.ChannelInstances) { if (!channelInstance.PhysicalStructure.GetElementsByTag(ChannelDefinition.ChannelDefinitionIndexTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.Observation, "ChannelDefinitionIndexTag", ChannelDefinition.ChannelDefinitionIndexTag)); } if (!channelInstance.PhysicalStructure.GetElementsByTag(ChannelInstance.SeriesInstancesTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.Observation, "SeriesInstancesTag", ChannelInstance.SeriesInstancesTag)); } if (channelInstance.SeriesInstances.Count == 0) { m_missingTags.Add(new MissingTag(RecordType.Observation, "OneSeriesInstanceTag", ChannelInstance.OneSeriesInstanceTag)); } foreach (SeriesInstance seriesInstance in channelInstance.SeriesInstances) { if (!seriesInstance.PhysicalStructure.GetElementsByTag(SeriesInstance.SeriesValuesTag).Any()) { m_missingTags.Add(new MissingTag(RecordType.Observation, "SeriesValuesTag", SeriesInstance.SeriesValuesTag)); } } } }
/// <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> /// <exception cref="ArgumentNullException"><paramref name="observationRecord"/> is null.</exception> /// <exception cref="ObjectDisposedException">The writer was disposed.</exception> /// <exception cref="InvalidOperationException"> /// <para>File does not yet have a <see cref="ContainerRecord"/>.</para> /// <para>- OR -</para> /// <para><paramref name="observationRecord"/> does not have a data source.</para> /// </exception> /// <exception cref="InvalidDataException">The PQDIF data is invalid.</exception> public async Task WriteAsync(ObservationRecord observationRecord, bool lastRecord = false) { if (observationRecord == null) { throw new ArgumentNullException("observationRecord"); } if (m_disposed) { throw new ObjectDisposedException(GetType().Name); } if (m_containerRecord == null) { throw new InvalidOperationException("Container record must be the first record in a PQDIF file."); } if (observationRecord.DataSource == null) { throw new InvalidOperationException("An observation record must have a data source."); } if (!ReferenceEquals(m_currentDataSource, observationRecord.DataSource)) { ValidateDataSourceRecord(observationRecord.DataSource); } if (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; await m_physicalWriter.WriteRecordAsync(observationRecord.DataSource.PhysicalRecord); } if (observationRecord.Settings != null && !ReferenceEquals(m_currentMonitorSettings, observationRecord.Settings)) { m_currentMonitorSettings = observationRecord.Settings; await m_physicalWriter.WriteRecordAsync(observationRecord.Settings.PhysicalRecord); } await m_physicalWriter.WriteRecordAsync(observationRecord.PhysicalRecord, lastRecord); }
// 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); }
/// <summary> /// Creates a new instance of the <see cref="ChannelInstance"/> class. /// </summary> /// <param name="physicalStructure">The collection element which is the physical structure of the channel instance.</param> /// <param name="observationRecord">The observation record in which the channel instance resides.</param> public ChannelInstance(CollectionElement physicalStructure, ObservationRecord observationRecord) { m_physicalStructure = physicalStructure; m_observationRecord = observationRecord; }