<commit_before>/*
 * APSRack.cpp
 *
 *  Created on: Jun 13, 2012
 *      Author: cryan
 */

#include "APSRack.h"

APSRack::APSRack() : numDevices_{0} {
}

APSRack::~APSRack()  {
	//Close the logging file so we don't leave it dangling
	fclose(Output2FILE::Stream());
}

//Initialize the rack by polling for devices and serial numbers
int APSRack::init() {

	//Create the logger
	FILE* pFile = fopen("libaps.log", "a");
	Output2FILE::Stream() = pFile;

	//Enumerate the serial numbers of the devices attached
	enumerate_devices();

	return 0;
}

//Initialize a specific APS unit
int APSRack::initAPS(const int & deviceID, const string & bitFile, const bool & forceReload){
	return APSs_[deviceID].init(bitFile, forceReload);
}

int APSRack::get_num_devices()  {
	int numDevices;
	FT_ListDevices(&numDevices, NULL, FT_LIST_NUMBER_ONLY);
	if (numDevices_ != numDevices) {
		update_device_enumeration();
	}
	return numDevices;
}

string APSRack::get_deviceSerial(const int & deviceID) {
	vector<string> testSerials;
	FTDI::get_device_serials(testSerials);

	bool runUpdate = false;

	if (testSerials.size() != (unsigned int) numDevices_) {
		runUpdate = true;
	} else {
		// match serials to make sure nothing has changed
		for(unsigned int cnt = 0; cnt < testSerials.size() && ! runUpdate; cnt++) {
			if (testSerials[cnt].compare(deviceSerials_[cnt]) != 0) {
				runUpdate = true;
			}
		}
	}

	if (runUpdate) {
		update_device_enumeration();
	}

	// test to make sure size is still valid
	if ((unsigned int) deviceID > deviceSerials_.size()) {
		return "InvalidID";
	}


	return deviceSerials_[deviceID];
}

//This will reset the APS vector so it really should only be called during initialization
void APSRack::enumerate_devices() {

	FTDI::get_device_serials(deviceSerials_);
	numDevices_ = deviceSerials_.size();

	APSs_.clear();
	APSs_.reserve(numDevices_);

	//	Now setup the map between device serials and number and assign the APS units appropriately
	//	Also setup the FPGA checksums
	size_t devicect = 0;
	for (string tmpSerial : deviceSerials_) {
		serial2dev[tmpSerial] = devicect;
		APSs_.emplace_back(devicect, tmpSerial);
		FILE_LOG(logDEBUG) << "Device " << devicect << " has serial number " << tmpSerial;
		devicect++;
	}
}

