Search in sources :

Example 1 with DeviceFactory

use of org.eclipse.kapua.service.device.registry.DeviceFactory in project kapua by eclipse.

the class GwtDeviceServiceImpl method createDevice.

public GwtDevice createDevice(GwtXSRFToken xsrfToken, GwtDeviceCreator gwtDeviceCreator) throws GwtKapuaException {
    // 
    // Checking validity of the given XSRF Token
    checkXSRFToken(xsrfToken);
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
    DeviceFactory deviceFactory = locator.getFactory(DeviceFactory.class);
    GwtDevice gwtDevice = null;
    try {
        KapuaId scopeId = KapuaEid.parseShortId(gwtDeviceCreator.getScopeId());
        DeviceCreator deviceCreator = deviceFactory.newCreator(scopeId, gwtDeviceCreator.getClientId());
        deviceCreator.setDisplayName(gwtDeviceCreator.getDisplayName());
        deviceCreator.setCredentialsMode(DeviceCredentialsMode.valueOf(gwtDeviceCreator.getGwtCredentialsTight().name()));
        deviceCreator.setCustomAttribute1(gwtDeviceCreator.getCustomAttribute1());
        deviceCreator.setCustomAttribute2(gwtDeviceCreator.getCustomAttribute2());
        deviceCreator.setCustomAttribute3(gwtDeviceCreator.getCustomAttribute3());
        deviceCreator.setCustomAttribute4(gwtDeviceCreator.getCustomAttribute4());
        deviceCreator.setCustomAttribute5(gwtDeviceCreator.getCustomAttribute5());
        Device device = deviceRegistryService.create(deviceCreator);
        gwtDevice = KapuaGwtConverter.convert(device);
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return gwtDevice;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtDevice(org.eclipse.kapua.app.console.shared.model.GwtDevice) DeviceCreator(org.eclipse.kapua.service.device.registry.DeviceCreator) GwtDeviceCreator(org.eclipse.kapua.app.console.shared.model.GwtDeviceCreator) Device(org.eclipse.kapua.service.device.registry.Device) GwtDevice(org.eclipse.kapua.app.console.shared.model.GwtDevice) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceFactory(org.eclipse.kapua.service.device.registry.DeviceFactory) KapuaId(org.eclipse.kapua.model.id.KapuaId)

Example 2 with DeviceFactory

use of org.eclipse.kapua.service.device.registry.DeviceFactory in project kapua by eclipse.

the class DeviceExporterServlet method internalDoGet.

private void internalDoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        // parameter extraction
        String format = request.getParameter("format");
        String scopeIdString = request.getParameter("scopeIdString");
        // data exporter
        DeviceExporter deviceExporter = null;
        if ("xls".equals(format)) {
            deviceExporter = new DeviceExporterExcel(response);
        } else if ("csv".equals(format)) {
            deviceExporter = new DeviceExporterCsv(response);
        } else {
            throw new IllegalArgumentException("format");
        }
        if (scopeIdString == null || scopeIdString.isEmpty()) {
            throw new IllegalArgumentException("account");
        }
        deviceExporter.init(scopeIdString);
        // 
        // get the devices and append them to the exporter
        KapuaLocator locator = KapuaLocator.getInstance();
        DeviceRegistryService drs = locator.getService(DeviceRegistryService.class);
        DeviceFactory drf = locator.getFactory(DeviceFactory.class);
        int resultsCount = 0;
        int offset = 0;
        // paginate through the matching message
        DeviceQuery dq = drf.newQuery(KapuaEid.parseShortId(scopeIdString));
        dq.setLimit(250);
        // Inserting filter parameter if specified
        AndPredicate andPred = new AndPredicate();
        String clientId = request.getParameter("clientId");
        if (clientId != null && !clientId.isEmpty()) {
            andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.CLIENT_ID, clientId, Operator.STARTS_WITH));
        }
        String displayName = request.getParameter("displayName");
        if (displayName != null && !displayName.isEmpty()) {
            andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.DISPLAY_NAME, displayName, Operator.STARTS_WITH));
        }
        String serialNumber = request.getParameter("serialNumber");
        if (serialNumber != null && !serialNumber.isEmpty()) {
            andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.SERIAL_NUMBER, serialNumber));
        }
        String deviceStatus = request.getParameter("deviceStatus");
        if (deviceStatus != null && !deviceStatus.isEmpty()) {
            andPred = andPred.and(new AttributePredicate<DeviceStatus>(DevicePredicates.STATUS, DeviceStatus.valueOf(deviceStatus)));
        }
        String sortAttribute = request.getParameter("sortAttribute");
        if (sortAttribute != null && !sortAttribute.isEmpty()) {
            String sortOrderString = request.getParameter("sortOrder");
            SortOrder sortOrder;
            if (sortOrderString != null && !sortOrderString.isEmpty()) {
                sortOrder = SortOrder.valueOf(sortOrderString);
            } else {
                sortOrder = SortOrder.ASCENDING;
            }
            if (sortAttribute.compareTo("CLIENT_ID") == 0) {
                dq.setSortCriteria(new FieldSortCriteria(DevicePredicates.CLIENT_ID, sortOrder));
            } else if (sortAttribute.compareTo("DISPLAY_NAME") == 0) {
                dq.setSortCriteria(new FieldSortCriteria(DevicePredicates.DISPLAY_NAME, sortOrder));
            } else if (sortAttribute.compareTo("LAST_EVENT_ON") == 0) {
                dq.setSortCriteria(new FieldSortCriteria(DevicePredicates.LAST_EVENT_ON, sortOrder));
            }
        }
        dq.setPredicate(andPred);
        KapuaListResult<Device> results = null;
        do {
            dq.setOffset(offset);
            results = drs.query(dq);
            deviceExporter.append(results);
            offset += results.getSize();
            resultsCount += results.getSize();
        } while (results.getSize() > 0);
        // Close things up
        deviceExporter.close();
    } catch (IllegalArgumentException iae) {
        response.sendError(400, "Illegal value for query parameter: " + iae.getMessage());
        return;
    } catch (KapuaEntityNotFoundException eenfe) {
        response.sendError(400, eenfe.getMessage());
        return;
    } catch (KapuaUnauthenticatedException eiae) {
        response.sendError(401, eiae.getMessage());
        return;
    } catch (KapuaIllegalAccessException eiae) {
        response.sendError(403, eiae.getMessage());
        return;
    } catch (Exception e) {
        s_logger.error("Error creating device export", e);
        throw new ServletException(e);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Device(org.eclipse.kapua.service.device.registry.Device) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) SortOrder(org.eclipse.kapua.commons.model.query.FieldSortCriteria.SortOrder) DeviceFactory(org.eclipse.kapua.service.device.registry.DeviceFactory) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) ServletException(javax.servlet.ServletException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) IOException(java.io.IOException) KapuaUnauthenticatedException(org.eclipse.kapua.KapuaUnauthenticatedException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) ServletException(javax.servlet.ServletException) FieldSortCriteria(org.eclipse.kapua.commons.model.query.FieldSortCriteria) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceQuery(org.eclipse.kapua.service.device.registry.DeviceQuery) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaUnauthenticatedException(org.eclipse.kapua.KapuaUnauthenticatedException)

