public void Calibrate(bool reuse) { int voltageG; int currentG; // Read uid from device this.uid = uidReader.ReadUid(); logger.DebugFormat("Read UID {0}", this.uid); var possibleCalibrations = getCalibrationDataFromRepo(); if (possibleCalibrations.Any() && reuse) { logger.Debug("Reusing existing values"); var existingCalibration = possibleCalibrations.OrderByDescending(c => c.DateCalibrated).First(); currentG = existingCalibration.CurrentGain; voltageG = existingCalibration.VoltageGain; } else { logger.Debug("Reading new values"); // Set voltage and current for signal generator sigGenerator.SetOutput(187, 60); // Load firmware onto chip for CLI commands loadFirmware.LoadFirmware(); UpdateStateChange(StateOfDevice.LoadFirmware); // Read voltage from device and calibrate voltageG = valueParser.ReadVoltageValue(); UpdateStateChange(StateOfDevice.CalibrateVoltage); // Read current from device and calibrate sigGenerator.SetOutput(38, 60); currentG = valueParser.ReadCurrentValue(); UpdateStateChange(StateOfDevice.CalibrateCurrent); // Insert values into table CalibrationValue cv = new CalibrationValue(); cv.UidId = uidRepository.GetBy(e => e.UID.Equals(this.uid)).Id; cv.CurrentGain = currentG; cv.VoltageGain = voltageG; calibrationRepository.Insert(cv); calibrationRepository.Submit(); UpdateStateChange(StateOfDevice.DatabaseInsert); } logger.DebugFormat("Updated database for current gain to {0} and voltage gain to {1}", currentG, voltageG); // Once here, device has passed all phases and device is calibrated UpdateStateChange(StateOfDevice.CalibrationComplete); }