Search in sources :

Example 1 with DeviceSnapshots

use of org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots in project kapua by eclipse.

the class Devices method getSnapshots.

/**
 * Returns the list of all the Snapshots available on the device.
 *
 * @return The list of Snapshot Ids.
 */
@GET
@Path("{deviceId}/snapshots")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public DeviceSnapshots getSnapshots(@PathParam("deviceId") String deviceId) {
    DeviceSnapshots deviceSnapshots = snapshotFactory.newDeviceSnapshots();
    try {
        KapuaId scopeId = KapuaSecurityUtils.getSession().getScopeId();
        KapuaId id = KapuaEid.parseShortId(deviceId);
        deviceSnapshots = snapshotService.get(scopeId, id, null);
    } catch (Throwable t) {
        handleException(t);
    }
    return returnNotNullEntity(deviceSnapshots);
}
Also used : KapuaId(org.eclipse.kapua.model.id.KapuaId) DeviceSnapshots(org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots)

Example 2 with DeviceSnapshots

use of org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots in project kapua by eclipse.

the class DeviceSnapshotManagementServiceImpl method get.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DeviceSnapshots 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
    SnapshotRequestChannel snapshotRequestChannel = new SnapshotRequestChannel();
    snapshotRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
    snapshotRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
    snapshotRequestChannel.setMethod(KapuaMethod.READ);
    SnapshotRequestPayload snapshotRequestPayload = new SnapshotRequestPayload();
    SnapshotRequestMessage snapshotRequestMessage = new SnapshotRequestMessage();
    snapshotRequestMessage.setScopeId(scopeId);
    snapshotRequestMessage.setDeviceId(deviceId);
    snapshotRequestMessage.setCapturedOn(new Date());
    snapshotRequestMessage.setPayload(snapshotRequestPayload);
    snapshotRequestMessage.setChannel(snapshotRequestChannel);
    // 
    // Do get
    DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(snapshotRequestMessage, timeout);
    SnapshotResponseMessage responseMessage = (SnapshotResponseMessage) deviceApplicationCall.send();
    SnapshotResponsePayload 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());
    }
    DeviceSnapshots deviceSnapshots = null;
    try {
        deviceSnapshots = XmlUtil.unmarshal(body, DeviceSnapshotsImpl.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(), DeviceSnapshotAppProperties.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 deviceSnapshots;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) SnapshotResponseMessage(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponseMessage) SnapshotRequestPayload(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestPayload) SnapshotRequestMessage(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestMessage) Date(java.util.Date) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) KapuaException(org.eclipse.kapua.KapuaException) SnapshotRequestChannel(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestChannel) SnapshotResponsePayload(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponsePayload) 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) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) DeviceSnapshots(org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots)

Example 3 with DeviceSnapshots

use of org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots in project kapua by eclipse.

the class GwtDeviceManagementServiceImpl method findDeviceSnapshots.

// 
// Snapshots
// 
@Override
public ListLoadResult<GwtSnapshot> findDeviceSnapshots(GwtDevice gwtDevice) throws GwtKapuaException {
    List<GwtSnapshot> snapshots = new ArrayList<GwtSnapshot>();
    try {
        // execute the command
        KapuaLocator locator = KapuaLocator.getInstance();
        DeviceSnapshotManagementService deviceSnapshotManagementService = locator.getService(DeviceSnapshotManagementService.class);
        KapuaId scopeId = KapuaEid.parseShortId(gwtDevice.getScopeId());
        KapuaId deviceId = KapuaEid.parseShortId(gwtDevice.getId());
        DeviceSnapshots snapshotIds = deviceSnapshotManagementService.get(scopeId, deviceId, null);
        // sort them by most recent first
        // sort the list alphabetically by service name
        Collections.sort(snapshotIds.getSnapshots(), new Comparator<DeviceSnapshot>() {

            @Override
            public int compare(DeviceSnapshot arg0, DeviceSnapshot arg1) {
                DeviceSnapshot snapshotId0 = arg0;
                DeviceSnapshot snapshotId1 = arg1;
                // Descending order
                return -1 * snapshotId0.getTimestamp().compareTo(snapshotId1.getTimestamp());
            }
        });
        for (DeviceSnapshot snapshot : snapshotIds.getSnapshots()) {
            Long timestamp = snapshot.getTimestamp();
            GwtSnapshot gwtSnapshot = new GwtSnapshot();
            gwtSnapshot.setCreatedOn(new Date(timestamp));
            snapshots.add(gwtSnapshot);
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return new BaseListLoadResult<GwtSnapshot>(snapshots);
}
Also used : GwtSnapshot(org.eclipse.kapua.app.console.shared.model.GwtSnapshot) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) ArrayList(java.util.ArrayList) Date(java.util.Date) BaseListLoadResult(com.extjs.gxt.ui.client.data.BaseListLoadResult) DeviceSnapshotManagementService(org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshotManagementService) KapuaId(org.eclipse.kapua.model.id.KapuaId) DeviceSnapshot(org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshot) DeviceSnapshots(org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots)

Example 4 with DeviceSnapshots

use of org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots in project kapua by eclipse.

the class TranslatorAppSnapshotKuraKapua method translateBody.

private void translateBody(SnapshotResponsePayload snapshotResponsePayload, String charEncoding, KuraSnapshotIds kuraSnapshotIdResult) throws TranslatorException {
    try {
        if (kuraSnapshotIdResult != null) {
            KapuaLocator locator = KapuaLocator.getInstance();
            DeviceSnapshotFactory deviceSnapshotFactory = locator.getFactory(DeviceSnapshotFactory.class);
            DeviceSnapshots deviceSnapshots = deviceSnapshotFactory.newDeviceSnapshots();
            List<Long> snapshotIds = kuraSnapshotIdResult.getSnapshotIds();
            for (Long snapshotId : snapshotIds) {
                DeviceSnapshot snapshot = deviceSnapshotFactory.newDeviceSnapshot();
                snapshot.setId(Long.toString(snapshotId));
                snapshot.setTimestamp(snapshotId);
                deviceSnapshots.getSnapshots().add(snapshot);
            }
            StringWriter sw = new StringWriter();
            XmlUtil.marshal(deviceSnapshots, sw);
            byte[] requestBody = sw.toString().getBytes(charEncoding);
            snapshotResponsePayload.setBody(requestBody);
        }
    } catch (Exception e) {
        throw new TranslatorException(TranslatorErrorCodes.INVALID_BODY, e, // null for now
        kuraSnapshotIdResult);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) StringWriter(java.io.StringWriter) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) DeviceSnapshot(org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshot) DeviceSnapshots(org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) KapuaException(org.eclipse.kapua.KapuaException) DeviceSnapshotFactory(org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshotFactory)

Aggregations

DeviceSnapshots (org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots)4 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)3 Date (java.util.Date)2 KapuaException (org.eclipse.kapua.KapuaException)2 KapuaId (org.eclipse.kapua.model.id.KapuaId)2 DeviceSnapshot (org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshot)2 BaseListLoadResult (com.extjs.gxt.ui.client.data.BaseListLoadResult)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 GwtSnapshot (org.eclipse.kapua.app.console.shared.model.GwtSnapshot)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 SnapshotRequestChannel (org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestChannel)1 SnapshotRequestMessage (org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestMessage)1 SnapshotRequestPayload (org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestPayload)1 SnapshotResponseMessage (org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponseMessage)1 SnapshotResponsePayload (org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponsePayload)1