Search in sources :

Example 1 with DeviceBundles

use of org.eclipse.kapua.service.device.management.bundle.DeviceBundles in project kapua by eclipse.

the class DeviceBundleManagementServiceImpl method get.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DeviceBundles get(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
    BundleRequestChannel bundleRequestChannel = new BundleRequestChannel();
    bundleRequestChannel.setAppName(DeviceBundleAppProperties.APP_NAME);
    bundleRequestChannel.setVersion(DeviceBundleAppProperties.APP_VERSION);
    bundleRequestChannel.setMethod(KapuaMethod.READ);
    BundleRequestPayload bundleRequestPayload = new BundleRequestPayload();
    BundleRequestMessage bundleRequestMessage = new BundleRequestMessage();
    bundleRequestMessage.setScopeId(scopeId);
    bundleRequestMessage.setDeviceId(deviceId);
    bundleRequestMessage.setCapturedOn(new Date());
    bundleRequestMessage.setPayload(bundleRequestPayload);
    bundleRequestMessage.setChannel(bundleRequestChannel);
    // 
    // Do get
    DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(bundleRequestMessage, timeout);
    BundleResponseMessage responseMessage = (BundleResponseMessage) deviceApplicationCall.send();
    // 
    // Parse the response
    BundleResponsePayload responsePayload = responseMessage.getPayload();
    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());
    }
    DeviceBundles deviceBundleList = null;
    try {
        deviceBundleList = XmlUtil.unmarshal(body, DeviceBundlesImpl.class);
    } catch (Exception e) {
        throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, body);
    }
    // 
    // Create event
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), DeviceBundleAppProperties.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 deviceBundleList;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) DeviceBundles(org.eclipse.kapua.service.device.management.bundle.DeviceBundles) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) BundleResponsePayload(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponsePayload) 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) BundleRequestMessage(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestMessage) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) DeviceManagementSetting(org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) BundleRequestChannel(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestChannel) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) BundleRequestPayload(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestPayload) BundleResponseMessage(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseMessage)

Example 2 with DeviceBundles

use of org.eclipse.kapua.service.device.management.bundle.DeviceBundles in project kapua by eclipse.

the class GwtDeviceManagementServiceImpl method findBundles.

// 
// Bundles
// 
@Override
public ListLoadResult<GwtGroupedNVPair> findBundles(GwtDevice device) throws GwtKapuaException {
    List<GwtGroupedNVPair> pairs = new ArrayList<GwtGroupedNVPair>();
    try {
        // get the configuration
        KapuaLocator locator = KapuaLocator.getInstance();
        DeviceBundleManagementService deviceBundleManagementService = locator.getService(DeviceBundleManagementService.class);
        KapuaId scopeId = KapuaEid.parseShortId(device.getScopeId());
        KapuaId id = KapuaEid.parseShortId(device.getId());
        DeviceBundles bundles = deviceBundleManagementService.get(scopeId, id, null);
        for (DeviceBundle bundle : bundles.getBundles()) {
            GwtGroupedNVPair pair = new GwtGroupedNVPair();
            pair.setId(String.valueOf(bundle.getId()));
            pair.setName(bundle.getName());
            pair.setStatus(toStateString(bundle));
            pair.setVersion(bundle.getVersion());
            pairs.add(pair);
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return new BaseListLoadResult<GwtGroupedNVPair>(pairs);
}
Also used : BaseListLoadResult(com.extjs.gxt.ui.client.data.BaseListLoadResult) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) DeviceBundles(org.eclipse.kapua.service.device.management.bundle.DeviceBundles) ArrayList(java.util.ArrayList) KapuaId(org.eclipse.kapua.model.id.KapuaId) DeviceBundle(org.eclipse.kapua.service.device.management.bundle.DeviceBundle) DeviceBundleManagementService(org.eclipse.kapua.service.device.management.bundle.DeviceBundleManagementService) GwtGroupedNVPair(org.eclipse.kapua.app.console.shared.model.GwtGroupedNVPair)

Example 3 with DeviceBundles

use of org.eclipse.kapua.service.device.management.bundle.DeviceBundles in project kapua by eclipse.

the class TranslatorAppBundleKuraKapua method translate.

private void translate(BundleResponsePayload bundleResponsePayload, String charEncoding, KuraBundles kuraBundles) throws KapuaException {
    try {
        KapuaLocator locator = KapuaLocator.getInstance();
        DeviceBundleFactory deviceBundleFactory = locator.getFactory(DeviceBundleFactory.class);
        KuraBundle[] kuraBundleArrays = kuraBundles.getBundles();
        DeviceBundles deviceBundles = deviceBundleFactory.newBundleListResult();
        List<DeviceBundle> deviceBundlesList = deviceBundles.getBundles();
        for (KuraBundle kuraBundle : kuraBundleArrays) {
            DeviceBundle deviceBundle = deviceBundleFactory.newDeviceBundle();
            deviceBundle.setId(kuraBundle.getId());
            deviceBundle.setName(kuraBundle.getName());
            deviceBundle.setVersion(kuraBundle.getVersion());
            deviceBundle.setState(kuraBundle.getState());
            deviceBundlesList.add(deviceBundle);
        }
        StringWriter sw = new StringWriter();
        XmlUtil.marshal(deviceBundles, sw);
        byte[] requestBody = sw.toString().getBytes(charEncoding);
        bundleResponsePayload.setBody(requestBody);
    } catch (Exception e) {
        throw new TranslatorException(TranslatorErrorCodes.INVALID_BODY, e, kuraBundles);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) DeviceBundles(org.eclipse.kapua.service.device.management.bundle.DeviceBundles) DeviceBundleFactory(org.eclipse.kapua.service.device.management.bundle.DeviceBundleFactory) StringWriter(java.io.StringWriter) KuraBundle(org.eclipse.kapua.service.device.call.kura.model.bundle.KuraBundle) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) DeviceBundle(org.eclipse.kapua.service.device.management.bundle.DeviceBundle) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) KapuaException(org.eclipse.kapua.KapuaException)

Aggregations

KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)3 DeviceBundles (org.eclipse.kapua.service.device.management.bundle.DeviceBundles)3 KapuaException (org.eclipse.kapua.KapuaException)2 DeviceBundle (org.eclipse.kapua.service.device.management.bundle.DeviceBundle)2 BaseListLoadResult (com.extjs.gxt.ui.client.data.BaseListLoadResult)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 GwtGroupedNVPair (org.eclipse.kapua.app.console.shared.model.GwtGroupedNVPair)1 KapuaId (org.eclipse.kapua.model.id.KapuaId)1 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)1 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)1 KuraBundle (org.eclipse.kapua.service.device.call.kura.model.bundle.KuraBundle)1 DeviceBundleFactory (org.eclipse.kapua.service.device.management.bundle.DeviceBundleFactory)1 DeviceBundleManagementService (org.eclipse.kapua.service.device.management.bundle.DeviceBundleManagementService)1 BundleRequestChannel (org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestChannel)1 BundleRequestMessage (org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestMessage)1 BundleRequestPayload (org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestPayload)1 BundleResponseMessage (org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseMessage)1 BundleResponsePayload (org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponsePayload)1