Пример #1
0
        private async void SetUp()
        {
            ServerEnabled.IsOn     = Settings.WebSocketsEnabled;
            ServerEnabled.Toggled += (sender, e) =>
            {
                Settings.WebSocketsEnabled = ServerEnabled.IsOn;
            };

            CheckInAction.IsOn = Settings.CheckInAction;
            if (string.IsNullOrEmpty(Settings.AuthCookie))
            {
                CheckInAuthorizationStatus.Text = "Not authorized";
                CheckInAuthorize.Content        = "Authorize";
                CheckInEnabled.IsEnabled        = false;
                CheckInEnabled.IsOn             = false;
                CheckInTag.Text         = "";
                CheckInTag.IsEnabled    = false;
                CheckInAction.IsEnabled = false;
            }
            else
            {
                CheckInAuthorizationStatus.Text = $"Logged in as {Settings.AuthUsername}";
                CheckInAuthorize.Content        = "Log out";
                CheckInEnabled.IsEnabled        = true;
                CheckInEnabled.IsOn             = Settings.CheckInEnabled;
                CheckInTag.IsEnabled            = Settings.CheckInEnabled;
                CheckInAction.IsEnabled         = Settings.CheckInEnabled;
                CheckInTag.Text = Settings.CheckInTag ?? "";
            }
            CheckInAuthorize.Click += async(sender, e) =>
            {
                if (string.IsNullOrEmpty(Settings.AuthCookie))
                {
                    // Ask for username / password and log in
                    TextBox usernameBox = new TextBox()
                    {
                        AcceptsReturn       = false,
                        Header              = "Username",
                        IsSpellCheckEnabled = false,
                    };
                    PasswordBox passwordBox = new PasswordBox()
                    {
                        Header = "Password",
                        Margin = new Thickness(0, 10, 0, 0),
                    };
                    var layout = new StackPanel();
                    layout.Children.Add(usernameBox);
                    layout.Children.Add(passwordBox);

                    ContentDialog dialog = new ContentDialog();
                    dialog.Content = layout;
                    dialog.Title   = "HackGT Check-in Login";
                    dialog.IsSecondaryButtonEnabled = true;
                    dialog.PrimaryButtonText        = "OK";
                    dialog.SecondaryButtonText      = "Cancel";
                    if (await dialog.ShowAsync() == ContentDialogResult.Primary)
                    {
                        CheckInAuthorizationLoading.IsActive = true;
                        if (await CheckInAPI.Login(usernameBox.Text, passwordBox.Password))
                        {
                            CheckInAuthorizationStatus.Text = $"Logged in as {Settings.AuthUsername}";
                            CheckInAuthorize.Content        = "Log out";
                            CheckInEnabled.IsEnabled        = true;
                            CheckInEnabled.IsOn             = Settings.CheckInEnabled;
                            CheckInTag.IsEnabled            = Settings.CheckInEnabled;
                            CheckInAction.IsEnabled         = Settings.CheckInEnabled;
                            CheckInTag.Text = Settings.CheckInTag ?? "";
                        }
                        else
                        {
                            CheckInAuthorizationStatus.Text = "Couldn't log in";
                        }
                        CheckInAuthorizationLoading.IsActive = false;
                    }
                }
                else
                {
                    // Log out
                    Settings.AuthCookie             = null;
                    Settings.AuthUsername           = null;
                    CheckInAuthorizationStatus.Text = "Not authorized";
                    CheckInAuthorize.Content        = "Authorize";
                    CheckInEnabled.IsEnabled        = false;
                    CheckInEnabled.IsOn             = false;
                    CheckInTag.Text         = "";
                    CheckInTag.IsEnabled    = false;
                    CheckInAction.IsEnabled = false;
                }
            };
            CheckInEnabled.Toggled += (sender, e) =>
            {
                Settings.CheckInEnabled = CheckInEnabled.IsOn;
                CheckInTag.IsEnabled    = CheckInEnabled.IsOn;
                CheckInAction.IsEnabled = CheckInEnabled.IsOn;
            };
            CheckInTag.TextChanged += (sender, e) =>
            {
                Settings.CheckInTag = CheckInTag.Text;
            };
            CheckInAction.Toggled += (sender, e) =>
            {
                Settings.CheckInAction = CheckInAction.IsOn;
            };

            CopyEnabled.IsOn     = Settings.ClipboardEnabled;
            CopyEnabled.Toggled += (sender, e) =>
            {
                Settings.ClipboardEnabled = CopyEnabled.IsOn;
            };

            ScanLogEnabled.IsOn = Settings.ScanLogEnabled && await Settings.GetScanLogLocation() != null;

            SetPath.IsEnabled       = ScanLogEnabled.IsOn;
            ScanLogLocation.Text    = (await Settings.GetScanLogLocation())?.Path ?? "None set!";
            ScanLogEnabled.Toggled += async(sender, e) =>
            {
                if (ScanLogEnabled.IsOn && (await Settings.GetScanLogLocation()) == null)
                {
                    StorageFile file = await pickSaveFile();

                    if (file == null)
                    {
                        ScanLogEnabled.IsOn = false;
                        return;
                    }
                    Settings.ScanLogLocation = file;
                    ScanLogLocation.Text     = file.Path;
                }
                Settings.ScanLogEnabled = ScanLogEnabled.IsOn;
                SetPath.IsEnabled       = ScanLogEnabled.IsOn;
            };
            SetPath.Click += async(sender, e) =>
            {
                StorageFile file = await pickSaveFile();

                if (file == null)
                {
                    return;
                }
                Settings.ScanLogLocation = file;
                ScanLogLocation.Text     = file.Path;
            };

            Frame    rootFrame = Window.Current.Content as Frame;
            MainPage page      = rootFrame.Content as MainPage;

            ResetNFC.Click += async(sender, e) =>
            {
                NFCLoading.IsActive = true;
                NFCStatus.Text      = "Loading...";
                await page.NFCInit();

                setNFCStatus();
                NFCLoading.IsActive = false;
            };
            setNFCStatus();
        }
