use of org.eclipse.kapua.service.device.registry.connection.DeviceConnectionQuery 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;
}
use of org.eclipse.kapua.service.device.registry.connection.DeviceConnectionQuery 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;
}
Aggregations