Example 3 with DeviceFactory

use of org.eclipse.kapua.service.device.registry.DeviceFactory in project kapua by eclipse.

the class GwtDeviceServiceImpl method findDevices.

public PagingLoadResult<GwtDevice> findDevices(PagingLoadConfig loadConfig, String scopeIdString, GwtDeviceQueryPredicates predicates) throws GwtKapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
    DeviceFactory deviceFactory = locator.getFactory(DeviceFactory.class);
    List<GwtDevice> gwtDevices = new ArrayList<GwtDevice>();
    BasePagingLoadResult<GwtDevice> gwtResults;
    int totalResult = 0;
    try {
        BasePagingLoadConfig bplc = (BasePagingLoadConfig) loadConfig;
        DeviceQuery deviceQuery = deviceFactory.newQuery(KapuaEid.parseShortId(scopeIdString));
        deviceQuery.setLimit(bplc.getLimit() + 1);
        deviceQuery.setOffset(bplc.getOffset());
        AndPredicate andPred = new AndPredicate();
        if (predicates.getClientId() != null) {
            andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.CLIENT_ID, predicates.getUnescapedClientId(), Operator.STARTS_WITH));
        }
        if (predicates.getDisplayName() != null) {
            andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.DISPLAY_NAME, predicates.getUnescapedDisplayName(), Operator.STARTS_WITH));
        }
        if (predicates.getSerialNumber() != null) {
            andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.SERIAL_NUMBER, predicates.getUnescapedSerialNumber()));
        }
        if (predicates.getDeviceStatus() != null) {
            andPred = andPred.and(new AttributePredicate<DeviceStatus>(DevicePredicates.STATUS, DeviceStatus.valueOf(predicates.getDeviceStatus())));
        }
        if (predicates.getSortAttribute() != null) {
            SortOrder sortOrder = SortOrder.ASCENDING;
            if (predicates.getSortOrder().equals(SortOrder.DESCENDING.name())) {
                sortOrder = SortOrder.DESCENDING;
            }
            if (predicates.getSortAttribute().equals(GwtDeviceQueryPredicates.GwtSortAttribute.CLIENT_ID.name())) {
                deviceQuery.setSortCriteria(new FieldSortCriteria(DevicePredicates.CLIENT_ID, sortOrder));
            } else if (predicates.getSortAttribute().equals(GwtDeviceQueryPredicates.GwtSortAttribute.DISPLAY_NAME.name())) {
                deviceQuery.setSortCriteria(new FieldSortCriteria(DevicePredicates.DISPLAY_NAME, sortOrder));
            } else if (predicates.getSortAttribute().equals(GwtDeviceQueryPredicates.GwtSortAttribute.LAST_EVENT_ON.name())) {
                deviceQuery.setSortCriteria(new FieldSortCriteria(DevicePredicates.LAST_EVENT_ON, sortOrder));
            }
        } else {
            deviceQuery.setSortCriteria(new FieldSortCriteria(DevicePredicates.CLIENT_ID, SortOrder.ASCENDING));
        }
        deviceQuery.setPredicate(andPred);
        KapuaListResult<Device> devices = deviceRegistryService.query(deviceQuery);
        totalResult = (int) deviceRegistryService.count(deviceQuery);
        DeviceConnectionService deviceConnectionService = locator.getService(DeviceConnectionService.class);
        DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
        DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
        DeviceEventQuery eventQuery = deviceEventFactory.newQuery(deviceQuery.getScopeId());
        eventQuery.setLimit(1);
        eventQuery.setSortCriteria(new FieldSortCriteria(DeviceEventPredicates.RECEIVED_ON, SortOrder.DESCENDING));
        for (Device d : devices.getItems()) {
            DeviceConnection deviceConnection = deviceConnectionService.findByClientId(d.getScopeId(), d.getClientId());
            // Connection info
            GwtDevice gwtDevice = KapuaGwtConverter.convert(d);
            gwtDevice.setConnectionIp(deviceConnection.getClientIp());
            gwtDevice.setGwtDeviceConnectionStatus(deviceConnection.getStatus().name());
            gwtDevice.setLastEventOn(deviceConnection.getModifiedOn());
            // Event infos
            eventQuery.setPredicate(new AttributePredicate<KapuaId>(DeviceEventPredicates.DEVICE_ID, d.getId()));
            KapuaListResult<DeviceEvent> events = deviceEventService.query(eventQuery);
            if (!events.isEmpty()) {
                DeviceEvent lastEvent = events.getItem(0);
                gwtDevice.setLastEventType(lastEvent.getResource());
                gwtDevice.setLastEventOn(lastEvent.getReceivedOn());
            }
            gwtDevices.add(gwtDevice);
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    gwtResults = new BasePagingLoadResult<GwtDevice>(gwtDevices);
    gwtResults.setOffset(loadConfig.getOffset());
    gwtResults.setTotalLength(totalResult);
    return gwtResults;
}
Also used : GwtDeviceEvent(org.eclipse.kapua.app.console.shared.model.GwtDeviceEvent) DeviceEvent(org.eclipse.kapua.service.device.registry.event.DeviceEvent) BasePagingLoadConfig(com.extjs.gxt.ui.client.data.BasePagingLoadConfig) ArrayList(java.util.ArrayList) DeviceEventQuery(org.eclipse.kapua.service.device.registry.event.DeviceEventQuery) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) GwtDevice(org.eclipse.kapua.app.console.shared.model.GwtDevice) FieldSortCriteria(org.eclipse.kapua.commons.model.query.FieldSortCriteria) KapuaId(org.eclipse.kapua.model.id.KapuaId) DeviceQuery(org.eclipse.kapua.service.device.registry.DeviceQuery) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Device(org.eclipse.kapua.service.device.registry.Device) GwtDevice(org.eclipse.kapua.app.console.shared.model.GwtDevice) KapuaAndPredicate(org.eclipse.kapua.model.query.predicate.KapuaAndPredicate) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) SortOrder(org.eclipse.kapua.commons.model.query.FieldSortCriteria.SortOrder) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) DeviceFactory(org.eclipse.kapua.service.device.registry.DeviceFactory) DeviceConnection(org.eclipse.kapua.service.device.registry.connection.DeviceConnection) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceConnectionService(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService)

