private void CreateContext(Export export) { ExportContext context; lock (m_contexts) { if (!m_contexts.TryGetValue(export.Name, out context)) { // Context for the export does not exist, so we create one. ExportSetting clientConfigSetting = export.FindSetting("CommunicationConfiguration"); ExportSetting legacyModeSetting = export.FindSetting("LegacyMode"); ExportSetting packetSizeSetting = export.FindSetting("PacketSize"); if ((clientConfigSetting != null) && !string.IsNullOrEmpty(clientConfigSetting.Value)) { // Create the client socket for the export context. context = new ExportContext(); context.Socket = ClientBase.Create(clientConfigSetting.Value + "; Port=0"); context.Socket.MaxConnectionAttempts = 1; // Try connection attempt no more than 1 time. // Determine the format of transmission. if (legacyModeSetting != null && Convert.ToBoolean(legacyModeSetting.Value)) { // Data is to be transmitted using PacketType1. context.TransmitHandler = TransmitPacketType1; if (packetSizeSetting != null) { // Custom packet size is specified, so determine the maximum number of data points in a packet. context.DataPerPacket = Convert.ToInt32(packetSizeSetting.Value) / PacketType1.FixedLength; } else { // Custom packet size is not specified, so used the defaults for the transmission protocol. context.DataPerPacket = (context.Socket.TransportProtocol == TransportProtocol.Tcp ? 50 : 1400); } } else { // Data is to be transmitted using PacketType101. context.TransmitHandler = TransmitPacketType101; if (packetSizeSetting != null) { // Custom packet size is specified, so determine the maximum number of data points in a packet. context.DataPerPacket = Convert.ToInt32(packetSizeSetting.Value) / PacketType101DataPoint.FixedLength; } else { // Custom packet size is not specified, so used the defaults for the transmission protocol. context.DataPerPacket = (context.Socket.TransportProtocol == TransportProtocol.Tcp ? 1000000 : 2200); } } // Save export context. m_contexts.Add(export.Name, context); // Provide a status update. OnStatusUpdate(string.Format("{0} based client created for export {1}", context.Socket.TransportProtocol.ToString().ToUpper(), export.Name)); } } } }
/// <summary> /// Processes the <paramref name="export"/> using the current <see cref="DataListener.Data"/>. /// </summary> /// <param name="export"><see cref="Export"/> to be processed.</param> /// <exception cref="ArgumentException"><b>OutputFile</b> setting is missing from the <see cref="Export.Settings"/> of the <paramref name="export"/>.</exception> protected override void ProcessExport(Export export) { // Ensure that required settings are present. ExportSetting outputFileSetting = export.FindSetting("OutputFile"); if (outputFileSetting == null) { throw new ArgumentException("OutputFile setting is missing"); } // Write the current data for the export to the specified files in CSV format. FileHelper.WriteToFile(outputFileSetting.Value, "CSV", GetExportDataAsDataset(export, null)); }
/// <summary> /// Processes the <paramref name="export"/> using the current <see cref="DataListener.Data"/>. /// </summary> /// <param name="export"><see cref="Export"/> to be processed.</param> /// <exception cref="ArgumentException"><b>OutputFile</b> or <b>OutputFormat</b> setting is missing from the <see cref="Export.Settings"/> of the <paramref name="export"/>.</exception> protected override void ProcessExport(Export export) { DataSet rawData = GetBufferedData(export.Name); try { // Ensure that required settings are present. ExportSetting outputFileSetting = export.FindSetting("OutputFile"); if (outputFileSetting == null) { throw new ArgumentException("OutputFile setting is missing"); } ExportSetting outputFormatSetting = export.FindSetting("OutputFormat"); if (outputFormatSetting == null) { throw new ArgumentException("OutputFormat setting is missing"); } if (rawData != null) { // Buffered data exists for exporting. lock (rawData) { // Update the export timestamp, row count and interval. rawData.Tables[1].Rows.Add(DateTime.UtcNow.ToString("MM/dd/yyyy hh:mm:ss tt"), rawData.Tables[0].Rows.Count, string.Format("{0} seconds", export.Interval)); // Write the buffered raw data for the export to the specified files in specified format. FileHelper.WriteToFile(outputFileSetting.Value, outputFormatSetting.Value, rawData); } } } catch { throw; } finally { if (rawData != null) { lock (rawData) { // Clear the buffered data to make space for new data that is to be buffered. rawData.Tables[0].Clear(); rawData.Tables[1].Clear(); } } } }
/// <summary> /// Processes the <paramref name="export"/> using the current <see cref="DataListener.Data"/>. /// </summary> /// <param name="export"><see cref="Export"/> to be processed.</param> /// <exception cref="ArgumentException"><b>OutputFile</b> or <b>OutputFormat</b> setting is missing from the <see cref="Export.Settings"/> of the <paramref name="export"/>.</exception> protected override void ProcessExport(Export export) { // Ensure that required settings are present. ExportSetting outputFileSetting = export.FindSetting("OutputFile"); if (outputFileSetting == null) { throw new ArgumentException("OutputFile setting is missing"); } ExportSetting outputFormatSetting = export.FindSetting("OutputFormat"); if (outputFormatSetting == null) { throw new ArgumentException("OutputFormat setting is missing"); } // Get the calculated statistics. Statistics stats = GetStatistics(export); // Get the dataset template we'll be outputting. DataSet output = DatasetTemplate("Statistics"); // Modify the dataset template for statistical values. DataTable data = output.Tables[0]; data.PrimaryKey = null; data.Columns.Clear(); data.Columns.Add("MinimumValue", typeof(double)); data.Columns.Add("MaximumValue", typeof(double)); data.Columns.Add("AverageValue", typeof(double)); foreach (DataColumn column in data.Columns) { column.ColumnMapping = MappingType.Attribute; } // Add the calculated statistical value. data.Rows.Add(stats.MinimumValue, stats.MaximumValue, stats.AverageValue); // Update the export timestamp, row count and interval. output.Tables[1].Rows.Add(DateTime.UtcNow.ToString("MM/dd/yyyy hh:mm:ss tt"), output.Tables[0].Rows.Count, string.Format("{0} seconds", export.Interval)); // Write the statistical data for the export to the specified files in specified format. FileHelper.WriteToFile(outputFileSetting.Value, outputFormatSetting.Value, output); }
private void CreateContext(Export export) { ExportContext context = null; lock (m_contexts) { if (!m_contexts.TryGetValue(export.Name, out context)) { // Context for the export does not exist, so we create one. ExportSetting serverPortSetting = export.FindSetting("ServerPort"); ExportSetting legacyModeSetting = export.FindSetting("LegacyMode"); if ((serverPortSetting != null) && !string.IsNullOrEmpty(serverPortSetting.Value)) { // Create the server socket for export context. context.Socket = ServerBase.Create(string.Format("Protocol=TCP;Port={0}", serverPortSetting.Value)); context.Socket.Start(); // Start the server; need to do only one time. context.Socket.ClientConnected += CommunicationServer_ClientConnected; context.Socket.ClientDisconnected += CommunicationServer_ClientDisconnected; // Determine the format of transmission. if (legacyModeSetting != null && Convert.ToBoolean(legacyModeSetting.Value)) { // Data is to be transmitted using PacketType1. context.DataPerPacket = 50; context.TransmitHandler = TransmitPacketType1; } else { // Data is to be transmitted using PacketType101. context.DataPerPacket = 1000000; context.TransmitHandler = TransmitPacketType101; } // Save export context m_contexts.Add(export.Name, context); // Provide a status update. OnStatusUpdate(string.Format("TCP server created for export {0}", export.Name)); } } } }
/// <summary> /// Returns the calculated <see cref="Statistics"/> from buffered data of the specified <paramref name="export"/>. /// </summary> /// <param name="export"><see cref="Export"/> for which statistical values are to be calculated.</param> /// <returns>The calculated <see cref="Statistics"/> from buffered data of the specified <paramref name="export"/>.</returns> protected virtual Statistics GetStatistics(Export export) { Statistics stats = new Statistics(); DataSet rawData = GetBufferedData(export.Name); ExportSetting filterClauseSetting = export.FindSetting("FilterClause"); ExportSetting slopeThresholdSetting = export.FindSetting("SlopeThreshold"); if (rawData != null) { // Buffered data is available for the export, so calculate the statistics. if (slopeThresholdSetting != null) { // Slope-based data elimination is to be employed. double slopeThreshold = Convert.ToDouble(slopeThresholdSetting.Value); lock (rawData) { foreach (ExportRecord record in export.Records) { // Run data for each record against the slope-based data elimination algorithm. double slope = 0; bool delete = false; DataRow[] records = rawData.Tables[0].Select(string.Format("Instance=\'{0}\' And ID={1}", record.Instance, record.Identifier), "TimeTag"); if (records.Length > 0) { for (int i = 0; i < records.Length - 1; i++) { // Calculate slope for the data using standard slope formula. slope = Math.Abs((((float)records[i]["Value"]) - (float)records[i + 1]["Value"]) / (Ticks.ToSeconds((Convert.ToDateTime(records[i]["TimeTag"])).Ticks) - Ticks.ToSeconds((Convert.ToDateTime(records[i + 1]["TimeTag"])).Ticks))); if (slope > slopeThreshold) { // Data for the point has slope that exceeds the specified slope threshold. delete = true; break; } } if (delete) { // Data for the record is to be excluded from the calculation. for (int i = 0; i < records.Length; i++) { records[i].Delete(); } OnStatusUpdate(string.Format("{0}:{1} data eliminated (Slope={2}; Threshold={3})", record.Instance, record.Identifier, slope, slopeThreshold)); } } } } } if (filterClauseSetting == null) { // No filter clause setting exist for the export. stats.Calculate(rawData, string.Empty); } else { // Filter clause setting exists for the export so use it. stats.Calculate(rawData, filterClauseSetting.Value); } } return(stats); }
/// <summary> /// Processes the <paramref name="export"/> using the current <see cref="DataListener.Data"/>. /// </summary> /// <param name="export"><see cref="Export"/> to be processed.</param> /// <exception cref="ArgumentException"><b>OutputFile</b>, <b>OutputFormat</b> or <b>OutputTimespan</b> setting is missing from the <see cref="Export.Settings"/> of the <paramref name="export"/>.</exception> protected override void ProcessExport(Export export) { // Ensure that required settings are present. ExportSetting outputFileSetting = export.FindSetting("OutputFile"); if (outputFileSetting == null) { throw new ArgumentException("OutputFile setting is missing"); } ExportSetting outputFormatSetting = export.FindSetting("OutputFormat"); if (outputFormatSetting == null) { throw new ArgumentException("OutputFormat setting is missing"); } ExportSetting outputTimespanSetting = export.FindSetting("OutputTimespan"); if (outputTimespanSetting == null) { throw new ArgumentException("OutputTimespan setting is missing"); } // Initialize local variables. DataSet output = null; string[] outputFiles = outputFileSetting.Value.Split(';', ','); DataSet current = GetExportDataAsDataset(export, null); DateTime dataStartDate = DateTime.UtcNow.AddMinutes(-Convert.ToInt32(outputTimespanSetting.Value)); // Make the output filenames absolute. for (int i = 0; i < outputFiles.Length; i++) { outputFiles[i] = FilePath.GetAbsolutePath(outputFiles[i].Trim()); } // Try to read-in data that might have been exported previously. foreach (string outputFile in outputFiles) { if (File.Exists(outputFile)) { // Wait for a lock on the file before reading. FilePath.WaitForReadLock(outputFile, FileLockWaitTime); // Read the file data and keep it in memory to be merged later. output = current.Clone(); switch (outputFormatSetting.Value.ToLower()) { case "xml": output.ReadXml(outputFile); break; case "csv": MergeTables(output.Tables[0], File.ReadAllText(outputFile).ToDataTable(",", true), dataStartDate); break; default: break; } break; // Don't look any further. } } if (output == null) { // Output will be just current data since there is no previously exported data. output = current; } else { // Merge previously exported data loaded from file with the current data for the export. MergeTables(output.Tables[0], current.Tables[0], dataStartDate); } // Delete any old data from the output that don't fall in the export's rolling window. string filterCondition = string.Format("Convert(Time, System.DateTime) < #{0}#", dataStartDate); foreach (DataRow row in output.Tables[0].Select(filterCondition)) { row.Delete(); } // Update the export timestamp, row count and interval. DataRowCollection info = output.Tables[1].Rows; info.Clear(); info.Add(DateTime.UtcNow.ToString("MM/dd/yyyy hh:mm:ss tt"), output.Tables[0].Rows.Count, string.Format("{0} seconds", export.Interval)); // Write the data for the export to the specified files in specified format. FileHelper.WriteToFile(outputFileSetting.Value, outputFormatSetting.Value, output); }