|
|
|
@ -22,17 +22,26 @@ import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
|
|
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
|
|
|
|
|
import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
|
|
|
|
|
import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
|
|
|
|
|
import org.wso2.carbon.base.MultitenantConstants;
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.util.APIUtil;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.util.RaspberrypiServiceUtils;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.transport.mqtt.MQTTTransportHandler;
|
|
|
|
|
import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
|
|
|
|
|
import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
|
|
|
|
|
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
|
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
|
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
|
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
@ -41,6 +50,8 @@ import java.util.UUID;
|
|
|
|
|
public class RaspberryPiMQTTConnector extends MQTTTransportHandler {
|
|
|
|
|
private static Log log = LogFactory.getLog(RaspberryPiMQTTConnector.class);
|
|
|
|
|
private static final String subscribeTopic = "wso2/" + RaspberrypiConstants.DEVICE_TYPE + "/+/publisher";
|
|
|
|
|
private static final String KEY_TYPE = "PRODUCTION";
|
|
|
|
|
private static final String EMPTY_STRING = "";
|
|
|
|
|
|
|
|
|
|
private static final String iotServerSubscriber = UUID.randomUUID().toString().substring(0, 5);
|
|
|
|
|
|
|
|
|
@ -54,25 +65,42 @@ public class RaspberryPiMQTTConnector extends MQTTTransportHandler {
|
|
|
|
|
Runnable connector = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
while (!isConnected()) {
|
|
|
|
|
try {
|
|
|
|
|
String brokerUsername = MqttConfig.getInstance().getMqttQueueUsername();
|
|
|
|
|
String brokerPassword = MqttConfig.getInstance().getMqttQueuePassword();
|
|
|
|
|
setUsernameAndPassword(brokerUsername, brokerPassword);
|
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(
|
|
|
|
|
RaspberrypiConstants.DEVICE_TYPE_PROVIDER_DOMAIN, true);
|
|
|
|
|
try {
|
|
|
|
|
String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
|
|
|
|
.getRealmConfiguration().getAdminUserName();
|
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(applicationUsername);
|
|
|
|
|
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
|
|
|
|
|
String[] tags = {RaspberrypiConstants.DEVICE_TYPE};
|
|
|
|
|
ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
|
|
|
|
|
RaspberrypiConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true);
|
|
|
|
|
JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient();
|
|
|
|
|
String scopes = "device_type_" + RaspberrypiConstants.DEVICE_TYPE + " device_mqtt_connector";
|
|
|
|
|
AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(),
|
|
|
|
|
apiApplicationKey.getConsumerSecret(), applicationUsername, scopes);
|
|
|
|
|
//create token
|
|
|
|
|
String accessToken = accessTokenInfo.getAccessToken();
|
|
|
|
|
setUsernameAndPassword(accessToken, EMPTY_STRING);
|
|
|
|
|
connectToQueue();
|
|
|
|
|
} catch (TransportHandlerException e) {
|
|
|
|
|
log.error("Connection to MQTT Broker at: " + mqttBrokerEndPoint + " failed", e);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(timeoutInterval);
|
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
|
log.error("MQTT-Connector: Thread Sleep Interrupt Exception.", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
subscribeToQueue();
|
|
|
|
|
} catch (TransportHandlerException e) {
|
|
|
|
|
log.warn("Subscription to MQTT Broker at: " + mqttBrokerEndPoint + " failed", e);
|
|
|
|
|
}
|
|
|
|
|
} catch (TransportHandlerException e) {
|
|
|
|
|
log.error("Connection/Subscription to MQTT Broker at: " + mqttBrokerEndPoint + " failed", e);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(timeoutInterval);
|
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
|
log.error("MQTT-Connector: Thread Sleep Interrupt Exception.", ex);
|
|
|
|
|
}
|
|
|
|
|
} catch (JWTClientException e) {
|
|
|
|
|
log.error("Failed to retrieve token from JWT Client.", e);
|
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
|
log.error("Failed to retrieve the user.", e);
|
|
|
|
|
} catch (APIManagerException e) {
|
|
|
|
|
log.error("Failed to create an application and generate keys.", e);
|
|
|
|
|
} finally {
|
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|