From ed0b110ad6cd7d806accca7a3de8be9cd4f1cb4a Mon Sep 17 00:00:00 2001 From: Sameera Wickramasekara Date: Fri, 11 Aug 2017 11:35:58 +0530 Subject: [PATCH] Fix code formatting --- .../api/DeviceTypeService.java | 14 ++--- .../api/DeviceTypeServiceImpl.java | 44 +++++++------- .../api/dto/DeviceJSON.java | 3 +- .../api/dto/SensorRecord.java | 2 +- .../api/exception/DeviceTypeException.java | 2 +- .../api/util/APIUtil.java | 10 ++-- .../api/util/ZipUtil.java | 29 +++++----- .../plugin/constants/DeviceTypeConstants.java | 23 ++++---- .../exception/DeviceMgtPluginException.java | 4 +- .../plugin/impl/DeviceTypeManager.java | 7 +-- .../plugin/impl/DeviceTypeManagerService.java | 30 +++++----- .../plugin/impl/dao/DeviceTypeDAO.java | 7 +-- .../impl/dao/impl/DeviceTypeDAOImpl.java | 9 +-- .../feature/DeviceTypeFeatureManager.java | 57 +++++++++---------- .../impl/util/DeviceSchemaInitializer.java | 30 +++++----- .../plugin/impl/util/DeviceTypeUtils.java | 31 ++++------ .../plugin/internal/ServiceComponent.java | 8 +-- .../src/main/resources/agent/src/agent.py | 6 +- .../src/main/resources/agent/src/iotUtils.py | 2 +- .../main/resources/agent/src/mqttHandler.py | 3 +- .../main/resources/agent/src/token_updater.py | 4 +- .../raspberrypicodebased-datasources.xml | 2 +- 22 files changed, 149 insertions(+), 178 deletions(-) diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/DeviceTypeService.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/DeviceTypeService.java index 793285e5..1d2eb2d5 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/DeviceTypeService.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/DeviceTypeService.java @@ -28,22 +28,20 @@ import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Path; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; -import javax.ws.rs.Produces; +import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; -import javax.ws.rs.PUT; -import javax.ws.rs.DELETE; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; /** - * This is the API which is used to control and manage device type functionality + * This is the API which is used to control and manage device type functionality. */ @SwaggerDefinition( info = @Info( @@ -98,7 +96,7 @@ public interface DeviceTypeService { @Context HttpServletResponse response); /** - * Retrieve Sensor data for the given time period + * Retrieve Sensor data for the given time period. * @param deviceId unique identifier for given device type instance * @param from starting time * @param to ending time @@ -124,7 +122,7 @@ public interface DeviceTypeService { Response getSensorStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from, @QueryParam("to") long to, @QueryParam("sensorType") String sensorType); /** - * To download device type agent source code as zip file + * To download device type agent source code as zip file. * @param deviceName name for the device type instance * @param sketchType folder name where device type agent was installed into server * @return Agent source code as zip file @@ -146,4 +144,4 @@ public interface DeviceTypeService { } ) Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType); -} \ No newline at end of file +} diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/DeviceTypeServiceImpl.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/DeviceTypeServiceImpl.java index bbbbb2e3..c3637d43 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/DeviceTypeServiceImpl.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/DeviceTypeServiceImpl.java @@ -18,13 +18,6 @@ package org.wso2.raspberrypicodebased.api; -import org.wso2.raspberrypicodebased.api.dto.DeviceJSON; -import org.wso2.raspberrypicodebased.api.dto.SensorRecord; -import org.wso2.raspberrypicodebased.api.util.APIUtil; -import org.wso2.raspberrypicodebased.api.util.ZipUtil; -import org.wso2.raspberrypicodebased.api.util.ZipArchive; -import org.wso2.raspberrypicodebased.plugin.constants.DeviceTypeConstants; - import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -39,23 +32,29 @@ 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.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; 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.device.mgt.common.*; -import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; +import org.wso2.raspberrypicodebased.api.dto.DeviceJSON; +import org.wso2.raspberrypicodebased.api.dto.SensorRecord; +import org.wso2.raspberrypicodebased.api.util.APIUtil; +import org.wso2.raspberrypicodebased.api.util.ZipArchive; +import org.wso2.raspberrypicodebased.api.util.ZipUtil; +import org.wso2.raspberrypicodebased.plugin.constants.DeviceTypeConstants; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Path; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; -import javax.ws.rs.Produces; +import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; @@ -63,17 +62,16 @@ import javax.ws.rs.core.Response; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; - -import java.util.UUID; -import java.util.Map; -import java.util.List; -import java.util.HashMap; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.Properties; +import java.util.UUID; /** - * This is the API which is used to control and manage device type functionality + * This is the API which is used to control and manage device type functionality. */ public class DeviceTypeServiceImpl implements DeviceTypeService { @@ -159,7 +157,7 @@ public class DeviceTypeServiceImpl implements DeviceTypeService { } /** - * Retrieve Sensor data for the given time period + * Retrieve Sensor data for the given time period. * * @param deviceId unique identifier for given device type instance * @param from starting time @@ -206,7 +204,7 @@ public class DeviceTypeServiceImpl implements DeviceTypeService { } /** - * To download device type agent source code as zip file + * To download device type agent source code as zip file. * * @param deviceName name for the device type instance * @param sketchType folder name where device type agent was installed into server @@ -227,7 +225,7 @@ public class DeviceTypeServiceImpl implements DeviceTypeService { zipFile.getZipFile().delete(); return resp; } catch (IllegalArgumentException ex) { - return Response.status(400).entity(ex.getMessage()).build();//bad request + return Response.status(400).entity(ex.getMessage()).build(); //bad request } catch (DeviceManagementException ex) { log.error(ex.getMessage(), ex); return Response.status(500).entity(ex.getMessage()).build(); @@ -313,4 +311,4 @@ public class DeviceTypeServiceImpl implements DeviceTypeService { deviceId, deviceName, accessToken, refreshToken, apiApplicationKey.toString()); return zipFile; } -} \ No newline at end of file +} diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/dto/DeviceJSON.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/dto/DeviceJSON.java index 6667bf53..c0d2f90e 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/dto/DeviceJSON.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/dto/DeviceJSON.java @@ -19,11 +19,12 @@ package org.wso2.raspberrypicodebased.api.dto; import org.codehaus.jackson.annotate.JsonIgnoreProperties; + import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** - * These information are sent by agent in each request to server + * These information are sent by agent in each request to server. */ @XmlRootElement @JsonIgnoreProperties(ignoreUnknown = true) diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/dto/SensorRecord.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/dto/SensorRecord.java index 36399f98..b4553339 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/dto/SensorRecord.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/dto/SensorRecord.java @@ -38,7 +38,7 @@ public class SensorRecord { private Map values; /** - * Unique identifier for each recode + * Unique identifier for each recode. */ @XmlElement(required = false, name = "id") private String id; diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/exception/DeviceTypeException.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/exception/DeviceTypeException.java index aa05a46b..61f23b72 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/exception/DeviceTypeException.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/exception/DeviceTypeException.java @@ -19,7 +19,7 @@ package org.wso2.raspberrypicodebased.api.exception; /** - * Device specific exception handler + * Device specific exception handler. */ public class DeviceTypeException extends Exception { diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/util/APIUtil.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/util/APIUtil.java index 586701d8..57c09c39 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/util/APIUtil.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/util/APIUtil.java @@ -18,11 +18,10 @@ package org.wso2.raspberrypicodebased.api.util; -import org.wso2.raspberrypicodebased.api.dto.SensorRecord; - -import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil; -import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.analytics.api.AnalyticsDataAPI; +import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil; import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse; import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry; import org.wso2.carbon.analytics.dataservice.commons.SortByField; @@ -34,8 +33,7 @@ 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.identity.jwt.client.extension.service.JWTClientManagerService; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.wso2.raspberrypicodebased.api.dto.SensorRecord; import java.util.ArrayList; import java.util.HashMap; diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/util/ZipUtil.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/util/ZipUtil.java index a7931686..16e045bd 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/util/ZipUtil.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/api/src/main/java/org.wso2/raspberrypicodebased/api/util/ZipUtil.java @@ -18,12 +18,15 @@ package org.wso2.raspberrypicodebased.api.util; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.json.JSONObject; import org.wso2.carbon.apimgt.application.extension.constants.ApiApplicationConstants; +import org.wso2.carbon.core.util.Utils; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.utils.CarbonUtils; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; @@ -43,9 +46,6 @@ import java.util.Map; import java.util.Properties; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; -import org.wso2.carbon.core.util.Utils; -import org.json.JSONObject; -import org.apache.commons.codec.binary.Base64; /** * This is used to create a zip file that includes the necessary configuration required for the agent. @@ -123,8 +123,8 @@ public class ZipUtil { , String zipFileName) throws DeviceManagementException, IOException { String sketchPath = CarbonUtils.getCarbonHome() + File.separator + templateSketchPath; - FileUtils.deleteDirectory(new File(archivesPath));//clear directory - FileUtils.deleteDirectory(new File(archivesPath + ".zip"));//clear zip + FileUtils.deleteDirectory(new File(archivesPath)); //clear directory + FileUtils.deleteDirectory(new File(archivesPath + ".zip")); //clear zip if (!new File(archivesPath).mkdirs()) { //new dir String message = "Could not create directory at path: " + archivesPath; throw new DeviceManagementException(message); @@ -269,10 +269,10 @@ public class ZipUtil { ZipOutputStream out = null; try { - final int BUFFER = 2048; + final int buffer = 2048; FileOutputStream dest = new FileOutputStream(new File(srcFolder + ".zip")); out = new ZipOutputStream(new BufferedOutputStream(dest)); - byte data[] = new byte[BUFFER]; + byte data[] = new byte[buffer]; File subDir = new File(srcFolder); String subdirList[] = subDir.list(); if (subdirList == null) { @@ -290,24 +290,23 @@ public class ZipUtil { for (int i = 0; i < files.length; i++) { FileInputStream fi = new FileInputStream(srcFolder + "/" + sd + "/" + files[i]); - origin = new BufferedInputStream(fi, BUFFER); + origin = new BufferedInputStream(fi, buffer); ZipEntry entry = new ZipEntry(sd + "/" + files[i]); out.putNextEntry(entry); int count; - while ((count = origin.read(data, 0, BUFFER)) != -1) { + while ((count = origin.read(data, 0, buffer)) != -1) { out.write(data, 0, count); out.flush(); } } - } else //it is just a file - { + } else { //it is just a file FileInputStream fi = new FileInputStream(f); - origin = new BufferedInputStream(fi, BUFFER); + origin = new BufferedInputStream(fi, buffer); ZipEntry entry = new ZipEntry(sd); out.putNextEntry(entry); int count; - while ((count = origin.read(data, 0, BUFFER)) != -1) { + while ((count = origin.read(data, 0, buffer)) != -1) { out.write(data, 0, count); out.flush(); } diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/constants/DeviceTypeConstants.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/constants/DeviceTypeConstants.java index 1d37aa49..0ea60e7f 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/constants/DeviceTypeConstants.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/constants/DeviceTypeConstants.java @@ -19,6 +19,7 @@ package org.wso2.raspberrypicodebased.plugin.constants; import org.wso2.carbon.utils.CarbonUtils; + import java.io.File; /** @@ -26,20 +27,20 @@ import java.io.File; * stream definition and device specific dome constants */ public class DeviceTypeConstants { - public final static String DEVICE_TYPE = "raspberrypicodebased"; - public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME"; - public final static String DEVICE_PLUGIN_DEVICE_ID = "raspberrypicodebased_DEVICE_ID"; - public final static String STATE_ON = "ON"; - public final static String STATE_OFF = "OFF"; - public final static String BULB_CONTEXT = "BULB"; + public static final String DEVICE_TYPE = "raspberrypicodebased"; + public static final String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME"; + public static final String DEVICE_PLUGIN_DEVICE_ID = "raspberrypicodebased_DEVICE_ID"; + public static final String STATE_ON = "ON"; + public static final String STATE_OFF = "OFF"; + public static final String BULB_CONTEXT = "BULB"; //sensor events summerized table name public static final String SENSOR_TYPE1_EVENT_TABLE = "ORG_WSO2_IOT_DEVICES_sensor_temp"; public static final String SENSOR_TYPE2_EVENT_TABLE = "ORG_WSO2_IOT_DEVICES_no_sensor1"; public static final String DATA_SOURCE_NAME = "jdbc/raspberrypicodebasedDM_DB"; - public final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super"; - public final static String SENSOR_TYPE1 = "sensor_temp"; - public final static String SENSOR_TYPE2 = "no_sensor1"; + public static final String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super"; + public static final String SENSOR_TYPE1 = "sensor_temp"; + public static final String SENSOR_TYPE2 = "no_sensor1"; //mqtt tranport related constants public static final String MQTT_ADAPTER_NAME = "sensor_temp_mqtt"; @@ -65,8 +66,8 @@ public class DeviceTypeConstants { public static final String CLEAR_SESSION_PROPERTY_KEY = "clearSession"; public static final String TOPIC = "topic"; - public final static String SENSOR_TYPE1_STREAM_DEFINITION = "org.wso2.iot.devices.sensor_temp"; - public final static String SENSOR_TYPE1_STREAM_DEFINITION_VERSION = "1.0.0"; + public static final String SENSOR_TYPE1_STREAM_DEFINITION = "org.wso2.iot.devices.sensor_temp"; + public static final String SENSOR_TYPE1_STREAM_DEFINITION_VERSION = "1.0.0"; public static final String MQTT_CONFIG_LOCATION = CarbonUtils.getEtcCarbonConfigDirPath() + File.separator diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/exception/DeviceMgtPluginException.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/exception/DeviceMgtPluginException.java index c4d14301..c5bc682b 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/exception/DeviceMgtPluginException.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/exception/DeviceMgtPluginException.java @@ -19,7 +19,7 @@ package org.wso2.raspberrypicodebased.plugin.exception; /** - * Device type plugin exception handler + * Device type plugin exception handler. */ public class DeviceMgtPluginException extends Exception { @@ -56,4 +56,4 @@ public class DeviceMgtPluginException extends Exception { this.errorMessage = errorMessage; } -} \ No newline at end of file +} diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/DeviceTypeManager.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/DeviceTypeManager.java index a3ecf00d..c69400e5 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/DeviceTypeManager.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/DeviceTypeManager.java @@ -20,9 +20,6 @@ package org.wso2.raspberrypicodebased.plugin.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.raspberrypicodebased.plugin.impl.dao.DeviceTypeDAO; -import org.wso2.raspberrypicodebased.plugin.exception.DeviceMgtPluginException; -import org.wso2.raspberrypicodebased.plugin.impl.feature.DeviceTypeFeatureManager; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; @@ -32,8 +29,10 @@ import org.wso2.carbon.device.mgt.common.FeatureManager; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; +import org.wso2.raspberrypicodebased.plugin.exception.DeviceMgtPluginException; +import org.wso2.raspberrypicodebased.plugin.impl.dao.DeviceTypeDAO; +import org.wso2.raspberrypicodebased.plugin.impl.feature.DeviceTypeFeatureManager; -import java.util.ArrayList; import java.util.List; diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/DeviceTypeManagerService.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/DeviceTypeManagerService.java index 391e5d6b..fc7e3b43 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/DeviceTypeManagerService.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/DeviceTypeManagerService.java @@ -18,29 +18,25 @@ package org.wso2.raspberrypicodebased.plugin.impl; -import java.util.HashMap; -import java.util.Map; - - +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig; import org.wso2.carbon.device.mgt.common.InitialOperationConfig; -import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; -import org.wso2.raspberrypicodebased.plugin.constants.DeviceTypeConstants; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.DeviceManager; -import org.wso2.carbon.device.mgt.common.app.mgt.Application; -import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; -import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; +import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; -import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; -import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; -import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; -import java.util.List; +import org.wso2.raspberrypicodebased.plugin.constants.DeviceTypeConstants; +import java.util.HashMap; +import java.util.Map; + +/** + *Devicetype manager service. + */ public class DeviceTypeManagerService implements DeviceManagementService { private DeviceManager deviceManager; private OperationMonitoringTaskConfig operationMonitoringTaskConfig; @@ -86,7 +82,7 @@ public class DeviceTypeManagerService implements DeviceManagementService { properties.put("qos", "0"); properties.put("clearSession", "true"); properties.put("scopes", ""); - return new PushNotificationConfig("MQTT",false, properties); + return new PushNotificationConfig("MQTT", false, properties); } @Override diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/dao/DeviceTypeDAO.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/dao/DeviceTypeDAO.java index 047698e4..69600049 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/dao/DeviceTypeDAO.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/dao/DeviceTypeDAO.java @@ -20,10 +20,9 @@ package org.wso2.raspberrypicodebased.plugin.impl.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.wso2.raspberrypicodebased.plugin.constants.DeviceTypeConstants; -import org.wso2.raspberrypicodebased.plugin.impl.dao.impl.DeviceTypeDAOImpl; import org.wso2.raspberrypicodebased.plugin.exception.DeviceMgtPluginException; +import org.wso2.raspberrypicodebased.plugin.impl.dao.impl.DeviceTypeDAOImpl; import javax.naming.Context; import javax.naming.InitialContext; @@ -33,7 +32,7 @@ import java.sql.Connection; import java.sql.SQLException; /** - * Database handler which is specified for raspberrypicodebased type + * Database handler which is specified for raspberrypicodebased type. */ public class DeviceTypeDAO { @@ -129,4 +128,4 @@ public class DeviceTypeDAO { public DeviceTypeDAOImpl getDeviceTypeDAO() { return new DeviceTypeDAOImpl(); } -} \ No newline at end of file +} diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/dao/impl/DeviceTypeDAOImpl.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/dao/impl/DeviceTypeDAOImpl.java index e77257a6..fc5696a4 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/dao/impl/DeviceTypeDAOImpl.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/dao/impl/DeviceTypeDAOImpl.java @@ -20,22 +20,18 @@ package org.wso2.raspberrypicodebased.plugin.impl.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.raspberrypicodebased.plugin.constants.DeviceTypeConstants; import org.wso2.raspberrypicodebased.plugin.exception.DeviceMgtPluginException; import org.wso2.raspberrypicodebased.plugin.impl.dao.DeviceTypeDAO; import org.wso2.raspberrypicodebased.plugin.impl.util.DeviceTypeUtils; -import org.wso2.carbon.device.mgt.common.Device; - import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * Implements IotDeviceDAO for raspberrypicodebased Devices. @@ -85,7 +81,8 @@ public class DeviceTypeDAOImpl { try { conn = DeviceTypeDAO.getConnection(); String createDBQuery = - "INSERT INTO raspberrypicodebased_DEVICE(raspberrypicodebased_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)"; + "INSERT INTO raspberrypicodebased_DEVICE(raspberrypicodebased_DEVICE_ID, DEVICE_NAME)" + + " VALUES (?, ?)"; stmt = conn.prepareStatement(createDBQuery); stmt.setString(1, device.getDeviceIdentifier()); stmt.setString(2, device.getName()); diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/feature/DeviceTypeFeatureManager.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/feature/DeviceTypeFeatureManager.java index 9fe6c8a4..e075a076 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/feature/DeviceTypeFeatureManager.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/feature/DeviceTypeFeatureManager.java @@ -15,20 +15,17 @@ */ package org.wso2.raspberrypicodebased.plugin.impl.feature; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - - import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.device.mgt.common.FeatureManager; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** - * Device type specific feature management server + * Device type specific feature management server. */ public class DeviceTypeFeatureManager implements FeatureManager { @@ -40,7 +37,7 @@ public class DeviceTypeFeatureManager implements FeatureManager { private static final String QUERY_PARAMS = "queryParams"; private static final String FORM_PARAMS = "formParams"; - public DeviceTypeFeatureManager () { + public DeviceTypeFeatureManager() { feature.setCode("change-status"); feature.setName("Change status of sensor: on/off"); feature.setDescription("Change status of sensor: on/off"); @@ -64,35 +61,35 @@ public class DeviceTypeFeatureManager implements FeatureManager { feature.setMetadataEntries(metadataEntries); } - @Override - public boolean addFeature(Feature feature) throws DeviceManagementException { - return false; - } + @Override + public boolean addFeature(Feature feature) throws DeviceManagementException { + return false; + } - @Override - public boolean addFeatures(List features) throws DeviceManagementException { - return false; - } + @Override + public boolean addFeatures(List features) throws DeviceManagementException { + return false; + } - @Override - public Feature getFeature(String name) throws DeviceManagementException { + @Override + public Feature getFeature(String name) throws DeviceManagementException { return feature; - } + } - @Override - public List getFeatures() throws DeviceManagementException { + @Override + public List getFeatures() throws DeviceManagementException { List features = new ArrayList<>(); features.add(feature); return features; - } + } - @Override - public boolean removeFeature(String name) throws DeviceManagementException { - return false; - } + @Override + public boolean removeFeature(String name) throws DeviceManagementException { + return false; + } - @Override - public boolean addSupportedFeaturesToDB() throws DeviceManagementException { - return false; - } + @Override + public boolean addSupportedFeaturesToDB() throws DeviceManagementException { + return false; + } } diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/util/DeviceSchemaInitializer.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/util/DeviceSchemaInitializer.java index d76723ba..40886624 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/util/DeviceSchemaInitializer.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/util/DeviceSchemaInitializer.java @@ -29,22 +29,22 @@ import java.io.File; /** * Provides methods for initializing the database script. */ -public class DeviceSchemaInitializer extends DatabaseCreator{ +public class DeviceSchemaInitializer extends DatabaseCreator { - private static final Log log = LogFactory.getLog(DeviceSchemaInitializer.class); - private static final String setupSQLScriptBaseLocation = CarbonUtils.getCarbonHome() + File.separator + "dbscripts" - + File.separator + "cdm" + File.separator + "plugins" + File.separator; + private static final Log log = LogFactory.getLog(DeviceSchemaInitializer.class); + private static final String setupSQLScriptBaseLocation = CarbonUtils.getCarbonHome() + + File.separator + "dbscripts" + File.separator + "cdm" + File.separator + "plugins" + File.separator; - public DeviceSchemaInitializer(DataSource dataSource) { - super(dataSource); - } + public DeviceSchemaInitializer(DataSource dataSource) { + super(dataSource); + } - @Override - protected String getDbScriptLocation(String databaseType) { - String scriptName = databaseType + ".sql"; - if (log.isDebugEnabled()) { - log.debug("Loading database script from :" + scriptName); - } - return setupSQLScriptBaseLocation.replaceFirst("DBTYPE", databaseType) + scriptName; - } + @Override + protected String getDbScriptLocation(String databaseType) { + String scriptName = databaseType + ".sql"; + if (log.isDebugEnabled()) { + log.debug("Loading database script from :" + scriptName); + } + return setupSQLScriptBaseLocation.replaceFirst("DBTYPE", databaseType) + scriptName; + } } diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/util/DeviceTypeUtils.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/util/DeviceTypeUtils.java index 7bdc636e..9444b5c6 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/util/DeviceTypeUtils.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/impl/util/DeviceTypeUtils.java @@ -18,32 +18,21 @@ package org.wso2.raspberrypicodebased.plugin.impl.util; -import org.wso2.raspberrypicodebased.plugin.constants.DeviceTypeConstants; -import org.wso2.raspberrypicodebased.plugin.exception.DeviceMgtPluginException; 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.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.raspberrypicodebased.plugin.constants.DeviceTypeConstants; +import org.wso2.raspberrypicodebased.plugin.exception.DeviceMgtPluginException; 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 raspberrypicodebased plugin. @@ -97,18 +86,18 @@ public class DeviceTypeUtils { } public static String replaceMqttProperty(String urlWithPlaceholders) { - String MQTT_BROKER_HOST = null; - String MQTT_PORT = null; - if(!DeviceTypeConstants.MQTT_BROKER_HOST.startsWith("$")){ - MQTT_BROKER_HOST = "\\$".concat(DeviceTypeConstants.MQTT_BROKER_HOST); + String mqttBrokerHost = null; + String mqttPort = null; + if (!DeviceTypeConstants.MQTT_BROKER_HOST.startsWith("$")) { + mqttBrokerHost = "\\$".concat(DeviceTypeConstants.MQTT_BROKER_HOST); } - if(!DeviceTypeConstants.MQTT_PORT.startsWith("$")){ - MQTT_PORT = "\\$".concat(DeviceTypeConstants.MQTT_PORT); + if (!DeviceTypeConstants.MQTT_PORT.startsWith("$")) { + mqttPort = "\\$".concat(DeviceTypeConstants.MQTT_PORT); } urlWithPlaceholders = Utils.replaceSystemProperty(urlWithPlaceholders); - urlWithPlaceholders = urlWithPlaceholders.replaceAll(MQTT_PORT, "" + + urlWithPlaceholders = urlWithPlaceholders.replaceAll(mqttPort, "" + (DeviceTypeConstants.DEFAULT_MQTT_PORT + getPortOffset())); - urlWithPlaceholders = urlWithPlaceholders.replaceAll(MQTT_BROKER_HOST, + urlWithPlaceholders = urlWithPlaceholders.replaceAll(mqttBrokerHost, System.getProperty(DeviceTypeConstants.DEFAULT_CARBON_LOCAL_IP_PROPERTY, "localhost")); return urlWithPlaceholders; } diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/internal/ServiceComponent.java b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/internal/ServiceComponent.java index 97191954..86e8f95a 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/internal/ServiceComponent.java +++ b/modules/distribution/src/core/samples/raspberrypicodebased/component/plugin/src/main/java/org.wso2/raspberrypicodebased/plugin/internal/ServiceComponent.java @@ -18,17 +18,15 @@ package org.wso2.raspberrypicodebased.plugin.internal; -import org.wso2.raspberrypicodebased.plugin.exception.DeviceMgtPluginException; -import org.wso2.raspberrypicodebased.plugin.impl.util.DeviceTypeUtils; -import org.wso2.raspberrypicodebased.plugin.impl.DeviceTypeManagerService; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.ComponentContext; -import org.wso2.carbon.core.ServerStartupObserver; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.raspberrypicodebased.plugin.exception.DeviceMgtPluginException; +import org.wso2.raspberrypicodebased.plugin.impl.DeviceTypeManagerService; +import org.wso2.raspberrypicodebased.plugin.impl.util.DeviceTypeUtils; /** * @scr.component name="org.wso2.raspberrypicodebased.plugin.internal.ServiceComponent" diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/agent.py b/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/agent.py index 17891765..66d7c037 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/agent.py +++ b/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/agent.py @@ -20,18 +20,18 @@ """ import argparse -import httplib +import calendar import logging import logging.handlers import signal import ssl import sys import threading -import time, calendar +import time from functools import wraps -import mqttHandler import iotUtils +import mqttHandler import running_mode diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/iotUtils.py b/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/iotUtils.py index 44972b22..3073048c 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/iotUtils.py +++ b/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/iotUtils.py @@ -23,6 +23,7 @@ import ConfigParser import os import random + import running_mode # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -81,7 +82,6 @@ def switchBulb(state): if running_mode == "N": - import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(BULB_PIN,GPIO.OUT) diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/mqttHandler.py b/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/mqttHandler.py index ef6f23a1..3e531c21 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/mqttHandler.py +++ b/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/mqttHandler.py @@ -20,8 +20,9 @@ **/ """ -import time import paho.mqtt.client as mqtt +import time + import iotUtils from token_updater import RefreshToken diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/token_updater.py b/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/token_updater.py index eeb8365c..3deb5002 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/token_updater.py +++ b/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/agent/src/token_updater.py @@ -21,10 +21,10 @@ """ import json -import urllib -import iotUtils import requests +import urllib +import iotUtils applicationKey = None refreshToken = None diff --git a/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/datasources/raspberrypicodebased-datasources.xml b/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/datasources/raspberrypicodebased-datasources.xml index 6720a00d..b2058cd7 100644 --- a/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/datasources/raspberrypicodebased-datasources.xml +++ b/modules/distribution/src/core/samples/raspberrypicodebased/feature/feature/src/main/resources/datasources/raspberrypicodebased-datasources.xml @@ -17,7 +17,7 @@ ~ under the License. --> - + org.wso2.carbon.ndatasource.rdbms.RDBMSDataSourceReader