mirror of
https://repository.entgra.net/community/device-mgt-plugins.git
synced 2025-09-16 23:42:15 +00:00
updated android sense and raspberry pi code
This commit is contained in:
parent
44fa7aa486
commit
d53942eeaa
@ -45,7 +45,9 @@ import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The api for
|
||||
@ -53,21 +55,22 @@ import java.util.List;
|
||||
public class AndroidSenseControllerServiceImpl implements AndroidSenseControllerService {
|
||||
|
||||
private static Log log = LogFactory.getLog(AndroidSenseControllerServiceImpl.class);
|
||||
private static AndroidSenseMQTTConnector androidSenseMQTTConnector;
|
||||
|
||||
@Path("device/{deviceId}/words")
|
||||
@POST
|
||||
public Response sendKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("keywords") String keywords) {
|
||||
try {
|
||||
if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
|
||||
AndroidSenseConstants.DEVICE_TYPE))) {
|
||||
AndroidSenseConstants.DEVICE_TYPE))) {
|
||||
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
|
||||
}
|
||||
androidSenseMQTTConnector.publishDeviceData(deviceId, "add", keywords);
|
||||
Map<String, String> dynamicProperties = new HashMap<>();
|
||||
String publishTopic = APIUtil.getAuthenticatedUserTenantDomain()
|
||||
+ "/" + AndroidSenseConstants.DEVICE_TYPE + "/" + deviceId + "/command/words";
|
||||
dynamicProperties.put(AndroidSenseConstants.ADAPTER_TOPIC_PROPERTY, publishTopic);
|
||||
APIUtil.getOutputEventAdapterService().publish(AndroidSenseConstants.MQTT_ADAPTER_NAME,
|
||||
dynamicProperties, keywords);
|
||||
return Response.ok().build();
|
||||
} catch (TransportHandlerException e) {
|
||||
log.error(e.getErrorMessage(), e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
} catch (DeviceAccessAuthorizationException e) {
|
||||
log.error(e.getErrorMessage(), e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
@ -82,11 +85,13 @@ public class AndroidSenseControllerServiceImpl implements AndroidSenseController
|
||||
AndroidSenseConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS)) {
|
||||
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
|
||||
}
|
||||
androidSenseMQTTConnector.publishDeviceData(deviceId, "threshold", threshold);
|
||||
Map<String, String> dynamicProperties = new HashMap<>();
|
||||
String publishTopic = APIUtil.getAuthenticatedUserTenantDomain()
|
||||
+ "/" + AndroidSenseConstants.DEVICE_TYPE + "/" + deviceId + "/command/threshold";
|
||||
dynamicProperties.put(AndroidSenseConstants.ADAPTER_TOPIC_PROPERTY, publishTopic);
|
||||
APIUtil.getOutputEventAdapterService().publish(AndroidSenseConstants.MQTT_ADAPTER_NAME,
|
||||
dynamicProperties, threshold);
|
||||
return Response.ok().build();
|
||||
} catch (TransportHandlerException e) {
|
||||
log.error(e.getErrorMessage(), e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
} catch (DeviceAccessAuthorizationException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
@ -100,11 +105,13 @@ public class AndroidSenseControllerServiceImpl implements AndroidSenseController
|
||||
AndroidSenseConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS)) {
|
||||
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
|
||||
}
|
||||
androidSenseMQTTConnector.publishDeviceData(deviceId, "remove", words);
|
||||
Map<String, String> dynamicProperties = new HashMap<>();
|
||||
String publishTopic = APIUtil.getAuthenticatedUserTenantDomain()
|
||||
+ "/" + AndroidSenseConstants.DEVICE_TYPE + "/" + deviceId + "/command/remove";
|
||||
dynamicProperties.put(AndroidSenseConstants.ADAPTER_TOPIC_PROPERTY, publishTopic);
|
||||
APIUtil.getOutputEventAdapterService().publish(AndroidSenseConstants.MQTT_ADAPTER_NAME,
|
||||
dynamicProperties, words);
|
||||
return Response.ok().build();
|
||||
} catch (TransportHandlerException e) {
|
||||
log.error(e.getErrorMessage(), e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
} catch (DeviceAccessAuthorizationException e) {
|
||||
log.error(e.getErrorMessage(), e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
@ -196,58 +203,4 @@ public class AndroidSenseControllerServiceImpl implements AndroidSenseController
|
||||
return sensorEventTableName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the `AndroidSenseMQTTConnector` specific to this Android Sense controller service.
|
||||
*
|
||||
* @return the 'AndroidSenseMQTTConnector' instance bound to the 'AndroidSenseMQTTConnector' variable of
|
||||
* this service.
|
||||
*/
|
||||
@SuppressWarnings("Unused")
|
||||
public AndroidSenseMQTTConnector getAndroidSenseMQTTConnector() {
|
||||
return androidSenseMQTTConnector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the `AndroidSenseMQTTConnector` variable of this Android Sense controller service.
|
||||
*
|
||||
* @param androidSenseMQTTConnector a 'AndroidSenseMQTTConnector' object that handles all MQTT related
|
||||
* communications of any connected Android Sense device-type
|
||||
*/
|
||||
@SuppressWarnings("Unused")
|
||||
public void setAndroidSenseMQTTConnector(final AndroidSenseMQTTConnector androidSenseMQTTConnector) {
|
||||
Runnable connector = new Runnable() {
|
||||
public void run() {
|
||||
if (waitForServerStartup()) {
|
||||
return;
|
||||
}
|
||||
//The delay is added for the server to starts up.
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
AndroidSenseControllerServiceImpl.androidSenseMQTTConnector = androidSenseMQTTConnector;
|
||||
if (MqttConfig.getInstance().isEnabled()) {
|
||||
synchronized (androidSenseMQTTConnector) {
|
||||
androidSenseMQTTConnector.connect();
|
||||
}
|
||||
} else {
|
||||
log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, VirtualFireAlarmMQTTConnector not started.");
|
||||
}
|
||||
}
|
||||
};
|
||||
Thread connectorThread = new Thread(connector);
|
||||
connectorThread.start();
|
||||
}
|
||||
|
||||
private boolean waitForServerStartup() {
|
||||
while (!IoTServerStartupListener.isServerReady()) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,6 +14,7 @@ import org.wso2.carbon.context.CarbonContext;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -166,4 +167,22 @@ public class APIUtil {
|
||||
}
|
||||
return deviceAccessAuthorizationService;
|
||||
}
|
||||
|
||||
public static OutputEventAdapterService getOutputEventAdapterService() {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
OutputEventAdapterService outputEventAdapterService =
|
||||
(OutputEventAdapterService) ctx.getOSGiService(OutputEventAdapterService.class, null);
|
||||
if (outputEventAdapterService == null) {
|
||||
String msg = "Device Authorization service has not initialized.";
|
||||
log.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
return outputEventAdapterService;
|
||||
}
|
||||
|
||||
public static String getTenantDomainOftheUser() {
|
||||
PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
return threadLocalCarbonContext.getTenantDomain();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -26,7 +26,6 @@
|
||||
<jaxrs:serviceBeans>
|
||||
<bean id="AndroidSenseControllerService"
|
||||
class="org.wso2.carbon.device.mgt.iot.androidsense.service.impl.AndroidSenseControllerServiceImpl">
|
||||
<property name="androidSenseMQTTConnector" ref="mqttConnectorBean"/>
|
||||
</bean>
|
||||
<bean id="AndroidSenseManagerService"
|
||||
class="org.wso2.carbon.device.mgt.iot.androidsense.service.impl.AndroidSenseManagerServiceImpl"/>
|
||||
@ -36,19 +35,5 @@
|
||||
</jaxrs:providers>
|
||||
</jaxrs:server>
|
||||
|
||||
<!--<jaxrs:server id="AndroidSense_Mgt" address="/enrollment/">-->
|
||||
<!--<jaxrs:serviceBeans>-->
|
||||
<!--<bean id="AndroidSenseManagerService"-->
|
||||
<!--class="org.wso2.carbon.device.mgt.iot.androidsense.service.impl.AndroidSenseManagerServiceImpl"/>-->
|
||||
<!--</jaxrs:serviceBeans>-->
|
||||
<!--<jaxrs:providers>-->
|
||||
<!--<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />-->
|
||||
<!--</jaxrs:providers>-->
|
||||
<!--</jaxrs:server>-->
|
||||
|
||||
<bean id="mqttConnectorBean"
|
||||
class="org.wso2.carbon.device.mgt.iot.androidsense.service.impl.transport.AndroidSenseMQTTConnector">
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
||||
|
||||
@ -103,5 +103,9 @@
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.utils</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.analytics-common</groupId>
|
||||
<artifactId>org.wso2.carbon.event.output.adapter.core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -50,4 +50,24 @@ public class AndroidSenseConstants {
|
||||
public static final String MQTT_SUBSCRIBE_WORDS_TOPIC = "wso2/+/android_sense/+/data";
|
||||
public static final String DATA_SOURCE_NAME = "jdbc/AndroidSenseDM_DB";
|
||||
public final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super";
|
||||
|
||||
//mqtt tranport related constants
|
||||
public static final String MQTT_ADAPTER_NAME = "android_sense_mqtt";
|
||||
public static final String MQTT_ADAPTER_TYPE = "oauth-mqtt";
|
||||
public static final String ADAPTER_TOPIC_PROPERTY = "topic";
|
||||
public static final String MQTT_PORT = "\\$\\{mqtt.broker.port\\}";
|
||||
public static final String MQTT_BROKER_HOST = "\\$\\{mqtt.broker.host\\}";
|
||||
public static final String CARBON_CONFIG_PORT_OFFSET = "Ports.Offset";
|
||||
public static final String DEFAULT_CARBON_SERVER_HOST_PROPERTY = "server.host";
|
||||
public static final int CARBON_DEFAULT_PORT_OFFSET = 0;
|
||||
public static final int DEFAULT_MQTT_PORT = 1883;
|
||||
|
||||
public static final String USERNAME_PROPERTY_KEY = "username";
|
||||
public static final String DCR_PROPERTY_KEY = "dcrUrl";
|
||||
public static final String BROKER_URL_PROPERTY_KEY = "url";
|
||||
public static final String SCOPES_PROPERTY_KEY = "scopes";
|
||||
public static final String QOS_PROPERTY_KEY = "qos";
|
||||
public static final String CLIENT_ID_PROPERTY_KEY = "qos";
|
||||
public static final String CLEAR_SESSION_PROPERTY_KEY = "clearSession";
|
||||
public static final String TOPIC = "topic";
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSenseConstants;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.exception.AndroidSenseDeviceMgtPluginException;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.dao.util.AndroidSenseUtils;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.util.AndroidSenseUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
|
||||
@ -1,109 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.dao.util;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSenseConstants;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.exception.AndroidSenseDeviceMgtPluginException;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Contains utility methods used by plugin.
|
||||
*/
|
||||
public class AndroidSenseUtils {
|
||||
|
||||
private static Log log = LogFactory.getLog(AndroidSenseUtils.class);
|
||||
|
||||
public static String getDeviceProperty(List<Device.Property> deviceProperties, String propertyKey) {
|
||||
String deviceProperty = "";
|
||||
for(Device.Property property :deviceProperties){
|
||||
if(propertyKey.equals(property.getName())){
|
||||
deviceProperty = property.getValue();
|
||||
}
|
||||
}
|
||||
return deviceProperty;
|
||||
}
|
||||
|
||||
public static Device.Property getProperty(String property, String value) {
|
||||
if (property != null) {
|
||||
Device.Property prop = new Device.Property();
|
||||
prop.setName(property);
|
||||
prop.setValue(value);
|
||||
return prop;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) {
|
||||
if (rs != null) {
|
||||
try {
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing result set", e);
|
||||
}
|
||||
}
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing prepared statement", e);
|
||||
}
|
||||
}
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing database connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
|
||||
cleanupResources(null, stmt, rs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the device management schema.
|
||||
*/
|
||||
public static void setupDeviceManagementSchema() throws AndroidSenseDeviceMgtPluginException {
|
||||
try {
|
||||
Context ctx = new InitialContext();
|
||||
DataSource dataSource = (DataSource) ctx.lookup(AndroidSenseConstants.DATA_SOURCE_NAME);
|
||||
DeviceSchemaInitializer initializer =
|
||||
new DeviceSchemaInitializer(dataSource);
|
||||
log.info("Initializing device management repository database schema");
|
||||
initializer.createRegistryDatabase();
|
||||
|
||||
} catch (NamingException e) {
|
||||
log.error("Error while looking up the data source: " + AndroidSenseConstants.DATA_SOURCE_NAME);
|
||||
} catch (Exception e) {
|
||||
throw new AndroidSenseDeviceMgtPluginException("Error occurred while initializing Iot Device " +
|
||||
"Management database schema", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.util;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.core.ServerStartupObserver;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Startup listener to create an output adapter after server starts up.
|
||||
*/
|
||||
public class AndroidSenseStartupListener implements ServerStartupObserver {
|
||||
private static final Log log = LogFactory.getLog(AndroidSenseStartupListener.class);
|
||||
|
||||
@Override
|
||||
public void completingServerStartup() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void completedServerStartup() {
|
||||
try {
|
||||
AndroidSenseUtils.setupMqttOutputAdapter();
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to intilaize the virtual firealarm output adapter", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.util;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.base.ServerConfiguration;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.core.util.Utils;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSenseConstants;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.exception.AndroidSenseDeviceMgtPluginException;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.internal.AndroidSenseManagementDataHolder;
|
||||
import org.wso2.carbon.event.output.adapter.core.MessageType;
|
||||
import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterConfiguration;
|
||||
import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException;
|
||||
import org.wso2.carbon.utils.CarbonUtils;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import javax.sql.DataSource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Contains utility methods used by plugin.
|
||||
*/
|
||||
public class AndroidSenseUtils {
|
||||
|
||||
private static Log log = LogFactory.getLog(AndroidSenseUtils.class);
|
||||
|
||||
private static final String VIRTUAL_FIREALARM_CONFIG_LOCATION =
|
||||
CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "conf" +
|
||||
File.separator + "iot" + File.separator + "mqtt.properties";
|
||||
|
||||
public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) {
|
||||
if (rs != null) {
|
||||
try {
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing result set", e);
|
||||
}
|
||||
}
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing prepared statement", e);
|
||||
}
|
||||
}
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing database connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
|
||||
cleanupResources(null, stmt, rs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the device management schema.
|
||||
*/
|
||||
public static void setupDeviceManagementSchema() throws AndroidSenseDeviceMgtPluginException {
|
||||
try {
|
||||
Context ctx = new InitialContext();
|
||||
DataSource dataSource = (DataSource) ctx.lookup(AndroidSenseConstants.DATA_SOURCE_NAME);
|
||||
DeviceSchemaInitializer initializer =
|
||||
new DeviceSchemaInitializer(dataSource);
|
||||
log.info("Initializing device management repository database schema");
|
||||
initializer.createRegistryDatabase();
|
||||
|
||||
} catch (NamingException e) {
|
||||
log.error("Error while looking up the data source: " + AndroidSenseConstants.DATA_SOURCE_NAME);
|
||||
} catch (Exception e) {
|
||||
throw new AndroidSenseDeviceMgtPluginException("Error occurred while initializing Iot Device " +
|
||||
"Management database schema", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void setupMqttOutputAdapter() throws IOException {
|
||||
OutputEventAdapterConfiguration outputEventAdapterConfiguration =
|
||||
createMqttOutputEventAdapterConfiguration(AndroidSenseConstants.MQTT_ADAPTER_NAME,
|
||||
AndroidSenseConstants.MQTT_ADAPTER_TYPE, MessageType.TEXT);
|
||||
try {
|
||||
PrivilegedCarbonContext.startTenantFlow();
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(
|
||||
AndroidSenseConstants.DEVICE_TYPE_PROVIDER_DOMAIN, true);
|
||||
AndroidSenseManagementDataHolder.getInstance().getOutputEventAdapterService()
|
||||
.create(outputEventAdapterConfiguration);
|
||||
} catch (OutputEventAdapterException e) {
|
||||
log.error("Unable to create Output Event Adapter : " + AndroidSenseConstants.MQTT_ADAPTER_NAME, e);
|
||||
} finally {
|
||||
PrivilegedCarbonContext.endTenantFlow();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Output Event Adapter Configuration for given configuration.
|
||||
*
|
||||
* @param name Output Event Adapter name
|
||||
* @param type Output Event Adapter type
|
||||
* @param msgFormat Output Event Adapter message format
|
||||
* @return OutputEventAdapterConfiguration instance for given configuration
|
||||
*/
|
||||
private static OutputEventAdapterConfiguration createMqttOutputEventAdapterConfiguration(String name, String type,
|
||||
String msgFormat) throws IOException {
|
||||
OutputEventAdapterConfiguration outputEventAdapterConfiguration = new OutputEventAdapterConfiguration();
|
||||
outputEventAdapterConfiguration.setName(name);
|
||||
outputEventAdapterConfiguration.setType(type);
|
||||
outputEventAdapterConfiguration.setMessageFormat(msgFormat);
|
||||
File configFile = new File(VIRTUAL_FIREALARM_CONFIG_LOCATION);
|
||||
if (configFile.exists()) {
|
||||
Map<String, String> mqttAdapterProperties = new HashMap<>();
|
||||
InputStream propertyStream = configFile.toURI().toURL().openStream();
|
||||
Properties properties = new Properties();
|
||||
properties.load(propertyStream);
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.USERNAME_PROPERTY_KEY, properties.getProperty(
|
||||
AndroidSenseConstants.USERNAME_PROPERTY_KEY));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.DCR_PROPERTY_KEY, Utils.replaceSystemProperty(
|
||||
properties.getProperty(AndroidSenseConstants.DCR_PROPERTY_KEY)));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.BROKER_URL_PROPERTY_KEY, replaceMqttProperty(
|
||||
properties.getProperty(AndroidSenseConstants.BROKER_URL_PROPERTY_KEY)));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.SCOPES_PROPERTY_KEY, properties.getProperty(
|
||||
AndroidSenseConstants.SCOPES_PROPERTY_KEY));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.CLEAR_SESSION_PROPERTY_KEY, properties.getProperty(
|
||||
AndroidSenseConstants.CLEAR_SESSION_PROPERTY_KEY));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.QOS_PROPERTY_KEY, properties.getProperty(
|
||||
AndroidSenseConstants.QOS_PROPERTY_KEY));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.CLIENT_ID_PROPERTY_KEY, "");
|
||||
outputEventAdapterConfiguration.setStaticProperties(mqttAdapterProperties);
|
||||
}
|
||||
return outputEventAdapterConfiguration;
|
||||
}
|
||||
|
||||
private static String replaceMqttProperty(String urlWithPlaceholders) {
|
||||
urlWithPlaceholders = Utils.replaceSystemProperty(urlWithPlaceholders);
|
||||
urlWithPlaceholders = urlWithPlaceholders.replaceAll(AndroidSenseConstants.MQTT_PORT, "" +
|
||||
(AndroidSenseConstants.DEFAULT_MQTT_PORT + getPortOffset()));
|
||||
urlWithPlaceholders = urlWithPlaceholders.replaceAll(AndroidSenseConstants.MQTT_BROKER_HOST,
|
||||
System.getProperty(AndroidSenseConstants.DEFAULT_CARBON_SERVER_HOST_PROPERTY, "localhost"));
|
||||
return urlWithPlaceholders;
|
||||
}
|
||||
|
||||
private static int getPortOffset() {
|
||||
ServerConfiguration carbonConfig = ServerConfiguration.getInstance();
|
||||
String portOffset = System.getProperty("portOffset", carbonConfig.getFirstProperty(
|
||||
AndroidSenseConstants.CARBON_CONFIG_PORT_OFFSET));
|
||||
try {
|
||||
if ((portOffset != null)) {
|
||||
return Integer.parseInt(portOffset.trim());
|
||||
} else {
|
||||
return AndroidSenseConstants.CARBON_DEFAULT_PORT_OFFSET;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return AndroidSenseConstants.CARBON_DEFAULT_PORT_OFFSET;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -16,7 +16,7 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.dao.util;
|
||||
package org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.util;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* you may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.iot.androidsense.plugin.internal;
|
||||
|
||||
import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService;
|
||||
|
||||
/**
|
||||
* DataHolder class of plugins component.
|
||||
*/
|
||||
public class AndroidSenseManagementDataHolder {
|
||||
|
||||
private OutputEventAdapterService outputEventAdapterService;
|
||||
|
||||
private static AndroidSenseManagementDataHolder thisInstance = new AndroidSenseManagementDataHolder();
|
||||
|
||||
private AndroidSenseManagementDataHolder() {
|
||||
}
|
||||
|
||||
public static AndroidSenseManagementDataHolder getInstance() {
|
||||
return thisInstance;
|
||||
}
|
||||
|
||||
public OutputEventAdapterService getOutputEventAdapterService() {
|
||||
return outputEventAdapterService;
|
||||
}
|
||||
|
||||
public void setOutputEventAdapterService(
|
||||
OutputEventAdapterService outputEventAdapterService) {
|
||||
this.outputEventAdapterService = outputEventAdapterService;
|
||||
}
|
||||
|
||||
}
|
||||
@ -24,11 +24,18 @@ import org.osgi.service.component.ComponentContext;
|
||||
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.exception.AndroidSenseDeviceMgtPluginException;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.AndroidSenseManagerService;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.dao.util.AndroidSenseUtils;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.util.AndroidSenseUtils;
|
||||
import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService;
|
||||
|
||||
/**
|
||||
* @scr.component name="org.wso2.carbon.device.mgt.iot.android.internal.AndroidSenseManagementServiceComponent"
|
||||
* immediate="true"
|
||||
* @scr.reference name="event.output.adapter.service"
|
||||
* interface="org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService"
|
||||
* cardinality="1..1"
|
||||
* policy="dynamic"
|
||||
* bind="setOutputEventAdapterService"
|
||||
* unbind="unsetOutputEventAdapterService"
|
||||
*/
|
||||
public class AndroidSenseManagementServiceComponent {
|
||||
|
||||
@ -81,4 +88,20 @@ public class AndroidSenseManagementServiceComponent {
|
||||
log.error("Error occurred while de-activating Android Device Management bundle", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the Output EventAdapter Service dependency
|
||||
*
|
||||
* @param outputEventAdapterService Output EventAdapter Service reference
|
||||
*/
|
||||
protected void setOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService) {
|
||||
AndroidSenseManagementDataHolder.getInstance().setOutputEventAdapterService(outputEventAdapterService);
|
||||
}
|
||||
|
||||
/**
|
||||
* De-reference the Output EventAdapter Service dependency.
|
||||
*/
|
||||
protected void unsetOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService) {
|
||||
AndroidSenseManagementDataHolder.getInstance().setOutputEventAdapterService(null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -122,9 +122,5 @@
|
||||
<groupId>org.wso2.carbon.analytics-common</groupId>
|
||||
<artifactId>org.wso2.carbon.event.output.adapter.core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.analytics-common</groupId>
|
||||
<artifactId>org.wso2.carbon.event.input.adapter.core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -185,5 +185,4 @@ public class RaspberrypiUtils {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -109,4 +109,5 @@ public class RaspberrypiManagementServiceComponent {
|
||||
protected void unsetOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService) {
|
||||
RaspberrypiManagementDataHolder.getInstance().setOutputEventAdapterService(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user