Set device status to active when poll operations

merge-requests/7/head
manoj 10 years ago
parent 6263a550ff
commit f3dd402e4b

@ -24,12 +24,15 @@ import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; 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.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
@ -57,6 +60,7 @@ public class OperationManagerImpl implements OperationManager {
private OperationMappingDAO operationMappingDAO; private OperationMappingDAO operationMappingDAO;
private OperationDAO operationDAO; private OperationDAO operationDAO;
private DeviceDAO deviceDAO; private DeviceDAO deviceDAO;
private DeviceTypeDAO deviceTypeDAO;
public OperationManagerImpl() { public OperationManagerImpl() {
commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO(); commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO();
@ -66,6 +70,7 @@ public class OperationManagerImpl implements OperationManager {
operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO(); operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO();
operationDAO = OperationManagementDAOFactory.getOperationDAO(); operationDAO = OperationManagementDAOFactory.getOperationDAO();
deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
} }
@Override @Override
@ -169,6 +174,21 @@ public class OperationManagerImpl implements OperationManager {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
device = deviceDAO.getDevice(deviceId, tenantId); device = deviceDAO.getDevice(deviceId, tenantId);
if (device.getEnrolmentInfo().getStatus() !=null && !device.getEnrolmentInfo().getStatus().equals(
EnrolmentInfo.Status.ACTIVE)){
try {
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.updateDeviceEnrolmentInfo(device,
EnrolmentInfo.Status.ACTIVE);
}catch (DeviceManagementException deviceMgtEx){
String errorMsg = "Error occurred while update enrol status: "+deviceId.toString();
log.error(errorMsg, deviceMgtEx);
throw new OperationManagementException(errorMsg, deviceMgtEx);
}
}
if (device == null) { if (device == null) {
throw new OperationManagementException("Device not found for given device " + throw new OperationManagementException("Device not found for given device " +
"Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType()); "Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType());
@ -225,6 +245,20 @@ public class OperationManagerImpl implements OperationManager {
} }
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO
.getNextOperation(device.getId()); .getNextOperation(device.getId());
if (device.getEnrolmentInfo().getStatus() !=null && !device.getEnrolmentInfo().getStatus().equals(
EnrolmentInfo.Status.ACTIVE)){
try {
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.updateDeviceEnrolmentInfo(device,
EnrolmentInfo.Status.ACTIVE);
}catch (DeviceManagementException deviceMgtEx){
String errorMsg = "Error occurred while update enrol status: "+deviceId.toString();
log.error(errorMsg, deviceMgtEx);
throw new OperationManagementException(errorMsg, deviceMgtEx);
}
}
if (dtoOperation != null) { if (dtoOperation != null) {
if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND
.equals(dtoOperation.getType())) { .equals(dtoOperation.getType())) {

@ -87,4 +87,5 @@ public interface DeviceManagementProviderService extends DeviceManager, LicenseM
*/ */
List<Application> getApplicationListForDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; List<Application> getApplicationListForDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException;
void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) throws DeviceManagementException;
} }

@ -137,7 +137,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); DeviceType type = deviceTypeDAO.getDeviceType(device.getType());
deviceDAO.updateDevice(type.getId(),device, tenantId); deviceDAO.updateDevice(type.getId(),device, tenantId);
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
try { try {
@ -652,6 +651,22 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return null; return null;
} }
@Override
public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType());
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.getEnrolmentInfo().setStatus(status);
deviceDAO.updateDevice(deviceType.getId(), device, tenantId);
}catch (DeviceManagementDAOException deviceDaoEx){
String errorMsg = "Error occured update device enrolment status:"+device.getId();
log.error(errorMsg, deviceDaoEx);
throw new DeviceManagementException(errorMsg, deviceDaoEx);
}
}
@Override @Override
public void registerDeviceManagementService(DeviceManagementService deviceManagementService) { public void registerDeviceManagementService(DeviceManagementService deviceManagementService) {
try { try {

Loading…
Cancel
Save