Add DeviceInfo event reporting

merge-requests/1/head
charitha 7 years ago
parent e103266d80
commit e256628ed7

@ -41,6 +41,7 @@ import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -50,7 +51,9 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
private DeviceDetailsDAO deviceDetailsDAO; private DeviceDetailsDAO deviceDetailsDAO;
private DeviceDAO deviceDAO; private DeviceDAO deviceDAO;
private static final Log log = LogFactory.getLog(DeviceInformationManagerImpl.class); private static final Log log = LogFactory.getLog(DeviceInformationManagerImpl.class);
private static final String EVENT_STREAM_DEFINITION = "org.wso2.iot.LocationStream"; 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";
public DeviceInformationManagerImpl() { public DeviceInformationManagerImpl() {
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
@ -70,6 +73,35 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
deviceDetailsDAO.addDeviceInformation(device.getId(), deviceInfo); deviceDetailsDAO.addDeviceInformation(device.getId(), deviceInfo);
deviceDetailsDAO.addDeviceProperties(deviceInfo.getDeviceDetailsMap(), device.getId()); deviceDetailsDAO.addDeviceProperties(deviceInfo.getDeviceDetailsMap(), device.getId());
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
if (DeviceManagerUtil.isPublishOperationResponseEnabled()) {
Object[] metaData = {device.getDeviceIdentifier(), device.getType()};
Object[] payload = new Object[]{
Calendar.getInstance().getTimeInMillis(),
deviceInfo.getDeviceDetailsMap().get("IMEI"),
deviceInfo.getDeviceDetailsMap().get("IMSI"),
deviceInfo.getDeviceModel(),
deviceInfo.getVendor(),
deviceInfo.getOsVersion(),
deviceInfo.getOsBuildDate(),
deviceInfo.getBatteryLevel(),
deviceInfo.getInternalTotalMemory(),
deviceInfo.getInternalAvailableMemory(),
deviceInfo.getExternalTotalMemory(),
deviceInfo.getExternalAvailableMemory(),
deviceInfo.getOperator(),
deviceInfo.getConnectionType(),
deviceInfo.getMobileSignalStrength(),
deviceInfo.getSsid(),
deviceInfo.getCpuUsage(),
deviceInfo.getTotalRAMMemory(),
deviceInfo.getAvailableRAMMemory(),
deviceInfo.isPluggedIn()
};
DeviceManagerUtil.getEventPublisherService().publishEvent(
DEVICE_INFO_EVENT_STREAM_DEFINITION, "1.0.0", metaData, new Object[0], payload
);
}
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction(); 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);
@ -83,6 +115,9 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceDetailsMgtException("Error occurred while updating the last update timestamp of the " + 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);
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
@ -90,20 +125,10 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
@Override @Override
public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException { public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException {
Device device; Device device = getDevice(deviceId);
try {
device = DeviceManagementDataHolder.getInstance().
getDeviceManagementProvider().getDevice(deviceId, false);
if (device == null) { 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");
}
return null; return null;
} }
} catch (DeviceManagementException e) {
throw new DeviceDetailsMgtException("Exception occurred while retrieving the device.", e);
}
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId()); DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId());
@ -111,7 +136,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
return deviceInfo; return deviceInfo;
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceDetailsMgtException("SQL error occurred while retrieving device from database.", e); throw new DeviceDetailsMgtException("SQL error occurred while retrieving device " + deviceId.toString()
+ "'s info from database.", e);
} catch (DeviceDetailsMgtDAOException e) { } catch (DeviceDetailsMgtDAOException e) {
throw new DeviceDetailsMgtException("Exception occurred while retrieving device details.", e); throw new DeviceDetailsMgtException("Exception occurred while retrieving device details.", e);
} finally { } finally {
@ -166,15 +192,15 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId());
deviceDetailsDAO.deleteDeviceLocation(deviceLocation.getDeviceId()); deviceDetailsDAO.deleteDeviceLocation(deviceLocation.getDeviceId());
deviceDetailsDAO.addDeviceLocation(deviceLocation); deviceDetailsDAO.addDeviceLocation(deviceLocation);
if (DeviceManagerUtil.isPublishLocationOperationResEnabled()) { if (DeviceManagerUtil.isPublishOperationResponseEnabled()) {
Object metaData[] = {device.getDeviceIdentifier(), device.getType()}; Object[] metaData = {device.getDeviceIdentifier(), device.getType()};
Object payload[] = new Object[]{ Object[] payload = new Object[]{
deviceLocation.getUpdatedTime().getTime(), deviceLocation.getUpdatedTime().getTime(),
deviceLocation.getLatitude(), deviceLocation.getLatitude(),
deviceLocation.getLongitude() deviceLocation.getLongitude()
}; };
DeviceManagerUtil.getEventPublisherService().publishEvent( DeviceManagerUtil.getEventPublisherService().publishEvent(
EVENT_STREAM_DEFINITION, "1.0.0", metaData, new Object[0], payload LOCATION_EVENT_STREAM_DEFINITION, "1.0.0", metaData, new Object[0], payload
); );
} }
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
@ -201,19 +227,10 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
@Override @Override
public DeviceLocation getDeviceLocation(DeviceIdentifier deviceId) throws DeviceDetailsMgtException { public DeviceLocation getDeviceLocation(DeviceIdentifier deviceId) throws DeviceDetailsMgtException {
Device device; Device device = getDevice(deviceId);
try {
device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId, false);
if (device == null) { 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");
}
return null; return null;
} }
} catch (DeviceManagementException e) {
throw new DeviceDetailsMgtException("Exception occurred while retrieving the device.", e);
}
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
return deviceDetailsDAO.getDeviceLocation(device.getId()); return deviceDetailsDAO.getDeviceLocation(device.getId());
@ -226,6 +243,23 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
} }
} }
private Device getDevice(DeviceIdentifier deviceId) throws DeviceDetailsMgtException {
Device device;
try {
device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId, false);
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");
}
return null;
}
} catch (DeviceManagementException e) {
throw new DeviceDetailsMgtException("Exception occurred while retrieving the device.", e);
}
return device;
}
@Override @Override
public List<DeviceLocation> getDeviceLocations( public List<DeviceLocation> getDeviceLocations(
List<DeviceIdentifier> deviceIdentifiers) throws DeviceDetailsMgtException { List<DeviceIdentifier> deviceIdentifiers) throws DeviceDetailsMgtException {

Loading…
Cancel
Save