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; }
public DeviceVO convertTo(String deviceId) { DeviceVO device = new DeviceVO(); if (deviceId != null){ device.setDeviceId(deviceId); } if (this.data != null){ device.setData(this.data); } if (this.name != null){ device.setName(this.name); } if (this.networkId != null){ device.setNetworkId(this.networkId); } if (this.deviceTypeId != null){ device.setDeviceTypeId(this.deviceTypeId); } if (this.blocked != null){ device.setBlocked(this.blocked); } return device; } }
public void unregisterDevice(DeviceVO device) { final Filter deviceFilter = new Filter(device.getNetworkId(), device.getDeviceTypeId(), device.getDeviceId(), null, null); subscriberTable.row(deviceFilter.getFirstKey()).clear(); }
if (existingDevice == null) { DeviceVO device = deviceUpdate.convertTo(deviceId); device.setNetworkId(networkId); device.setDeviceTypeId(deviceTypeId); if (device.getBlocked() == null) { device.setBlocked(false); 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()); existingDevice.setData(deviceUpdate.getData().orElse(null)); existingDevice.setNetworkId(networkId); existingDevice.setDeviceTypeId(deviceTypeId); existingDevice.setName(deviceUpdate.getName().get()); existingDevice.setBlocked(deviceUpdate.getBlocked().get());
@Transactional public DeviceNotification deviceSave(String deviceId, DeviceUpdate deviceUpdate) { logger.debug("Device save executed for device update: id {}", deviceId); Long networkId = deviceUpdate.getNetworkId().isPresent() ? deviceUpdate.getNetworkId().get() : null; //TODO [requires a lot of details] DeviceVO existingDevice = deviceDao.findById(deviceId); if (existingDevice == null) { DeviceVO device = deviceUpdate.convertTo(deviceId); device.setNetworkId(networkId); deviceDao.persist(device); return ServerResponsesFactory.createNotificationForDevice(device, SpecialNotifications.DEVICE_ADD); } else { if (deviceUpdate.getData().isPresent()){ existingDevice.setData(deviceUpdate.getData().get()); } if (deviceUpdate.getNetworkId().isPresent()){ existingDevice.setNetworkId(networkId); } if (deviceUpdate.getBlocked().isPresent()){ existingDevice.setBlocked(deviceUpdate.getBlocked().get()); } deviceDao.merge(existingDevice); return ServerResponsesFactory.createNotificationForDevice(existingDevice, SpecialNotifications.DEVICE_UPDATE); } }
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; } }
return deviceTypes.contains(device.getDeviceTypeId()); return networks.contains(device.getNetworkId()); return networks.contains(device.getNetworkId()) && deviceTypes.contains(device.getDeviceTypeId());
if (permittedNetworks != null && !permittedNetworks.isEmpty()) { Set<DeviceVO> allowed = deviceType.getDevices().stream() .filter(device -> permittedNetworks.contains(device.getNetworkId())) .collect(Collectors.toSet()); deviceType.setDevices(allowed);
@Transactional(propagation = Propagation.NOT_SUPPORTED) public NetworkWithUsersAndDevicesVO getWithDevices(@NotNull Long networkId) { HivePrincipal principal = (HivePrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); Set<Long> permittedNetworks = principal.getNetworkIds(); Set<Long> permittedDeviceTypes = principal.getDeviceTypeIds(); Optional<NetworkWithUsersAndDevicesVO> result = of(principal) .flatMap(pr -> { if (pr.getUser() != null) { return of(pr.getUser()); } else { return empty(); } }).flatMap(user -> { Long idForFiltering = user.isAdmin() ? null : user.getId(); List<NetworkWithUsersAndDevicesVO> found = networkDao.getNetworksByIdsAndUsers(idForFiltering, Collections.singleton(networkId), permittedNetworks); return found.stream().findFirst(); }).map(network -> { if (permittedDeviceTypes != null && !permittedDeviceTypes.isEmpty()) { Set<DeviceVO> allowed = network.getDevices().stream() .filter(device -> permittedDeviceTypes.contains(device.getDeviceTypeId())) .collect(Collectors.toSet()); network.setDevices(allowed); } return network; }); return result.orElse(null); }
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 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; } }
if (names != null) { filters = names.stream().map(name -> new Filter(device.getNetworkId(), device.getDeviceTypeId(), deviceId, eventName, name)) .collect(Collectors.toSet()); } else { filters = Collections.singleton(new Filter(device.getNetworkId(), device.getDeviceTypeId(), deviceId, eventName, null));
if (device.getNetworkId() == null) { logger.error("notification/insert. No network specified for device with Device ID = {}", deviceId); throw new HiveException(String.format(Messages.DEVICE_IS_NOT_CONNECTED_TO_NETWORK, deviceId), SC_FORBIDDEN);
public static DeviceVO convertToVo(Device dc) { DeviceVO vo = null; if (dc != null) { vo = new DeviceVO(); vo.setId(dc.getId()); vo.setDeviceId(dc.getDeviceId()); vo.setName(dc.getName()); vo.setData(dc.getData()); vo.setBlocked(dc.getBlocked()); NetworkVO networkVO = Network.convertNetwork(dc.getNetwork()); vo.setNetworkId(networkVO.getId()); DeviceTypeVO deviceTypeVO = DeviceType.convertDeviceType(dc.getDeviceType()); vo.setDeviceTypeId(deviceTypeVO.getId()); } return vo; }
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; }
@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); }); }
asyncResponse.resume(response); } else { if (device.getNetworkId() == null) { logger.warn("DeviceNotification insert proceed with error. FORBIDDEN: Device {} is not connected to network.", deviceId); Response response = ResponseFactory.response(FORBIDDEN, new ErrorResponse(FORBIDDEN.getStatusCode(),
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()); command.setIsUpdated(false);
commandService.findOne(Long.valueOf(commandId), device.getDeviceId(), returnUpdatedCommands) .thenApply(command -> { if (!command.isPresent()) {
commandService.findOne(Long.valueOf(commandId), device.getDeviceId()) .thenAccept(command -> { if (!command.isPresent()) {