From 2f0249ef7ac3bd0ac4963ea01811584b26b96a67 Mon Sep 17 00:00:00 2001 From: shagihan Date: Mon, 1 Oct 2018 22:58:57 +0530 Subject: [PATCH 1/9] Reduce the size of device info payload. --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 19 ++ .../core/dao/impl/AbstractDeviceDAOImpl.java | 87 ++++++- .../impl/DeviceInformationManagerImpl.java | 234 +++++++++++++++++- .../main/resources/dbscripts/cdm/mysql.sql | 11 + 4 files changed, 346 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index b1f2ffb043..001f1385ca 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; import org.wso2.carbon.device.mgt.common.PaginationRequest; +import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.geo.GeoCluster; import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; @@ -30,6 +31,7 @@ import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Map; /** * This class represents the key operations associated with persisting device related information. @@ -410,5 +412,22 @@ List getDevicesByStatus(PaginationRequest request, int tenantId) */ List findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast, int geohashLength,int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve the details of latest device info payload. + * + * @param deviceIdentifier the device identofore. + * @param tenantId tenant id. + * @return returns a map of device info objects. + */ + Map getLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceDetailsMgtException; + + /** + * This method is used to set the details of latest device info payload. + * + * @param deviceIdentifier the device identofore. + * @param tenantId tenant id. + */ + void setLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId, Map latestDeviceInfoMap, boolean isUpdate) + throws DeviceDetailsMgtException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 2d0d877c03..08e51747b1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -27,10 +27,16 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.geo.GeoCluster; import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; +import java.io.ByteArrayOutputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.ObjectInputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -39,8 +45,8 @@ import java.util.ArrayList; import java.util.Date; import java.util.HashMap; -import java.util.Iterator; import java.util.List; +import java.util.Map; public abstract class AbstractDeviceDAOImpl implements DeviceDAO { @@ -1122,4 +1128,83 @@ public List findGeoClusters(String deviceType, GeoCoordinate southWe } return geoClusters; } + + @Override + public Map getLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId) + throws DeviceDetailsMgtException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + ByteArrayInputStream bais; + ObjectInputStream ois; + Map deviceInfoMap = null; + byte[] deviceInfoPayloadByteArray = null; + try { + Device device = this.getDevice(deviceIdentifier, tenantId); + int deviceId = device.getId(); + String sql = "select LATEST_OPERATION_RESPONSE FROM " + + "DM_DEVICE_INFO_OPERATION_RESPONSE WHERE DEVICE_ID = ?"; + conn = this.getConnection(); + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + rs = stmt.executeQuery(); + if (rs.next()) { + deviceInfoPayloadByteArray = rs.getBytes("LATEST_OPERATION_RESPONSE"); + } else { + return null; + } + bais = new ByteArrayInputStream(deviceInfoPayloadByteArray); + ois = new ObjectInputStream(bais); + deviceInfoMap = (Map) ois.readObject(); + } catch (DeviceManagementDAOException e) { + throw new DeviceDetailsMgtException("Cannot find valid device for device identifier"); + } catch (SQLException e) { + throw new DeviceDetailsMgtException("Database connection issue"); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return deviceInfoMap; + } + + @Override + public void setLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId, Map latestDeviceInfoMap, boolean isUpdate) + throws DeviceDetailsMgtException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + ByteArrayOutputStream bao = null; + ObjectOutputStream oos = null; + String sql; + try { + Device device = this.getDevice(deviceIdentifier,tenantId); + conn = this.getConnection(); + bao = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(bao); + oos.writeObject(latestDeviceInfoMap); + if(isUpdate) { + sql = "UPDATE DM_DEVICE_INFO_OPERATION_RESPONSE SET LATEST_OPERATION_RESPONSE = ?, LAST_UPDATED_TIMESTAMP = ?" + + " WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setBytes(1, bao.toByteArray()); + stmt.setTimestamp(2, new Timestamp(new Date().getTime())); + stmt.setInt(3, device.getId()); + } else { + sql = "INSERT INTO DM_DEVICE_INFO_OPERATION_RESPONSE " + + "(DEVICE_ID, LATEST_OPERATION_RESPONSE, LAST_UPDATED_TIMESTAMP) VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, device.getId()); + stmt.setBytes(2, bao.toByteArray()); + stmt.setTimestamp(3, new Timestamp(new Date().getTime())); + } + stmt.execute(); + } catch (SQLException e) { + throw new DeviceDetailsMgtException("Database connection issue"); + } catch (IOException e) { + throw new DeviceDetailsMgtException("Error while serializing the map"); + } catch (DeviceManagementDAOException e) { + throw new DeviceDetailsMgtException("Cannot find valid device for device identifier"); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index f265509910..723cd34689 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -41,6 +41,7 @@ import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.HashMap; import java.util.List; @@ -53,7 +54,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { private static final Log log = LogFactory.getLog(DeviceInformationManagerImpl.class); private static final String LOCATION_EVENT_STREAM_DEFINITION = "org.wso2.iot.LocationStream"; private static final String DEVICE_INFO_EVENT_STREAM_DEFINITION = "org.wso2.iot.DeviceInfoStream"; - + private Device newDevice; + private DeviceInfo newDeviceInfo; public DeviceInformationManagerImpl() { this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); @@ -67,11 +69,12 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro getDeviceManagementProvider().getDevice(deviceId, false); DeviceManagementDAOFactory.beginTransaction(); - deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); + processDeviceInfo(deviceId, deviceInfo, device); + deviceDAO.updateDevice(newDevice, CarbonContext.getThreadLocalCarbonContext().getTenantId()); deviceDetailsDAO.deleteDeviceInformation(device.getId(), device.getEnrolmentInfo().getId()); deviceDetailsDAO.deleteDeviceProperties(device.getId(), device.getEnrolmentInfo().getId()); - deviceDetailsDAO.addDeviceInformation(device.getId(), device.getEnrolmentInfo().getId(), deviceInfo); - deviceDetailsDAO.addDeviceProperties(deviceInfo.getDeviceDetailsMap(), device.getId(), + deviceDetailsDAO.addDeviceInformation(device.getId(), device.getEnrolmentInfo().getId(), newDeviceInfo); + deviceDetailsDAO.addDeviceProperties(newDeviceInfo.getDeviceDetailsMap(), device.getId(), device.getEnrolmentInfo().getId()); DeviceManagementDAOFactory.commitTransaction(); @@ -125,6 +128,229 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro } } + private void processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo, Device device) throws DeviceDetailsMgtException { + + Map previousDeviceInfo = null; + previousDeviceInfo = deviceDAO.getLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); + + Map propertyMap = deviceInfo.getDeviceDetailsMap(); + if (previousDeviceInfo == null) { + previousDeviceInfo = new HashMap(); + previousDeviceInfo.put("NAME", device.getName()); + previousDeviceInfo.put("DESCRIPTION", device.getDescription()); + previousDeviceInfo.put("DEVICE_IDENTIFICATION", device.getDeviceIdentifier()); + previousDeviceInfo.put("DEVICE_ID", String.valueOf(device.getId())); + + previousDeviceInfo.put("DEVICE_MODEL", deviceInfo.getDeviceModel()); + previousDeviceInfo.put("VENDOR", deviceInfo.getVendor()); + previousDeviceInfo.put("OS_VERSION", deviceInfo.getOsVersion()); + previousDeviceInfo.put("OS_BUILD_DATE", deviceInfo.getOsBuildDate()); + previousDeviceInfo.put("BATTERY_LEVEL", String.valueOf(deviceInfo.getBatteryLevel())); + previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory())); + previousDeviceInfo.put("INTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getInternalAvailableMemory())); + previousDeviceInfo.put("EXTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getExternalTotalMemory())); + previousDeviceInfo.put("EXTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getExternalAvailableMemory())); + previousDeviceInfo.put("CONNECTION_TYPE", deviceInfo.getConnectionType()); + previousDeviceInfo.put("SSID", deviceInfo.getSsid()); + previousDeviceInfo.put("CPU_USAGE", String.valueOf(deviceInfo.getCpuUsage())); + previousDeviceInfo.put("TOTAL_RAM_MEMORY", String.valueOf(deviceInfo.getTotalRAMMemory())); + previousDeviceInfo.put("AVAILABLE_RAM_MEMORY", String.valueOf(deviceInfo.getAvailableRAMMemory())); + previousDeviceInfo.put("PLUGGED_IN", String.valueOf(deviceInfo.isPluggedIn())); + List deviceDetailsMapKeylist = new ArrayList<>(); + for (Map.Entry entry : deviceInfo.getDeviceDetailsMap().entrySet()) { + previousDeviceInfo.put(entry.getKey(), entry.getValue()); + deviceDetailsMapKeylist.add(entry.getKey()); + } + previousDeviceInfo.put("DEVICE_DETAILS_KEY", deviceDetailsMapKeylist.toString()); + newDevice = device; + newDeviceInfo = deviceInfo; + deviceDAO.setLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId(), + previousDeviceInfo, false); + } else { + newDevice = new Device(); + newDeviceInfo = new DeviceInfo(); + if (device.getName() == null || device.getName().equals("")) { + newDevice.setName(previousDeviceInfo.get("NAME")); + } else { + newDevice.setName(device.getName()); + } + if (device.getDescription() == null || device.getDescription().equals("")) { + newDevice.setDescription(previousDeviceInfo.get("DESCRIPTION")); + } else { + newDevice.setDescription(device.getDescription()); + } + if (device.getDeviceIdentifier() == null || device.getDeviceIdentifier().equals("")) { + newDevice.setDeviceIdentifier(previousDeviceInfo.get("DEVICE_IDENTIFICATION")); + } else { + newDevice.setDeviceIdentifier(device.getDeviceIdentifier()); + } + if (device.getName() == null || device.getName().equals("")) { + newDevice.setName(previousDeviceInfo.get("NAME")); + } else { + newDevice.setName(device.getName()); + } + if (deviceInfo.getDeviceModel() == null || deviceInfo.getDeviceModel().equals("")) { + newDeviceInfo.setDeviceModel(previousDeviceInfo.get("DEVICE_MODEL")); + } else { + newDeviceInfo.setDeviceModel(deviceInfo.getDeviceModel()); + } + if (deviceInfo.getVendor() == null || deviceInfo.getDeviceModel().equals("")) { + newDeviceInfo.setVendor(previousDeviceInfo.get("VENDOR")); + } else { + newDeviceInfo.setVendor(deviceInfo.getVendor()); + } + if (deviceInfo.getOsVersion() == null || deviceInfo.getOsVersion().equals("")) { + newDeviceInfo.setOsVersion(previousDeviceInfo.get("OS_VERSION")); + } else { + newDeviceInfo.setOsVersion(deviceInfo.getOsVersion()); + } + if (deviceInfo.getOsBuildDate() == null || deviceInfo.getOsBuildDate().equals("")) { + newDeviceInfo.setOsBuildDate(previousDeviceInfo.get("OS_BUILD_DATE")); + } else { + newDeviceInfo.setOsBuildDate(deviceInfo.getOsBuildDate()); + } + if (deviceInfo.getBatteryLevel() == null || deviceInfo.getBatteryLevel().equals("")) { + newDeviceInfo.setBatteryLevel(Double.valueOf(previousDeviceInfo.get("BATTERY_LEVEL"))); + } else { + newDeviceInfo.setBatteryLevel(deviceInfo.getBatteryLevel()); + } + if (deviceInfo.getInternalTotalMemory() == null || deviceInfo.getInternalTotalMemory().equals("")) { + newDeviceInfo.setInternalTotalMemory(Double.valueOf(previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY"))); + } else { + newDeviceInfo.setInternalTotalMemory(deviceInfo.getInternalTotalMemory()); + } + if (deviceInfo.getInternalAvailableMemory() == null || deviceInfo.getInternalAvailableMemory().equals("")) { + newDeviceInfo.setInternalAvailableMemory(Double.valueOf(previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY"))); + } else { + newDeviceInfo.setInternalAvailableMemory(deviceInfo.getInternalAvailableMemory()); + } + if (deviceInfo.getExternalTotalMemory() == null || deviceInfo.getExternalAvailableMemory().equals("")) { + newDeviceInfo.setExternalTotalMemory(Double.valueOf(previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY"))); + } else { + newDeviceInfo.setExternalTotalMemory(deviceInfo.getExternalTotalMemory()); + } + if (deviceInfo.getExternalAvailableMemory() == null || deviceInfo.getExternalAvailableMemory().equals("")) { + newDeviceInfo.setExternalAvailableMemory(Double.valueOf(previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY"))); + } else { + newDeviceInfo.setExternalAvailableMemory(deviceInfo.getExternalAvailableMemory()); + } + if (deviceInfo.getConnectionType() == null || deviceInfo.getConnectionType().equals("")) { + newDeviceInfo.setConnectionType(previousDeviceInfo.get("CONNECTION_TYPE")); + } else { + newDeviceInfo.setConnectionType(deviceInfo.getConnectionType()); + } + if (deviceInfo.getSsid() == null || deviceInfo.getSsid().equals("")) { + newDeviceInfo.setSsid(previousDeviceInfo.get("SSID")); + } else { + newDeviceInfo.setSsid(deviceInfo.getSsid()); + } + if (deviceInfo.getCpuUsage() == null || deviceInfo.getCpuUsage().equals("")) { + newDeviceInfo.setCpuUsage(Double.valueOf(previousDeviceInfo.get("CPU_USAGE"))); + } else { + newDeviceInfo.setCpuUsage(deviceInfo.getCpuUsage()); + } + if (deviceInfo.getTotalRAMMemory() == null || deviceInfo.getTotalRAMMemory().equals("")) { + newDeviceInfo.setTotalRAMMemory(Double.valueOf(previousDeviceInfo.get("TOTAL_RAM_MEMORY"))); + } else { + newDeviceInfo.setTotalRAMMemory(deviceInfo.getTotalRAMMemory()); + } + if (deviceInfo.getAvailableRAMMemory() == null || deviceInfo.getAvailableRAMMemory().equals("")) { + newDeviceInfo.setAvailableRAMMemory(Double.valueOf(previousDeviceInfo.get("AVAILABLE_RAM_MEMORY"))); + } else { + newDeviceInfo.setAvailableRAMMemory(deviceInfo.getAvailableRAMMemory()); + } + if (previousDeviceInfo.get("NAME") != null && !previousDeviceInfo.get("NAME").equals(device.getName())) { + previousDeviceInfo.put("NAME", device.getName()); + } + if (previousDeviceInfo.get("DESCRIPTION") != null && !previousDeviceInfo.get("DESCRIPTION").equals(device.getDescription())) { + previousDeviceInfo.put("DESCRIPTION", device.getDescription()); + } + if (previousDeviceInfo.get("DEVICE_IDENTIFICATION") != null && !previousDeviceInfo.get("DEVICE_IDENTIFICATION").equals(device.getDeviceIdentifier())) { + previousDeviceInfo.put("DEVICE_IDENTIFICATION", device.getDeviceIdentifier()); + } + if (previousDeviceInfo.get("DEVICE_ID") != null && !previousDeviceInfo.get("DEVICE_ID").equals(String.valueOf(device.getId()))) { + previousDeviceInfo.put("DEVICE_ID", String.valueOf(device.getId())); + } + if (previousDeviceInfo.get("DEVICE_MODEL") != null && !previousDeviceInfo.get("DEVICE_MODEL").equals(deviceInfo.getDeviceModel())) { + previousDeviceInfo.put("DEVICE_MODEL", deviceInfo.getDeviceModel()); + } + if (previousDeviceInfo.get("VENDOR") != null && !previousDeviceInfo.get("VENDOR").equals(deviceInfo.getVendor())) { + previousDeviceInfo.put("VENDOR", deviceInfo.getVendor()); + } + if (previousDeviceInfo.get("OS_VERSION") != null && !previousDeviceInfo.get("OS_VERSION").equals(deviceInfo.getOsVersion())) { + previousDeviceInfo.put("OS_VERSION", deviceInfo.getOsVersion()); + } + if (previousDeviceInfo.get("OS_BUILD_DATE") != null && !previousDeviceInfo.get("OS_BUILD_DATE").equals(deviceInfo.getOsBuildDate())) { + previousDeviceInfo.put("OS_BUILD_DATE", deviceInfo.getOsBuildDate()); + } + if (previousDeviceInfo.get("BATTERY_LEVEL") != null && !previousDeviceInfo.get("BATTERY_LEVEL").equals(String.valueOf(deviceInfo.getBatteryLevel()))) { + previousDeviceInfo.put("BATTERY_LEVEL", String.valueOf(deviceInfo.getBatteryLevel())); + } + if (previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY") != null && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) { + previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory())); + } + if (previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY") != null && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) { + previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory())); + } + if (previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY") != null && !previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY").equals(String.valueOf(deviceInfo.getInternalAvailableMemory()))) { + previousDeviceInfo.put("INTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getInternalAvailableMemory())); + } + if (previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY") != null && !previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getExternalTotalMemory()))) { + previousDeviceInfo.put("EXTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getExternalTotalMemory())); + } + if (previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY") != null && previousDeviceInfo.get("NAME") != null && !previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY").equals(String.valueOf(deviceInfo.getExternalAvailableMemory()))) { + previousDeviceInfo.put("EXTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getExternalAvailableMemory())); + } + if (previousDeviceInfo.get("CONNECTION_TYPE") != null && !previousDeviceInfo.get("CONNECTION_TYPE").equals(deviceInfo.getConnectionType())) { + previousDeviceInfo.put("CONNECTION_TYPE", deviceInfo.getConnectionType()); + } + if (previousDeviceInfo.get("SSID") != null && !previousDeviceInfo.get("SSID").equals(deviceInfo.getSsid())) { + previousDeviceInfo.put("SSID", deviceInfo.getSsid()); + } + if (previousDeviceInfo.get("CPU_USAGE") != null && !previousDeviceInfo.get("CPU_USAGE").equals(String.valueOf(deviceInfo.getCpuUsage()))) { + previousDeviceInfo.put("CPU_USAGE", String.valueOf(deviceInfo.getCpuUsage())); + } + if (previousDeviceInfo.get("TOTAL_RAM_MEMORY") != null && !previousDeviceInfo.get("TOTAL_RAM_MEMORY").equals(String.valueOf(deviceInfo.getTotalRAMMemory()))) { + previousDeviceInfo.put("TOTAL_RAM_MEMORY", String.valueOf(deviceInfo.getTotalRAMMemory())); + } + if (previousDeviceInfo.get("AVAILABLE_RAM_MEMORY") != null && !previousDeviceInfo.get("AVAILABLE_RAM_MEMORY").equals(String.valueOf(deviceInfo.getAvailableRAMMemory()))) { + previousDeviceInfo.put("AVAILABLE_RAM_MEMORY", String.valueOf(deviceInfo.getAvailableRAMMemory())); + } + if (previousDeviceInfo.get("PLUGGED_IN") != null && !previousDeviceInfo.get("PLUGGED_IN").equals(String.valueOf(deviceInfo.isPluggedIn()))) { + previousDeviceInfo.put("PLUGGED_IN", String.valueOf(deviceInfo.isPluggedIn())); + } + Map tempDetailsMap = new HashMap<>(); + Map oldDetailsMap = deviceInfo.getDeviceDetailsMap(); + String tempDetailsMapKeys = previousDeviceInfo.get("DEVICE_DETAILS_KEY"); + tempDetailsMapKeys = tempDetailsMapKeys.substring(1, (tempDetailsMapKeys.length() - 1)); + List tempDetailsMapKeyList = Arrays.asList(tempDetailsMapKeys.split(",")); + List newDetailsMapKeys = new ArrayList<>(); + for (String eachKey : tempDetailsMapKeyList) { + eachKey = eachKey.replaceAll(" ", ""); + if (oldDetailsMap.get(eachKey) == null) { + tempDetailsMap.put(eachKey, previousDeviceInfo.get(eachKey)); + } else if (!oldDetailsMap.get(eachKey).equals(previousDeviceInfo.get(eachKey))) { + tempDetailsMap.put(eachKey, oldDetailsMap.get(eachKey)); + previousDeviceInfo.put(eachKey, oldDetailsMap.get(eachKey)); + } + } + for (String eachKey : oldDetailsMap.keySet()) { + if (!previousDeviceInfo.containsKey(eachKey)) { + tempDetailsMap.put(eachKey, previousDeviceInfo.get(eachKey)); + previousDeviceInfo.put(eachKey, oldDetailsMap.get(eachKey)); + newDetailsMapKeys.add(eachKey); + } + } + for (String eachKey : tempDetailsMapKeyList) { + newDetailsMapKeys.add(eachKey); + } + previousDeviceInfo.put("DEVICE_DETAILS_KEY", newDetailsMapKeys.toString()); + newDeviceInfo.setDeviceDetailsMap(tempDetailsMap); + deviceDAO.setLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId(), + previousDeviceInfo, true); + } + } + @Override public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException { Device device = getDevice(deviceId); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index e0597745d5..e5ea76453f 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -556,6 +556,17 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( ) ENGINE = InnoDB; +CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO_OPERATION_RESPONSE ( + ID INT(11) NOT NULL AUTO_INCREMENT, + DEVICE_ID INTEGER NOT NULL, + LATEST_OPERATION_RESPONSE LONGBLOB, + LAST_UPDATED_TIMESTAMP TIMESTAMP NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_operation_response_device FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +) +ENGINE = InnoDB; + -- DASHBOARD RELATED VIEWS -- CREATE VIEW DEVICE_INFO_VIEW AS From a89b1eaa53454bcca1af833e7579fdce077e662e Mon Sep 17 00:00:00 2001 From: shagihan Date: Wed, 3 Oct 2018 09:26:23 +0530 Subject: [PATCH 2/9] Coding fixes for logic --- .../impl/DeviceInformationManagerImpl.java | 155 +++++------------- 1 file changed, 42 insertions(+), 113 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 723cd34689..3376b203a0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -54,8 +54,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { private static final Log log = LogFactory.getLog(DeviceInformationManagerImpl.class); private static final String LOCATION_EVENT_STREAM_DEFINITION = "org.wso2.iot.LocationStream"; private static final String DEVICE_INFO_EVENT_STREAM_DEFINITION = "org.wso2.iot.DeviceInfoStream"; - private Device newDevice; - private DeviceInfo newDeviceInfo; + + public DeviceInformationManagerImpl() { this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); @@ -69,8 +69,8 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro getDeviceManagementProvider().getDevice(deviceId, false); DeviceManagementDAOFactory.beginTransaction(); - processDeviceInfo(deviceId, deviceInfo, device); - deviceDAO.updateDevice(newDevice, CarbonContext.getThreadLocalCarbonContext().getTenantId()); + DeviceInfo newDeviceInfo = processDeviceInfo(deviceId, deviceInfo, device); + deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); deviceDetailsDAO.deleteDeviceInformation(device.getId(), device.getEnrolmentInfo().getId()); deviceDetailsDAO.deleteDeviceProperties(device.getId(), device.getEnrolmentInfo().getId()); deviceDetailsDAO.addDeviceInformation(device.getId(), device.getEnrolmentInfo().getId(), newDeviceInfo); @@ -128,12 +128,10 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro } } - private void processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo, Device device) throws DeviceDetailsMgtException { + private DeviceInfo processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo, Device device) throws DeviceDetailsMgtException { Map previousDeviceInfo = null; previousDeviceInfo = deviceDAO.getLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - - Map propertyMap = deviceInfo.getDeviceDetailsMap(); if (previousDeviceInfo == null) { previousDeviceInfo = new HashMap(); previousDeviceInfo.put("NAME", device.getName()); @@ -162,103 +160,11 @@ private void processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo, deviceDetailsMapKeylist.add(entry.getKey()); } previousDeviceInfo.put("DEVICE_DETAILS_KEY", deviceDetailsMapKeylist.toString()); - newDevice = device; - newDeviceInfo = deviceInfo; deviceDAO.setLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId(), previousDeviceInfo, false); + return deviceInfo; } else { - newDevice = new Device(); - newDeviceInfo = new DeviceInfo(); - if (device.getName() == null || device.getName().equals("")) { - newDevice.setName(previousDeviceInfo.get("NAME")); - } else { - newDevice.setName(device.getName()); - } - if (device.getDescription() == null || device.getDescription().equals("")) { - newDevice.setDescription(previousDeviceInfo.get("DESCRIPTION")); - } else { - newDevice.setDescription(device.getDescription()); - } - if (device.getDeviceIdentifier() == null || device.getDeviceIdentifier().equals("")) { - newDevice.setDeviceIdentifier(previousDeviceInfo.get("DEVICE_IDENTIFICATION")); - } else { - newDevice.setDeviceIdentifier(device.getDeviceIdentifier()); - } - if (device.getName() == null || device.getName().equals("")) { - newDevice.setName(previousDeviceInfo.get("NAME")); - } else { - newDevice.setName(device.getName()); - } - if (deviceInfo.getDeviceModel() == null || deviceInfo.getDeviceModel().equals("")) { - newDeviceInfo.setDeviceModel(previousDeviceInfo.get("DEVICE_MODEL")); - } else { - newDeviceInfo.setDeviceModel(deviceInfo.getDeviceModel()); - } - if (deviceInfo.getVendor() == null || deviceInfo.getDeviceModel().equals("")) { - newDeviceInfo.setVendor(previousDeviceInfo.get("VENDOR")); - } else { - newDeviceInfo.setVendor(deviceInfo.getVendor()); - } - if (deviceInfo.getOsVersion() == null || deviceInfo.getOsVersion().equals("")) { - newDeviceInfo.setOsVersion(previousDeviceInfo.get("OS_VERSION")); - } else { - newDeviceInfo.setOsVersion(deviceInfo.getOsVersion()); - } - if (deviceInfo.getOsBuildDate() == null || deviceInfo.getOsBuildDate().equals("")) { - newDeviceInfo.setOsBuildDate(previousDeviceInfo.get("OS_BUILD_DATE")); - } else { - newDeviceInfo.setOsBuildDate(deviceInfo.getOsBuildDate()); - } - if (deviceInfo.getBatteryLevel() == null || deviceInfo.getBatteryLevel().equals("")) { - newDeviceInfo.setBatteryLevel(Double.valueOf(previousDeviceInfo.get("BATTERY_LEVEL"))); - } else { - newDeviceInfo.setBatteryLevel(deviceInfo.getBatteryLevel()); - } - if (deviceInfo.getInternalTotalMemory() == null || deviceInfo.getInternalTotalMemory().equals("")) { - newDeviceInfo.setInternalTotalMemory(Double.valueOf(previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY"))); - } else { - newDeviceInfo.setInternalTotalMemory(deviceInfo.getInternalTotalMemory()); - } - if (deviceInfo.getInternalAvailableMemory() == null || deviceInfo.getInternalAvailableMemory().equals("")) { - newDeviceInfo.setInternalAvailableMemory(Double.valueOf(previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY"))); - } else { - newDeviceInfo.setInternalAvailableMemory(deviceInfo.getInternalAvailableMemory()); - } - if (deviceInfo.getExternalTotalMemory() == null || deviceInfo.getExternalAvailableMemory().equals("")) { - newDeviceInfo.setExternalTotalMemory(Double.valueOf(previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY"))); - } else { - newDeviceInfo.setExternalTotalMemory(deviceInfo.getExternalTotalMemory()); - } - if (deviceInfo.getExternalAvailableMemory() == null || deviceInfo.getExternalAvailableMemory().equals("")) { - newDeviceInfo.setExternalAvailableMemory(Double.valueOf(previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY"))); - } else { - newDeviceInfo.setExternalAvailableMemory(deviceInfo.getExternalAvailableMemory()); - } - if (deviceInfo.getConnectionType() == null || deviceInfo.getConnectionType().equals("")) { - newDeviceInfo.setConnectionType(previousDeviceInfo.get("CONNECTION_TYPE")); - } else { - newDeviceInfo.setConnectionType(deviceInfo.getConnectionType()); - } - if (deviceInfo.getSsid() == null || deviceInfo.getSsid().equals("")) { - newDeviceInfo.setSsid(previousDeviceInfo.get("SSID")); - } else { - newDeviceInfo.setSsid(deviceInfo.getSsid()); - } - if (deviceInfo.getCpuUsage() == null || deviceInfo.getCpuUsage().equals("")) { - newDeviceInfo.setCpuUsage(Double.valueOf(previousDeviceInfo.get("CPU_USAGE"))); - } else { - newDeviceInfo.setCpuUsage(deviceInfo.getCpuUsage()); - } - if (deviceInfo.getTotalRAMMemory() == null || deviceInfo.getTotalRAMMemory().equals("")) { - newDeviceInfo.setTotalRAMMemory(Double.valueOf(previousDeviceInfo.get("TOTAL_RAM_MEMORY"))); - } else { - newDeviceInfo.setTotalRAMMemory(deviceInfo.getTotalRAMMemory()); - } - if (deviceInfo.getAvailableRAMMemory() == null || deviceInfo.getAvailableRAMMemory().equals("")) { - newDeviceInfo.setAvailableRAMMemory(Double.valueOf(previousDeviceInfo.get("AVAILABLE_RAM_MEMORY"))); - } else { - newDeviceInfo.setAvailableRAMMemory(deviceInfo.getAvailableRAMMemory()); - } + DeviceInfo newDeviceInfo = new DeviceInfo(); if (previousDeviceInfo.get("NAME") != null && !previousDeviceInfo.get("NAME").equals(device.getName())) { previousDeviceInfo.put("NAME", device.getName()); } @@ -319,36 +225,59 @@ private void processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo, if (previousDeviceInfo.get("PLUGGED_IN") != null && !previousDeviceInfo.get("PLUGGED_IN").equals(String.valueOf(deviceInfo.isPluggedIn()))) { previousDeviceInfo.put("PLUGGED_IN", String.valueOf(deviceInfo.isPluggedIn())); } + newDeviceInfo.setDeviceModel(previousDeviceInfo.get("DEVICE_MODEL")); + newDeviceInfo.setVendor(previousDeviceInfo.get("VENDOR")); + newDeviceInfo.setOsVersion(previousDeviceInfo.get("OS_VERSION")); + newDeviceInfo.setOsBuildDate(previousDeviceInfo.get("OS_BUILD_DATE")); + newDeviceInfo.setBatteryLevel(Double.valueOf(previousDeviceInfo.get("BATTERY_LEVEL"))); + newDeviceInfo.setInternalTotalMemory(Double.valueOf(previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY"))); + newDeviceInfo.setInternalAvailableMemory(Double.valueOf(previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY"))); + newDeviceInfo.setExternalTotalMemory(Double.valueOf(previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY"))); + newDeviceInfo.setExternalAvailableMemory(Double.valueOf(previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY"))); + newDeviceInfo.setConnectionType(previousDeviceInfo.get("CONNECTION_TYPE")); + newDeviceInfo.setSsid(previousDeviceInfo.get("SSID")); + newDeviceInfo.setCpuUsage(Double.valueOf(previousDeviceInfo.get("CPU_USAGE"))); + newDeviceInfo.setTotalRAMMemory(Double.valueOf(previousDeviceInfo.get("TOTAL_RAM_MEMORY"))); + newDeviceInfo.setAvailableRAMMemory(Double.valueOf(previousDeviceInfo.get("AVAILABLE_RAM_MEMORY"))); + Map tempDetailsMap = new HashMap<>(); - Map oldDetailsMap = deviceInfo.getDeviceDetailsMap(); + + Map agentDetailsMap = deviceInfo.getDeviceDetailsMap(); + String tempDetailsMapKeys = previousDeviceInfo.get("DEVICE_DETAILS_KEY"); + tempDetailsMapKeys = tempDetailsMapKeys.substring(1, (tempDetailsMapKeys.length() - 1)); + List tempDetailsMapKeyList = Arrays.asList(tempDetailsMapKeys.split(",")); + List newDetailsMapKeys = new ArrayList<>(); + for (String eachKey : tempDetailsMapKeyList) { eachKey = eachKey.replaceAll(" ", ""); - if (oldDetailsMap.get(eachKey) == null) { + if (agentDetailsMap.get(eachKey) == null) { tempDetailsMap.put(eachKey, previousDeviceInfo.get(eachKey)); - } else if (!oldDetailsMap.get(eachKey).equals(previousDeviceInfo.get(eachKey))) { - tempDetailsMap.put(eachKey, oldDetailsMap.get(eachKey)); - previousDeviceInfo.put(eachKey, oldDetailsMap.get(eachKey)); + } else if (!agentDetailsMap.get(eachKey).equals(previousDeviceInfo.get(eachKey))) { + tempDetailsMap.put(eachKey, agentDetailsMap.get(eachKey)); + previousDeviceInfo.put(eachKey, agentDetailsMap.get(eachKey)); + } else { + tempDetailsMap.put(eachKey, agentDetailsMap.get(eachKey)); } + newDetailsMapKeys.add(eachKey); } - for (String eachKey : oldDetailsMap.keySet()) { - if (!previousDeviceInfo.containsKey(eachKey)) { - tempDetailsMap.put(eachKey, previousDeviceInfo.get(eachKey)); - previousDeviceInfo.put(eachKey, oldDetailsMap.get(eachKey)); + for (String eachKey : agentDetailsMap.keySet()) { + if(!newDetailsMapKeys.contains(eachKey)){ + tempDetailsMap.put(eachKey, agentDetailsMap.get(eachKey)); + previousDeviceInfo.put(eachKey, agentDetailsMap.get(eachKey)); newDetailsMapKeys.add(eachKey); } } - for (String eachKey : tempDetailsMapKeyList) { - newDetailsMapKeys.add(eachKey); - } previousDeviceInfo.put("DEVICE_DETAILS_KEY", newDetailsMapKeys.toString()); newDeviceInfo.setDeviceDetailsMap(tempDetailsMap); deviceDAO.setLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId(), previousDeviceInfo, true); + return newDeviceInfo; } + } @Override From 147841adcb582652cf0cdc49623e580e8bd47806 Mon Sep 17 00:00:00 2001 From: shagihan Date: Wed, 3 Oct 2018 15:08:15 +0530 Subject: [PATCH 3/9] Adding logic changes for matching previous information --- .../impl/DeviceInformationManagerImpl.java | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 3376b203a0..83d2380960 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -165,64 +165,64 @@ private DeviceInfo processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo devic return deviceInfo; } else { DeviceInfo newDeviceInfo = new DeviceInfo(); - if (previousDeviceInfo.get("NAME") != null && !previousDeviceInfo.get("NAME").equals(device.getName())) { + if (device.getName() != null && !previousDeviceInfo.get("NAME").equals(device.getName())) { previousDeviceInfo.put("NAME", device.getName()); } - if (previousDeviceInfo.get("DESCRIPTION") != null && !previousDeviceInfo.get("DESCRIPTION").equals(device.getDescription())) { + if (device.getDescription() != null && !previousDeviceInfo.get("DESCRIPTION").equals(device.getDescription())) { previousDeviceInfo.put("DESCRIPTION", device.getDescription()); } - if (previousDeviceInfo.get("DEVICE_IDENTIFICATION") != null && !previousDeviceInfo.get("DEVICE_IDENTIFICATION").equals(device.getDeviceIdentifier())) { + if (device.getDeviceIdentifier() != null && !previousDeviceInfo.get("DEVICE_IDENTIFICATION").equals(device.getDeviceIdentifier())) { previousDeviceInfo.put("DEVICE_IDENTIFICATION", device.getDeviceIdentifier()); } - if (previousDeviceInfo.get("DEVICE_ID") != null && !previousDeviceInfo.get("DEVICE_ID").equals(String.valueOf(device.getId()))) { + if (String.valueOf(device.getId()) != null && !previousDeviceInfo.get("DEVICE_ID").equals(String.valueOf(device.getId()))) { previousDeviceInfo.put("DEVICE_ID", String.valueOf(device.getId())); } - if (previousDeviceInfo.get("DEVICE_MODEL") != null && !previousDeviceInfo.get("DEVICE_MODEL").equals(deviceInfo.getDeviceModel())) { + if (deviceInfo.getDeviceModel() != null && !previousDeviceInfo.get("DEVICE_MODEL").equals(deviceInfo.getDeviceModel())) { previousDeviceInfo.put("DEVICE_MODEL", deviceInfo.getDeviceModel()); } - if (previousDeviceInfo.get("VENDOR") != null && !previousDeviceInfo.get("VENDOR").equals(deviceInfo.getVendor())) { + if (deviceInfo.getVendor() != null && !previousDeviceInfo.get("VENDOR").equals(deviceInfo.getVendor())) { previousDeviceInfo.put("VENDOR", deviceInfo.getVendor()); } - if (previousDeviceInfo.get("OS_VERSION") != null && !previousDeviceInfo.get("OS_VERSION").equals(deviceInfo.getOsVersion())) { + if (deviceInfo.getOsVersion() != null && !previousDeviceInfo.get("OS_VERSION").equals(deviceInfo.getOsVersion())) { previousDeviceInfo.put("OS_VERSION", deviceInfo.getOsVersion()); } - if (previousDeviceInfo.get("OS_BUILD_DATE") != null && !previousDeviceInfo.get("OS_BUILD_DATE").equals(deviceInfo.getOsBuildDate())) { + if (deviceInfo.getOsBuildDate() != null && !previousDeviceInfo.get("OS_BUILD_DATE").equals(deviceInfo.getOsBuildDate())) { previousDeviceInfo.put("OS_BUILD_DATE", deviceInfo.getOsBuildDate()); } - if (previousDeviceInfo.get("BATTERY_LEVEL") != null && !previousDeviceInfo.get("BATTERY_LEVEL").equals(String.valueOf(deviceInfo.getBatteryLevel()))) { + if (String.valueOf(deviceInfo.getBatteryLevel()) != null && !previousDeviceInfo.get("BATTERY_LEVEL").equals(String.valueOf(deviceInfo.getBatteryLevel()))) { previousDeviceInfo.put("BATTERY_LEVEL", String.valueOf(deviceInfo.getBatteryLevel())); } - if (previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY") != null && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) { + if (String.valueOf(deviceInfo.getInternalTotalMemory()) != null && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) { previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory())); } - if (previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY") != null && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) { + if (String.valueOf(deviceInfo.getInternalTotalMemory()) != null && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) { previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory())); } - if (previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY") != null && !previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY").equals(String.valueOf(deviceInfo.getInternalAvailableMemory()))) { + if (String.valueOf(deviceInfo.getInternalAvailableMemory()) != null && !previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY").equals(String.valueOf(deviceInfo.getInternalAvailableMemory()))) { previousDeviceInfo.put("INTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getInternalAvailableMemory())); } - if (previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY") != null && !previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getExternalTotalMemory()))) { + if (String.valueOf(deviceInfo.getExternalTotalMemory()) != null && !previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getExternalTotalMemory()))) { previousDeviceInfo.put("EXTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getExternalTotalMemory())); } - if (previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY") != null && previousDeviceInfo.get("NAME") != null && !previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY").equals(String.valueOf(deviceInfo.getExternalAvailableMemory()))) { + if (String.valueOf(deviceInfo.getExternalAvailableMemory()) != null && previousDeviceInfo.get("NAME") != null && !previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY").equals(String.valueOf(deviceInfo.getExternalAvailableMemory()))) { previousDeviceInfo.put("EXTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getExternalAvailableMemory())); } - if (previousDeviceInfo.get("CONNECTION_TYPE") != null && !previousDeviceInfo.get("CONNECTION_TYPE").equals(deviceInfo.getConnectionType())) { + if (deviceInfo.getConnectionType() != null && !previousDeviceInfo.get("CONNECTION_TYPE").equals(deviceInfo.getConnectionType())) { previousDeviceInfo.put("CONNECTION_TYPE", deviceInfo.getConnectionType()); } - if (previousDeviceInfo.get("SSID") != null && !previousDeviceInfo.get("SSID").equals(deviceInfo.getSsid())) { + if (deviceInfo.getSsid() != null && !previousDeviceInfo.get("SSID").equals(deviceInfo.getSsid())) { previousDeviceInfo.put("SSID", deviceInfo.getSsid()); } - if (previousDeviceInfo.get("CPU_USAGE") != null && !previousDeviceInfo.get("CPU_USAGE").equals(String.valueOf(deviceInfo.getCpuUsage()))) { + if (String.valueOf(deviceInfo.getCpuUsage()) != null && !previousDeviceInfo.get("CPU_USAGE").equals(String.valueOf(deviceInfo.getCpuUsage()))) { previousDeviceInfo.put("CPU_USAGE", String.valueOf(deviceInfo.getCpuUsage())); } - if (previousDeviceInfo.get("TOTAL_RAM_MEMORY") != null && !previousDeviceInfo.get("TOTAL_RAM_MEMORY").equals(String.valueOf(deviceInfo.getTotalRAMMemory()))) { + if (String.valueOf(deviceInfo.getTotalRAMMemory()) != null && !previousDeviceInfo.get("TOTAL_RAM_MEMORY").equals(String.valueOf(deviceInfo.getTotalRAMMemory()))) { previousDeviceInfo.put("TOTAL_RAM_MEMORY", String.valueOf(deviceInfo.getTotalRAMMemory())); } - if (previousDeviceInfo.get("AVAILABLE_RAM_MEMORY") != null && !previousDeviceInfo.get("AVAILABLE_RAM_MEMORY").equals(String.valueOf(deviceInfo.getAvailableRAMMemory()))) { + if (String.valueOf(deviceInfo.getAvailableRAMMemory()) != null && !previousDeviceInfo.get("AVAILABLE_RAM_MEMORY").equals(String.valueOf(deviceInfo.getAvailableRAMMemory()))) { previousDeviceInfo.put("AVAILABLE_RAM_MEMORY", String.valueOf(deviceInfo.getAvailableRAMMemory())); } - if (previousDeviceInfo.get("PLUGGED_IN") != null && !previousDeviceInfo.get("PLUGGED_IN").equals(String.valueOf(deviceInfo.isPluggedIn()))) { + if (String.valueOf(deviceInfo.isPluggedIn()) != null && !previousDeviceInfo.get("PLUGGED_IN").equals(String.valueOf(deviceInfo.isPluggedIn()))) { previousDeviceInfo.put("PLUGGED_IN", String.valueOf(deviceInfo.isPluggedIn())); } newDeviceInfo.setDeviceModel(previousDeviceInfo.get("DEVICE_MODEL")); From 2cc7414627356a570b6df61f9d035000460a972b Mon Sep 17 00:00:00 2001 From: shagihan Date: Thu, 4 Oct 2018 15:04:24 +0530 Subject: [PATCH 4/9] Fixing unit test failing --- .../src/test/resources/sql/h2.sql | 10 ++++++++++ .../src/main/resources/dbscripts/cdm/h2.sql | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index 232123d86f..f7eb92751c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -509,6 +509,16 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY ( ON DELETE CASCADE ON UPDATE CASCADE ); + +CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO_OPERATION_RESPONSE ( + ID INT(11) NOT NULL AUTO_INCREMENT, + DEVICE_ID INTEGER NOT NULL, + LATEST_OPERATION_RESPONSE LONGBLOB, + LAST_UPDATED_TIMESTAMP TIMESTAMP NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_operation_response_device FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); -- END OF POLICY AND DEVICE GROUP MAPPING -- -- DASHBOARD RELATED VIEWS -- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index e22badf0b5..f956260d69 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -488,6 +488,15 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( ON UPDATE NO ACTION ); +CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO_OPERATION_RESPONSE ( + ID INT(11) NOT NULL AUTO_INCREMENT, + DEVICE_ID INTEGER NOT NULL, + LATEST_OPERATION_RESPONSE LONGBLOB, + LAST_UPDATED_TIMESTAMP TIMESTAMP NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_operation_response_device FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); -- POLICY AND DEVICE GROUP MAPPING -- CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY ( ID INT NOT NULL AUTO_INCREMENT, From 95733775d4f66ab7c72c2466109ca26df06ddf95 Mon Sep 17 00:00:00 2001 From: shagihan Date: Tue, 9 Oct 2018 10:07:37 +0530 Subject: [PATCH 5/9] Fixing issues related to coding standards --- .../carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 08e51747b1..253c256bd4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -1142,7 +1142,7 @@ public Map getLatestDeviceInfoMap(DeviceIdentifier deviceIdentif try { Device device = this.getDevice(deviceIdentifier, tenantId); int deviceId = device.getId(); - String sql = "select LATEST_OPERATION_RESPONSE FROM " + + String sql = "SELECT LATEST_OPERATION_RESPONSE FROM " + "DM_DEVICE_INFO_OPERATION_RESPONSE WHERE DEVICE_ID = ?"; conn = this.getConnection(); stmt = conn.prepareStatement(sql); From 461504dc802a72782b81d2ba7a78f3bd309351c8 Mon Sep 17 00:00:00 2001 From: shagihan Date: Thu, 11 Oct 2018 10:14:00 +0530 Subject: [PATCH 6/9] Remove unnecessary checks. --- .../impl/DeviceInformationManagerImpl.java | 77 +++++++++++-------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 83d2380960..a5fed3dd88 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -138,7 +138,6 @@ private DeviceInfo processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo devic previousDeviceInfo.put("DESCRIPTION", device.getDescription()); previousDeviceInfo.put("DEVICE_IDENTIFICATION", device.getDeviceIdentifier()); previousDeviceInfo.put("DEVICE_ID", String.valueOf(device.getId())); - previousDeviceInfo.put("DEVICE_MODEL", deviceInfo.getDeviceModel()); previousDeviceInfo.put("VENDOR", deviceInfo.getVendor()); previousDeviceInfo.put("OS_VERSION", deviceInfo.getOsVersion()); @@ -165,64 +164,82 @@ private DeviceInfo processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo devic return deviceInfo; } else { DeviceInfo newDeviceInfo = new DeviceInfo(); - if (device.getName() != null && !previousDeviceInfo.get("NAME").equals(device.getName())) { + if (device.getName() != null && !device.getName().equals("") && + !previousDeviceInfo.get("NAME").equals(device.getName())) { previousDeviceInfo.put("NAME", device.getName()); } - if (device.getDescription() != null && !previousDeviceInfo.get("DESCRIPTION").equals(device.getDescription())) { + if (device.getDescription() != null && !device.getDescription().equals("") && + !previousDeviceInfo.get("DESCRIPTION").equals(device.getDescription())) { previousDeviceInfo.put("DESCRIPTION", device.getDescription()); } - if (device.getDeviceIdentifier() != null && !previousDeviceInfo.get("DEVICE_IDENTIFICATION").equals(device.getDeviceIdentifier())) { + if (device.getDeviceIdentifier() != null && !device.getDeviceIdentifier().equals("") && + !previousDeviceInfo.get("DEVICE_IDENTIFICATION").equals(device.getDeviceIdentifier())) { previousDeviceInfo.put("DEVICE_IDENTIFICATION", device.getDeviceIdentifier()); } - if (String.valueOf(device.getId()) != null && !previousDeviceInfo.get("DEVICE_ID").equals(String.valueOf(device.getId()))) { + if (!String.valueOf(device.getId()).equals("") && !previousDeviceInfo.get("DEVICE_ID") + .equals(String.valueOf(device.getId()))) { previousDeviceInfo.put("DEVICE_ID", String.valueOf(device.getId())); } - if (deviceInfo.getDeviceModel() != null && !previousDeviceInfo.get("DEVICE_MODEL").equals(deviceInfo.getDeviceModel())) { + if (!deviceInfo.getDeviceModel().equals("") && + !previousDeviceInfo.get("DEVICE_MODEL").equals(deviceInfo.getDeviceModel())) { previousDeviceInfo.put("DEVICE_MODEL", deviceInfo.getDeviceModel()); } - if (deviceInfo.getVendor() != null && !previousDeviceInfo.get("VENDOR").equals(deviceInfo.getVendor())) { + if (!deviceInfo.getVendor().equals("") && + !previousDeviceInfo.get("VENDOR").equals(deviceInfo.getVendor())) { previousDeviceInfo.put("VENDOR", deviceInfo.getVendor()); } - if (deviceInfo.getOsVersion() != null && !previousDeviceInfo.get("OS_VERSION").equals(deviceInfo.getOsVersion())) { + if (!deviceInfo.getOsVersion().equals("") && + !previousDeviceInfo.get("OS_VERSION").equals(deviceInfo.getOsVersion())) { previousDeviceInfo.put("OS_VERSION", deviceInfo.getOsVersion()); } - if (deviceInfo.getOsBuildDate() != null && !previousDeviceInfo.get("OS_BUILD_DATE").equals(deviceInfo.getOsBuildDate())) { + if (!deviceInfo.getOsBuildDate().equals("") && + !previousDeviceInfo.get("OS_BUILD_DATE").equals(deviceInfo.getOsBuildDate())) { previousDeviceInfo.put("OS_BUILD_DATE", deviceInfo.getOsBuildDate()); } - if (String.valueOf(deviceInfo.getBatteryLevel()) != null && !previousDeviceInfo.get("BATTERY_LEVEL").equals(String.valueOf(deviceInfo.getBatteryLevel()))) { + if (deviceInfo.getBatteryLevel() != 0.0 && + !previousDeviceInfo.get("BATTERY_LEVEL").equals(String.valueOf(deviceInfo.getBatteryLevel()))) { previousDeviceInfo.put("BATTERY_LEVEL", String.valueOf(deviceInfo.getBatteryLevel())); } - if (String.valueOf(deviceInfo.getInternalTotalMemory()) != null && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) { - previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory())); - } - if (String.valueOf(deviceInfo.getInternalTotalMemory()) != null && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) { + if (deviceInfo.getInternalTotalMemory() != 0.0 && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY") + .equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) { previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory())); } - if (String.valueOf(deviceInfo.getInternalAvailableMemory()) != null && !previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY").equals(String.valueOf(deviceInfo.getInternalAvailableMemory()))) { - previousDeviceInfo.put("INTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getInternalAvailableMemory())); + if (deviceInfo.getInternalAvailableMemory() != 0.0 && !previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY") + .equals(String.valueOf(deviceInfo.getInternalAvailableMemory()))) { + previousDeviceInfo.put("INTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo + .getInternalAvailableMemory())); } - if (String.valueOf(deviceInfo.getExternalTotalMemory()) != null && !previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY").equals(String.valueOf(deviceInfo.getExternalTotalMemory()))) { + if (deviceInfo.getExternalTotalMemory() != 0.0 && !previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY") + .equals(String.valueOf(deviceInfo.getExternalTotalMemory()))) { previousDeviceInfo.put("EXTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getExternalTotalMemory())); } - if (String.valueOf(deviceInfo.getExternalAvailableMemory()) != null && previousDeviceInfo.get("NAME") != null && !previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY").equals(String.valueOf(deviceInfo.getExternalAvailableMemory()))) { - previousDeviceInfo.put("EXTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getExternalAvailableMemory())); + if (deviceInfo.getExternalAvailableMemory() != 0.0 && !previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY") + .equals(String.valueOf(deviceInfo.getExternalAvailableMemory()))) { + previousDeviceInfo.put("EXTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo + .getExternalAvailableMemory())); } - if (deviceInfo.getConnectionType() != null && !previousDeviceInfo.get("CONNECTION_TYPE").equals(deviceInfo.getConnectionType())) { + if (!deviceInfo.getConnectionType().equals("") && !previousDeviceInfo.get("CONNECTION_TYPE") + .equals(deviceInfo.getConnectionType())) { previousDeviceInfo.put("CONNECTION_TYPE", deviceInfo.getConnectionType()); } - if (deviceInfo.getSsid() != null && !previousDeviceInfo.get("SSID").equals(deviceInfo.getSsid())) { + if (deviceInfo.getSsid() != null && !deviceInfo.getSsid().equals("") && + !previousDeviceInfo.get("SSID").equals(deviceInfo.getSsid())) { previousDeviceInfo.put("SSID", deviceInfo.getSsid()); } - if (String.valueOf(deviceInfo.getCpuUsage()) != null && !previousDeviceInfo.get("CPU_USAGE").equals(String.valueOf(deviceInfo.getCpuUsage()))) { + if (deviceInfo.getCpuUsage() != 0.0 && + !previousDeviceInfo.get("CPU_USAGE").equals(String.valueOf(deviceInfo.getCpuUsage()))) { previousDeviceInfo.put("CPU_USAGE", String.valueOf(deviceInfo.getCpuUsage())); } - if (String.valueOf(deviceInfo.getTotalRAMMemory()) != null && !previousDeviceInfo.get("TOTAL_RAM_MEMORY").equals(String.valueOf(deviceInfo.getTotalRAMMemory()))) { + if (deviceInfo.getTotalRAMMemory() != 0.0 && !previousDeviceInfo.get("TOTAL_RAM_MEMORY") + .equals(String.valueOf(deviceInfo.getTotalRAMMemory()))) { previousDeviceInfo.put("TOTAL_RAM_MEMORY", String.valueOf(deviceInfo.getTotalRAMMemory())); } - if (String.valueOf(deviceInfo.getAvailableRAMMemory()) != null && !previousDeviceInfo.get("AVAILABLE_RAM_MEMORY").equals(String.valueOf(deviceInfo.getAvailableRAMMemory()))) { + if (String.valueOf(deviceInfo.getAvailableRAMMemory()) != null && !previousDeviceInfo + .get("AVAILABLE_RAM_MEMORY").equals(String.valueOf(deviceInfo.getAvailableRAMMemory()))) { previousDeviceInfo.put("AVAILABLE_RAM_MEMORY", String.valueOf(deviceInfo.getAvailableRAMMemory())); } - if (String.valueOf(deviceInfo.isPluggedIn()) != null && !previousDeviceInfo.get("PLUGGED_IN").equals(String.valueOf(deviceInfo.isPluggedIn()))) { + if (String.valueOf(deviceInfo.isPluggedIn()) != null && + !previousDeviceInfo.get("PLUGGED_IN").equals(String.valueOf(deviceInfo.isPluggedIn()))) { previousDeviceInfo.put("PLUGGED_IN", String.valueOf(deviceInfo.isPluggedIn())); } newDeviceInfo.setDeviceModel(previousDeviceInfo.get("DEVICE_MODEL")); @@ -239,19 +256,12 @@ private DeviceInfo processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo devic newDeviceInfo.setCpuUsage(Double.valueOf(previousDeviceInfo.get("CPU_USAGE"))); newDeviceInfo.setTotalRAMMemory(Double.valueOf(previousDeviceInfo.get("TOTAL_RAM_MEMORY"))); newDeviceInfo.setAvailableRAMMemory(Double.valueOf(previousDeviceInfo.get("AVAILABLE_RAM_MEMORY"))); - Map tempDetailsMap = new HashMap<>(); - Map agentDetailsMap = deviceInfo.getDeviceDetailsMap(); - String tempDetailsMapKeys = previousDeviceInfo.get("DEVICE_DETAILS_KEY"); - tempDetailsMapKeys = tempDetailsMapKeys.substring(1, (tempDetailsMapKeys.length() - 1)); - List tempDetailsMapKeyList = Arrays.asList(tempDetailsMapKeys.split(",")); - List newDetailsMapKeys = new ArrayList<>(); - for (String eachKey : tempDetailsMapKeyList) { eachKey = eachKey.replaceAll(" ", ""); if (agentDetailsMap.get(eachKey) == null) { @@ -265,7 +275,7 @@ private DeviceInfo processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo devic newDetailsMapKeys.add(eachKey); } for (String eachKey : agentDetailsMap.keySet()) { - if(!newDetailsMapKeys.contains(eachKey)){ + if (!newDetailsMapKeys.contains(eachKey)) { tempDetailsMap.put(eachKey, agentDetailsMap.get(eachKey)); previousDeviceInfo.put(eachKey, agentDetailsMap.get(eachKey)); newDetailsMapKeys.add(eachKey); @@ -277,7 +287,6 @@ private DeviceInfo processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo devic previousDeviceInfo, true); return newDeviceInfo; } - } @Override From 68c0bcaab72ec2d23c45a2d09abc6505e0598a59 Mon Sep 17 00:00:00 2001 From: shagihan Date: Mon, 15 Oct 2018 18:40:52 +0530 Subject: [PATCH 7/9] Removing seperate databse table to save Device_Info. --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 17 -- .../core/dao/impl/AbstractDeviceDAOImpl.java | 79 ------ .../impl/DeviceInformationManagerImpl.java | 244 ++++++------------ .../src/test/resources/sql/h2.sql | 10 - .../src/main/resources/dbscripts/cdm/h2.sql | 9 - .../main/resources/dbscripts/cdm/mysql.sql | 11 - 6 files changed, 79 insertions(+), 291 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 001f1385ca..690fab2f46 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -412,22 +412,5 @@ List getDevicesByStatus(PaginationRequest request, int tenantId) */ List findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast, int geohashLength,int tenantId) throws DeviceManagementDAOException; - /** - * This method is used to retrieve the details of latest device info payload. - * - * @param deviceIdentifier the device identofore. - * @param tenantId tenant id. - * @return returns a map of device info objects. - */ - Map getLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceDetailsMgtException; - - /** - * This method is used to set the details of latest device info payload. - * - * @param deviceIdentifier the device identofore. - * @param tenantId tenant id. - */ - void setLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId, Map latestDeviceInfoMap, boolean isUpdate) - throws DeviceDetailsMgtException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 253c256bd4..f76dd5ae4d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -1128,83 +1128,4 @@ public List findGeoClusters(String deviceType, GeoCoordinate southWe } return geoClusters; } - - @Override - public Map getLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId) - throws DeviceDetailsMgtException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - ByteArrayInputStream bais; - ObjectInputStream ois; - Map deviceInfoMap = null; - byte[] deviceInfoPayloadByteArray = null; - try { - Device device = this.getDevice(deviceIdentifier, tenantId); - int deviceId = device.getId(); - String sql = "SELECT LATEST_OPERATION_RESPONSE FROM " + - "DM_DEVICE_INFO_OPERATION_RESPONSE WHERE DEVICE_ID = ?"; - conn = this.getConnection(); - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - rs = stmt.executeQuery(); - if (rs.next()) { - deviceInfoPayloadByteArray = rs.getBytes("LATEST_OPERATION_RESPONSE"); - } else { - return null; - } - bais = new ByteArrayInputStream(deviceInfoPayloadByteArray); - ois = new ObjectInputStream(bais); - deviceInfoMap = (Map) ois.readObject(); - } catch (DeviceManagementDAOException e) { - throw new DeviceDetailsMgtException("Cannot find valid device for device identifier"); - } catch (SQLException e) { - throw new DeviceDetailsMgtException("Database connection issue"); - } catch (IOException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - return deviceInfoMap; - } - - @Override - public void setLatestDeviceInfoMap(DeviceIdentifier deviceIdentifier, int tenantId, Map latestDeviceInfoMap, boolean isUpdate) - throws DeviceDetailsMgtException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - ByteArrayOutputStream bao = null; - ObjectOutputStream oos = null; - String sql; - try { - Device device = this.getDevice(deviceIdentifier,tenantId); - conn = this.getConnection(); - bao = new ByteArrayOutputStream(); - oos = new ObjectOutputStream(bao); - oos.writeObject(latestDeviceInfoMap); - if(isUpdate) { - sql = "UPDATE DM_DEVICE_INFO_OPERATION_RESPONSE SET LATEST_OPERATION_RESPONSE = ?, LAST_UPDATED_TIMESTAMP = ?" + - " WHERE DEVICE_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setBytes(1, bao.toByteArray()); - stmt.setTimestamp(2, new Timestamp(new Date().getTime())); - stmt.setInt(3, device.getId()); - } else { - sql = "INSERT INTO DM_DEVICE_INFO_OPERATION_RESPONSE " + - "(DEVICE_ID, LATEST_OPERATION_RESPONSE, LAST_UPDATED_TIMESTAMP) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, device.getId()); - stmt.setBytes(2, bao.toByteArray()); - stmt.setTimestamp(3, new Timestamp(new Date().getTime())); - } - stmt.execute(); - } catch (SQLException e) { - throw new DeviceDetailsMgtException("Database connection issue"); - } catch (IOException e) { - throw new DeviceDetailsMgtException("Error while serializing the map"); - } catch (DeviceManagementDAOException e) { - throw new DeviceDetailsMgtException("Cannot find valid device for device identifier"); - } - } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index a5fed3dd88..6a27d35ae4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -56,7 +56,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { private static final String DEVICE_INFO_EVENT_STREAM_DEFINITION = "org.wso2.iot.DeviceInfoStream"; - public DeviceInformationManagerImpl() { this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.deviceDetailsDAO = DeviceManagementDAOFactory.getDeviceDetailsDAO(); @@ -67,9 +66,22 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro try { Device device = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider().getDevice(deviceId, false); - + DeviceInfo newDeviceInfo = null; DeviceManagementDAOFactory.beginTransaction(); - DeviceInfo newDeviceInfo = processDeviceInfo(deviceId, deviceInfo, device); + DeviceInfo previousDeviceInfo = deviceDetailsDAO + .getDeviceInformation(device.getId(), device.getEnrolmentInfo().getId()); + Map previousDeviceProperties = deviceDetailsDAO + .getDeviceProperties(device.getId(), device.getEnrolmentInfo().getId()); + if (previousDeviceInfo != null && previousDeviceProperties != null) { + previousDeviceInfo.setDeviceDetailsMap(previousDeviceProperties); + newDeviceInfo = processDeviceInfo(previousDeviceInfo, deviceInfo); + } else if (previousDeviceInfo == null && previousDeviceProperties != null) { + previousDeviceInfo = new DeviceInfo(); + previousDeviceInfo.setDeviceDetailsMap(previousDeviceProperties); + newDeviceInfo = processDeviceInfo(previousDeviceInfo, deviceInfo); + } else { + newDeviceInfo = deviceInfo; + } deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); deviceDetailsDAO.deleteDeviceInformation(device.getId(), device.getEnrolmentInfo().getId()); deviceDetailsDAO.deleteDeviceProperties(device.getId(), device.getEnrolmentInfo().getId()); @@ -109,7 +121,8 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro } } catch (TransactionManagementException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceDetailsMgtException("Transactional error occurred while adding the device information.", e); + throw new DeviceDetailsMgtException("Transactional error occurred while adding the device " + + "information.", e); } catch (DeviceDetailsMgtDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceDetailsMgtException("Error occurred while adding the device information.", e); @@ -119,7 +132,7 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceDetailsMgtException("Error occurred while updating the last update timestamp of the " + - "device", e); + "device", e); } catch (DataPublisherConfigurationException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceDetailsMgtException("Error occurred while publishing the device location information.", e); @@ -128,165 +141,66 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro } } - private DeviceInfo processDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo, Device device) throws DeviceDetailsMgtException { - - Map previousDeviceInfo = null; - previousDeviceInfo = deviceDAO.getLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - if (previousDeviceInfo == null) { - previousDeviceInfo = new HashMap(); - previousDeviceInfo.put("NAME", device.getName()); - previousDeviceInfo.put("DESCRIPTION", device.getDescription()); - previousDeviceInfo.put("DEVICE_IDENTIFICATION", device.getDeviceIdentifier()); - previousDeviceInfo.put("DEVICE_ID", String.valueOf(device.getId())); - previousDeviceInfo.put("DEVICE_MODEL", deviceInfo.getDeviceModel()); - previousDeviceInfo.put("VENDOR", deviceInfo.getVendor()); - previousDeviceInfo.put("OS_VERSION", deviceInfo.getOsVersion()); - previousDeviceInfo.put("OS_BUILD_DATE", deviceInfo.getOsBuildDate()); - previousDeviceInfo.put("BATTERY_LEVEL", String.valueOf(deviceInfo.getBatteryLevel())); - previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory())); - previousDeviceInfo.put("INTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getInternalAvailableMemory())); - previousDeviceInfo.put("EXTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getExternalTotalMemory())); - previousDeviceInfo.put("EXTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo.getExternalAvailableMemory())); - previousDeviceInfo.put("CONNECTION_TYPE", deviceInfo.getConnectionType()); - previousDeviceInfo.put("SSID", deviceInfo.getSsid()); - previousDeviceInfo.put("CPU_USAGE", String.valueOf(deviceInfo.getCpuUsage())); - previousDeviceInfo.put("TOTAL_RAM_MEMORY", String.valueOf(deviceInfo.getTotalRAMMemory())); - previousDeviceInfo.put("AVAILABLE_RAM_MEMORY", String.valueOf(deviceInfo.getAvailableRAMMemory())); - previousDeviceInfo.put("PLUGGED_IN", String.valueOf(deviceInfo.isPluggedIn())); - List deviceDetailsMapKeylist = new ArrayList<>(); - for (Map.Entry entry : deviceInfo.getDeviceDetailsMap().entrySet()) { - previousDeviceInfo.put(entry.getKey(), entry.getValue()); - deviceDetailsMapKeylist.add(entry.getKey()); - } - previousDeviceInfo.put("DEVICE_DETAILS_KEY", deviceDetailsMapKeylist.toString()); - deviceDAO.setLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId(), - previousDeviceInfo, false); - return deviceInfo; - } else { - DeviceInfo newDeviceInfo = new DeviceInfo(); - if (device.getName() != null && !device.getName().equals("") && - !previousDeviceInfo.get("NAME").equals(device.getName())) { - previousDeviceInfo.put("NAME", device.getName()); - } - if (device.getDescription() != null && !device.getDescription().equals("") && - !previousDeviceInfo.get("DESCRIPTION").equals(device.getDescription())) { - previousDeviceInfo.put("DESCRIPTION", device.getDescription()); - } - if (device.getDeviceIdentifier() != null && !device.getDeviceIdentifier().equals("") && - !previousDeviceInfo.get("DEVICE_IDENTIFICATION").equals(device.getDeviceIdentifier())) { - previousDeviceInfo.put("DEVICE_IDENTIFICATION", device.getDeviceIdentifier()); - } - if (!String.valueOf(device.getId()).equals("") && !previousDeviceInfo.get("DEVICE_ID") - .equals(String.valueOf(device.getId()))) { - previousDeviceInfo.put("DEVICE_ID", String.valueOf(device.getId())); - } - if (!deviceInfo.getDeviceModel().equals("") && - !previousDeviceInfo.get("DEVICE_MODEL").equals(deviceInfo.getDeviceModel())) { - previousDeviceInfo.put("DEVICE_MODEL", deviceInfo.getDeviceModel()); - } - if (!deviceInfo.getVendor().equals("") && - !previousDeviceInfo.get("VENDOR").equals(deviceInfo.getVendor())) { - previousDeviceInfo.put("VENDOR", deviceInfo.getVendor()); - } - if (!deviceInfo.getOsVersion().equals("") && - !previousDeviceInfo.get("OS_VERSION").equals(deviceInfo.getOsVersion())) { - previousDeviceInfo.put("OS_VERSION", deviceInfo.getOsVersion()); - } - if (!deviceInfo.getOsBuildDate().equals("") && - !previousDeviceInfo.get("OS_BUILD_DATE").equals(deviceInfo.getOsBuildDate())) { - previousDeviceInfo.put("OS_BUILD_DATE", deviceInfo.getOsBuildDate()); - } - if (deviceInfo.getBatteryLevel() != 0.0 && - !previousDeviceInfo.get("BATTERY_LEVEL").equals(String.valueOf(deviceInfo.getBatteryLevel()))) { - previousDeviceInfo.put("BATTERY_LEVEL", String.valueOf(deviceInfo.getBatteryLevel())); - } - if (deviceInfo.getInternalTotalMemory() != 0.0 && !previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY") - .equals(String.valueOf(deviceInfo.getInternalTotalMemory()))) { - previousDeviceInfo.put("INTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getInternalTotalMemory())); - } - if (deviceInfo.getInternalAvailableMemory() != 0.0 && !previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY") - .equals(String.valueOf(deviceInfo.getInternalAvailableMemory()))) { - previousDeviceInfo.put("INTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo - .getInternalAvailableMemory())); - } - if (deviceInfo.getExternalTotalMemory() != 0.0 && !previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY") - .equals(String.valueOf(deviceInfo.getExternalTotalMemory()))) { - previousDeviceInfo.put("EXTERNAL_TOTAL_MEMORY", String.valueOf(deviceInfo.getExternalTotalMemory())); - } - if (deviceInfo.getExternalAvailableMemory() != 0.0 && !previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY") - .equals(String.valueOf(deviceInfo.getExternalAvailableMemory()))) { - previousDeviceInfo.put("EXTERNAL_AVAILABLE_MEMORY", String.valueOf(deviceInfo - .getExternalAvailableMemory())); - } - if (!deviceInfo.getConnectionType().equals("") && !previousDeviceInfo.get("CONNECTION_TYPE") - .equals(deviceInfo.getConnectionType())) { - previousDeviceInfo.put("CONNECTION_TYPE", deviceInfo.getConnectionType()); - } - if (deviceInfo.getSsid() != null && !deviceInfo.getSsid().equals("") && - !previousDeviceInfo.get("SSID").equals(deviceInfo.getSsid())) { - previousDeviceInfo.put("SSID", deviceInfo.getSsid()); - } - if (deviceInfo.getCpuUsage() != 0.0 && - !previousDeviceInfo.get("CPU_USAGE").equals(String.valueOf(deviceInfo.getCpuUsage()))) { - previousDeviceInfo.put("CPU_USAGE", String.valueOf(deviceInfo.getCpuUsage())); - } - if (deviceInfo.getTotalRAMMemory() != 0.0 && !previousDeviceInfo.get("TOTAL_RAM_MEMORY") - .equals(String.valueOf(deviceInfo.getTotalRAMMemory()))) { - previousDeviceInfo.put("TOTAL_RAM_MEMORY", String.valueOf(deviceInfo.getTotalRAMMemory())); - } - if (String.valueOf(deviceInfo.getAvailableRAMMemory()) != null && !previousDeviceInfo - .get("AVAILABLE_RAM_MEMORY").equals(String.valueOf(deviceInfo.getAvailableRAMMemory()))) { - previousDeviceInfo.put("AVAILABLE_RAM_MEMORY", String.valueOf(deviceInfo.getAvailableRAMMemory())); - } - if (String.valueOf(deviceInfo.isPluggedIn()) != null && - !previousDeviceInfo.get("PLUGGED_IN").equals(String.valueOf(deviceInfo.isPluggedIn()))) { - previousDeviceInfo.put("PLUGGED_IN", String.valueOf(deviceInfo.isPluggedIn())); - } - newDeviceInfo.setDeviceModel(previousDeviceInfo.get("DEVICE_MODEL")); - newDeviceInfo.setVendor(previousDeviceInfo.get("VENDOR")); - newDeviceInfo.setOsVersion(previousDeviceInfo.get("OS_VERSION")); - newDeviceInfo.setOsBuildDate(previousDeviceInfo.get("OS_BUILD_DATE")); - newDeviceInfo.setBatteryLevel(Double.valueOf(previousDeviceInfo.get("BATTERY_LEVEL"))); - newDeviceInfo.setInternalTotalMemory(Double.valueOf(previousDeviceInfo.get("INTERNAL_TOTAL_MEMORY"))); - newDeviceInfo.setInternalAvailableMemory(Double.valueOf(previousDeviceInfo.get("INTERNAL_AVAILABLE_MEMORY"))); - newDeviceInfo.setExternalTotalMemory(Double.valueOf(previousDeviceInfo.get("EXTERNAL_TOTAL_MEMORY"))); - newDeviceInfo.setExternalAvailableMemory(Double.valueOf(previousDeviceInfo.get("EXTERNAL_AVAILABLE_MEMORY"))); - newDeviceInfo.setConnectionType(previousDeviceInfo.get("CONNECTION_TYPE")); - newDeviceInfo.setSsid(previousDeviceInfo.get("SSID")); - newDeviceInfo.setCpuUsage(Double.valueOf(previousDeviceInfo.get("CPU_USAGE"))); - newDeviceInfo.setTotalRAMMemory(Double.valueOf(previousDeviceInfo.get("TOTAL_RAM_MEMORY"))); - newDeviceInfo.setAvailableRAMMemory(Double.valueOf(previousDeviceInfo.get("AVAILABLE_RAM_MEMORY"))); - Map tempDetailsMap = new HashMap<>(); - Map agentDetailsMap = deviceInfo.getDeviceDetailsMap(); - String tempDetailsMapKeys = previousDeviceInfo.get("DEVICE_DETAILS_KEY"); - tempDetailsMapKeys = tempDetailsMapKeys.substring(1, (tempDetailsMapKeys.length() - 1)); - List tempDetailsMapKeyList = Arrays.asList(tempDetailsMapKeys.split(",")); - List newDetailsMapKeys = new ArrayList<>(); - for (String eachKey : tempDetailsMapKeyList) { - eachKey = eachKey.replaceAll(" ", ""); - if (agentDetailsMap.get(eachKey) == null) { - tempDetailsMap.put(eachKey, previousDeviceInfo.get(eachKey)); - } else if (!agentDetailsMap.get(eachKey).equals(previousDeviceInfo.get(eachKey))) { - tempDetailsMap.put(eachKey, agentDetailsMap.get(eachKey)); - previousDeviceInfo.put(eachKey, agentDetailsMap.get(eachKey)); - } else { - tempDetailsMap.put(eachKey, agentDetailsMap.get(eachKey)); - } - newDetailsMapKeys.add(eachKey); - } - for (String eachKey : agentDetailsMap.keySet()) { - if (!newDetailsMapKeys.contains(eachKey)) { - tempDetailsMap.put(eachKey, agentDetailsMap.get(eachKey)); - previousDeviceInfo.put(eachKey, agentDetailsMap.get(eachKey)); - newDetailsMapKeys.add(eachKey); - } + private DeviceInfo processDeviceInfo(DeviceInfo previousDeviceInfo, DeviceInfo newDeviceInfo) { + if (newDeviceInfo.getDeviceModel().equals("")) { + newDeviceInfo.setDeviceModel(previousDeviceInfo.getDeviceModel()); + } + if (newDeviceInfo.getVendor().equals("")) { + newDeviceInfo.setVendor(previousDeviceInfo.getVendor()); + } + if (newDeviceInfo.getOsBuildDate().equals("")) { + newDeviceInfo.setOsBuildDate(previousDeviceInfo.getOsBuildDate()); + } + if (newDeviceInfo.getOsVersion().equals("")) { + newDeviceInfo.setOsVersion(previousDeviceInfo.getOsVersion()); + } + if (newDeviceInfo.getBatteryLevel() == 0.0) { + newDeviceInfo.setBatteryLevel(previousDeviceInfo.getBatteryLevel()); + } + if (newDeviceInfo.getInternalTotalMemory() == 0.0) { + newDeviceInfo.setInternalTotalMemory(previousDeviceInfo.getInternalTotalMemory()); + } + if (newDeviceInfo.getInternalAvailableMemory() == 0.0) { + newDeviceInfo.setInternalAvailableMemory(previousDeviceInfo.getInternalAvailableMemory()); + } + if (newDeviceInfo.getExternalTotalMemory() == 0.0) { + newDeviceInfo.setExternalTotalMemory(previousDeviceInfo.getExternalTotalMemory()); + } + if (newDeviceInfo.getExternalAvailableMemory() == 0.0) { + newDeviceInfo.setExternalAvailableMemory(previousDeviceInfo.getExternalAvailableMemory()); + } + if (newDeviceInfo.getOperator().equals("")) { + newDeviceInfo.setOperator(previousDeviceInfo.getOperator()); + } + if (newDeviceInfo.getConnectionType().equals("")) { + newDeviceInfo.setConnectionType(previousDeviceInfo.getConnectionType()); + } + if (newDeviceInfo.getMobileSignalStrength() == 0.0) { + newDeviceInfo.setMobileSignalStrength(previousDeviceInfo.getMobileSignalStrength()); + } + if (newDeviceInfo.getSsid().equals("")) { + newDeviceInfo.setSsid(previousDeviceInfo.getSsid()); + } + if (newDeviceInfo.getCpuUsage() == 0.0) { + newDeviceInfo.setCpuUsage(previousDeviceInfo.getCpuUsage()); + } + if (newDeviceInfo.getTotalRAMMemory() == 0.0) { + newDeviceInfo.setTotalRAMMemory(previousDeviceInfo.getTotalRAMMemory()); + } + if (newDeviceInfo.getAvailableRAMMemory() == 0.0) { + newDeviceInfo.setAvailableRAMMemory(previousDeviceInfo.getAvailableRAMMemory()); + } + if (!newDeviceInfo.isPluggedIn()) { + newDeviceInfo.setPluggedIn(previousDeviceInfo.isPluggedIn()); + } + Map newDeviceDetailsMap = newDeviceInfo.getDeviceDetailsMap(); + Map previousDeviceDetailsMap = previousDeviceInfo.getDeviceDetailsMap(); + for (String eachKey : previousDeviceDetailsMap.keySet()) { + if (!newDeviceDetailsMap.containsKey(eachKey)) { + newDeviceDetailsMap.put(eachKey, previousDeviceDetailsMap.get(eachKey)); } - previousDeviceInfo.put("DEVICE_DETAILS_KEY", newDetailsMapKeys.toString()); - newDeviceInfo.setDeviceDetailsMap(tempDetailsMap); - deviceDAO.setLatestDeviceInfoMap(deviceId, CarbonContext.getThreadLocalCarbonContext().getTenantId(), - previousDeviceInfo, true); - return newDeviceInfo; } + return newDeviceInfo; } @Override @@ -305,7 +219,7 @@ public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsM } catch (SQLException e) { throw new DeviceDetailsMgtException("SQL error occurred while retrieving device " + deviceId.toString() - + "'s info from database.", e); + + "'s info from database.", e); } catch (DeviceDetailsMgtDAOException e) { throw new DeviceDetailsMgtException("Exception occurred while retrieving device details.", e); } finally { @@ -420,7 +334,7 @@ private Device getDevice(DeviceIdentifier deviceId) throws DeviceDetailsMgtExcep if (device == null) { if (log.isDebugEnabled()) { log.debug("No device is found upon the device identifier '" + deviceId.getId() + - "' and type '" + deviceId.getType() + "'. Therefore returning null"); + "' and type '" + deviceId.getType() + "'. Therefore returning null"); } return null; } @@ -450,7 +364,7 @@ public List getDeviceLocations( throw new DeviceDetailsMgtException("SQL error occurred while retrieving device from database.", e); } catch (DeviceDetailsMgtDAOException e) { throw new DeviceDetailsMgtException("Exception occurred while retrieving device locations.", e); - } finally{ + } finally { DeviceManagementDAOFactory.closeConnection(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index f7eb92751c..232123d86f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -509,16 +509,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY ( ON DELETE CASCADE ON UPDATE CASCADE ); - -CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO_OPERATION_RESPONSE ( - ID INT(11) NOT NULL AUTO_INCREMENT, - DEVICE_ID INTEGER NOT NULL, - LATEST_OPERATION_RESPONSE LONGBLOB, - LAST_UPDATED_TIMESTAMP TIMESTAMP NULL, - PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_operation_response_device FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); -- END OF POLICY AND DEVICE GROUP MAPPING -- -- DASHBOARD RELATED VIEWS -- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index f956260d69..e22badf0b5 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -488,15 +488,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( ON UPDATE NO ACTION ); -CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO_OPERATION_RESPONSE ( - ID INT(11) NOT NULL AUTO_INCREMENT, - DEVICE_ID INTEGER NOT NULL, - LATEST_OPERATION_RESPONSE LONGBLOB, - LAST_UPDATED_TIMESTAMP TIMESTAMP NULL, - PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_operation_response_device FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); -- POLICY AND DEVICE GROUP MAPPING -- CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY ( ID INT NOT NULL AUTO_INCREMENT, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index e5ea76453f..e0597745d5 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -556,17 +556,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( ) ENGINE = InnoDB; -CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO_OPERATION_RESPONSE ( - ID INT(11) NOT NULL AUTO_INCREMENT, - DEVICE_ID INTEGER NOT NULL, - LATEST_OPERATION_RESPONSE LONGBLOB, - LAST_UPDATED_TIMESTAMP TIMESTAMP NULL, - PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_operation_response_device FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -) -ENGINE = InnoDB; - -- DASHBOARD RELATED VIEWS -- CREATE VIEW DEVICE_INFO_VIEW AS From 453fed77eda279a61bd646ca35ddfb44f20229ef Mon Sep 17 00:00:00 2001 From: shagihan Date: Mon, 15 Oct 2018 19:53:03 +0530 Subject: [PATCH 8/9] Remove unused imports --- .../device/details/mgt/impl/DeviceInformationManagerImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 6a27d35ae4..b27df8ebb0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -41,7 +41,6 @@ import java.sql.SQLException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Calendar; import java.util.HashMap; import java.util.List; From 12034d28f262a73545667454ca0cad49054f4f17 Mon Sep 17 00:00:00 2001 From: shagihan Date: Thu, 18 Oct 2018 10:38:44 +0530 Subject: [PATCH 9/9] Add default value checking when json payload did not have an entry. --- .../mgt/impl/DeviceInformationManagerImpl.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index b27df8ebb0..d990c0ab5a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -153,19 +153,19 @@ private DeviceInfo processDeviceInfo(DeviceInfo previousDeviceInfo, DeviceInfo n if (newDeviceInfo.getOsVersion().equals("")) { newDeviceInfo.setOsVersion(previousDeviceInfo.getOsVersion()); } - if (newDeviceInfo.getBatteryLevel() == 0.0) { + if (newDeviceInfo.getBatteryLevel() == -1D) { newDeviceInfo.setBatteryLevel(previousDeviceInfo.getBatteryLevel()); } - if (newDeviceInfo.getInternalTotalMemory() == 0.0) { + if (newDeviceInfo.getInternalTotalMemory() == -1D) { newDeviceInfo.setInternalTotalMemory(previousDeviceInfo.getInternalTotalMemory()); } - if (newDeviceInfo.getInternalAvailableMemory() == 0.0) { + if (newDeviceInfo.getInternalAvailableMemory() == -1D) { newDeviceInfo.setInternalAvailableMemory(previousDeviceInfo.getInternalAvailableMemory()); } - if (newDeviceInfo.getExternalTotalMemory() == 0.0) { + if (newDeviceInfo.getExternalTotalMemory() == -1D) { newDeviceInfo.setExternalTotalMemory(previousDeviceInfo.getExternalTotalMemory()); } - if (newDeviceInfo.getExternalAvailableMemory() == 0.0) { + if (newDeviceInfo.getExternalAvailableMemory() == -1D) { newDeviceInfo.setExternalAvailableMemory(previousDeviceInfo.getExternalAvailableMemory()); } if (newDeviceInfo.getOperator().equals("")) { @@ -183,10 +183,10 @@ private DeviceInfo processDeviceInfo(DeviceInfo previousDeviceInfo, DeviceInfo n if (newDeviceInfo.getCpuUsage() == 0.0) { newDeviceInfo.setCpuUsage(previousDeviceInfo.getCpuUsage()); } - if (newDeviceInfo.getTotalRAMMemory() == 0.0) { + if (newDeviceInfo.getTotalRAMMemory() == -1D) { newDeviceInfo.setTotalRAMMemory(previousDeviceInfo.getTotalRAMMemory()); } - if (newDeviceInfo.getAvailableRAMMemory() == 0.0) { + if (newDeviceInfo.getAvailableRAMMemory() == -1D) { newDeviceInfo.setAvailableRAMMemory(previousDeviceInfo.getAvailableRAMMemory()); } if (!newDeviceInfo.isPluggedIn()) {