@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 void unregisterDevice(DeviceVO device) { final Filter deviceFilter = new Filter(device.getNetworkId(), device.getDeviceTypeId(), device.getDeviceId(), null, null); subscriberTable.row(deviceFilter.getFirstKey()).clear(); }
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));
return deviceTypes.contains(device.getDeviceTypeId()); return networks.contains(device.getNetworkId()) && deviceTypes.contains(device.getDeviceTypeId());
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; }
command.setDeviceId(device.getDeviceId()); command.setNetworkId(device.getNetworkId()); command.setDeviceTypeId(device.getDeviceTypeId()); command.setIsUpdated(false);
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 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; }