예제 #1
0
        static void Connect()
        {
            UpdateStatus();

            status.Id = 0;
            server?.Dispose();
            controller?.Dispose();
            server     = null;
            controller = null;
            config     = null;

            // Use a background thread to do the connect to avoid stalling UI
            var connectingThread = new Thread(ConnectThread);

            connectingThread.Start();

            // Spin the foreground thread to collect and throw away any user input while we are connecting. Also allow people to access the service menu if they need to.
            var inputBuffer = "";

            while (config == null)
            {
                var input = ReaderHardware.Read();

                if (input == "A")
                {
                    inputBuffer = "";
                }
                else if (input == "B")
                {
                    if (inputBuffer == ServiceMenuMagicCode)
                    {
                        inputBuffer = "";

                        EnterServiceMenu();
                    }
                    else
                    {
                        inputBuffer = "";
                    }
                }
                else
                {
                    inputBuffer += input;
                }
            }

            inputCleared = true;

            Draw.Heading(config.Name, status.Warning);
            Draw.Status(-1, false);
            Logout();
            ClearEntry();
        }
예제 #2
0
        static void ConnectThread()
        {
            while (true)
            {
                var random = new Random();

                var message = loadingMessages[random.Next(loadingMessages.Length - 1)];

                UpdateStatus();
                Draw.Loading(message);

                try {
                    status.Id = int.Parse(File.ReadAllText("readerid.txt"));
                }
                catch (Exception ex) {
                    Log.Exception(ex);

                    Draw.Fatal("Reader ID is not set");

                    Thread.Sleep(60000);
                }

                try {
                    if (status.Id != 0)
                    {
                        server?.Dispose();
                        server = new MilwaukeeMakerspaceApiClient(status);
                        controller?.Dispose();
                        controller = server;
                    }
                }
                catch (Exception ex) {
                    Log.Exception(ex);

                    Draw.Fatal($"Cannot reach server\nReader IP: {status.Ip}");

                    Thread.Sleep(2000);

                    try {
                        // Fall back to a local snapshot database if one exists.
                        controller?.Dispose();
                        controller = new LocalController(status);
                    }
                    catch {
                        // Wait some more, and loop around
                        Thread.Sleep(20000);
                    }
                }

                try {
                    if (controller != null)
                    {
                        config = controller.Initialize();

                        cabinetMode = false;
                        warningBeep = true;
                        cabinetItems?.Clear();

                        try {
                            var settings = JObject.Parse(config.Settings);

                            cabinetMode = settings?["mode"]?.ToString() == "cabinet";
                            warningBeep = (bool?)settings?["warn"] ?? true;

                            if (cabinetMode)
                            {
                                var itemsList = settings?["items"] as JArray;

                                if (itemsList == null)
                                {
                                    Draw.Fatal("Cannot Read Item List");

                                    continue;
                                }

                                cabinetItems = new Dictionary <int, string>(itemsList.Count);

                                foreach (var item in itemsList)
                                {
                                    cabinetItems.Add(int.Parse(item?["id"].ToString()), item?["name"].ToString());
                                }
                            }
                        }
                        catch {
                            // Do Nothing
                        }

                        // Exit the loop after we've setup everything
                        break;
                    }
                }
                catch (Exception ex) {
                    Log.Exception(ex);

                    Draw.Fatal("Server does not recognise reader ID");

                    Thread.Sleep(10000);
                }
            }
        }