void createClient(const std::string& baseConfig, const std::string& id) { std::string serverURI = getStringConfig(baseConfig + ".serverURI", ""); std::string clientId = getStringConfig(baseConfig + ".clientId", ""); std::string persistencePath = getStringConfig(baseConfig + ".persistence.path", ""); MQTTClientImpl::Persistence persistence = persistencePath.empty() ? MQTTClientImpl::MQTT_PERSISTENCE_NONE : MQTTClientImpl::MQTT_PERSISTENCE_FILE; if (!serverURI.empty()) { MQTTClientImpl::ConnectOptions options; options.keepAliveInterval = getIntConfig(baseConfig + ".keepAliveInterval", 60); options.retryInterval = getIntConfig(baseConfig + ".retryInterval", 30); options.connectTimeout = getIntConfig(baseConfig + ".connectTimeout", 20); options.cleanSession = getBoolConfig(baseConfig + ".cleanSession", true); options.reliable = getBoolConfig(baseConfig + ".reliable", false); options.username = getStringConfig(baseConfig + ".username", ""); options.password = getStringConfig(baseConfig + ".password", ""); options.willQoS = getIntConfig(baseConfig + ".will.qos", 0); options.mqttVersion = getIntConfig(baseConfig + ".mqttVersion", 0); Poco::StringTokenizer tok(getStringConfig(baseConfig + ".serverURIs", ""), ";,", Poco::StringTokenizer::TOK_TRIM | Poco::StringTokenizer::TOK_IGNORE_EMPTY); options.serverURIs.assign(tok.begin(), tok.end()); options.willTopic = getStringConfig(baseConfig + ".will.topic", ""); options.willMessage = getStringConfig(baseConfig + ".will.message", ""); options.willRetained = getBoolConfig(baseConfig + ".will.retained", false); options.sslTrustStore = getStringConfig(baseConfig + ".ssl.trustStore", ""); options.sslKeyStore = getStringConfig(baseConfig + ".ssl.keyStore", ""); options.sslPrivateKey = getStringConfig(baseConfig + ".ssl.privateKey", ""); options.sslPrivateKeyPassword = getStringConfig(baseConfig + ".ssl.privateKeyPassword", ""); options.sslEnabledCipherSuites = getStringConfig(baseConfig + ".ssl.enabledCipherSuites", ""); options.sslEnableServerCertAuth = getBoolConfig(baseConfig + ".ssl.enableServerCertAuth", false); MQTTClientImpl::Ptr pMQTTClient = new MQTTClientImpl(serverURI, clientId, persistence, persistencePath, options); std::string oid(Poco::format("io.macchina.mqtt.client#%z", _clients.size())); ServerHelper::RemoteObjectPtr pMQTTClientRemoteObject = ServerHelper::createRemoteObject(pMQTTClient, oid); Poco::OSP::Properties props; props.set("io.macchina.protocol", "io.macchina.mqtt"); props.set("io.macchina.mqtt.clientId", clientId); props.set("io.macchina.mqtt.serverURI", serverURI); props.set("io.macchina.mqtt.id", id); Poco::OSP::ServiceRef::Ptr pServiceRef = _pContext->registry().registerService(oid, pMQTTClientRemoteObject, props); _clients.push_back(pMQTTClient); _serviceRefs.push_back(pServiceRef); } }
void start(Poco::OSP::BundleContext::Ptr pContext) { _pContext = pContext; _pPrefs = Poco::OSP::ServiceFinder::find<Poco::OSP::PreferencesService>(pContext); const std::string clientId = getStringConfig("mqttSubscribe.clientId"); const int qos = getIntConfig("mqttSubscribe.qos"); _topic = getStringConfig("mqttSubscribe.topic"); auto mqttClientRefs = pContext->registry().find(Poco::format("io.macchina.mqtt.id == \"%s\"", clientId)); if (!mqttClientRefs.empty()) { _pMQTTClient = mqttClientRefs[0]->castedInstance<IoT::MQTT::IMQTTClient>(); _pMQTTClient->messageArrived += Poco::delegate(this, &BundleActivator::onMessageArrived); _pMQTTClient->subscribe(_topic, qos); } else { _pContext->logger().warning("No MQTT client found."); } }