Exemplo n.º 1
0
        /// <summary>
        /// Executes an instrument settings update operation.
        /// </summary>
        /// <returns>Docking station event</returns>
        public DockingStationEvent Execute()
        {
            Stopwatch stopwatch = Log.TimingBegin("INSTRUMENT SETTINGS UPDATE");

            // Check for a docked instrument.
            if (!Master.Instance.ControllerWrapper.IsDocked())
            {
                throw new InstrumentNotDockedException();
            }

            string     serialNumber   = Master.Instance.SwitchService.Instrument.SerialNumber;
            DeviceType instrumentType = Master.Instance.SwitchService.Instrument.Type;

            if (serialNumber == string.Empty || instrumentType == DeviceType.Unknown)
            {
                throw new InstrumentNotDockedException();
            }

            // Create the return event.
            _instrumentSettingsUpdateEvent = new InstrumentSettingsUpdateEvent(this);

            // Retrieve the docking station's information.
            _instrumentSettingsUpdateEvent.DockingStation = Master.Instance.ControllerWrapper.GetDockingStation();
            _instrumentSettingsUpdateEvent.DockedTime     = Master.Instance.SwitchService.DockedTime;

            // Get the settings from the database to update the instrument with.
            Instrument settings = new InstrumentDataAccess().FindApplicableSettings(serialNumber, instrumentType);

            // If no instrument settings found, then there's nothing more we can do.
            // since we can't update the instrument's settings if we have no settings.
            if (settings == null)
            {
                string errMsg = string.Format("Unable to find instrument settings for S/N \"{0}\"", serialNumber);
                _instrumentSettingsUpdateEvent.Errors.Add(new DockingStationError(errMsg, DockingStationErrorLevel.Warning));
                return(_instrumentSettingsUpdateEvent);
            }

            // The settings loaded only apply to the discovered instrument.
            // We don't know if the settings loaded are the defaults are not
            // so we just set the type to what we searched for above.
            settings.Type = instrumentType;

            // Merge the loaded settings with our Instrument.

            using (_instCtrlr = SwitchService.CreateInstrumentController())
            {
                // Open the serial port connection needed to communicate with the instrument.
                _instCtrlr.Initialize(InstrumentController.Mode.Batch);

                // After an instrument settings read, we need to send a command to the instrument
                // to have it clear its base unit log.  Only SafeCore needs this command.
                _instCtrlr.ClearBaseUnits();

                // Update the docked instrument.
                _instrumentSettingsUpdateEvent.DockedInstrument = UpdateInstrument(settings, serialNumber);

                // Settings RefId will contain the ID of the settings used during the Update Event.  Otherwise, will be Nullid.
                // We place it into the event's Instrument in order to upload it to iNet.
                _instrumentSettingsUpdateEvent.DockedInstrument.RefId = settings.RefId;

                if (_instrumentSettingsUpdateEvent.DockingStation.ClearPeaksUponDocking)
                {
                    _instCtrlr.ClearInstrumentSensorPeaks(_instrumentSettingsUpdateEvent.DockedInstrument.InstalledComponents);
                }
            } // end-using

            Log.TimingEnd("INSTRUMENT SETTINGS UPDATE", stopwatch);

            return(_instrumentSettingsUpdateEvent);
        }