Search in sources :

Example 1 with DeviceConnectionFactory

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

use of org.eclipse.kapua.service.device.registry.connection.DeviceConnectionFactory 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)2 DeviceConnectionFactory (org.eclipse.kapua.service.device.registry.connection.DeviceConnectionFactory)2 DeviceConnectionQuery (org.eclipse.kapua.service.device.registry.connection.DeviceConnectionQuery)2 DeviceConnectionService (org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService)2 HashMap (java.util.HashMap)1 GwtDevice (org.eclipse.kapua.app.console.shared.model.GwtDevice)1 AndPredicate (org.eclipse.kapua.commons.model.query.predicate.AndPredicate)1 AttributePredicate (org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)1 KapuaId (org.eclipse.kapua.model.id.KapuaId)1 KapuaAndPredicate (org.eclipse.kapua.model.query.predicate.KapuaAndPredicate)1 DeviceConnection (org.eclipse.kapua.service.device.registry.connection.DeviceConnection)1 DeviceConnectionStatus (org.eclipse.kapua.service.device.registry.connection.DeviceConnectionStatus)1