Search in sources :

Example 1 with DevicePackagesImpl

use of org.eclipse.kapua.service.device.management.packages.model.internal.DevicePackagesImpl in project kapua by eclipse.

the class DevicePackageManagementServiceImpl method getInstalled.

@Override
public DevicePackages getInstalled(KapuaId scopeId, KapuaId deviceId, Long timeout) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(deviceId, "deviceId");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(DeviceManagementDomain.DEVICE_MANAGEMENT, Actions.read, scopeId));
    // 
    // Prepare the request
    PackageRequestChannel packageRequestChannel = new PackageRequestChannel();
    packageRequestChannel.setAppName(PackageAppProperties.APP_NAME);
    packageRequestChannel.setVersion(PackageAppProperties.APP_VERSION);
    packageRequestChannel.setMethod(KapuaMethod.READ);
    packageRequestChannel.setPackageResource(null);
    PackageRequestPayload packageRequestPayload = new PackageRequestPayload();
    PackageRequestMessage packageRequestMessage = new PackageRequestMessage();
    packageRequestMessage.setScopeId(scopeId);
    packageRequestMessage.setDeviceId(deviceId);
    packageRequestMessage.setCapturedOn(new Date());
    packageRequestMessage.setPayload(packageRequestPayload);
    packageRequestMessage.setChannel(packageRequestChannel);
    // 
    // Do get
    @SuppressWarnings({ "rawtypes", "unchecked" }) DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(packageRequestMessage, timeout);
    PackageResponseMessage responseMessage = (PackageResponseMessage) deviceApplicationCall.send();
    // 
    // Parse the response
    PackageResponsePayload responsePayload = responseMessage.getPayload();
    DevicePackages devicePackages;
    if (responsePayload.getBody() != null) {
        DeviceManagementSetting config = DeviceManagementSetting.getInstance();
        String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING);
        String body = null;
        try {
            body = new String(responsePayload.getBody(), charEncoding);
        } catch (Exception e) {
            throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, responsePayload.getBody());
        }
        try {
            devicePackages = XmlUtil.unmarshal(body, DevicePackagesImpl.class);
        } catch (Exception e) {
            throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, body);
        }
    } else {
        devicePackages = new DevicePackagesImpl();
    }
    // 
    // Create event
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), PackageAppProperties.APP_NAME.getValue());
    deviceEventCreator.setPosition(responseMessage.getPosition());
    deviceEventCreator.setSentOn(responseMessage.getSentOn());
    deviceEventCreator.setAction(KapuaMethod.READ);
    deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
    deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
    deviceEventService.create(deviceEventCreator);
    return devicePackages;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PackageResponseMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage) DevicePackagesImpl(org.eclipse.kapua.service.device.management.packages.model.internal.DevicePackagesImpl) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) Date(java.util.Date) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) KapuaException(org.eclipse.kapua.KapuaException) DevicePackages(org.eclipse.kapua.service.device.management.packages.model.DevicePackages) PackageRequestChannel(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestChannel) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) PackageResponsePayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PackageRequestMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestMessage) DeviceManagementSetting(org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) PackageRequestPayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestPayload)

Aggregations

Date (java.util.Date)1 KapuaException (org.eclipse.kapua.KapuaException)1 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)1 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)1 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)1 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)1 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)1 DeviceManagementSetting (org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting)1 PackageRequestChannel (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestChannel)1 PackageRequestMessage (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestMessage)1 PackageRequestPayload (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestPayload)1 PackageResponseMessage (org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage)1 PackageResponsePayload (org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload)1 DevicePackages (org.eclipse.kapua.service.device.management.packages.model.DevicePackages)1 DevicePackagesImpl (org.eclipse.kapua.service.device.management.packages.model.internal.DevicePackagesImpl)1 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)1 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)1 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)1