Winify – Diff between revs 24 and 25
?pathlinks?
Rev 24 | Rev 25 | |||
---|---|---|---|---|
Line 22... | Line 22... | |||
22 | |
22 | |
|
Line 23... | Line 23... | |||
23 | #endregion |
23 | #endregion |
|
Line -... | Line 24... | |||
- | 24 | |
||
- | 25 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
24 | |
26 | |
|
Line 25... | Line 27... | |||
25 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
27 | private readonly Server _server; |
|
Line 26... | Line -... | |||
26 | |
- | ||
27 | private CancellationToken _cancellationToken; |
- | ||
28 | |
28 | |
|
Line 29... | Line 29... | |||
29 | private CancellationTokenSource _cancellationTokenSource; |
29 | private CancellationToken _cancellationToken; |
|
Line 30... | Line 30... | |||
30 | |
30 | |
|
Line 31... | Line 31... | |||
31 | private HttpClient _httpClient; |
31 | private CancellationTokenSource _cancellationTokenSource; |
|
32 | |
32 | |
|
33 | private Task _runTask; |
33 | private Task _runTask; |
|
34 | |
34 | |
|
Line 35... | Line -... | |||
35 | private ClientWebSocket _webSocketClient; |
- | ||
36 | |
- | ||
37 | private readonly Server _server; |
- | ||
38 | |
- | ||
39 | public GotifyConnection(Server server) |
35 | #endregion |
|
40 | { |
36 | |
|
41 | _server = server; |
37 | #region Constructors, Destructors and Finalizers |
|
42 | } |
38 | |
|
43 | |
39 | public GotifyConnection(Server server) |
|
44 | #endregion |
40 | { |
|
45 | |
41 | _server = server; |
|
46 | #region Constructors, Destructors and Finalizers |
- | ||
47 | |
- | ||
48 | public void Dispose() |
- | ||
49 | { |
- | ||
50 | if (_cancellationTokenSource != null) |
- | ||
51 | { |
- | ||
52 | _cancellationTokenSource.Dispose(); |
- | ||
53 | _cancellationTokenSource = null; |
- | ||
54 | } |
- | ||
55 | |
- | ||
56 | if (_webSocketClient != null) |
- | ||
57 | { |
- | ||
58 | _webSocketClient.Dispose(); |
42 | } |
|
Line 59... | Line 43... | |||
59 | _webSocketClient = null; |
43 | |
|
Line 60... | Line 44... | |||
60 | } |
44 | public void Dispose() |
|
Line 61... | Line 45... | |||
61 | |
45 | { |
|
62 | if (_httpClient != null) |
46 | if (_cancellationTokenSource != null) |
|
63 | { |
47 | { |
|
64 | _httpClient.Dispose(); |
48 | _cancellationTokenSource.Dispose(); |
|
65 | _httpClient = null; |
49 | _cancellationTokenSource = null; |
|
66 | } |
50 | } |
|
Line 67... | Line 51... | |||
67 | } |
51 | } |
|
Line 84... | Line 68... | |||
84 | var webSocketsUri = webSocketsUriBuilder.Uri; |
68 | var webSocketsUri = webSocketsUriBuilder.Uri; |
|
Line 85... | Line 69... | |||
85 | |
69 | |
|
86 | _cancellationTokenSource = new CancellationTokenSource(); |
70 | _cancellationTokenSource = new CancellationTokenSource(); |
|
Line 87... | Line -... | |||
87 | _cancellationToken = _cancellationTokenSource.Token; |
- | ||
88 | |
- | ||
89 | _httpClient = new HttpClient(); |
- | ||
90 | |
- | ||
91 | var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}")); |
- | ||
92 | |
- | ||
93 | _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth); |
71 | _cancellationToken = _cancellationTokenSource.Token; |
|
94 | |
72 | |
|
Line 95... | Line 73... | |||
95 | _runTask = Run(webSocketsUri, httpUri, username, password, _cancellationToken); |
73 | _runTask = Run(webSocketsUri, httpUri, _server.Username, _server.Password, _cancellationToken); |
|
96 | } |
74 | } |
|
97 | |
75 | |
|
98 | public void Stop() |
76 | public void Stop() |
|
99 | { |
77 | { |
|
100 | if (_cancellationTokenSource != null) |
78 | if (_cancellationTokenSource != null) |
|
101 | { |
- | ||
102 | _cancellationTokenSource.Cancel(); |
- | ||
103 | } |
79 | { |
|
Line 104... | Line 80... | |||
104 | |
80 | _cancellationTokenSource.Cancel(); |
|
Line 105... | Line 81... | |||
105 | _runTask.Wait(CancellationToken.None); |
81 | } |
|
Line 116... | Line 92... | |||
116 | { |
92 | { |
|
117 | do |
93 | do |
|
118 | { |
94 | { |
|
119 | try |
95 | try |
|
120 | { |
96 | { |
|
121 | using (_webSocketClient = new ClientWebSocket()) |
97 | using (var webSocketClient = new ClientWebSocket()) |
|
122 | { |
98 | { |
|
123 | var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}")); |
99 | var auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}")); |
|
Line 124... | Line 100... | |||
124 | |
100 | |
|
Line 125... | Line 101... | |||
125 | _webSocketClient.Options.SetRequestHeader("Authorization", $"Basic {auth}"); |
101 | webSocketClient.Options.SetRequestHeader("Authorization", $"Basic {auth}"); |
|
Line 126... | Line 102... | |||
126 | |
102 | |
|
127 | await _webSocketClient.ConnectAsync(webSocketsUri, cancellationToken); |
103 | await webSocketClient.ConnectAsync(webSocketsUri, cancellationToken); |
|
128 | |
104 | |
|
Line 129... | Line 105... | |||
129 | do |
105 | do |
|
Line 130... | Line 106... | |||
130 | { |
106 | { |
|
131 | var payload = new ArraySegment<byte>(new byte[1024]); |
107 | var payload = new ArraySegment<byte>(new byte[1024]); |
|
132 | |
108 | |
|
133 | var result = await _webSocketClient.ReceiveAsync(payload, cancellationToken); |
109 | var result = await webSocketClient.ReceiveAsync(payload, cancellationToken); |
|
Line 142... | Line 118... | |||
142 | continue; |
118 | continue; |
|
143 | } |
119 | } |
|
Line 144... | Line 120... | |||
144 | |
120 | |
|
Line 145... | Line 121... | |||
145 | var message = Encoding.UTF8.GetString(payload.Array, 0, payload.Count); |
121 | var message = Encoding.UTF8.GetString(payload.Array, 0, payload.Count); |
|
146 | |
122 | |
|
147 | var gotifyNotification = JsonConvert.DeserializeObject<GotifyNotification>(message); |
123 | if (!(JsonConvert.DeserializeObject<GotifyNotification>(message) is GotifyNotification |
|
- | 124 | gotifyNotification)) |
||
- | 125 | { |
||
148 | if (gotifyNotification == null) |
126 | Log.Warning($"Could not deserialize gotify notification: {message}"); |
|
149 | { |
127 | |
|
Line 150... | Line 128... | |||
150 | continue; |
128 | continue; |
|
Line 151... | Line 129... | |||
151 | } |
129 | } |
|
- | 130 | |
||
152 | |
131 | gotifyNotification.Server = _server; |
|
153 | gotifyNotification.Server = _server; |
132 | |
|
154 | |
133 | if (!Uri.TryCreate($"{httpUri}/application", UriKind.Absolute, |
|
Line -... | Line 134... | |||
- | 134 | out var applicationUri)) |
||
- | 135 | { |
||
- | 136 | continue; |
||
- | 137 | } |
||
- | 138 | |
||
- | 139 | var image = await RetrieveGotifyApplicationImage(gotifyNotification.AppId, httpUri, |
||
- | 140 | applicationUri, auth, cancellationToken); |
||
- | 141 | |
||
- | 142 | GotifyNotification?.Invoke(this, |
||
- | 143 | new GotifyNotificationEventArgs(gotifyNotification, image)); |
||
- | 144 | |
||
- | 145 | Log.Debug($"Notification message received: {gotifyNotification.Message}"); |
||
- | 146 | } while (!cancellationToken.IsCancellationRequested); |
||
- | 147 | } |
||
- | 148 | } |
||
- | 149 | catch (Exception ex) when (ex is WebSocketException || ex is HttpRequestException) |
||
- | 150 | { |
||
- | 151 | // Reconnect |
||
- | 152 | Log.Warning($"Unable to connect to gotify server: {ex.Message}"); |
||
- | 153 | |
||
- | 154 | await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); |
||
- | 155 | } |
||
- | 156 | } while (!cancellationToken.IsCancellationRequested); |
||
- | 157 | } |
||
- | 158 | catch (Exception ex) when (ex is OperationCanceledException || ex is ObjectDisposedException) |
||
- | 159 | { |
||
- | 160 | } |
||
- | 161 | catch (Exception ex) |
||
- | 162 | { |
||
- | 163 | Log.Warning(ex, "Failure running connection loop."); |
||
- | 164 | } |
||
- | 165 | |
||
- | 166 | var e = "o"; |
||
- | 167 | } |
||
- | 168 | |
||
- | 169 | private static async Task<Image> RetrieveGotifyApplicationImage(int appId, Uri httpUri, Uri applicationUri, |
||
- | 170 | string auth, |
||
- | 171 | CancellationToken cancellationToken) |
||
- | 172 | { |
||
155 | if (!Uri.TryCreate($"{httpUri}/application", UriKind.Absolute, out var applicationUri)) |
173 | using (var httpClient = new HttpClient()) |
|
- | 174 | { |
||
- | 175 | httpClient.DefaultRequestHeaders.Authorization = |
||
Line 156... | Line 176... | |||
156 | { |
176 | new AuthenticationHeaderValue("Basic", auth); |
|
157 | continue; |
177 | |
|
- | 178 | var applicationResponse = await httpClient.GetAsync(applicationUri, cancellationToken); |
||
158 | } |
179 | |
|
159 | |
180 | var applications = await applicationResponse.Content.ReadAsStringAsync(); |
|
160 | var applications = await _httpClient.GetStringAsync(applicationUri); |
181 | |
|
161 | |
182 | var gotifyApplications = |
|
Line 162... | Line 183... | |||
162 | var gotifyApplications = |
183 | JsonConvert.DeserializeObject<GotifyApplication[]>(applications); |
|
163 | JsonConvert.DeserializeObject<GotifyApplication[]>(applications); |
184 | |
|
164 | if (gotifyApplications == null) |
185 | if (gotifyApplications == null) |
|
165 | { |
186 | { |
|
166 | continue; |
187 | return null; |
|
167 | } |
188 | } |
|
Line 168... | Line 189... | |||
168 | |
189 | |
|
169 | foreach (var application in gotifyApplications) |
190 | foreach (var application in gotifyApplications) |
|
170 | { |
191 | { |
|
171 | if (application.Id != gotifyNotification.AppId) |
192 | if (application.Id != appId) |
|
172 | { |
193 | { |
|
Line 173... | Line 194... | |||
173 | continue; |
194 | continue; |
|
174 | } |
- | ||
175 | |
- | ||
176 | if (!Uri.TryCreate($"{httpUri}/{application.Image}", UriKind.Absolute, |
- | ||
177 | out var applicationImageUri)) |
- | ||
178 | { |
- | ||
Line 179... | Line 195... | |||
179 | continue; |
195 | } |
|
180 | } |
196 | |
|
181 | |
197 | if (!Uri.TryCreate($"{httpUri}/{application.Image}", UriKind.Absolute, |
|
182 | var imageBytes = await _httpClient.GetByteArrayAsync(applicationImageUri); |
- | ||
183 | |
- | ||
184 | if (imageBytes == null || imageBytes.Length == 0) |
- | ||
185 | { |
- | ||
186 | continue; |
- | ||
187 | } |
- | ||
188 | |
- | ||
189 | using (var memoryStream = new MemoryStream(imageBytes)) |
- | ||
190 | { |
- | ||
191 | var image = Image.FromStream(memoryStream); |
- | ||
192 | |
- | ||
Line 193... | Line -... | |||
193 | GotifyNotification?.Invoke(this, |
- | ||
194 | new GotifyNotificationEventArgs(gotifyNotification, image)); |
198 | out var applicationImageUri)) |
|
195 | } |
199 | { |
|
196 | |
- | ||
197 | |
- | ||
198 | break; |
200 | continue; |
|
199 | } |
- | ||
200 | |
- | ||
201 | Log.Debug($"Notification message received: {gotifyNotification.Message}"); |
- | ||
202 | } while (!cancellationToken.IsCancellationRequested); |
- | ||
203 | |
- | ||
204 | await _webSocketClient.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, |
- | ||
205 | CancellationToken.None); |
- | ||
206 | } |
- | ||
207 | |
- | ||
208 | _webSocketClient = null; |
- | ||
209 | } |
201 | } |
|
Line 210... | Line -... | |||
210 | catch (Exception ex) when (ex is WebSocketException || ex is HttpRequestException) |
- | ||
211 | { |
202 | |
|
212 | Log.Warning($"Unable to connect to gotify server: {ex.Message}"); |
- | ||
213 | |
- | ||
214 | // Reconnect |
- | ||
215 | if (_webSocketClient != null) |
- | ||
216 | { |
- | ||
217 | _webSocketClient.Abort(); |
- | ||
218 | _webSocketClient.Dispose(); |
- | ||
219 | _webSocketClient = null; |
- | ||
220 | } |
- | ||
221 | |
203 | var imageResponse = await httpClient.GetAsync(applicationImageUri, cancellationToken); |
|
Line 222... | Line 204... | |||
222 | await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); |
204 | |
|
223 | } |
205 | using (var memoryStream = new MemoryStream()) |
|
224 | } while (!cancellationToken.IsCancellationRequested); |
206 | { |
|
225 | } |
207 | await imageResponse.Content.CopyToAsync(memoryStream); |