/// <summary>
        /// Determines which ports have pressure switches and which do not.
        /// For those that do, it reads current pressure state (good/bad).
        /// </summary>
        private void CheckPressureSwitches()
        {
            // Reset these member booleans to false on every call.
            for (int i = 0; i < _checkedSwitchIsPresent.Length; i++)
            {
                _checkedSwitchIsPresent[i] = _checkedIsPressureGood[i] = false;
            }

            for (int i = 0; i < _checkedSwitchIsPresent.Length; i++)
            {
                _checkedSwitchIsPresent[i] = SmartCardManager.IsPressureSwitchPresent(i + 1);

                if (_wasSwitchPresent[i] != _checkedSwitchIsPresent[i])
                {
                    Thread.Sleep(500);
                    // Try again.
                    _checkedSwitchIsPresent[i] = SmartCardManager.IsPressureSwitchPresent(i + 1);
                }
                else if (_checkedSwitchIsPresent[i])   // If pressure switch is still present since last check, we need to then re-check the pressure.
                {
                    // Check the pressure.
                    // Save the state for setting later to avoid synchronization issues.
                    _checkedIsPressureGood[i] = SmartCardManager.CheckPressureSwitch(i + 1);
                    if (_wasPressureGood[i] != _checkedIsPressureGood[i])
                    {
                        Thread.Sleep(500);
                        // Try again.
                        _checkedIsPressureGood[i] = SmartCardManager.CheckPressureSwitch(i + 1);
                    }
                }
            }
        }
        /// <summary>
        /// If pressure switch is present, then this methods reads the pressure level from it,
        /// otherwise this method returns Full.
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        static private PressureLevel ReadPressureLevel(int position)
        {
            const string funcName = "ReadPressureLevel";

            if (!SmartCardManager.IsPressureSwitchPresent(position))
            {
                Log.Debug(funcName + ": Cylinder " + position + " has no pressure switch. Defaulting to Full");
                return(PressureLevel.Full);
            }

            if (SmartCardManager.CheckPressureSwitch(position))
            {
                Log.Debug(funcName + ": Cylinder " + position + " pressure is Full");
                return(PressureLevel.Full);
            }

            Log.Debug(funcName + ": Cylinder " + position + " pressure is Low");
            return(PressureLevel.Low);
        }
        /// <summary>
        /// Return the information from all smart cards that are present.
        /// </summary>
        /// <returns>All of the installed cylinders.</returns>
        static private void ReadChangedCylinders(bool[] changedSmartCards, IList <GasEndPoint> changedGasEndPoints, PortRestrictions port1Restrictions)
        {
            const string funcName = "ReadChangedCylinders: ";

            Log.Assert(changedSmartCards != null, funcName + "changedSmartCards should not be null.");

            DockingStation dockingStation = Controller.GetDockingStation(); // SGF  8-Nov-2012  INS-2686

            changedGasEndPoints.Clear();

            for (int i = 0; i < changedSmartCards.Length; i++)
            {
                int position = i + 1;

                Log.Debug(funcName + "POSITION " + position);

                if (changedSmartCards[position - 1] == false)     // No detection of a card insertion, nor a removal?
                {
                    Log.Debug(string.Format("{0}Position {1} Smart card SKIPPED; No insertion change detected.", funcName, position));

                    if (position == Controller.FRESH_AIR_GAS_PORT)
                    {
                        GasEndPoint persistedPort1GasEndPoint = new GasEndPointDataAccess().FindByPosition(Controller.FRESH_AIR_GAS_PORT);

                        // If there's nothing known to be installed on port 1, then create a fresh air
                        // cylinder for the port.
                        // This could happen if, while nothing was installed on the port, a SettingsUpdate
                        // just occurred previously where the port setting was changed to allow fresh air.
                        // In that situation, we have to then make the fresh air cylinder.
                        if (persistedPort1GasEndPoint == null)
                        {
                            Log.Debug(string.Format("{0}Position {1}, No persisted cylinder; assuming Fresh Air.", funcName, position));
                            GasEndPoint freshAirCylinder = GasEndPoint.CreateFreshAir(position);
                            freshAirCylinder.GasChangeType = GasEndPoint.ChangeType.Installed;
                            changedGasEndPoints.Add(freshAirCylinder);
                        }
                    }

                    // SGF  8-Nov-2012  INS-2686 -- begin
                    // Soon, we will check to see if a pressure switch is present, and if so, read the pressure level.
                    // Before we do that, we must check for two scenarios in which it is not necessary or appropriate
                    // to read a pressure switch.
                    //     1. If there is no cylinder attached to the port, there is no reason to check for a pressure switch.
                    //     2. If this is the first port, and we know that the port is drawing fresh air, there is no reason
                    //        to check for a pressure switch.  This case cannot be handled by case #1, as we define a "logical"
                    //        cylinder to represent fresh air.
                    // If we find either scenario for the current port, we skip further processing on this port, and proceed to
                    // the next one.
                    GasEndPoint currentInstalledCylinder = dockingStation.GasEndPoints.Find(ic => ic.Position == position);
                    if (currentInstalledCylinder == null)
                    {
                        Log.Debug(string.Format("{0}Position {1} No cylinder present; do not check for pressure switch.", funcName, position));
                        continue;
                    }
                    else
                    {
                        bool isFreshAir = currentInstalledCylinder.Cylinder.IsFreshAir;
                        if (isFreshAir)
                        {
                            Log.Debug(string.Format("{0}Position {1} Fresh air; do not check for pressure switch.", funcName, position));
                            continue;
                        }
                    }
                    // SGF  8-Nov-2012  INS-2686 -- end

                    // SMARTCARD NOT CHANGED (NO INSERT OR REMOVAL DETECTED).
                    // WE NEED TO AT LEAST ALWAYS READ THE PRESSURE SWITCH THEN.
                    if (SmartCardManager.IsPressureSwitchPresent(position))
                    {
                        GasEndPoint pressureCylinder = new GasEndPoint();
                        pressureCylinder.Position          = position;
                        pressureCylinder.Cylinder.Pressure = ReadPressureLevel(position);
                        pressureCylinder.GasChangeType     = GasEndPoint.ChangeType.PressureChanged;

                        Log.Debug(string.Format("{0}Position {1} Pressure Switch reports {2}.", funcName, position, pressureCylinder.Cylinder.Pressure));

                        changedGasEndPoints.Add(pressureCylinder);
                    }
                    else
                    {
                        Log.Debug(string.Format("{0}Position {1} Pressure Switch not present.", funcName, position));
                    }

                    continue;
                }

                // IF WE MAKE IT TO HERE, THE CARD HAS BEEN EITHER INSERTED OR REMOVED.

                if (!SmartCardManager.IsCardPresent(position))      // CARD REMOVED?
                {
                    Log.Debug(string.Format("{0}Position {1} SmartCard not present, Returning CardRemoved", funcName, position));

                    // Server needs to know specifically that cylinder is missing.
                    // Indicate this with an InstalledCylinder containing an empty Cylinder object.
                    GasEndPoint missingCylinder = new GasEndPoint();
                    missingCylinder.Position      = position;
                    missingCylinder.GasChangeType = GasEndPoint.ChangeType.Uninstalled;
                    changedGasEndPoints.Add(missingCylinder);

                    // If a cylinder is not installed on the fresh air port, then assume fresh air
                    // for the port
                    if (position == Controller.FRESH_AIR_GAS_PORT)
                    {
                        Log.Debug(string.Format("{0}Position {1} is assumed to be Fresh Air.", funcName, position));
                        GasEndPoint freshAirCylinder = GasEndPoint.CreateFreshAir(position);
                        freshAirCylinder.GasChangeType = GasEndPoint.ChangeType.Installed;
                        changedGasEndPoints.Add(freshAirCylinder);
                    }

                    continue;
                }

                // IF WE MAKE IT TO HERE, THE CARD HAS BEEN INSERTED.

                Cylinder cylinder = SmartCardManager.ReadCard(position);
                if (cylinder == null)    // Couldn't read valid cylinder?  Driver error or corrupt card.
                {
                    Log.Debug(string.Format("{0}Position {1}, ReadCard returned null. SKIPPING cylinder.", funcName, position));
                    continue;
                }

                // Dates read from card will be in 'local' time, but everything we deal with is in UTC.
                cylinder.ExpirationDate = Configuration.ToUniversalTime(cylinder.ExpirationDate);
                cylinder.RefillDate     = Configuration.ToUniversalTime(cylinder.RefillDate);

                Thread.Sleep(1000);

                cylinder.Pressure = ReadPressureLevel(position);

                GasEndPoint gasEndPoint = new GasEndPoint(cylinder, position, GasEndPoint.Type.iGas);
                gasEndPoint.GasChangeType = GasEndPoint.ChangeType.Installed;
                Log.Debug(string.Format("{0}Position {1}, Returning CardInserted. Pressure={2}", funcName, position, cylinder.Pressure));

                changedGasEndPoints.Add((GasEndPoint)gasEndPoint.Clone());
            }  // end-for

            return;
        }
        /// <summary>
        /// Return the information from all smart cards, manifolds and manual cylinders that are present at start-up.
        /// </summary>
        /// <param name="installedCylinders">Information about the cylinders is placed into this passed-in list.</param>
        static private void ReadInstalledCylinders(List <GasEndPoint> gasEndPoints, PortRestrictions port1Restrictions)
        {
            const string funcName = "ReadInstalledCylinders: ";

            // Get all currently attached manifolds and manually-assigned cylinders.
            List <GasEndPoint> manGasEndPoints
                = new GasEndPointDataAccess().FindAll().FindAll(m => m.InstallationType == GasEndPoint.Type.Manifold ||
                                                                m.InstallationType == GasEndPoint.Type.Manual);

            for (int position = 1; position <= Configuration.DockingStation.NumGasPorts; position++)
            {
                Log.Debug(funcName + "POSITION " + position);

                // iGas cylinders take precendence
                if (!SmartCardManager.IsCardPresent(position))
                {
                    Log.Debug(string.Format("{0}Position {1}, No iGas card detected.", funcName, position));

                    // Does the port have a manifold or manual cylinder attached?  Then make sure we include
                    // that cylinder in the returned list. If no cylinder exists on port 1, then create a
                    // virtual fresh air cylinder.
                    GasEndPoint man = manGasEndPoints.Find(m => m.Position == position);
                    if (man != null)
                    {
                        Log.Debug(string.Format("{0}Position {1} {2} found (\"{3}\", \"{4}\", Pressure {5}).", funcName, position,
                                                man.InstallationType == GasEndPoint.Type.Manifold ? "Manifold" : "Manual Cylinder",
                                                man.Cylinder.FactoryId, man.Cylinder.PartNumber, man.Cylinder.Pressure));
                        gasEndPoints.Add(man);
                    }
                    else if (position == Controller.FRESH_AIR_GAS_PORT)
                    {
                        Log.Debug(string.Format("{0}Position {1} is assumed to be Fresh Air.", funcName, position));
                        GasEndPoint freshAirEndPoint = GasEndPoint.CreateFreshAir(position);
                        freshAirEndPoint.GasChangeType = GasEndPoint.ChangeType.Installed;
                        gasEndPoints.Add(freshAirEndPoint);
                    }
                    continue;
                }

                // IF WE MAKE IT TO HERE, THEN WE KNOW WE HAVE AN INSERTED SMART CARD WHICH MEANS iGas IS ATTACHED.

                Cylinder cylinder = SmartCardManager.ReadCard(position);
                if (cylinder == null)   // Check for a valid cylinder.
                {
                    Log.Debug(string.Format("{0}Position {1}, ReadCard returned null. SKIPPING cylinder.", funcName, position));
                    continue;
                }

                // Dates read from card will be in 'local' time, but everything we deal with is in UTC.
                cylinder.ExpirationDate = Configuration.ToUniversalTime(cylinder.ExpirationDate);
                cylinder.RefillDate     = Configuration.ToUniversalTime(cylinder.RefillDate);

                Thread.Sleep(1000);

                if (SmartCardManager.IsPressureSwitchPresent(position))
                {
                    if (SmartCardManager.CheckPressureSwitch(position))
                    {
                        cylinder.Pressure = PressureLevel.Full;
                    }
                    else
                    {
                        cylinder.Pressure = PressureLevel.Low;
                    }

                    Log.Debug(string.Format("{0}Position {1} Pressure Switch reports {2}.", funcName, position, cylinder.Pressure));
                }
                else
                {
                    Log.Debug(string.Format("{0}Position {1} Pressure Switch not detected.", funcName, position));
                }

                GasEndPoint gasEndPoint = new GasEndPoint(cylinder, position, GasEndPoint.Type.iGas);

                // Add the installed cylinder to the DockingStation (IDS).
                gasEndPoints.Add(gasEndPoint);
            }

            return;
        }