Improved config code / xml format

This commit is contained in:
Charles J. Cliffe 2015-04-21 23:19:45 -04:00
parent adc1500091
commit ca851add76
4 changed files with 92 additions and 40 deletions

View File

@ -1,7 +1,53 @@
#include "AppConfig.h"
std::string AppConfig::getConfigDir() {
DeviceConfig::DeviceConfig() : ppm(0), deviceId("") {
}
DeviceConfig::DeviceConfig(std::string deviceId) : ppm(0) {
this->deviceId = deviceId;
}
void DeviceConfig::setPPM(int ppm) {
this->ppm = ppm;
}
int DeviceConfig::getPPM() {
return ppm;
}
void DeviceConfig::setDeviceId(std::string deviceId) {
this->deviceId = deviceId;
}
std::string DeviceConfig::getDeviceId() {
return deviceId;
}
void DeviceConfig::save(DataNode *node) {
node->newChild("id")->element()->set(deviceId);
DataNode *ppm_node = node->newChild("ppm");
ppm_node->element()->set((int)ppm);
}
void DeviceConfig::load(DataNode *node) {
if (node->hasAnother("ppm")) {
DataNode *ppm_node = node->getNext("ppm");
int ppmValue = 0;
ppm_node->element()->get(ppmValue);
setPPM(ppmValue);
std::cout << "Loaded PPM for device '" << deviceId << "' at " << ppmValue << "ppm" << std::endl;
}
}
DeviceConfig *AppConfig::getDevice(std::string deviceId) {
DeviceConfig *conf = &deviceConfig[deviceId];
conf->setDeviceId(deviceId);
return conf;
}
std::string AppConfig::getConfigDir() {
std::string dataDir = wxStandardPaths::Get().GetUserDataDir().ToStdString();
bool mkStatus = false;
@ -19,33 +65,21 @@ std::string AppConfig::getConfigDir() {
return dataDir;
}
void AppConfig::setPPM(std::string deviceId, int ppm) {
device_ppm[deviceId] = ppm;
}
int AppConfig::getPPM(std::string deviceId) {
if (device_ppm.find(deviceId) == device_ppm.end()) {
return 0;
}
return device_ppm[deviceId];
}
bool AppConfig::save() {
DataTree cfg;
cfg.rootNode()->setName("cubicsdr_config");
DataNode *ppm_data = cfg.rootNode()->newChild("ppm");
DataNode *devices_node = cfg.rootNode()->newChild("devices");
std::map<std::string, int>::iterator device_ppm_i;
for (device_ppm_i = device_ppm.begin(); device_ppm_i != device_ppm.end(); device_ppm_i++) {
DataNode *ppm_ent = ppm_data->newChild("device");
ppm_ent->newChild("id")->element()->set(device_ppm_i->first);
ppm_ent->newChild("value")->element()->set((int)device_ppm_i->second);
std::map<std::string, DeviceConfig>::iterator device_config_i;
for (device_config_i = deviceConfig.begin(); device_config_i != deviceConfig.end(); device_config_i++) {
DataNode *device_node = devices_node->newChild("device");
device_config_i->second.save(device_node);
}
std::string cfgFileDir = getConfigDir();
wxFileName cfgFile = wxFileName(cfgFileDir, "cubicsdr.xml");
wxFileName cfgFile = wxFileName(cfgFileDir, "config.xml");
std::string cfgFileName = cfgFile.GetFullPath(wxPATH_NATIVE).ToStdString();
if (!cfg.SaveToFileXML(cfgFileName)) {
@ -60,7 +94,7 @@ bool AppConfig::load() {
DataTree cfg;
std::string cfgFileDir = getConfigDir();
wxFileName cfgFile = wxFileName(cfgFileDir, "cubicsdr.xml");
wxFileName cfgFile = wxFileName(cfgFileDir, "config.xml");
std::string cfgFileName = cfgFile.GetFullPath(wxPATH_NATIVE).ToStdString();
if (!cfgFile.Exists()) {
@ -68,25 +102,24 @@ bool AppConfig::load() {
}
if (cfgFile.IsFileReadable()) {
std::cout << "Loading:: configuration file '" << cfgFileName << "'" << std::endl;
cfg.LoadFromFileXML(cfgFileName);
} else {
std::cout << "Error loading:: configuration file '" << cfgFileName << "' is not readable!" << std::endl;
return false;
}
if (cfg.rootNode()->hasAnother("ppm")) {
device_ppm.clear();
if (cfg.rootNode()->hasAnother("devices")) {
DataNode *devices_node = cfg.rootNode()->getNext("devices");
DataNode *ppm_data = cfg.rootNode()->getNext("ppm");
while (devices_node->hasAnother("device")) {
DataNode *device_node = devices_node->getNext("device");
if (device_node->hasAnother("id")) {
std::string deviceId;
device_node->getNext("id")->element()->get(deviceId);
while (ppm_data->hasAnother("device")) {
DataNode *ppm_ent = ppm_data->getNext("device");
if (ppm_ent->hasAnother("id") && ppm_ent->hasAnother("value")) {
std::string deviceId(*ppm_ent->getNext("id"));
int ppmValue = *ppm_ent->getNext("value");
setPPM(deviceId, ppmValue);
std::cout << "Loaded PPM for device '" << deviceId << "' at " << ppmValue << "ppm" << std::endl;
getDevice(deviceId)->load(device_node);
}
}
}

View File

@ -6,16 +6,35 @@
#include "DataTree.h"
class DeviceConfig {
public:
DeviceConfig();
DeviceConfig(std::string deviceId);
void setPPM(int ppm);
int getPPM();
void setDeviceId(std::string deviceId);
std::string getDeviceId();
void save(DataNode *node);
void load(DataNode *node);
private:
std::string deviceId;
int ppm;
};
class AppConfig {
public:
std::string getConfigDir();
DeviceConfig *getDevice(std::string deviceId);
void setPPM(std::string deviceId, int ppm);
int getPPM(std::string deviceId);
bool save();
bool load();
bool reset();
private:
std::map<std::string, int> device_ppm;
std::map<std::string, DeviceConfig> deviceConfig;
};

View File

@ -38,7 +38,7 @@ bool CubicSDR::OnInit() {
return false;
}
wxApp::SetAppName("cubicsdr");
wxApp::SetAppName("CubicSDR");
config.load();
@ -226,7 +226,7 @@ void CubicSDR::setDevice(int deviceId) {
SDRDeviceInfo *dev = (*getDevices())[deviceId];
SDRThreadCommand command_ppm(SDRThreadCommand::SDR_THREAD_CMD_SET_PPM);
ppm = config.getPPM(dev->getDeviceId());
ppm = config.getDevice(dev->getDeviceId())->getPPM();
command_ppm.llong_value = ppm;
threadCmdQueueSDR->push(command_ppm);
}
@ -251,7 +251,7 @@ void CubicSDR::setPPM(int ppm_in) {
SDRDeviceInfo *dev = (*getDevices())[getDevice()];
config.setPPM(dev->getDeviceId(), ppm_in);
config.getDevice(dev->getDeviceId())->setPPM(ppm_in);
config.save();
}
@ -262,7 +262,7 @@ int CubicSDR::getPPM() {
SDRDeviceInfo *dev = (*getDevices())[getDevice()];
SDRThreadCommand command_ppm(SDRThreadCommand::SDR_THREAD_CMD_SET_PPM);
ppm = config.getPPM(dev->getDeviceId());
ppm = config.getDevice(dev->getDeviceId())->getPPM();
return ppm;
}

View File

@ -137,7 +137,7 @@ void SDRThread::threadMain() {
signed char buf[BUF_SIZE];
long long frequency = DEFAULT_FREQ;
int ppm = wxGetApp().getConfig()->getPPM(devs[deviceId]->getDeviceId());
int ppm = wxGetApp().getConfig()->getDevice(devs[deviceId]->getDeviceId())->getPPM();
rtlsdr_open(&dev, deviceId);
rtlsdr_set_sample_rate(dev, sampleRate);