예제 #1
0
        /// <summary>
        /// Connects to the modem thru the serial port.
        /// </summary>
        private void OpenShop()
        {
            var selected = _view.SelectedModem;

            // If somehow this method gets called when
            // the modem is already connected
            if (_modem.IsOpen)
            {
                return;
            }

            if (selected.Equals(string.Empty))
            {
                return;
            }

            var port = _modemList.Find(device => device.Model.Equals(selected)).Port;

            // Config and open port
            _modem.SetPort(port);
            _modem.Received += Modem_ReceiveEvent;
            _modem.Error    += Modem_ErrorEvent;
            _modem.Open();

            Task.Delay(5000);

            if (!_modem.IsOpen)
            {
                _view.UpdateToolStripStatus("Could not open the port.");
                return;
            }

            /*
             *  Update RAS profile with the connected device
             */
            var book = new RasPhoneBook();

            book.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User));

            // Selected profile must exist
            if (_view.SelectedProfile.Equals(string.Empty))
            {
                _view.UpdateToolStripStatus("Profile not valid.");
                return;
            }

            // Get profile from book, it needs to match the one selected
            var selectedEntry = book.Entries.First(x => x.Name == _view.SelectedProfile);

            // Get devices that are available to RAS
            var deviceList = RasDevice.GetDevices();

            // Get the device we're using
            var openedDevice = _modemList.First(x => x.Model.Equals(_view.SelectedModem));

            // Look for the device that matches the one that we're using
            var deviceToBeUsed = deviceList.First(x => x.Name == openedDevice.Name);

            // Update the profile with the device we're using
            selectedEntry.Device = deviceToBeUsed;
            selectedEntry.Update();

            _view.UpdateToolStripStatus($"Connected to {_view.SelectedModem}");
        }