1- using System ;
1+ using Microsoft . Extensions . Options ;
2+ using System ;
23using System . Collections . Generic ;
3- using System . Globalization ;
44using System . IO ;
55using System . Linq ;
6- using System . Text ;
76using System . Threading ;
87using System . Threading . Tasks ;
98
109namespace CFNReader ;
1110
12- public class CFNCSVConverter (
13- IEnumerable < Channel > ? channels = null ,
14- Func < Datapoint , bool > ? predicate = null ,
15- IFormatProvider ? formatProvider = null ,
16- string separator = ";" ,
17- Encoding ? encoding = null ,
18- string timeFormat = "G" ,
19- string valueFormat = "N4" ,
20- bool includeHeader = true ,
21- bool includeTime = true ,
22- bool includeUnit = false ,
23- int limitDataPoints = int . MaxValue
24- )
11+ public class CFNCSVConverter ( IOptions < CFNCSVConverterOptions > options )
2512{
26- private readonly IEnumerable < Channel > ? _channels = channels ;
27- private readonly Func < Datapoint , bool > _predicate = predicate ?? ( _ => true ) ;
28- private readonly IFormatProvider _formatprovider = formatProvider ?? CultureInfo . InvariantCulture ;
29- private readonly string _separator = separator ;
30- private readonly Encoding _encoding = encoding ?? Encoding . UTF8 ;
31- private readonly string _timeformat = timeFormat ;
32- private readonly string _valueformat = valueFormat ;
33- private readonly bool _includeheader = includeHeader ;
34- private readonly bool _includetime = includeTime ;
35- private readonly bool _includeunit = includeUnit ;
36- private readonly int _maxdatapoints = limitDataPoints ;
13+ private readonly CFNCSVConverterOptions _options = options ? . Value ?? throw new ArgumentNullException ( nameof ( options ) ) ;
14+
15+ public CFNCSVConverter ( CFNCSVConverterOptions options )
16+ : this ( Options . Create ( options ) ) { }
3717
3818 public async Task ConvertToCSVAsync ( Stream cnfStream , Stream csvStream , CancellationToken cancellationToken = default )
3919 {
4020 var cfnreader = new CFNStreamReader ( cnfStream ) ;
4121
4222 var fileinfo = await cfnreader . ReadFileInfoAsync ( cancellationToken ) ;
4323
44- var exportchannels = ( _channels ?? fileinfo . Channels . Keys ) . Where ( fileinfo . Channels . ContainsKey ) . ToArray ( ) ;
24+ var exportchannels = ( _options . Channels ?? fileinfo . Channels . Keys ) . Where ( fileinfo . Channels . ContainsKey ) . ToArray ( ) ;
4525
46- using var sw = new StreamWriter ( csvStream , _encoding , - 1 , true ) ;
26+ using var sw = new StreamWriter ( csvStream , _options . Encoding , - 1 , true ) ;
4727
48- if ( _includeheader )
28+ if ( _options . IncludeHeader )
4929 {
50- await sw . WriteLineAsync ( string . Join ( _separator , GetHeaderNames ( exportchannels ) ) ) ;
30+ await sw . WriteLineAsync ( string . Join ( _options . Separator , GetHeaderNames ( exportchannels ) ) ) ;
5131 }
5232
53- await foreach ( var dp in cfnreader . ReadDatapointsAsync ( fileinfo , cancellationToken ) . Where ( _predicate ) . Take ( _maxdatapoints ) )
33+ await foreach ( var dp in cfnreader . ReadDatapointsAsync ( fileinfo , cancellationToken ) . Where ( _options . Predicate ) . Take ( _options . LimitDataPoints ) )
5434 {
55- await sw . WriteLineAsync ( string . Join ( _separator , GetValues ( exportchannels , dp ) ) ) ;
35+ await sw . WriteLineAsync ( string . Join ( _options . Separator , GetValues ( exportchannels , dp ) ) ) ;
5636 }
5737 }
5838
5939 private IEnumerable < string > GetHeaderNames ( Channel [ ] channels )
6040 {
61- if ( _includetime )
41+ if ( _options . IncludeTime )
6242 {
6343 yield return nameof ( Datapoint . Time ) ;
6444 }
@@ -70,9 +50,9 @@ private IEnumerable<string> GetHeaderNames(Channel[] channels)
7050
7151 private IEnumerable < string > GetValues ( Channel [ ] channels , Datapoint datapoint )
7252 {
73- if ( _includetime )
53+ if ( _options . IncludeTime )
7454 {
75- yield return datapoint . Time . ToString ( _timeformat , _formatprovider ) ;
55+ yield return datapoint . Time . ToString ( _options . TimeFormat , _options . FormatProvider ) ;
7656 }
7757 foreach ( var channel in channels )
7858 {
@@ -81,5 +61,5 @@ private IEnumerable<string> GetValues(Channel[] channels, Datapoint datapoint)
8161 }
8262
8363 private string FormatValue ( UnitValue value )
84- => $ "{ value . Value . ToString ( _valueformat , _formatprovider ) } { ( _includeunit ? value . Unit : string . Empty ) } ";
64+ => $ "{ value . Value . ToString ( _options . ValueFormat , _options . FormatProvider ) } { ( _options . IncludeUnit ? value . Unit : string . Empty ) } ";
8565}
0 commit comments