public static DeviceNotification createNotificationForDevice(DeviceVO device, String notificationName) { DeviceNotification notification = new DeviceNotification(); notification.setId(Math.abs(new Random().nextInt())); // TODO: remove this when id generation will be moved to backend notification.setNotification(notificationName); notification.setDeviceId(device.getDeviceId()); Gson gson = GsonFactory.createGson(DEVICE_PUBLISHED); JsonElement deviceAsJson = gson.toJsonTree(device); JsonStringWrapper wrapperOverDevice = new JsonStringWrapper(deviceAsJson.toString()); notification.setParameters(wrapperOverDevice); return notification; } }
public static DeviceNotification createNotificationForDevice(DeviceVO device, String notificationName) { DeviceNotification notification = new DeviceNotification(); notification.setId(Math.abs(new Random().nextInt())); // TODO: remove this when id generation will be moved to backend notification.setNotification(notificationName); notification.setDeviceId(device.getDeviceId()); Gson gson = GsonFactory.createGson(DEVICE_PUBLISHED); JsonElement deviceAsJson = gson.toJsonTree(device); JsonStringWrapper wrapperOverDevice = new JsonStringWrapper(deviceAsJson.toString()); notification.setParameters(wrapperOverDevice); return notification; } }
@HiveWebsocketAuth @PreAuthorize("isAuthenticated() and hasPermission(#deviceId, 'UPDATE_DEVICE_COMMAND')") public void processCommandUpdate(String deviceId, JsonObject request, WebSocketSession session) { HivePrincipal principal = (HivePrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); final Long id = gson.fromJson(request.get(COMMAND_ID), Long.class); final DeviceCommandUpdate commandUpdate = gson .fromJson(request.getAsJsonObject(COMMAND), DeviceCommandUpdate.class); logger.debug("command/update requested for session: {}. Device ID: {}. Command id: {}", session, deviceId, id); if (id == null) { logger.debug("command/update canceled for session: {}. Command id is not provided", session); throw new HiveException(Messages.COMMAND_ID_REQUIRED, SC_BAD_REQUEST); } if (deviceId == null) { throw new HiveException(DEVICE_ID_REQUIRED, SC_BAD_REQUEST); } DeviceVO deviceVO = deviceService.findByIdWithPermissionsCheck(deviceId, principal); if (deviceVO == null) { throw new HiveException(String.format(DEVICE_NOT_FOUND, deviceId), SC_NOT_FOUND); } commandService.findOne(id, deviceVO.getDeviceId()) .thenAccept(optionalCommand -> { optionalCommand.map(deviceCommand -> commandService.update(deviceCommand, commandUpdate)) .orElseThrow(() -> new HiveException(String.format(COMMAND_NOT_FOUND, id), SC_NOT_FOUND)); }).thenAccept(whenUpdated -> { logger.debug("command/update proceed successfully for session: {}. Device ID: {}. Command id: {}", session, deviceId, id); clientHandler.sendMessage(request, new WebSocketResponse(), session); }); }
commandService.findOne(Long.valueOf(commandId), device.getDeviceId(), returnUpdatedCommands) .thenApply(command -> { if (!command.isPresent()) {
public void unregisterDevice(DeviceVO device) { final Filter deviceFilter = new Filter(device.getNetworkId(), device.getDeviceTypeId(), device.getDeviceId(), null, null); subscriberTable.row(deviceFilter.getFirstKey()).clear(); }
commandService.findOne(Long.valueOf(commandId), device.getDeviceId()) .thenAccept(command -> { if (!command.isPresent()) {
public CompletableFuture<DeviceCommand> insert(DeviceCommandWrapper commandWrapper, DeviceVO device, UserVO user) { hiveValidator.validate(commandWrapper); DeviceCommand command = convertWrapperToCommand(commandWrapper, device, user); CompletableFuture<Response> future = new CompletableFuture<>(); rpcClient.call(Request.newBuilder() .withBody(new CommandInsertRequest(command)) .withPartitionKey(device.getDeviceId()) .build(), new ResponseConsumer(future)); return future.thenApply(r -> ((CommandInsertResponse) r.getBody()).getDeviceCommand()); }
public CompletableFuture<DeviceNotification> insert(final DeviceNotification notification, final DeviceVO device) { hiveValidator.validate(notification); CompletableFuture<Response> future = new CompletableFuture<>(); rpcClient.call(Request.newBuilder() .withBody(new NotificationInsertRequest(notification)) .withPartitionKey(device.getDeviceId()) .build(), new ResponseConsumer(future)); return future.thenApply(r -> r.getBody().cast(NotificationInsertResponse.class).getDeviceNotification()); }
private DeviceCommand convertWrapperToCommand(DeviceCommandWrapper commandWrapper, DeviceVO device, UserVO user) { DeviceCommand command = new DeviceCommand(); command.setId(Math.abs(new Random().nextInt())); command.setDeviceId(device.getDeviceId()); command.setNetworkId(device.getNetworkId()); command.setDeviceTypeId(device.getDeviceTypeId());
private void waitForCommand(DeviceVO device, final String commandId, final long timeout, DeviceCommand command, final AsyncResponse asyncResponse) { String deviceId = device.getDeviceId(); if (!command.getDeviceId().equals(device.getDeviceId())) { logger.warn("DeviceCommand wait request failed. BAD REQUEST: Command with id = {} was not sent for device with id = {}", commandId, deviceId);
public DeviceNotification convertWrapperToNotification(DeviceNotificationWrapper notificationSubmit, DeviceVO device) { DeviceNotification notification = new DeviceNotification(); notification.setId(Math.abs(new Random().nextInt())); notification.setDeviceId(device.getDeviceId()); notification.setNetworkId(device.getNetworkId()); notification.setDeviceTypeId(device.getDeviceTypeId()); if (notificationSubmit.getTimestamp() == null) { notification.setTimestamp(timestampService.getDate()); } else { notification.setTimestamp(notificationSubmit.getTimestamp()); } notification.setNotification(notificationSubmit.getNotification()); notification.setParameters(notificationSubmit.getParameters()); return notification; }
public CompletableFuture<Pair<Long, DeviceCommand>> sendSubscribeToUpdateRequest(final long commandId, final DeviceVO device, BiConsumer<DeviceCommand, Long> callback) { CompletableFuture<Pair<Long, DeviceCommand>> future = new CompletableFuture<>(); final Long subscriptionId = idGenerator.generate(); Consumer<Response> responseConsumer = response -> { Action resAction = response.getBody().getAction(); if (resAction.equals(Action.COMMAND_UPDATE_SUBSCRIBE_RESPONSE)) { future.complete(Pair.of(response.getBody().cast(CommandUpdateSubscribeResponse.class).getSubscriptionId(), response.getBody().cast(CommandUpdateSubscribeResponse.class).getDeviceCommand())); } else if (resAction.equals(COMMAND_UPDATE_EVENT)) { callback.accept(response.getBody().cast(CommandUpdateEvent.class).getDeviceCommand(), subscriptionId); } else { logger.warn("Unknown action received from backend {}", resAction); } }; Filter filter = new Filter(device.getNetworkId(), device.getDeviceTypeId(), Long.toString(commandId), COMMAND_UPDATE_EVENT.name(), null); rpcClient.call(Request.newBuilder() .withBody(new CommandUpdateSubscribeRequest(commandId, device.getDeviceId(), subscriptionId, filter)) .build(), responseConsumer); return future; }
return ServerResponsesFactory.createNotificationForDevice(device, SpecialNotifications.DEVICE_ADD); } else { if (!userService.hasAccessToDevice(user, existingDevice.getDeviceId())) { logger.error("User {} has no access to device {}", user.getId(), existingDevice.getId()); throw new HiveException(Messages.NO_ACCESS_TO_DEVICE, FORBIDDEN.getStatusCode());
public static Device convertToEntity(DeviceVO dc) { Device entity = null; if (dc != null) { entity = new Device(); entity.setId(dc.getId()); entity.setDeviceId(dc.getDeviceId()); entity.setName(dc.getName()); entity.setData(dc.getData()); entity.setBlocked(dc.getBlocked()); Network network = new Network(); network.setId(dc.getNetworkId()); entity.setNetwork(network); DeviceType deviceType = new DeviceType(); deviceType.setId(dc.getDeviceTypeId()); entity.setDeviceType(deviceType); } return entity; }