Exemple #1
0
void Utility::copyDeltaProperties(const Poco::Util::AbstractConfiguration& ref, const Poco::Util::AbstractConfiguration& source, Poco::Util::AbstractConfiguration& target, const std::set<std::string>& excludeSet, const std::string& root)
{
	Poco::Util::AbstractConfiguration::Keys keys;
	source.keys(root, keys);

	if (keys.empty() && source.hasProperty(root))
	{
		if ((ref.hasProperty(root) && ref.getRawString(root) != source.getRawString(root)) || !ref.hasProperty(root))
		{
			target.setString(root, source.getRawString(root));
		}
	}
	else
	{
		for (Poco::Util::AbstractConfiguration::Keys::const_iterator it = keys.begin(); it != keys.end(); ++it)
		{
			std::string fullKey = root;
			if (!fullKey.empty()) fullKey += '.';
			fullKey.append(*it);
			if (excludeSet.find(fullKey) == excludeSet.end())
			{
				copyDeltaProperties(ref, source, target, excludeSet, fullKey);
			}
		}
	}
}
	void start(BundleContext::Ptr pContext)
	{
		_pTimer = new Poco::Util::Timer;
		_pContext = pContext;
		_pPrefs = ServiceFinder::find<PreferencesService>(pContext);

		Poco::Util::AbstractConfiguration::Keys keys;
		_pPrefs->configuration()->keys("simulation.sensors", keys);
		for (std::vector<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it)
		{
			std::string baseKey = "simulation.sensors.";
			baseKey += *it;

			SimulatedSensor::Params params;
			params.id = SimulatedSensor::SYMBOLIC_NAME;
			params.id += "#";
			params.id += Poco::NumberFormatter::format(_serviceRefs.size());

			params.physicalQuantity = _pPrefs->configuration()->getString(baseKey + ".physicalQuantity", "");
			params.physicalUnit     = _pPrefs->configuration()->getString(baseKey + ".physicalUnit", "");
			params.initialValue     = _pPrefs->configuration()->getDouble(baseKey + ".initialValue", 0.0);
			params.delta            = _pPrefs->configuration()->getDouble(baseKey + ".delta", 0.0);
			params.cycles           = _pPrefs->configuration()->getInt(baseKey + ".cycles", 0);
			params.updateRate       = _pPrefs->configuration()->getDouble(baseKey + ".updateRate", 0.0);

			std::string mode = _pPrefs->configuration()->getString(baseKey + ".mode", "linear");
			if (mode == "linear")
				params.mode = SimulatedSensor::SIM_LINEAR;
			else if (mode == "random")
				params.mode = SimulatedSensor::SIM_RANDOM;

			try
			{
				createSensor(params);
			}
			catch (Poco::Exception& exc)
			{
				pContext->logger().error(Poco::format("Cannot create simulated sensor: %s", exc.displayText()));
			}
		}

		std::string gpxPath = _pPrefs->configuration()->getString("simulation.gnss.gpxPath", "");
		if (!gpxPath.empty())
		{
			SimulatedGNSSSensor::Params params;
			params.id = SimulatedGNSSSensor::SYMBOLIC_NAME;
			params.gpxPath = gpxPath;
			params.loopReplay = _pPrefs->configuration()->getBool("simulation.gnss.loopReplay", true);
			params.speedUp = _pPrefs->configuration()->getDouble("simulation.gnss.speedUp", 1.0);

			try
			{
				createGNSSSensor(params);
			}
			catch (Poco::Exception& exc)
			{
				pContext->logger().error(Poco::format("Cannot create simulated GNSS sensor: %s", exc.displayText()));
			}
		}
	}
	void start(BundleContext::Ptr pContext)
	{
		_pContext = pContext;
		_pPrefs = ServiceFinder::find<PreferencesService>(pContext);
		
		Poco::Util::AbstractConfiguration::Keys keys;
		_pPrefs->configuration()->keys("serial.ports", keys);
		int index = 0;
		for (std::vector<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it)
		{
			std::string baseKey = "serial.ports.";
			baseKey += *it;

			std::string device = _pPrefs->configuration()->getString(baseKey + ".device", "");
			std::string params = _pPrefs->configuration()->getString(baseKey + ".params", "8N1");
			int speed = _pPrefs->configuration()->getInt(baseKey + ".speed", 9600);
		
			try
			{
				pContext->logger().information(Poco::format("Creating serial port for device '%s'.", device));

				SerialDeviceImpl::SerialPortPtr pSerialPort = new SerialPort(device, speed, params);
				createSerialDevice(Poco::NumberFormatter::format(index), pSerialPort);
			}
			catch (Poco::Exception& exc)
			{
				pContext->logger().error(Poco::format("Cannot create serial port for device '%s': %s", device, exc.displayText())); 
			}
			index++;
		}
	}
	void start(BundleContext::Ptr pContext)
	{
		_pContext = pContext;
		_pPrefs = ServiceFinder::find<PreferencesService>(pContext);
		
		Poco::Util::AbstractConfiguration::Keys keys;
		_pPrefs->configuration()->keys("mqtt.clients", keys);
		for (std::vector<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it)
		{
			std::string baseKey = "mqtt.clients.";
			baseKey += *it;
			createClient(baseKey, *it);
		}
	}
