1
- using System ;
1
+ using Microsoft . Extensions . Options ;
2
+ using System ;
2
3
using System . Collections . Generic ;
3
- using System . Globalization ;
4
4
using System . IO ;
5
5
using System . Linq ;
6
- using System . Text ;
7
6
using System . Threading ;
8
7
using System . Threading . Tasks ;
9
8
10
9
namespace CFNReader ;
11
10
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 )
25
12
{
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 ) ) { }
37
17
38
18
public async Task ConvertToCSVAsync ( Stream cnfStream , Stream csvStream , CancellationToken cancellationToken = default )
39
19
{
40
20
var cfnreader = new CFNStreamReader ( cnfStream ) ;
41
21
42
22
var fileinfo = await cfnreader . ReadFileInfoAsync ( cancellationToken ) ;
43
23
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 ( ) ;
45
25
46
- using var sw = new StreamWriter ( csvStream , _encoding , - 1 , true ) ;
26
+ using var sw = new StreamWriter ( csvStream , _options . Encoding , - 1 , true ) ;
47
27
48
- if ( _includeheader )
28
+ if ( _options . IncludeHeader )
49
29
{
50
- await sw . WriteLineAsync ( string . Join ( _separator , GetHeaderNames ( exportchannels ) ) ) ;
30
+ await sw . WriteLineAsync ( string . Join ( _options . Separator , GetHeaderNames ( exportchannels ) ) ) ;
51
31
}
52
32
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 ) )
54
34
{
55
- await sw . WriteLineAsync ( string . Join ( _separator , GetValues ( exportchannels , dp ) ) ) ;
35
+ await sw . WriteLineAsync ( string . Join ( _options . Separator , GetValues ( exportchannels , dp ) ) ) ;
56
36
}
57
37
}
58
38
59
39
private IEnumerable < string > GetHeaderNames ( Channel [ ] channels )
60
40
{
61
- if ( _includetime )
41
+ if ( _options . IncludeTime )
62
42
{
63
43
yield return nameof ( Datapoint . Time ) ;
64
44
}
@@ -70,9 +50,9 @@ private IEnumerable<string> GetHeaderNames(Channel[] channels)
70
50
71
51
private IEnumerable < string > GetValues ( Channel [ ] channels , Datapoint datapoint )
72
52
{
73
- if ( _includetime )
53
+ if ( _options . IncludeTime )
74
54
{
75
- yield return datapoint . Time . ToString ( _timeformat , _formatprovider ) ;
55
+ yield return datapoint . Time . ToString ( _options . TimeFormat , _options . FormatProvider ) ;
76
56
}
77
57
foreach ( var channel in channels )
78
58
{
@@ -81,5 +61,5 @@ private IEnumerable<string> GetValues(Channel[] channels, Datapoint datapoint)
81
61
}
82
62
83
63
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 ) } ";
85
65
}
0 commit comments