Search in sources :

Example 1 with DeviceConnectionService

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

the class GwtDeviceServiceImpl method findDeviceProfile.

public ListLoadResult<GwtGroupedNVPair> findDeviceProfile(String scopeIdString, String clientId) throws GwtKapuaException {
    List<GwtGroupedNVPair> pairs = new ArrayList<GwtGroupedNVPair>();
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceRegistryService drs = locator.getService(DeviceRegistryService.class);
    DeviceConnectionService dcs = locator.getService(DeviceConnectionService.class);
    try {
        KapuaId scopeId = KapuaEid.parseShortId(scopeIdString);
        Device device = drs.findByClientId(scopeId, clientId);
        if (device != null) {
            pairs.add(new GwtGroupedNVPair("devInfo", "devStatus", device.getStatus().toString()));
            DeviceConnection deviceConnection = dcs.findByClientId(scopeId, device.getClientId());
            DeviceConnectionStatus connectionStatus = null;
            if (deviceConnection != null) {
                connectionStatus = deviceConnection.getStatus();
            } else {
                connectionStatus = DeviceConnectionStatus.DISCONNECTED;
            }
            pairs.add(new GwtGroupedNVPair("devInfo", "devConnectionStatus", connectionStatus.toString()));
            pairs.add(new GwtGroupedNVPair("devInfo", "devClientId", device.getClientId()));
            pairs.add(new GwtGroupedNVPair("devInfo", "devDisplayName", device.getDisplayName()));
            String lastEventType = device.getLastEventType() != null ? device.getLastEventType().toString() : "";
            pairs.add(new GwtGroupedNVPair("devInfo", "devLastEventType", lastEventType));
            if (device.getLastEventOn() != null) {
                pairs.add(new GwtGroupedNVPair("devInfo", "devLastEventOn", String.valueOf(device.getLastEventOn().getTime())));
            } else {
                pairs.add(new GwtGroupedNVPair("devInfo", "devLastEventOn", null));
            }
            if (device.getPreferredUserId() != null) {
                pairs.add(new GwtGroupedNVPair("devInfo", "devLastUserUsed", device.getPreferredUserId().getShortId()));
            }
            pairs.add(new GwtGroupedNVPair("devInfo", "devApps", device.getApplicationIdentifiers()));
            pairs.add(new GwtGroupedNVPair("devInfo", "devAccEnc", device.getAcceptEncoding()));
            pairs.add(new GwtGroupedNVPair("devAttributesInfo", "devCustomAttribute1", device.getCustomAttribute1()));
            pairs.add(new GwtGroupedNVPair("devAttributesInfo", "devCustomAttribute2", device.getCustomAttribute2()));
            pairs.add(new GwtGroupedNVPair("devAttributesInfo", "devCustomAttribute3", device.getCustomAttribute3()));
            pairs.add(new GwtGroupedNVPair("devAttributesInfo", "devCustomAttribute4", device.getCustomAttribute4()));
            pairs.add(new GwtGroupedNVPair("devAttributesInfo", "devCustomAttribute5", device.getCustomAttribute5()));
            // Credentials tight
            pairs.add(new GwtGroupedNVPair("devSecurity", "devSecurityCredentialsTight", GwtDeviceCredentialsTight.valueOf(device.getCredentialsMode().name()).getLabel()));
            pairs.add(new GwtGroupedNVPair("devSecurity", "devSecurityAllowCredentialsChange", device.getPreferredUserId() == null ? "Yes" : "No"));
            pairs.add(new GwtGroupedNVPair("devHw", "devModelName", device.getModelId()));
            pairs.add(new GwtGroupedNVPair("devHw", "devModelId", device.getModelId()));
            pairs.add(new GwtGroupedNVPair("devHw", "devSerialNumber", device.getSerialNumber()));
            pairs.add(new GwtGroupedNVPair("devSw", "devFirmwareVersion", device.getFirmwareVersion()));
            pairs.add(new GwtGroupedNVPair("devSw", "devBiosVersion", device.getBiosVersion()));
            pairs.add(new GwtGroupedNVPair("devSw", "devOsVersion", device.getOsVersion()));
            pairs.add(new GwtGroupedNVPair("devJava", "devJvmVersion", device.getJvmVersion()));
            pairs.add(new GwtGroupedNVPair("netInfo", "netConnIp", deviceConnection.getClientIp()));
            pairs.add(new GwtGroupedNVPair("gpsInfo", "gpsLat", String.valueOf(device.getGpsLatitude())));
            pairs.add(new GwtGroupedNVPair("gpsInfo", "gpsLong", String.valueOf(device.getGpsLongitude())));
            pairs.add(new GwtGroupedNVPair("modemInfo", "modemImei", device.getImei()));
            pairs.add(new GwtGroupedNVPair("modemInfo", "modemImsi", device.getImsi()));
            pairs.add(new GwtGroupedNVPair("modemInfo", "modemIccid", device.getIccid()));
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return new BaseListLoadResult<GwtGroupedNVPair>(pairs);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Device(org.eclipse.kapua.service.device.registry.Device) GwtDevice(org.eclipse.kapua.app.console.shared.model.GwtDevice) ArrayList(java.util.ArrayList) GwtGroupedNVPair(org.eclipse.kapua.app.console.shared.model.GwtGroupedNVPair) BaseListLoadResult(com.extjs.gxt.ui.client.data.BaseListLoadResult) DeviceConnectionStatus(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionStatus) DeviceConnection(org.eclipse.kapua.service.device.registry.connection.DeviceConnection) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceConnectionService(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService) KapuaId(org.eclipse.kapua.model.id.KapuaId)

Example 2 with DeviceConnectionService

use of org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService 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 3 with DeviceConnectionService

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

the class GwtDeviceServiceImpl method getNumOfDevicesConnected.

public Map<String, Integer> getNumOfDevicesConnected(String scopeIdString) throws GwtKapuaException {
    KapuaId scopeId = KapuaEid.parseShortId(scopeIdString);
    Map<String, Integer> devicesStatus = new HashMap<String, Integer>();
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceConnectionService dcs = locator.getService(DeviceConnectionService.class);
    DeviceConnectionFactory deviceConnectionFactory = locator.getFactory(DeviceConnectionFactory.class);
    try {
        DeviceConnectionQuery query = deviceConnectionFactory.newQuery(scopeId);
        query.setPredicate(new AttributePredicate<DeviceConnectionStatus>(DeviceConnectionPredicates.CONNECTION_STATUS, DeviceConnectionStatus.CONNECTED));
        int count = new Long(dcs.count(query)).intValue();
        if (count > 0) {
            devicesStatus.put("connected", count);
        }
        query.setPredicate(new AttributePredicate<DeviceConnectionStatus>(DeviceConnectionPredicates.CONNECTION_STATUS, DeviceConnectionStatus.DISCONNECTED));
        count = new Long(dcs.count(query)).intValue();
        if (count > 0) {
            devicesStatus.put("disconnected", count);
        }
        query.setPredicate(new AttributePredicate<DeviceConnectionStatus>(DeviceConnectionPredicates.CONNECTION_STATUS, DeviceConnectionStatus.MISSING));
        count = new Long(dcs.count(query)).intValue();
        if (count > 0) {
            devicesStatus.put("missing", count);
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
        return null;
    }
    return devicesStatus;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) HashMap(java.util.HashMap) DeviceConnectionStatus(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionStatus) DeviceConnectionFactory(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionFactory) DeviceConnectionQuery(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionQuery) KapuaId(org.eclipse.kapua.model.id.KapuaId) DeviceConnectionService(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService)

Example 4 with DeviceConnectionService

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

the class KapuaGwtConverter method convert.

public static GwtDevice convert(Device device) throws KapuaException {
    GwtDevice gwtDevice = new GwtDevice();
    gwtDevice.setId(device.getId().getShortId());
    gwtDevice.setScopeId(device.getScopeId().getShortId());
    gwtDevice.setGwtDeviceStatus(device.getStatus().toString());
    gwtDevice.setClientId(device.getClientId());
    gwtDevice.setDisplayName(device.getDisplayName());
    gwtDevice.setModelId(device.getModelId());
    gwtDevice.setSerialNumber(device.getSerialNumber());
    gwtDevice.setFirmwareVersion(device.getFirmwareVersion());
    gwtDevice.setBiosVersion(device.getBiosVersion());
    gwtDevice.setOsVersion(device.getOsVersion());
    gwtDevice.setJvmVersion(device.getJvmVersion());
    gwtDevice.setOsgiVersion(device.getOsgiFrameworkVersion());
    gwtDevice.setAcceptEncoding(device.getAcceptEncoding());
    gwtDevice.setApplicationIdentifiers(device.getApplicationIdentifiers());
    gwtDevice.setGpsLatitude(device.getGpsLatitude());
    gwtDevice.setGpsLongitude(device.getGpsLongitude());
    gwtDevice.setLastEventOn(device.getLastEventOn());
    gwtDevice.setIccid(device.getIccid());
    gwtDevice.setImei(device.getImei());
    gwtDevice.setImsi(device.getImsi());
    String lastEventType = device.getLastEventType() != null ? device.getLastEventType().name() : "";
    gwtDevice.setLastEventType(lastEventType);
    // custom Attributes
    gwtDevice.setCustomAttribute1(device.getCustomAttribute1());
    gwtDevice.setCustomAttribute2(device.getCustomAttribute2());
    gwtDevice.setCustomAttribute3(device.getCustomAttribute3());
    gwtDevice.setCustomAttribute4(device.getCustomAttribute4());
    gwtDevice.setCustomAttribute5(device.getCustomAttribute5());
    gwtDevice.setOptlock(device.getOptlock());
    // Device connection
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceConnectionService deviceConnectionService = locator.getService(DeviceConnectionService.class);
    DeviceConnectionFactory deviceConnectionFactory = locator.getFactory(DeviceConnectionFactory.class);
    DeviceConnectionQuery query = deviceConnectionFactory.newQuery(device.getScopeId());
    KapuaAndPredicate andPredicate = new AndPredicate();
    andPredicate = andPredicate.and(new AttributePredicate<String>(DeviceConnectionPredicates.CLIENT_ID, device.getClientId()));
    // andPredicate = andPredicate.and(new AttributePredicate<DeviceConnectionStatus[]>(DeviceConnectionPredicates.CONNECTION_STATUS,
    // new DeviceConnectionStatus[] { DeviceConnectionStatus.CONNECTED, DeviceConnectionStatus.MISSING }));
    query.setPredicate(andPredicate);
    KapuaListResult<DeviceConnection> deviceConnections = deviceConnectionService.query(query);
    if (!deviceConnections.isEmpty()) {
        DeviceConnection connection = deviceConnections.getItem(0);
        gwtDevice.setGwtDeviceConnectionStatus(connection.getStatus().toString());
        gwtDevice.setConnectionIp(connection.getClientIp());
        gwtDevice.setDeviceUserId(connection.getUserId().getShortId());
    }
    return gwtDevice;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtDevice(org.eclipse.kapua.app.console.shared.model.GwtDevice) DeviceConnectionFactory(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionFactory) KapuaAndPredicate(org.eclipse.kapua.model.query.predicate.KapuaAndPredicate) DeviceConnectionQuery(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionQuery) KapuaAndPredicate(org.eclipse.kapua.model.query.predicate.KapuaAndPredicate) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) DeviceConnection(org.eclipse.kapua.service.device.registry.connection.DeviceConnection) DeviceConnectionService(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)

Aggregations

KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)4 DeviceConnectionService (org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService)4 GwtDevice (org.eclipse.kapua.app.console.shared.model.GwtDevice)3 KapuaId (org.eclipse.kapua.model.id.KapuaId)3 DeviceConnection (org.eclipse.kapua.service.device.registry.connection.DeviceConnection)3 ArrayList (java.util.ArrayList)2 AndPredicate (org.eclipse.kapua.commons.model.query.predicate.AndPredicate)2 AttributePredicate (org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)2 KapuaAndPredicate (org.eclipse.kapua.model.query.predicate.KapuaAndPredicate)2 Device (org.eclipse.kapua.service.device.registry.Device)2 DeviceRegistryService (org.eclipse.kapua.service.device.registry.DeviceRegistryService)2 DeviceConnectionFactory (org.eclipse.kapua.service.device.registry.connection.DeviceConnectionFactory)2 DeviceConnectionQuery (org.eclipse.kapua.service.device.registry.connection.DeviceConnectionQuery)2 DeviceConnectionStatus (org.eclipse.kapua.service.device.registry.connection.DeviceConnectionStatus)2 BaseListLoadResult (com.extjs.gxt.ui.client.data.BaseListLoadResult)1 BasePagingLoadConfig (com.extjs.gxt.ui.client.data.BasePagingLoadConfig)1 HashMap (java.util.HashMap)1 GwtDeviceEvent (org.eclipse.kapua.app.console.shared.model.GwtDeviceEvent)1 GwtGroupedNVPair (org.eclipse.kapua.app.console.shared.model.GwtGroupedNVPair)1 FieldSortCriteria (org.eclipse.kapua.commons.model.query.FieldSortCriteria)1