Example 4 with DeviceFactory

use of org.eclipse.kapua.service.device.registry.DeviceFactory in project kapua by eclipse.

the class GwtDeviceServiceImpl method getNumOfDevices.

public long getNumOfDevices(String scopeIdString) throws GwtKapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
    DeviceFactory deviceFactory = locator.getFactory(DeviceFactory.class);
    try {
        DeviceQuery query = deviceFactory.newQuery(KapuaEid.parseShortId(scopeIdString));
        return deviceRegistryService.count(query);
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
        return 0;
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceFactory(org.eclipse.kapua.service.device.registry.DeviceFactory) DeviceQuery(org.eclipse.kapua.service.device.registry.DeviceQuery)

Example 5 with DeviceFactory

use of org.eclipse.kapua.service.device.registry.DeviceFactory in project kapua by eclipse.

the class DeviceLifeCycleServiceImpl method birth.

@Override
public void birth(KapuaId connectionId, KapuaBirthMessage message) throws KapuaException {
    KapuaBirthPayload payload = message.getPayload();
    KapuaBirthChannel channel = message.getChannel();
    KapuaId scopeId = message.getScopeId();
    KapuaId deviceId = message.getDeviceId();
    // 
    // Device update
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
    Device device = null;
    if (deviceId == null) {
        String clientId = channel.getClientId();
        DeviceFactory deviceFactory = locator.getFactory(DeviceFactory.class);
        DeviceCreator deviceCreator = deviceFactory.newCreator(scopeId, clientId);
        deviceCreator.setDisplayName(payload.getDisplayName());
        deviceCreator.setSerialNumber(payload.getSerialNumber());
        deviceCreator.setModelId(payload.getModelId());
        deviceCreator.setImei(payload.getModemImei());
        deviceCreator.setImsi(payload.getModemImsi());
        deviceCreator.setIccid(payload.getModemIccid());
        deviceCreator.setBiosVersion(payload.getBiosVersion());
        deviceCreator.setFirmwareVersion(payload.getFirmwareVersion());
        deviceCreator.setOsVersion(payload.getOsVersion());
        deviceCreator.setJvmVersion(payload.getJvmVersion());
        deviceCreator.setOsgiFrameworkVersion(payload.getContainerFrameworkVersion());
        deviceCreator.setApplicationIdentifiers(payload.getApplicationIdentifiers());
        deviceCreator.setAcceptEncoding(payload.getAcceptEncoding());
        deviceCreator.setCredentialsMode(DeviceCredentialsMode.LOOSE);
        device = deviceRegistryService.create(deviceCreator);
    } else {
        device = deviceRegistryService.find(scopeId, deviceId);
        device.setDisplayName(payload.getDisplayName());
        device.setSerialNumber(payload.getSerialNumber());
        device.setModelId(payload.getModelId());
        device.setImei(payload.getModemImei());
        device.setImsi(payload.getModemImsi());
        device.setIccid(payload.getModemIccid());
        device.setBiosVersion(payload.getBiosVersion());
        device.setFirmwareVersion(payload.getFirmwareVersion());
        device.setOsVersion(payload.getOsVersion());
        device.setJvmVersion(payload.getJvmVersion());
        device.setOsgiFrameworkVersion(payload.getContainerFrameworkVersion());
        device.setApplicationIdentifiers(payload.getApplicationIdentifiers());
        device.setAcceptEncoding(payload.getAcceptEncoding());
        deviceRegistryService.update(device);
    }
    // 
    // Event create
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, device.getId(), message.getReceivedOn(), "BIRTH");
    deviceEventCreator.setEventMessage(payload.toDisplayString());
    // TODO check this change
    deviceEventCreator.setResponseCode(KapuaResponseCode.ACCEPTED);
    deviceEventCreator.setSentOn(message.getSentOn());
    KapuaPosition position = message.getPosition();
    if (position != null) {
        deviceEventCreator.setPosition(position);
    }
    deviceEventService.create(deviceEventCreator);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) KapuaPosition(org.eclipse.kapua.message.KapuaPosition) Device(org.eclipse.kapua.service.device.registry.Device) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) DeviceFactory(org.eclipse.kapua.service.device.registry.DeviceFactory) KapuaBirthChannel(org.eclipse.kapua.message.device.lifecycle.KapuaBirthChannel) DeviceCreator(org.eclipse.kapua.service.device.registry.DeviceCreator) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaBirthPayload(org.eclipse.kapua.message.device.lifecycle.KapuaBirthPayload)

Aggregations

KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)5 DeviceFactory (org.eclipse.kapua.service.device.registry.DeviceFactory)5 DeviceRegistryService (org.eclipse.kapua.service.device.registry.DeviceRegistryService)5 Device (org.eclipse.kapua.service.device.registry.Device)4 KapuaId (org.eclipse.kapua.model.id.KapuaId)3 DeviceQuery (org.eclipse.kapua.service.device.registry.DeviceQuery)3 GwtDevice (org.eclipse.kapua.app.console.shared.model.GwtDevice)2 FieldSortCriteria (org.eclipse.kapua.commons.model.query.FieldSortCriteria)2 SortOrder (org.eclipse.kapua.commons.model.query.FieldSortCriteria.SortOrder)2 AndPredicate (org.eclipse.kapua.commons.model.query.predicate.AndPredicate)2 AttributePredicate (org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)2 DeviceCreator (org.eclipse.kapua.service.device.registry.DeviceCreator)2 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)2 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)2 BasePagingLoadConfig (com.extjs.gxt.ui.client.data.BasePagingLoadConfig)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ServletException (javax.servlet.ServletException)1 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)1 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)1