// This will update enumerate of devices by matchin serial numbers
// If a device is missing it will be remove
// New devices are added 
// Old devices are left as is
void APSRack::update_device_enumeration() {
	vector<string> newSerials;
	FTDI::get_device_serials(newSerials);

	// construct new APS_ vector * new serial2dev map
	vector<APS> newAPS_;
	map<string, int> newSerial2dev;

	size_t devicect = 0;
	for (string tmpSerial : newSerials) {

		newSerial2dev[tmpSerial] = devicect;

		// does the new serial number exist in the old list?
		if ( serial2dev.count(tmpSerial) > 0) {
			// copy from old APS_ vector to new
			newAPS_.push_back(std::move(APSs_[serial2dev[tmpSerial]]));
			FILE_LOG(logDEBUG) << "Old Device " << devicect << " [ " << tmpSerial << " ] Copied";
		} else {
			newAPS_.emplace_back(devicect, tmpSerial);
			FILE_LOG(logDEBUG) << "New Device " << devicect << " has serial number " << tmpSerial;
		}

		newSerial2dev[tmpSerial] = devicect;
		devicect++;
	}

	// update APSRack members with new lists

	numDevices_ = deviceSerials_.size();  // number of devices
	deviceSerials_ = newSerials;		  // device serial vector<commit_msg>Added additional comments to update_device_enumeration.<commit_after>/*
 * APSRack.cpp
 *
 *  Created on: Jun 13, 2012
 *      Author: cryan
 */

#include "APSRack.h"

APSRack::APSRack() : numDevices_{0} {
}

APSRack::~APSRack()  {
	//Close the logging file so we don't leave it dangling
	fclose(Output2FILE::Stream());
}

//Initialize the rack by polling for devices and serial numbers
int APSRack::init() {

	//Create the logger
	FILE* pFile = fopen("libaps.log", "a");
	Output2FILE::Stream() = pFile;

	//Enumerate the serial numbers of the devices attached
	enumerate_devices();

	return 0;
}

//Initialize a specific APS unit
int APSRack::initAPS(const int & deviceID, const string & bitFile, const bool & forceReload){
	return APSs_[deviceID].init(bitFile, forceReload);
}

int APSRack::get_num_devices()  {
	int numDevices;
	FT_ListDevices(&numDevices, NULL, FT_LIST_NUMBER_ONLY);
	if (numDevices_ != numDevices) {
		update_device_enumeration();
	}
	return numDevices;
}

string APSRack::get_deviceSerial(const int & deviceID) {

	// Get serials from FTDI layer to check for change in devices
	vector<string> testSerials;
	FTDI::get_device_serials(testSerials);

	bool runUpdate = false;

	// if the number of devices has changed re-enumerate
	if (testSerials.size() != (unsigned int) numDevices_) {
		runUpdate = true;
	} else {
		// match serials for each device id to make sure nothing has changed
		for(unsigned int cnt = 0; cnt < testSerials.size() && ! runUpdate; cnt++) {
			if (testSerials[cnt].compare(deviceSerials_[cnt]) != 0) {
				runUpdate = true;  
			}
		}
	}

	// number or serials do not match so update enumeration
	if (runUpdate) {
		update_device_enumeration();
	}

	// test to make sure ID is still valid relative to vector size
	if ((unsigned int) deviceID > deviceSerials_.size()) {
		return "InvalidID";
	}


	return deviceSerials_[deviceID];
}

//This will reset the APS vector so it really should only be called during initialization
void APSRack::enumerate_devices() {

	FTDI::get_device_serials(deviceSerials_);
	numDevices_ = deviceSerials_.size();

	APSs_.clear();
	APSs_.reserve(numDevices_);

	//	Now setup the map between device serials and number and assign the APS units appropriately
	//	Also setup the FPGA checksums
	size_t devicect = 0;
	for (string tmpSerial : deviceSerials_) {
		serial2dev[tmpSerial] = devicect;
		APSs_.emplace_back(devicect, tmpSerial);
		FILE_LOG(logDEBUG) << "Device " << devicect << " has serial number " << tmpSerial;
		devicect++;
	}
}

// This will update enumerate of devices by matching serial numbers
// If a device is missing it will be remove
// New devices are added 
// Old devices are left as is (moved in place to new vector)
void APSRack::update_device_enumeration() {

	// get serial numbers from FTDI layer
	vector<string> newSerials;
	FTDI::get_device_serials(newSerials);

	// construct new APS_ vector & new serial2dev map
	vector<APS> newAPS_;
	map<string, int> newSerial2dev;

	size_t devicect = 0;
	for (string tmpSerial : newSerials) {

		// does the new serial number exist in the old list?
		if ( serial2dev.count(tmpSerial) > 0) {
			// move from old APSs_ vector to new
			newAPS_.push_back(std::move(APSs_[serial2dev[tmpSerial]]));
			FILE_LOG(logDEBUG) << "Old Device " << devicect << " [ " << tmpSerial << " ] Copied";
		} else {
			// does not exist so construct it in the new vector
			newAPS_.emplace_back(devicect, tmpSerial);
			FILE_LOG(logDEBUG) << "New Device " << devicect << " has serial number " << tmpSerial;
		}

		newSerial2dev[tmpSerial] = devicect;
		devicect++;
	}

	// update APSRack members with new lists

	numDevices_ = deviceSerials_.size();  // number of devices
	deviceSerials_ = newSerials;		  // device serial vector
{'fraction_non_alphanumeric': 0.11487811711964135, 'fraction_numerical': 0.006024096385542169, 'mean_word_length': 4.222384784198976, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3934305', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2367, 'n_words': 699}