@@ -17,7 +17,7 @@ namespace WeatherDisp
17
17
{
18
18
public class WeatherInfo : HttpFunctionBase
19
19
{
20
- static int execCounter = 0 ;
20
+ static int execCounter ;
21
21
public WeatherInfo ( IHttpContextAccessor httpContextAccessor ) : base ( httpContextAccessor )
22
22
{
23
23
}
@@ -35,10 +35,10 @@ public async Task<IActionResult> Run(
35
35
return BadRequest ( ) ;
36
36
}
37
37
38
- int maxCount = 0 ;
39
- if ( int . TryParse ( Environment . GetEnvironmentVariable ( "MAX_EXEC" , EnvironmentVariableTarget . Process ) , out maxCount )
40
- && maxCount > 0
41
- && execCounter <= maxCount ) {
38
+ if ( int . TryParse ( Environment . GetEnvironmentVariable ( "MAX_EXEC" , EnvironmentVariableTarget . Process ) , out int maxCount )
39
+ && maxCount > 0
40
+ && execCounter <= maxCount )
41
+ {
42
42
model . DarkskyKey = Environment . GetEnvironmentVariable ( "DARKSKY_API" , EnvironmentVariableTarget . Process ) ?? model . DarkskyKey ;
43
43
model . Lag = Environment . GetEnvironmentVariable ( "LAG" , EnvironmentVariableTarget . Process ) ?? model . Lag ;
44
44
model . Lat = Environment . GetEnvironmentVariable ( "LAT" , EnvironmentVariableTarget . Process ) ?? model . Lat ;
@@ -50,51 +50,86 @@ public async Task<IActionResult> Run(
50
50
}
51
51
52
52
log . LogInformation ( $ "Lag:{ model . Lag } , Lat:{ model . Lat } ") ;
53
-
54
- using ( var browser = await Puppeteer . LaunchAsync ( new LaunchOptions ( )
53
+
54
+ using var browser = await Puppeteer . LaunchAsync ( new LaunchOptions ( )
55
55
{
56
56
Headless = true ,
57
57
LogProcess = true ,
58
58
Args = new [ ]
59
59
{
60
60
"--no-sandbox" ,
61
61
"--disable-web-security"
62
+ } ,
63
+ DumpIO = true
64
+ } ) ;
65
+ using var page = await browser . NewPageAsync ( ) ;
66
+ page . Console += ( target , e ) => {
67
+ switch ( e . Message . Type )
68
+ {
69
+ case ConsoleType . Error :
70
+ log . LogError ( $ "ConsoleError:{ e . Message . Text } ") ;
71
+ break ;
72
+ case ConsoleType . Warning :
73
+ log . LogWarning ( $ "ConsoleWarning:{ e . Message . Text } ") ;
74
+ break ;
75
+ default :
76
+ log . LogInformation ( $ "Console{ e . Message . Type } :{ e . Message . Text } ") ;
77
+ break ;
62
78
}
63
- } ) )
79
+ } ;
80
+ page . Error += ( target , e ) =>
81
+ {
82
+ log . LogError ( $ "Error:{ e . Error } ") ;
83
+ } ;
84
+ page . PageError += ( target , e ) =>
85
+ {
86
+ log . LogError ( $ "PageError:{ e . Message } ") ;
87
+ } ;
88
+ page . Response += ( target , e ) =>
89
+ {
90
+ log . LogInformation ( $ "Response:{ e . Response . Status } :{ e . Response . StatusText } ") ;
91
+ } ;
92
+ page . RequestFailed += ( target , e ) =>
93
+ {
94
+ log . LogError ( $ "RequestFailed:{ e . Request . Failure } ") ;
95
+ } ;
96
+ await page . SetViewportAsync ( new ViewPortOptions ( )
97
+ {
98
+ Width = 298 ,
99
+ Height = 128
100
+ } ) ;
101
+
102
+ using ( var fs = new StreamReader ( Path . Combine ( context . FunctionAppDirectory , Path . Combine ( "dist" , "index.html" ) ) ) )
64
103
{
65
- using var page = await browser . NewPageAsync ( ) ;
66
- await page . SetViewportAsync ( new ViewPortOptions ( )
104
+ var html = ( await fs . ReadToEndAsync ( ) )
105
+ . Replace ( "DARKSKY_KEY_PLACEHOLDER" , model . DarkskyKey , StringComparison . OrdinalIgnoreCase )
106
+ . Replace ( "LAT_PLACEHOLDER" , model . Lat , StringComparison . OrdinalIgnoreCase )
107
+ . Replace ( "LAG_PLACEHOLDER" , model . Lag , StringComparison . OrdinalIgnoreCase ) ;
108
+ await page . SetContentAsync ( html , new NavigationOptions ( )
67
109
{
68
- Width = 298 ,
69
- Height = 128
110
+ WaitUntil = new [ ] { WaitUntilNavigation . Networkidle0 }
70
111
} ) ;
112
+ }
71
113
72
- using ( var fs = new StreamReader ( Path . Combine ( context . FunctionAppDirectory , Path . Combine ( "dist" , "index.html" ) ) ) )
73
- {
74
- var html = ( await fs . ReadToEndAsync ( ) )
75
- . Replace ( "DARKSKY_KEY_PLACEHOLDER" , model . DarkskyKey , StringComparison . OrdinalIgnoreCase )
76
- . Replace ( "LAT_PLACEHOLDER" , model . Lat , StringComparison . OrdinalIgnoreCase )
77
- . Replace ( "LAG_PLACEHOLDER" , model . Lag , StringComparison . OrdinalIgnoreCase ) ;
78
- await page . SetContentAsync ( html , new NavigationOptions ( )
114
+ using var im = new MagickImage ( await page . ScreenshotDataAsync (
115
+ new ScreenshotOptions ( )
79
116
{
80
- WaitUntil = new [ ] { WaitUntilNavigation . Networkidle0 }
81
- } ) ;
82
- }
83
-
84
- using var im = new MagickImage ( await page . ScreenshotDataAsync (
85
- new ScreenshotOptions ( )
86
- {
87
- Type = ScreenshotType . Png
88
- } ) ) ;
89
- im . Map ( new [ ] {
90
- new MagickColor ( 0 , 0 , 0 ) ,
91
- new MagickColor ( 255 , 255 , 255 )
92
- } , new QuantizeSettings ( ) {
117
+ Type = ScreenshotType . Png
118
+ } ) ) ;
119
+ im . Map ( new [ ] {
120
+ new MagickColor ( 0 , 0 , 0 ) ,
121
+ new MagickColor ( 255 , 255 , 255 )
122
+ } , new QuantizeSettings ( )
123
+ {
93
124
DitherMethod = DitherMethod . No
94
125
} ) ;
95
- im . Quality = 100 ;
96
- return File ( im . ToByteArray ( MagickFormat . Jpg ) , "image/jpeg" ) ;
97
- }
126
+ im . Quality = 100 ;
127
+ return File ( im . ToByteArray ( MagickFormat . Jpg ) , "image/jpeg" ) ;
128
+ }
129
+
130
+ private void Page_Console ( object sender , ConsoleEventArgs e )
131
+ {
132
+ throw new NotImplementedException ( ) ;
98
133
}
99
134
}
100
135
0 commit comments