Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the size of device info payload. #1261

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
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;

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,27 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro
try {
Device device = DeviceManagementDataHolder.getInstance().
getDeviceManagementProvider().getDevice(deviceId, false);

DeviceInfo newDeviceInfo = null;
DeviceManagementDAOFactory.beginTransaction();
DeviceInfo previousDeviceInfo = deviceDetailsDAO
.getDeviceInformation(device.getId(), device.getEnrolmentInfo().getId());
Map<String, String> 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());
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();

Expand Down Expand Up @@ -106,7 +120,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);
Expand All @@ -116,7 +131,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);
Expand All @@ -125,6 +140,68 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro
}
}

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<String, String> newDeviceDetailsMap = newDeviceInfo.getDeviceDetailsMap();
Map<String, String> previousDeviceDetailsMap = previousDeviceInfo.getDeviceDetailsMap();
for (String eachKey : previousDeviceDetailsMap.keySet()) {
if (!newDeviceDetailsMap.containsKey(eachKey)) {
newDeviceDetailsMap.put(eachKey, previousDeviceDetailsMap.get(eachKey));
}
}
return newDeviceInfo;
}

@Override
public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException {
Device device = getDevice(deviceId);
Expand All @@ -141,7 +218,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 {
Expand Down Expand Up @@ -256,7 +333,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;
}
Expand Down Expand Up @@ -286,7 +363,7 @@ public List<DeviceLocation> 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();
}
}
Expand Down