-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMainPage.xaml.cs
422 lines (376 loc) · 17.6 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
// Pour plus d'informations sur le modèle d'élément Page vierge, consultez la page https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace ScreenlyManager
{
/// <summary>
/// Une page vide peut être utilisée seule ou constituer une page de destination au sein d'un frame.
/// </summary>
public sealed partial class MainPage : Page
{
private const string DB_FILE = "db.json";
private List<Device> Devices;
private Device CurrentDevice;
private Device CurrentRightClickDevice;
private string PathDbFile;
private Windows.ApplicationModel.Resources.ResourceLoader Loader;
public MainPage()
{
this.Loader = new Windows.ApplicationModel.Resources.ResourceLoader();
this.InitializeComponent();
// AppData folder access
var localFolder = ApplicationData.Current.LocalFolder;
this.PathDbFile = localFolder.Path + Path.DirectorySeparatorChar + DB_FILE;
// Load user config or create db file (if not exists) (containing IPs and informations about RPi)
if (!File.Exists(this.PathDbFile))
{
var file = File.Create(this.PathDbFile);
file.Dispose();
}
else
{
this.ProgressRingLoadingDevice.IsActive = true;
this.LoadConfigurationAsync(this.PathDbFile);
}
}
/// <summary>
/// Load devices from JSON file and refresh list in ListView
/// </summary>
/// <param name="path">Path to "db.json" file (default storage : appdata/local)</param>
public async void LoadConfigurationAsync(string path)
{
try
{
StorageFile file = await StorageFile.GetFileFromPathAsync(path);
string json = await FileIO.ReadTextAsync(file);
this.Devices = JsonConvert.DeserializeObject<List<Device>>(json);
// Need to get list of assets for each device
if (this.Devices != null)
{
foreach (var device in this.Devices)
{
if (await device.IsReachable())
await device.GetAssetsAsync();
}
this.AppBarButtonAddAsset.IsEnabled = true;
this.ListViewDevice.ItemsSource = this.Devices;
}
}
catch(Exception ex)
{
var dialog = new MessageDialog(ex.Message + Environment.NewLine + ex.InnerException.Message + Environment.NewLine + ex.InnerException.StackTrace);
dialog.Commands.Add(new UICommand("Ok") { Id = 0 });
dialog.DefaultCommandIndex = 0;
var result = await dialog.ShowAsync();
}
this.ProgressRingLoadingDevice.IsActive = false;
}
/// <summary>
/// Load assets for current device trought API and refresh assets view
/// </summary>
private async void RefreshAssetsForCurrentDeviceAsync()
{
await this.CurrentDevice.GetAssetsAsync();
this.ListViewActiveAssets.ItemsSource = this.CurrentDevice.ActiveAssets;
this.ListViewInactiveAssets.ItemsSource = this.CurrentDevice.InactiveAssets;
this.TextBlockActiveAsset.Text = $"{ this.Loader.GetString("ActiveAssets") } ({this.ListViewActiveAssets.Items.Count})";
this.TextBlockInactiveAsset.Text = $"{ this.Loader.GetString("InactiveAssets") } ({this.ListViewInactiveAssets.Items.Count})";
}
/// <summary>
/// Serialize devices list to file
/// </summary>
/// <param name="file">StorageFile item where JSON will write</param>
/// <returns>True if export was ok, false otherwise</returns>
private async Task<bool> SaveFileConfiguration(StorageFile file)
{
var dbContent = JsonConvert.SerializeObject(this.Devices);
if (file != null)
{
CachedFileManager.DeferUpdates(file);
await FileIO.WriteTextAsync(file, dbContent);
Windows.Storage.Provider.FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
if (status != Windows.Storage.Provider.FileUpdateStatus.Complete)
{
var dialog = new MessageDialog(string.Format(this.Loader.GetString("ErrorCannotSaveFile"), file.Name), this.Loader.GetString("Error"));
dialog.Commands.Add(new UICommand("Ok") { Id = 0 });
dialog.DefaultCommandIndex = 0;
var result = await dialog.ShowAsync();
return false;
}
else
return true;
}
else
return false;
}
#region View's events
/// <summary>
/// Refresh button click in left command bar. Used to refresh devices list.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AppBarButtonRefreshDevice_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
this.ProgressRingLoadingDevice.IsActive = true;
this.AppBarButtonAddAsset.IsEnabled = false;
this.LoadConfigurationAsync(this.PathDbFile);
}
/// <summary>
/// Refresh button click in main command bar. Used to refresh assets for current device.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AppBarButtonRefreshAssets_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if (this.CurrentDevice != null)
this.RefreshAssetsForCurrentDeviceAsync();
}
/// <summary>
/// Event for selection in device list
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void ListViewDeviceItem_Click(object sender, ItemClickEventArgs e)
{
var deviceCliked = e.ClickedItem as Device;
this.TextBlockCommandBarMain.Text = $"{ this.Loader.GetString("ScheduleOverview") } - {deviceCliked.Name}";
this.CurrentDevice = deviceCliked;
if(this.CurrentDevice.IsUp)
this.RefreshAssetsForCurrentDeviceAsync();
else
{
this.ListViewActiveAssets.ItemsSource = null;
this.ListViewInactiveAssets.ItemsSource = null;
this.TextBlockActiveAsset.Text = this.Loader.GetString("ActiveAssets");
this.TextBlockInactiveAsset.Text = this.Loader.GetString("InactiveAssets");
var dialog = new MessageDialog(this.Loader.GetString("DeviceDown"));
var resultDialog = dialog.ShowAsync();
await Task.Delay(TimeSpan.FromSeconds(3));
resultDialog.Cancel();
}
}
/// <summary>
/// Open asset link with default browser if it's a web link
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void ButtonPreview_Click(object sender, RoutedEventArgs e)
{
if (Uri.TryCreate(((sender as Button).Tag.ToString()), System.UriKind.Absolute, out Uri uriResult))
await Windows.System.Launcher.LaunchUriAsync(uriResult);
}
/// <summary>
/// Duplicate selected asset
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDuplicate_Click(object sender, RoutedEventArgs e)
{
var assetId = (sender as Button).Tag.ToString();
this.Frame.Navigate(typeof(AddOrChangeAssetPage), new Tuple<List<Device>, Device, string>(this.Devices, this.CurrentDevice, assetId));
}
/// <summary>
/// Edit selected asset
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonEdit_Click(object sender, RoutedEventArgs e)
{
var assetId = (sender as Button).Tag.ToString();
this.Frame.Navigate(typeof(AddOrChangeAssetPage), new Tuple<Device, string>(this.CurrentDevice, assetId));
}
/// <summary>
/// Event to remove an asset
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void ButtonRemove_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
var dialog = new MessageDialog(this.Loader.GetString("ConfirmDelete"), this.Loader.GetString("Delete"));
dialog.Commands.Add(new UICommand(this.Loader.GetString("Yes")) { Id = 0 });
dialog.Commands.Add(new UICommand(this.Loader.GetString("No")) { Id = 1 });
dialog.DefaultCommandIndex = 0;
dialog.CancelCommandIndex = 1;
var result = await dialog.ShowAsync();
var assetId = (sender as Button).Tag.ToString();
if((int)result.Id == 0)
await Task.Run(() => this.CurrentDevice.RemoveAssetAsync(assetId));
this.RefreshAssetsForCurrentDeviceAsync();
}
/// <summary>
/// Enable/disable an asset
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void ToggleSwitchEnable_Toggled(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if ((sender as ToggleSwitch).Tag == null)
return;
var newState = (sender as ToggleSwitch).IsOn;
var assetId = (sender as ToggleSwitch).Tag.ToString();
Asset currentAsset = await this.CurrentDevice.GetAssetAsync(assetId);
if (currentAsset != null)
{
// Don't know why, but toggled event is fired when dragging item in listview (and IsOn value's keep unchanged!), so I found this work around...
if ((currentAsset.IsEnabled.Equals(1) ? true : false) != newState)
{
currentAsset.StartDate = currentAsset.StartDate.ToUniversalTime();
currentAsset.EndDate = currentAsset.EndDate.ToUniversalTime();
currentAsset.IsEnabled = (sender as ToggleSwitch).IsOn ? 1 : 0;
await this.CurrentDevice.UpdateAssetAsync(currentAsset);
this.RefreshAssetsForCurrentDeviceAsync();
}
}
}
/// <summary>
/// Export devices list to JSON file
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void AppBarButtonExportConf_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
var dbContent = JsonConvert.SerializeObject(this.Devices);
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads;
savePicker.FileTypeChoices.Add("JSON", new List<string>() { ".json" });
savePicker.SuggestedFileName = "db";
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
bool status = await this.SaveFileConfiguration(file);
var dialog = new MessageDialog(this.Loader.GetString("ConfigurationExported") + file.Name, this.Loader.GetString("BackupCreated"));
if (!status)
{
dialog.Content = string.Format(this.Loader.GetString("ErrorCannotSaveFile"), file.Name);
dialog.Title = this.Loader.GetString("Error");
}
dialog.Commands.Add(new UICommand("Ok") { Id = 0 });
dialog.DefaultCommandIndex = 0;
var result = await dialog.ShowAsync();
}
}
/// <summary>
/// Import devices from JSON file
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void AppBarButtonImportConf_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads;
openPicker.FileTypeFilter.Add(".json");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
CachedFileManager.DeferUpdates(file);
string content = await FileIO.ReadTextAsync(file, Windows.Storage.Streams.UnicodeEncoding.Utf8);
this.ProgressRingLoadingDevice.IsActive = true;
try
{
this.Devices = JsonConvert.DeserializeObject<List<Device>>(content);
StorageFile dbFile = await StorageFile.GetFileFromPathAsync(this.PathDbFile);
await FileIO.WriteTextAsync(dbFile, content);
this.LoadConfigurationAsync(this.PathDbFile);
var dialog = new MessageDialog(this.Loader.GetString("ConfigurationImported") + file.Name, this.Loader.GetString("ImportCompleted"));
dialog.Commands.Add(new UICommand("Ok") { Id = 0 });
dialog.DefaultCommandIndex = 0;
var result = await dialog.ShowAsync();
}
catch(Exception ex)
{
var dialogError = new MessageDialog(string.Format(this.Loader.GetString("InvalidFile"), file.Name) + Environment.NewLine + ex.Message);
dialogError.Commands.Add(new UICommand("Ok") { Id = 0 });
dialogError.DefaultCommandIndex = 0;
await dialogError.ShowAsync();
this.ProgressRingLoadingDevice.IsActive = false;
}
}
}
/// <summary>
/// Event fired when we change ListView active assets order
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private async void ListViewActiveAssets_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
{
var assetsInListView = (sender as ListView).Items.OfType<Asset>().ToList();
await this.CurrentDevice.UpdateOrderAssetsAsync(string.Join(",", assetsInListView.Select(x => x.AssetId)));
}
/// <summary>
/// Go to creation of new asset
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AppBarButtonAddAsset_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
this.Frame.Navigate(typeof(AddOrChangeAssetPage), this.Devices);
}
/// <summary>
/// Go to creation of new device
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AppBarButtonAddDevice_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
this.Frame.Navigate(typeof(AddOrChangeDevicePage), null);
}
/// <summary>
/// Right click on device item
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListViewDevice_RightTapped(object sender, Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e)
{
ListView listView = sender as ListView;
this.MenuFlyoutDevice.ShowAt(listView, e.GetPosition(listView));
var device = (e.OriginalSource as FrameworkElement).DataContext;
if (device == null)
this.MenuFlyoutDevice.Hide();
else
this.CurrentRightClickDevice = device as Device;
}
/// <summary>
/// Remove item from devices list
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void MenuFlyoutItemRemoveDevice_Click(object sender, RoutedEventArgs e)
{
this.ProgressRingLoadingDevice.IsActive = true;
this.Devices.Remove(this.CurrentRightClickDevice);
StorageFile file = await StorageFile.GetFileFromPathAsync(this.PathDbFile);
await this.SaveFileConfiguration(file);
this.CurrentRightClickDevice = null;
this.LoadConfigurationAsync(this.PathDbFile);
}
/// <summary>
/// Edit item from devices list
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuFlyoutItemEditDevice_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(AddOrChangeDevicePage), this.CurrentRightClickDevice);
}
/// <summary>
/// Open the configuration device link
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void MenuFlyoutItemOpenDevice_Click(object sender, RoutedEventArgs e)
{
if (Uri.TryCreate(this.CurrentRightClickDevice.HttpLink, System.UriKind.Absolute, out Uri uriResult))
await Windows.System.Launcher.LaunchUriAsync(uriResult);
}
#endregion
}
}