/
MainPage.xaml.cs
329 lines (298 loc) · 10.7 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using OPG.Signage.APIServer;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.Graphics.Printing;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Printing;
using Windows.UI.Xaml.Shapes;
namespace OPG.Signage.Screen
{
public sealed partial class MainPage : Page
{
private string url = "http://dev.opgs.org/signage/screen/";
private string cUrl = "http://dev.opgs.org/signage/screen/";
private readonly ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
private static int APIVersion = 6;
private string hwReplyformat = "{{\"board\":\"{0}\", \"processor\":\"{1}\", \"manufacturer\":\"{2}\", \"name\":\"{3}\"}}";
private string API4replyformat = "{{\"os\":\"{0}\", \"wifi\":\"{1}\", \"ssid\":\"{2}\", \"url\":\"{3}\"}}";
private string API4Format = "{{\"apiversion\":\"" + APIVersion + "\",\"name\":\"" + Info.Hardware.Network.HostName + "\",\"version\":\"" + Info.APP.AppVersion + "\",\"reply\":{0}}}";
private PrintManager printMan;
private PrintDocument printDoc;
private IPrintDocumentSource printDocSource;
private Rectangle RectangleToPrint = new Rectangle();
private void RefreshTick()
{
if (cUrl != url)
{
if (cUrl == "refresh")
{
ScreenView.Refresh();
lock (cUrl) cUrl = url;
}
else
{
ScreenView.Navigate(new Uri(cUrl));
localSettings.Values["url"] = cUrl;
lock (cUrl) url = cUrl;
}
}
}
private void StartRefreshTimer()
{
DispatcherTimer refreshTimer = new DispatcherTimer
{
Interval = new TimeSpan(0, 0, 30)
};
refreshTimer.Tick += (_, __) => RefreshTick();
refreshTimer.Start();
}
private void SetupServer()
{
Debug.WriteLine("Starting server");
JSONServerSSL webserverssl = new JSONServerSSL(new OPGInternalCert(), Info.Hardware.Network.IP, Info.Hardware.Network.HostName, false)
{
RegUrl = new Uri("http://dev.opgs.org/signage/index.php"),
ReplyFormat = API4Format
};
webserverssl.Get("/api", (_) =>
{
return new JSONReply((new StreamReader(@"APIDescriptor.html")).ReadToEnd(), "text/html");
});
webserverssl.Get("/api/os", (_) =>
{
return new JSONReply(Info.OS.Version);
});
webserverssl.Get("/api/5", (_) => //minimum for 5+
{
return new JSONReply(String.Format(API4replyformat, Info.OS.Version, Info.Hardware.Network.IsWifi() ? Info.Hardware.Network.GetWifiStrength().ToString() : "false", Info.Hardware.Network.IsWifi() ? Info.Hardware.Network.GetWifiAPSSID() : "false", cUrl));
});
webserverssl.Get("/api/6", (_) =>
{
return new JSONReply(String.Format(API4replyformat, Info.OS.Version, Info.Hardware.Network.IsWifi() ? Info.Hardware.Network.GetWifiStrength().ToString() : "false", Info.Hardware.Network.IsWifi() ? Info.Hardware.Network.GetWifiAPSSID() : "false", cUrl));
});
webserverssl.Get("/api/hardware", (_) =>
{
return new JSONReply(String.Format(hwReplyformat, Info.Hardware.Board, Info.Hardware.CPU, Info.Hardware.Manufacturer, Info.Hardware.ProductName));
});
webserverssl.Post("/api/url", (request) =>
{
if(request.KVPairs.TryGetValue("url", out string newUrl))
{
try
{
new Uri(newUrl); //check for valid url, throws exception if not
lock (cUrl) cUrl = newUrl;
}
catch(Exception e)
{
throw new HTTPException(Windows.Web.Http.HttpStatusCode.BadRequest, e.Message);
}
}
return new JSONReply(cUrl);
});
webserverssl.Get("/api/refresh", (_) =>
{
lock (cUrl) cUrl = "refresh";
return new JSONReply("refresh - ok");
});
webserverssl.Post("/api/print", (request) =>
{
if(request.KVPairs.TryGetValue("idcard", out string idbase64))
{
Task.Run(async () =>
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
try
{
PrintJpeg(idbase64);
}
catch (Exception)
{
Task.Run(async () =>
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
await ShowDialog("Printing error", "\nSorry, printing can' t proceed at this time.\n", "OK");
});
});
}
});
});
}
else
{
return new JSONReply("no print job specified", "text/html", Windows.Web.Http.HttpStatusCode.BadRequest);
}
return new JSONReply("ok");
});
Task.Run(async () =>
{
await webserverssl.StartAsync();
});
}
private void ScreenView_PermissionRequested(WebView sender, WebViewPermissionRequestedEventArgs args)
{
if (args.PermissionRequest.PermissionType == WebViewPermissionType.Media)
{
args.PermissionRequest.Allow();
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
RectangleToPrint = printRect;
RectangleToPrint.Height = 240; /*bitmapImage.PixelHeight;*/
RectangleToPrint.Width = 340; /*bitmapImage.PixelWidth;*/
// Register for PrintTaskRequested event
printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested += PrintTaskRequested;
// Build a PrintDocument and register for callbacks
printDoc = new PrintDocument();
printDocSource = printDoc.DocumentSource;
printDoc.Paginate += Paginate;
printDoc.GetPreviewPage += GetPreviewPage;
printDoc.AddPages += AddPages;
}
public BitmapImage AsBitmapImage(byte[] byteArray)
{
if (byteArray != null)
{
using (var stream = new InMemoryRandomAccessStream())
{
stream.WriteAsync(byteArray.AsBuffer()).GetResults();
// I made this one synchronous on the UI thread;
// this is not a best practice.
var image = new BitmapImage();
stream.Seek(0);
image.SetSource(stream);
return image;
}
}
return null;
}
public void PrintJpeg(string dataIn)
{
Debug.WriteLine("Start print....");
Byte[] binData = Convert.FromBase64String(Regex.Match(dataIn, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value);
Debug.WriteLine("Converted....");
BitmapImage bitmapImage = AsBitmapImage(binData);
Debug.WriteLine("Source set....");
ImageBrush ib = new ImageBrush() { ImageSource = bitmapImage };
RectangleToPrint.Fill = ib;
Debug.WriteLine("Brush set....");
PrintButtonClick(null, null);
}
private async Task<ContentDialog> ShowDialog(string title, string content, string button = "OK")
{
ContentDialog dialog = new ContentDialog()
{
Title = title,
Content = content,
PrimaryButtonText = button
};
await dialog.ShowAsync();
return dialog;
}
private async Task ShowPrintingErrorDialog()
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
await ShowDialog("Printing error", "\nSorry, printing can' t proceed at this time.\n");
});
}
private async void PrintButtonClick(object sender, RoutedEventArgs e)
{
if (PrintManager.IsSupported())
{
try
{
// Show print UI
await PrintManager.ShowPrintUIAsync();
}
catch (Exception)
{
// Printing cannot proceed at this time
await ShowPrintingErrorDialog();
}
}
else
{
// Printing is not supported on this device
await ShowPrintingErrorDialog();
}
}
private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// Create the PrintTask.
// Defines the title and delegate for PrintTaskSourceRequested
var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequrested);
// Handle PrintTask.Completed to catch failed print jobs
printTask.Completed += PrintTaskCompleted;
}
private void PrintTaskSourceRequrested(PrintTaskSourceRequestedArgs args)
{
// Set the document source.
args.SetSource(printDocSource);
}
private void Paginate(object sender, PaginateEventArgs e)
{
// As I only want to print one Rectangle, so I set the count to 1
printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
}
private void GetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
// Provide a UIElement as the print preview.
printDoc.SetPreviewPage(e.PageNumber, RectangleToPrint);
}
private void AddPages(object sender, AddPagesEventArgs e)
{
printDoc.AddPage(RectangleToPrint);
// Indicate that all of the print pages have been provided
printDoc.AddPagesComplete();
}
private async void PrintTaskCompleted(PrintTask sender, PrintTaskCompletedEventArgs args)
{
// Notify the user when the print operation fails.
if (args.Completion == PrintTaskCompletion.Failed)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, failed to print.",
PrimaryButtonText = "OK"
};
await noPrintingDialog.ShowAsync();
});
}
}
public MainPage()
{
InitializeComponent();
if (localSettings.Values.ContainsKey("url"))
{
cUrl = localSettings.Values["url"].ToString();
} else {
localSettings.Values["url"] = cUrl;
}
ScreenView.Navigate(new Uri(cUrl));
ScreenView.PermissionRequested += ScreenView_PermissionRequested;
Debug.WriteLine("Browser done, server next");
StartRefreshTimer();
SetupServer();
}
}
}