VESPERSEnergyScanActionController::VESPERSEnergyScanActionController(VESPERSEnergyScanConfiguration *configuration, QObject *parent)
	: AMStepScanActionController(configuration, parent), VESPERSScanController(configuration)
{
	configuration_ = configuration;

	scan_ = new AMXASScan();
	scan_->setName(configuration_->name());
	scan_->setScanConfiguration(configuration_);
	scan_->setFileFormat("amCDFv1");
	scan_->setIndexType("fileSystem");
	scan_->setNotes(buildNotes());
	scan_->rawData()->addScanAxis(AMAxisInfo("eV", 0, "Incident Energy", "eV"));

	useFeedback_ = true;
	originalEnergy_ = VESPERSBeamline::vespers()->mono()->energy();

	AMControlInfoList list;
	list.append(VESPERSBeamline::vespers()->energy()->toInfo());
	configuration_->setAxisControlInfos(list);

	AMDetectorInfoSet detectors;
	detectors.addDetectorInfo(VESPERSBeamline::vespers()->exposedDetectorByName("SplitIonChamber")->toInfo());
	detectors.addDetectorInfo(VESPERSBeamline::vespers()->exposedDetectorByName("PreKBIonChamber")->toInfo());
	detectors.addDetectorInfo(VESPERSBeamline::vespers()->exposedDetectorByName("MiniIonChamber")->toInfo());
	detectors.addDetectorInfo(VESPERSBeamline::vespers()->exposedDetectorByName("PostIonChamber")->toInfo());
	detectors.addDetectorInfo(VESPERSBeamline::vespers()->exposedDetectorByName("EnergySetpoint")->toInfo());
	detectors.addDetectorInfo(VESPERSBeamline::vespers()->exposedDetectorByName("MasterDwellTime")->toInfo());
	detectors.addDetectorInfo(VESPERSBeamline::vespers()->exposedDetectorByName("RingCurrent")->toInfo());

	VESPERS::CCDDetectors ccdDetector = configuration_->ccdDetector();

	if (ccdDetector.testFlag(VESPERS::Roper))
		detectors.addDetectorInfo(VESPERSBeamline::vespers()->exposedDetectorByName("RoperFileNumber")->toInfo());

	if (ccdDetector.testFlag(VESPERS::Mar))
		detectors.addDetectorInfo(VESPERSBeamline::vespers()->exposedDetectorByName("MarFileNumber")->toInfo());

	if (ccdDetector.testFlag(VESPERS::Pilatus))
		detectors.addDetectorInfo(VESPERSBeamline::vespers()->exposedDetectorByName("PilatusFileNumber")->toInfo());

	configuration_->setDetectorConfigurations(detectors);

	secondsElapsed_ = 0;
	secondsTotal_ = configuration_->totalTime();
	elapsedTime_.setInterval(1000);
	connect(this, SIGNAL(started()), &elapsedTime_, SLOT(start()));
	connect(this, SIGNAL(cancelled()), &elapsedTime_, SLOT(stop()));
	connect(this, SIGNAL(paused()), &elapsedTime_, SLOT(stop()));
	connect(this, SIGNAL(resumed()), &elapsedTime_, SLOT(start()));
	connect(this, SIGNAL(failed()), &elapsedTime_, SLOT(stop()));
	connect(this, SIGNAL(finished()), &elapsedTime_, SLOT(stop()));
	connect(&elapsedTime_, SIGNAL(timeout()), this, SLOT(onScanTimerUpdate()));
}
VESPERSEnergyDacqScanController::VESPERSEnergyDacqScanController(VESPERSEnergyScanConfiguration *cfg, QObject *parent)
    : AMDacqScanController(cfg, parent), VESPERSScanController(cfg)
{
    config_ = cfg;

    secondsElapsed_ = 0;
    secondsTotal_ = config_->totalTime();
    elapsedTime_.setInterval(1000);
    connect(this, SIGNAL(started()), &elapsedTime_, SLOT(start()));
    connect(this, SIGNAL(cancelled()), &elapsedTime_, SLOT(stop()));
    connect(this, SIGNAL(paused()), &elapsedTime_, SLOT(stop()));
    connect(this, SIGNAL(resumed()), &elapsedTime_, SLOT(start()));
    connect(this, SIGNAL(failed()), &elapsedTime_, SLOT(stop()));
    connect(this, SIGNAL(finished()), &elapsedTime_, SLOT(stop()));
    connect(&elapsedTime_, SIGNAL(timeout()), this, SLOT(onScanTimerUpdate()));

    scan_ = new AMXASScan(); 	// MB: Moved from line 363 in startImplementation.
    scan_->setName(config_->name());
    scan_->setScanConfiguration(config_);
    scan_->setRunId(AMUser::user()->currentRunId());
    scan_->setIndexType("fileSystem");
    scan_->setFilePath(AMUserSettings::defaultRelativePathForScan(QDateTime::currentDateTime())+".cdf");
    scan_->setFileFormat("amCDFv1");
    scan_->replaceRawDataStore(new AMCDFDataStore(AMUserSettings::userDataFolder % scan_->filePath(), false));
    scan_->rawData()->addScanAxis(AMAxisInfo("eV", 0, "Incident Energy", "eV"));

    scan_->setNotes(buildNotes());

    AMOldDetectorSet *ionChambers = VESPERSBeamline::vespers()->ionChambers();
    AMMeasurementInfo temp = AMMeasurementInfo("", "");

    for (int i = 0; i < ionChambers->count(); i++) {

        temp = AMMeasurementInfo(*(ionChambers->detectorAt(i)->toInfo()));
        temp.name = ionChambers->detectorAt(i)->detectorName();
        scan_->rawData()->addMeasurement(temp);
        scan_->addRawDataSource(new AMRawDataSource(scan_->rawData(), scan_->rawData()->measurementCount() - 1), true, false);
    }

    addExtraDatasources();
    useDwellTimes(VESPERSBeamline::vespers()->dwellTimeTrigger(), VESPERSBeamline::vespers()->dwellTimeConfirmed());
}