Пример #2
0
        private async void Nfc_BadgeTapped(object sender, BadgeEventArgs e)
        {
            if (!Settings.CheckInEnabled)
            {
                var toastContent = new ToastContent()
                {
                    Visual = new ToastVisual()
                    {
                        BindingGeneric = new ToastBindingGeneric()
                        {
                            Children =
                            {
                                new AdaptiveText()
                                {
                                    Text = "Badge scanned"
                                },
                                new AdaptiveText()
                                {
                                    Text = "ID: " + e.uuid
                                },
                            }
                        }
                    }
                };
                var toast = new ToastNotification(toastContent.GetXml())
                {
                    ExpirationTime = DateTime.Now.AddSeconds(10)
                };
                ToastNotificationManager.CreateToastNotifier().Show(toast);
            }

            if (Settings.CheckInEnabled)
            {
                string name;
                if (Settings.CheckInAction)
                {
                    name = await CheckInAPI.CheckIn(e.uuid, Settings.CheckInTag);
                }
                else
                {
                    name = await CheckInAPI.CheckOut(e.uuid, Settings.CheckInTag);
                }

                var toastContent = new ToastContent()
                {
                    Visual = new ToastVisual()
                    {
                        BindingGeneric = new ToastBindingGeneric()
                        {
                            Children =
                            {
                                new AdaptiveText()
                                {
                                    Text = $"Checked {(Settings.CheckInAction ? "in" : "out")} user"
                                },
                                new AdaptiveText()
                                {
                                    Text = name != null ? $"Name: {name}" : "Invalid tag"
                                },
                            }
                        }
                    }
                };
                var toast = new ToastNotification(toastContent.GetXml())
                {
                    ExpirationTime = DateTime.Now.AddSeconds(10)
                };
                ToastNotificationManager.CreateToastNotifier().Show(toast);
            }
            if (Settings.WebSocketsEnabled)
            {
                // Run on UI thread
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                {
                    try
                    {
                        await SportalFrame.InvokeScriptAsync("eval", new string[] { $"nfcService.onReceiveID(\"{e.uuid}\")" });
                    }
                    catch (Exception err)
                    {
                        Debug.WriteLine($"{err.Message}: {err.StackTrace}");
                    }
                });
            }
            if (Settings.ClipboardEnabled)
            {
                var package = new DataPackage();
                package.RequestedOperation = DataPackageOperation.Copy;
                package.SetText(e.uuid);

                // Clipboard operations must occur on the UI thread
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
                    try
                    {
                        Clipboard.SetContent(package);
                    }
                    catch (Exception err)
                    {
                        Debug.WriteLine($"{err.Message}: {err.StackTrace}");
                    }
                });
            }
            if (Settings.ScanLogEnabled)
            {
                StorageFile logFile = await Settings.GetScanLogLocation();

                await FileIO.AppendTextAsync(logFile, $"[{DateTime.Now.ToShortDateString()} {DateTime.Now.ToLongTimeString()}] Scanned badge: {e.uuid}\r\n");
            }
        }