void WinConfigurationTest::testConfiguration()
{
    WinRegistryKey regKey("HKEY_CURRENT_USER\\Software\\Applied Informatics\\Test");
    if (regKey.exists()) regKey.deleteKey();
    assert (!regKey.exists());

    AutoPtr<WinRegistryConfiguration> pReg = new WinRegistryConfiguration("HKEY_CURRENT_USER\\Software\\Applied Informatics\\Test");
    pReg->setString("name1", "value1");
    assert (pReg->getString("name1") == "value1");
    pReg->setInt("name1", 1); // overwrite should also change type
    assert (pReg->getInt("name1") == 1);
    pReg->setString("name2", "value2");
    assert (pReg->getString("name2") == "value2");
#if defined(POCO_HAVE_INT64)
    pReg->setUInt64("name2", std::numeric_limits<UInt64>::max()); // overwrite should also change type
    assert (pReg->getUInt64("name2") == std::numeric_limits<UInt64>::max());
    pReg->setInt64("name2", std::numeric_limits<Int64>::min());
    assert (pReg->getInt64("name2") == std::numeric_limits<Int64>::min());
#endif
    assert (pReg->hasProperty("name1"));
    assert (pReg->hasProperty("name2"));

    std::string dfl = pReg->getString("nonexistent", "default");
    assert (dfl == "default");

    AutoPtr<Poco::Util::AbstractConfiguration> pView = pReg->createView("config");
    dfl = pView->getString("sub.foo", "default");
    assert (dfl == "default");

    pView->setString("sub.foo", "bar");
    assert (pView->getString("sub.foo", "default") == "bar");

    std::string value;
    assert (pReg->convertToRegFormat("A.B.C", value) == "A\\B");
    assert (value == "C");

    Poco::Util::AbstractConfiguration::Keys keys;
    pReg->keys(keys);
    assert (keys.size() == 3);
    assert (std::find(keys.begin(), keys.end(), "name1") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "name2") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "config") != keys.end());

    pReg->keys("config", keys);
    assert (keys.size() == 1);
    assert (std::find(keys.begin(), keys.end(), "sub") != keys.end());

    AutoPtr<WinRegistryConfiguration> pRootReg = new WinRegistryConfiguration("");

    assert (pRootReg->getInt("HKEY_CURRENT_USER.Software.Applied Informatics.Test.name1") == 1);

    pRootReg->keys(keys);
    assert (keys.size() == 6);
    assert (std::find(keys.begin(), keys.end(), "HKEY_CLASSES_ROOT") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "HKEY_CURRENT_CONFIG") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "HKEY_CURRENT_USER") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "HKEY_LOCAL_MACHINE") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "HKEY_PERFORMANCE_DATA") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "HKEY_USERS") != keys.end());

    pRootReg->keys("HKEY_CURRENT_USER.Software.Applied Informatics.Test", keys);
    assert (keys.size() == 3);
    assert (std::find(keys.begin(), keys.end(), "name1") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "name2") != keys.end());
    assert (std::find(keys.begin(), keys.end(), "config") != keys.end());
}