@@ -37,7 +37,7 @@ public class PrinterModel
3737 private const string HttpError = "Ошибка сети" ;
3838
3939 private const string SumatraError =
40- "[Error] program SumatraPdf is not found\n inform the responsible person\n \n [Ошибка] программа SumatraPdf не найдена\n сообщите ответственному лицу" ;
40+ "[Error] program SumatraPdf is not found\n inform the responsible person\n \n [Ошибка] программа SumatraPdf не найдена\n сообщите ответственному лицу. " ;
4141
4242 private static readonly string SumatraPathSuffix =
4343 Path . DirectorySeparatorChar + "SumatraPDF" +
@@ -60,6 +60,8 @@ public class PrinterModel
6060
6161 public PrinterModel ( ConfigFile configFile , AutoUpdater autoUpdater )
6262 {
63+ Log . Information ( "=== [CONSTRUCTOR START] PrinterModel initialization ===" ) ;
64+
6365 _configFile = configFile ;
6466 _autoUpdater = autoUpdater ;
6567
@@ -71,40 +73,71 @@ public PrinterModel(ConfigFile configFile, AutoUpdater autoUpdater)
7173 _httpClient . DefaultRequestHeaders . Authorization
7274 = new AuthenticationHeaderValue ( _configFile . AuthorizationToken ) ;
7375
76+ Log . Information ( $ "[CONSTRUCTOR] Token set: { _configFile . AuthorizationToken } ") ;
77+ Log . Information ( $ "[CONSTRUCTOR] API URL: { ApiUrl } ") ;
78+
7479 try
7580 {
81+ Log . Information ( $ "[CONSTRUCTOR] Calling { ApiUrl } /auth/me") ;
7682 var response = _httpClient . GetAsync ( $ "{ ApiUrl } /auth/me") ;
7783 response . Wait ( 5000 ) ;
84+
85+ Log . Information ( $ "[CONSTRUCTOR] Response status code: { response . Result . StatusCode } ") ;
7886 response . Result . EnsureSuccessStatusCode ( ) ;
87+
7988 var responseBody = response . Result . Content . ReadAsStringAsync ( ) ;
8089 responseBody . Wait ( 1000 ) ;
8190 var responceString = responseBody . Result ;
91+
92+ Log . Information ( $ "[CONSTRUCTOR] Response body: { responceString } ") ;
93+
8294 var htmlAttributes =
8395 JsonConvert . DeserializeObject < Dictionary < string , string > > ( responceString ) ??
8496 throw new InvalidOperationException ( ) ;
85- Log . Information ( htmlAttributes [ "id" ] ) ;
86- Marketing . TerminalUserId = htmlAttributes [ "id" ] ;
97+
98+ var terminalId = htmlAttributes [ "id" ] ;
99+ Log . Information ( $ "[CONSTRUCTOR] Terminal ID: { terminalId } ") ;
100+ Marketing . TerminalUserId = terminalId ;
87101 }
88102 catch ( Exception e )
89103 {
90- Log . Error ( $ "{ GetType ( ) . Name } { MethodBase . GetCurrentMethod ( ) ? . Name } : { e } ") ;
104+ Log . Error ( $ "[CONSTRUCTOR] *** AUTH FAILED ***") ;
105+ Log . Error ( $ "[CONSTRUCTOR] Exception type: { e . GetType ( ) . Name } ") ;
106+ Log . Error ( $ "[CONSTRUCTOR] Exception message: { e . Message } ") ;
107+ Log . Error ( $ "[CONSTRUCTOR] Inner exception: { e . InnerException } ") ;
108+ Log . Error ( $ "[CONSTRUCTOR] Stack trace: { e . StackTrace } ") ;
109+
91110 Marketing . TerminalUserIdError ( ) ;
92111 MessageBox . Show (
93112 "Терминал не смог получить id. Сообщите ответственному лицу. Перезапустите программу." ,
94113 "Ошибка" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
95114 Close ( ) ;
115+ return ;
96116 }
97117
98-
99- if ( SearchSumatraPdf ( ) == "" )
118+ Log . Information ( "[CONSTRUCTOR] Auth check passed" ) ;
119+ Log . Information ( "[CONSTRUCTOR] Searching for SumatraPDF..." ) ;
120+
121+ var sumatraPath = SearchSumatraPdf ( ) ;
122+ Log . Information ( $ "[CONSTRUCTOR] SumatraPDF search result: '{ sumatraPath } '") ;
123+
124+ if ( sumatraPath == "" )
100125 {
126+ Log . Error ( "[CONSTRUCTOR] *** SUMATRPDF NOT FOUND ***" ) ;
101127 MessageBox . Show ( SumatraError ) ;
102128 Close ( ) ;
129+ return ;
103130 }
104131
132+ Log . Information ( $ "[CONSTRUCTOR] SumatraPDF found at: { sumatraPath } ") ;
133+ Log . Information ( "[CONSTRUCTOR] Starting WebSocket connection..." ) ;
134+
105135 SocketsStartAsync ( ) ;
106136
137+ Log . Information ( "[CONSTRUCTOR] Calling AsyncSaveScreen..." ) ;
107138 AsyncSaveScreen ( "LoadTerminal" ) ;
139+
140+ Log . Information ( "=== [CONSTRUCTOR END] PrinterModel initialization completed ===" ) ;
108141 }
109142
110143 ~ PrinterModel ( )
@@ -114,30 +147,61 @@ public PrinterModel(ConfigFile configFile, AutoUpdater autoUpdater)
114147
115148 private static void Close ( )
116149 {
150+ Log . Information ( "[CLOSE] Closing application..." ) ;
117151 Log . CloseAndFlush ( ) ;
118152 Environment . Exit ( 0 ) ;
119153 }
120154
121155 private static string SearchSumatraPdf ( )
122156 {
157+ Log . Debug ( "[SUMATRPDF] Starting search..." ) ;
158+
123159 var path =
124160 Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) +
125161 SumatraPathSuffix ;
162+ Log . Debug ( $ "[SUMATRPDF] Checking: { path } ") ;
126163 if ( File . Exists ( path ) )
164+ {
165+ Log . Information ( $ "[SUMATRPDF] Found at LocalApplicationData: { path } ") ;
127166 return path ;
167+ }
168+
128169 path = Directory . GetCurrentDirectory ( ) + SumatraPathSuffix ;
170+ Log . Debug ( $ "[SUMATRPDF] Checking: { path } ") ;
129171 if ( File . Exists ( path ) )
172+ {
173+ Log . Information ( $ "[SUMATRPDF] Found at CurrentDirectory: { path } ") ;
130174 return path ;
175+ }
176+
131177 path = Directory . GetCurrentDirectory ( ) + Path . DirectorySeparatorChar + "SumatraPDF.exe" ;
178+ Log . Debug ( $ "[SUMATRPDF] Checking: { path } ") ;
132179 if ( File . Exists ( path ) )
180+ {
181+ Log . Information ( $ "[SUMATRPDF] Found at CurrentDirectory root: { path } ") ;
133182 return path ;
183+ }
184+
134185 path = Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFiles ) +
135186 SumatraPathSuffix ;
187+ Log . Debug ( $ "[SUMATRPDF] Checking: { path } ") ;
136188 if ( File . Exists ( path ) )
189+ {
190+ Log . Information ( $ "[SUMATRPDF] Found at ProgramFiles: { path } ") ;
137191 return path ;
192+ }
193+
138194 path = Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFilesX86 ) +
139195 SumatraPathSuffix ;
140- return File . Exists ( path ) ? path : "" ;
196+ Log . Debug ( $ "[SUMATRPDF] Checking: { path } ") ;
197+ if ( File . Exists ( path ) )
198+ {
199+ Log . Information ( $ "[SUMATRPDF] Found at ProgramFilesX86: { path } ") ;
200+ return path ;
201+ }
202+
203+ Log . Error ( "[SUMATRPDF] NOT FOUND in any location!" ) ;
204+ return "" ;
141205 }
142206
143207 private static void AsyncSaveScreen ( string imageName = "" )
@@ -384,54 +448,116 @@ public void SocketsClose()
384448
385449 private async void SocketsStartAsync ( )
386450 {
451+ Log . Information ( "=== [WEBSOCKET START] ===" ) ;
452+ Log . Information ( $ "[WEBSOCKET] Target URL: { WebSockUrl } ") ;
453+
387454 var socket = new ClientWebSocket ( ) ;
388- socket . Options . SetRequestHeader ( "Authorization" ,
389- _httpClient . DefaultRequestHeaders . Authorization ! . ToString ( ) ) ;
455+
390456 try
391457 {
392- await socket . ConnectAsync ( new Uri ( WebSockUrl ) , CancellationToken . None ) ;
458+ Log . Information ( $ "[WEBSOCKET] Authorization header: { _httpClient . DefaultRequestHeaders . Authorization } ") ;
459+ socket . Options . SetRequestHeader ( "Authorization" ,
460+ _httpClient . DefaultRequestHeaders . Authorization ! . ToString ( ) ) ;
461+
462+ Log . Information ( "[WEBSOCKET] Attempting connection..." ) ;
463+
464+ // Добавим timeout
465+ var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 10 ) ) ;
466+ await socket . ConnectAsync ( new Uri ( WebSockUrl ) , cts . Token ) ;
467+
468+ Log . Information ( $ "[WEBSOCKET] Connected! State: { socket . State } ") ;
469+
393470 if ( socket . State != WebSocketState . Open )
394471 {
472+ Log . Error ( $ "[WEBSOCKET] *** STATE IS NOT OPEN: { socket . State } ***") ;
395473 Marketing . SocketException (
396474 status : $ "WebSocketState not Open state:{ socket . State } ") ;
397475 Log . Error (
398476 $ "{ GetType ( ) . Name } { MethodBase . GetCurrentMethod ( ) ? . Name } : WebSocketState not Open state:{ socket . State } ") ;
399477 return ;
400478 }
401479
480+ Log . Information ( "[WEBSOCKET] Socket state is OPEN, starting message loop..." ) ;
402481 Marketing . SocketConnected ( ) ;
403482 _socketClose = false ;
404483 var buffer = new byte [ 128 * 1024 ] ;
484+
405485 while ( ! _socketClose )
406486 {
487+ Log . Debug ( "[WEBSOCKET] Waiting for message..." ) ;
407488 var result = await socket . ReceiveAsync ( new ArraySegment < byte > ( buffer ) ,
408489 CancellationToken . None ) ;
490+
491+ Log . Debug ( $ "[WEBSOCKET] Received { result . Count } bytes, EndOfMessage: { result . EndOfMessage } ") ;
492+
409493 var json = Encoding . UTF8 . GetString ( buffer , 0 , result . Count ) ;
494+ Log . Debug ( $ "[WEBSOCKET] Message content: { json } ") ;
495+
410496 if ( ! result . EndOfMessage )
411497 {
412498 Thread . Sleep ( 100 ) ;
413499 result = await socket . ReceiveAsync ( new ArraySegment < byte > ( buffer ) ,
414500 CancellationToken . None ) ;
415501 json += Encoding . UTF8 . GetString ( buffer , 0 , result . Count ) ;
502+ Log . Debug ( $ "[WEBSOCKET] Combined message: { json } ") ;
416503 }
417504
418505 await ParseResponseFromSocket (
419506 JsonConvert . DeserializeObject < WebsocketReceiveOptions > ( json ) ) ;
420507 }
421508
509+ Log . Information ( "[WEBSOCKET] Closing connection..." ) ;
422510 await socket . CloseAsync ( WebSocketCloseStatus . NormalClosure , "Good Bye" ,
423511 CancellationToken . None ) ;
512+ Log . Information ( "[WEBSOCKET] Closed successfully" ) ;
513+ }
514+ catch ( OperationCanceledException timeoutEx )
515+ {
516+ Log . Error ( "[WEBSOCKET] *** TIMEOUT EXCEPTION ***" ) ;
517+ Log . Error ( $ "[WEBSOCKET] Message: { timeoutEx . Message } ") ;
518+ Log . Error ( $ "[WEBSOCKET] Stack trace: { timeoutEx . StackTrace } ") ;
519+ _socketClose = true ;
520+ Marketing . SocketException ( status : $ "Timeout: { timeoutEx . Message } ") ;
521+ socket . Abort ( ) ;
522+ }
523+ catch ( WebSocketException wsEx )
524+ {
525+ Log . Error ( "[WEBSOCKET] *** WEBSOCKET EXCEPTION ***" ) ;
526+ Log . Error ( $ "[WEBSOCKET] Type: { wsEx . GetType ( ) . Name } ") ;
527+ Log . Error ( $ "[WEBSOCKET] Message: { wsEx . Message } ") ;
528+ Log . Error ( $ "[WEBSOCKET] Inner exception: { wsEx . InnerException ? . GetType ( ) . Name } ") ;
529+ Log . Error ( $ "[WEBSOCKET] Inner message: { wsEx . InnerException ? . Message } ") ;
530+ Log . Error ( $ "[WEBSOCKET] Stack trace: { wsEx . StackTrace } ") ;
531+ _socketClose = true ;
532+ Marketing . SocketException ( status : wsEx . Message ) ;
533+ socket . Abort ( ) ;
534+
535+ Log . Information ( "[WEBSOCKET] Retrying in 5 seconds..." ) ;
536+ await Task . Delay ( 5000 ) ;
537+ SocketsStartAsync ( ) ;
424538 }
425539 catch ( Exception exception )
426540 {
541+ Log . Error ( "[WEBSOCKET] *** GENERAL EXCEPTION ***" ) ;
542+ Log . Error ( $ "[WEBSOCKET] Type: { exception . GetType ( ) . Name } ") ;
543+ Log . Error ( $ "[WEBSOCKET] Message: { exception . Message } ") ;
544+ Log . Error ( $ "[WEBSOCKET] Inner exception: { exception . InnerException ? . GetType ( ) . Name } ") ;
545+ Log . Error ( $ "[WEBSOCKET] Inner message: { exception . InnerException ? . Message } ") ;
546+ Log . Error ( $ "[WEBSOCKET] Stack trace: { exception . StackTrace } ") ;
547+
427548 _socketClose = true ;
428549 Marketing . SocketException ( status : exception . Message ) ;
429- Log . Error ( $ "{ GetType ( ) . Name } { MethodBase . GetCurrentMethod ( ) ? . Name } : { exception } ") ;
430550 PrinterViewModel . PrintQr = null ! ;
431551 socket . Abort ( ) ;
552+
553+ Log . Information ( "[WEBSOCKET] Retrying in 5 seconds..." ) ;
432554 await Task . Delay ( 5000 ) ;
433555 SocketsStartAsync ( ) ;
434556 }
557+ finally
558+ {
559+ Log . Information ( "=== [WEBSOCKET END] ===" ) ;
560+ }
435561 }
436562
437563 private async Task ParseResponseFromSocket ( WebsocketReceiveOptions ? websocketReceiveOptions )
@@ -535,4 +661,4 @@ private void GenerateQr(string value)
535661 PrinterViewModel . PrintQr = null ! ;
536662 }
537663 }
538- }
664+ }
0 commit comments