22using NLog . Extensions . Logging ;
33using Microsoft . Extensions . Configuration ;
44using CommandLine ;
5+ using RS485_Monitor . Utils . Storage ;
6+ using System . Runtime . CompilerServices ;
7+
8+ const string FILE_EXTENSION = ".ssm" ;
59
610// Configuration
711var config = new ConfigurationBuilder ( )
1014
1115var sec = config . GetSection ( "NLog" ) ;
1216LogManager . Configuration = new NLogLoggingConfiguration ( sec ) ;
13- NLog . Logger log = LogManager . GetCurrentClassLogger ( ) ;
17+ Logger log = LogManager . GetCurrentClassLogger ( ) ;
1418
1519Configuration cfg = new ( config . GetSection ( "Monitor" ) ) ;
1620
17- // Commandline parser
18- String ? parseFile = null ;
21+ // command line parser
22+ string ? parseFile = null ;
1923bool replayToSerial = false ;
2024bool groupOutput = false ;
21-
25+ bool writeToFile = false ;
2226
2327var result = Parser . Default . ParseArguments < CmdOptions > ( args )
24- . WithParsed < CmdOptions > ( o =>
28+ . WithParsed ( o =>
2529 {
2630 if ( o . InputFile != null )
2731 {
2832 parseFile = o . InputFile ;
2933 }
30- replayToSerial = o . replayOnSerial ;
31- groupOutput = o . groupOutput ;
34+ replayToSerial = o . ReplayOnSerial ;
35+ groupOutput = o . GroupOutput ;
36+ writeToFile = o . WriteToFile ;
3237 }
3338 ) ;
3439
4045}
4146
4247// Configure output
43- IUserVisualizable printer ;
44- if ( groupOutput )
45- {
46- printer = new ConsolePrinter ( ) ;
47- }
48- else
49- {
50- printer = new LogPrinter ( ) ;
51- }
48+ IUserVisualizable printer = groupOutput ? new ConsolePrinter ( ) : new LogPrinter ( ) ;
5249
5350// Select execution
5451if ( parseFile != null )
6360 StartMonitor ( cfg ) ;
6461}
6562
63+ /// <summary>
64+ /// Start the serial monitor
65+ /// </summary>
66+ void StartMonitor ( Configuration cfg )
67+ {
68+ // Semaphore for ending the monitor
69+ Semaphore endMonitor = new ( 0 , 1 ) ;
70+
71+ // Register CTRL+C
72+ Console . CancelKeyPress += new ConsoleCancelEventHandler ( ( sender , args ) =>
73+ {
74+ log . Info ( "Exiting ..." ) ;
75+
76+ // Set cancel to true to prevent default CTRL+C behaviour
77+ args . Cancel = true ;
78+
79+ // release semaphore
80+ endMonitor . Release ( ) ;
81+ } ) ;
82+
83+ log . Info ( $ "Starting Serial Monitor on port { cfg . ComPort } ") ;
84+
85+ // create telegram exporter
86+ TelegramExporter ? exporter = null ;
87+ if ( writeToFile )
88+ {
89+ FileInfo ? outputFile = null ;
90+ string filename = DateTime . Now . ToString ( "yyyyMMdd_HHmmss" ) + "_telegram" + FILE_EXTENSION ;
91+
92+ // Determine full output file path
93+ if ( cfg . OutputDir != null )
94+ {
95+ outputFile = new ( cfg . OutputDir + "/" + filename ) ;
96+ }
97+ else
98+ {
99+ outputFile = new ( filename ) ;
100+ }
101+
102+ // create exporter
103+ exporter = new ( outputFile . OpenWrite ( ) ) ;
104+ }
105+
106+ // Start serial monitor on the given port
107+ using ( SerialMonitor monitor = new ( cfg . ComPort , cfg . WriteRawData ) )
108+ {
109+ if ( cfg . OutputDir != null )
110+ {
111+ monitor . OutputDir = cfg . OutputDir ;
112+ }
113+ monitor . TelegramReceived += ( o , e ) =>
114+ {
115+ // Print the received telegram
116+ printer . PrintTelegram ( e . Telegram ) ;
117+ exporter ? . PushTelegram ( e . Telegram ) ;
118+ } ;
119+
120+ // Start reading monitor
121+ try
122+ {
123+ monitor . Start ( ) ;
124+ }
125+ catch ( IOException ex )
126+ {
127+ log . Fatal ( ex , "Could not start monitor" ) ;
128+ return ;
129+ }
130+
131+ // Wait for CTRL+C
132+ endMonitor . WaitOne ( ) ;
133+ }
134+ log . Info ( "Monitor stopped" ) ;
135+
136+ // close exporter
137+ exporter ? . Dispose ( ) ;
138+ }
139+
66140
67141/// <summary>
68142/// Read a local file and parse it
@@ -72,7 +146,7 @@ async Task ReadLocalFile(String file)
72146 TelegramParser parser = new ( ) ;
73147 TelegramPlayer player = new ( cfg . ReplayCycle ) ;
74148
75- // Register new telegram handler
149+ // Register new telegram handler
76150 parser . NewTelegram += ( sender , evt ) =>
77151 {
78152 BaseTelegram telegram = ( ( TelegramParser . TelegramArgs ) evt ) . Telegram ;
@@ -105,49 +179,6 @@ async Task ReadLocalFile(String file)
105179}
106180
107181
108- /// <summary>
109- /// Start the serial monitor
110- /// </summary>
111- void StartMonitor ( Configuration cfg )
112- {
113- log . Info ( $ "Starting Serial Monitor on port { cfg . ComPort } ") ;
114-
115- SerialMonitor monitor = new ( cfg . ComPort , cfg . WriteRawData ) ;
116- if ( cfg . OutputDir != null )
117- {
118- monitor . OutputDir = cfg . OutputDir ;
119- }
120- monitor . TelegramReceived += ( o , e ) =>
121- {
122- // Print the received telegram
123- printer . PrintTelegram ( e . Telegram ) ;
124- } ;
125-
126- // Register CTRL+C
127- Console . CancelKeyPress += delegate
128- {
129- log . Info ( "Exiting ..." ) ;
130- monitor . Stop ( ) ;
131- } ;
132-
133- // Start reading monitor
134- try
135- {
136- monitor . Start ( ) ;
137- }
138- catch ( System . IO . IOException ex )
139- {
140- log . Fatal ( ex , "Could not start monitor" ) ;
141- return ;
142- }
143-
144- // Endless loop
145- while ( monitor . Running )
146- {
147- Thread . Sleep ( 10 ) ;
148- }
149- }
150-
151182
152183/// <summary>
153184/// Read a local file and parse it
0 commit comments