Winify – Diff between revs 26 and 28
?pathlinks?
Rev 26 | Rev 28 | |||
---|---|---|---|---|
Line 54... | Line 54... | |||
54 | |
54 | |
|
Line 55... | Line 55... | |||
55 | #region Public Methods |
55 | #region Public Methods |
|
56 | |
56 | |
|
57 | public void Start() |
57 | public void Start() |
|
58 | { |
- | ||
59 | if (!Uri.TryCreate(_server.Url, UriKind.Absolute, out var httpUri)) |
- | ||
60 | { |
- | ||
Line 61... | Line 58... | |||
61 | return; |
58 | { |
|
62 | } |
59 | if (!Uri.TryCreate(_server.Url, UriKind.Absolute, out var httpUri)) return; |
|
63 | |
60 | |
|
64 | // Build the web sockets URI. |
61 | // Build the web sockets URI. |
|
65 | var webSocketsUriBuilder = new UriBuilder(httpUri); |
62 | var webSocketsUriBuilder = new UriBuilder(httpUri); |
|
Line 66... | Line 63... | |||
66 | webSocketsUriBuilder.Scheme = "ws"; |
63 | webSocketsUriBuilder.Scheme = "ws"; |
|
67 | webSocketsUriBuilder.Path = $"{webSocketsUriBuilder.Path}/stream"; |
64 | webSocketsUriBuilder.Path = Path.Combine($"{webSocketsUriBuilder.Path}", "stream"); |
|
Line 68... | Line 65... | |||
68 | var webSocketsUri = webSocketsUriBuilder.Uri; |
65 | var webSocketsUri = webSocketsUriBuilder.Uri; |
|
69 | |
66 | |
|
Line 70... | Line 67... | |||
70 | _cancellationTokenSource = new CancellationTokenSource(); |
67 | _cancellationTokenSource = new CancellationTokenSource(); |
|
71 | _cancellationToken = _cancellationTokenSource.Token; |
68 | _cancellationToken = _cancellationTokenSource.Token; |
|
72 | |
- | ||
73 | _runTask = Run(webSocketsUri, httpUri, _server.Username, _server.Password, _cancellationToken); |
- | ||
74 | } |
69 | |
|
75 | |
- | ||
76 | public void Stop() |
70 | _runTask = Run(webSocketsUri, httpUri, _server.Username, _server.Password, _cancellationToken); |
|
Line 77... | Line 71... | |||
77 | { |
71 | } |
|
Line 78... | Line 72... | |||
78 | if (_cancellationTokenSource != null) |
72 | |
|
Line 79... | Line 73... | |||
79 | { |
73 | public void Stop() |
|
80 | _cancellationTokenSource.Cancel(); |
74 | { |
|
81 | } |
75 | if (_cancellationTokenSource != null) _cancellationTokenSource.Cancel(); |
|
82 | } |
76 | } |
|
83 | |
77 | |
|
84 | #endregion |
78 | #endregion |
|
85 | |
79 | |
|
Line 106... | Line 100... | |||
106 | { |
100 | { |
|
107 | var payload = new ArraySegment<byte>(new byte[1024]); |
101 | var payload = new ArraySegment<byte>(new byte[1024]); |
|
Line 108... | Line 102... | |||
108 | |
102 | |
|
Line 109... | Line 103... | |||
109 | var result = await webSocketClient.ReceiveAsync(payload, cancellationToken); |
103 | var result = await webSocketClient.ReceiveAsync(payload, cancellationToken); |
|
110 | |
- | ||
111 | if (result.Count == 0) |
- | ||
112 | { |
- | ||
Line 113... | Line 104... | |||
113 | continue; |
104 | |
|
114 | } |
- | ||
115 | |
- | ||
116 | if (payload.Array == null || payload.Count == 0) |
- | ||
Line 117... | Line 105... | |||
117 | { |
105 | if (result.Count == 0) continue; |
|
Line 118... | Line 106... | |||
118 | continue; |
106 | |
|
- | 107 | if (payload.Array == null || payload.Count == 0) continue; |
||
119 | } |
108 | |
|
120 | |
109 | var message = Encoding.UTF8.GetString(payload.Array, 0, payload.Count); |
|
121 | var message = Encoding.UTF8.GetString(payload.Array, 0, payload.Count); |
110 | |
|
Line 122... | Line 111... | |||
122 | |
111 | var gotifyNotification = JsonConvert.DeserializeObject<GotifyNotification>(message); |
|
123 | if (!(JsonConvert.DeserializeObject<GotifyNotification>(message) is GotifyNotification |
112 | |
|
Line 124... | Line 113... | |||
124 | gotifyNotification)) |
113 | if (gotifyNotification == null) |
|
Line 125... | Line 114... | |||
125 | { |
114 | { |
|
126 | Log.Warning($"Could not deserialize gotify notification: {message}"); |
115 | Log.Warning($"Could not deserialize gotify notification: {message}"); |
|
127 | |
- | ||
128 | continue; |
116 | |
|
129 | } |
- | ||
Line 130... | Line 117... | |||
130 | |
117 | continue; |
|
131 | gotifyNotification.Server = _server; |
118 | } |
|
Line 132... | Line 119... | |||
132 | |
119 | |
|
Line 163... | Line 150... | |||
163 | Log.Warning(ex, "Failure running connection loop."); |
150 | Log.Warning(ex, "Failure running connection loop."); |
|
164 | } |
151 | } |
|
165 | } |
152 | } |
|
Line 166... | Line 153... | |||
166 | |
153 | |
|
167 | private static async Task<Image> RetrieveGotifyApplicationImage(int appId, Uri httpUri, Uri applicationUri, |
154 | private static async Task<Image> RetrieveGotifyApplicationImage(int appId, Uri httpUri, Uri applicationUri, |
|
168 | string auth, |
155 | string auth, |
|
169 | CancellationToken cancellationToken) |
156 | CancellationToken cancellationToken) |
|
170 | { |
157 | { |
|
171 | using (var httpClient = new HttpClient()) |
158 | using (var httpClient = new HttpClient()) |
|
172 | { |
159 | { |
|
173 | httpClient.DefaultRequestHeaders.Authorization = |
160 | httpClient.DefaultRequestHeaders.Authorization = |
|
Line 178... | Line 165... | |||
178 | var applications = await applicationResponse.Content.ReadAsStringAsync(); |
165 | var applications = await applicationResponse.Content.ReadAsStringAsync(); |
|
Line 179... | Line 166... | |||
179 | |
166 | |
|
180 | var gotifyApplications = |
167 | var gotifyApplications = |
|
Line 181... | Line 168... | |||
181 | JsonConvert.DeserializeObject<GotifyApplication[]>(applications); |
168 | JsonConvert.DeserializeObject<GotifyApplication[]>(applications); |
|
182 | |
- | ||
183 | if (gotifyApplications == null) |
- | ||
184 | { |
- | ||
Line 185... | Line 169... | |||
185 | return null; |
169 | |
|
186 | } |
170 | if (gotifyApplications == null) return null; |
|
187 | |
171 | |
|
188 | foreach (var application in gotifyApplications) |
- | ||
189 | { |
- | ||
190 | if (application.Id != appId) |
- | ||
Line 191... | Line 172... | |||
191 | { |
172 | foreach (var application in gotifyApplications) |
|
192 | continue; |
173 | { |
|
193 | } |
- | ||
194 | |
174 | if (application.Id != appId) continue; |
|
195 | if (!Uri.TryCreate($"{httpUri}/{application.Image}", UriKind.Absolute, |
- | ||
Line 196... | Line 175... | |||
196 | out var applicationImageUri)) |
175 | |
|
Line 197... | Line 176... | |||
197 | { |
176 | if (!Uri.TryCreate(Path.Combine($"{httpUri}", $"{application.Image}"), UriKind.Absolute, |
|
198 | continue; |
177 | out var applicationImageUri)) |