@Override public URI getDeviceIdentifier(IDevice device) throws SiteWhereException { return createUri(SITEWHERE_PROTOCOL + "device/" + device.getToken()); }
/** * Gets devices in all groups that have the given role. Duplicates are removed. * * @param groupRole * @param criteria * @param deviceManagement * @param assetManagement * @return * @throws SiteWhereException */ public static Collection<IDevice> getDevicesInGroupsWithRole(String groupRole, IDeviceSearchCriteria criteria, IDeviceManagement deviceManagement, IAssetManagement assetManagement) throws SiteWhereException { Map<String, IDevice> devices = new HashMap<String, IDevice>(); ISearchCriteria groupCriteria = new SearchCriteria(1, 0); ISearchResults<IDeviceGroup> groups = deviceManagement.listDeviceGroupsWithRole(groupRole, groupCriteria); for (IDeviceGroup group : groups.getResults()) { List<IDevice> groupDevices = getDevicesInGroup(group, criteria, deviceManagement, assetManagement); for (IDevice groupDevice : groupDevices) { devices.put(groupDevice.getToken(), groupDevice); } } return devices.values(); } }
@Override public MqttParameters extractDeliveryParameters(IDeviceNestingContext nesting, IDeviceAssignment assignment, IDeviceCommandExecution execution) throws SiteWhereException { MqttParameters params = new MqttParameters(); Map<String, String> values = new HashMap<>(); values.put("tenant", getTenantEngine().getTenant().getToken()); values.put("device", nesting.getGateway().getToken()); values.put("assignment", assignment.getToken()); String commandTopic = StrSubstitutor.replace(getCommandTopicExpr(), values); params.setCommandTopic(commandTopic); String systemTopic = StrSubstitutor.replace(getSystemTopicExpr(), values); params.setSystemTopic(systemTopic); return params; }
/** * Get the list of unique devices in a group. (Recurses into subgroups and * removes duplicates). Also prevents loops in group references. * * @param groupId * @param deviceManagement * @param assetManagement * @param devices * @param groups * @throws SiteWhereException */ protected static void getDevicesInGroup(UUID groupId, IDeviceManagement deviceManagement, IAssetManagement assetManagement, Map<String, IDevice> devices, Map<String, IDeviceGroup> groups) throws SiteWhereException { ISearchResults<IDeviceGroupElement> elements = deviceManagement.listDeviceGroupElements(groupId, SearchCriteria.ALL); DeviceGroupElementMarshalHelper helper = new DeviceGroupElementMarshalHelper(deviceManagement); for (IDeviceGroupElement element : elements.getResults()) { MarshaledDeviceGroupElement inflated = helper.convert(element, assetManagement); if (inflated.getDevice() != null) { IDevice device = inflated.getDevice(); devices.put(device.getToken(), device); } else if (inflated.getDeviceGroup() != null) { IDeviceGroup nested = inflated.getDeviceGroup(); // Prevent loops. if (groups.get(nested.getToken()) != null) { } } } }
/** * Assure that a bucket is created for the given assignment. * * @param assignmentToken * @return * @throws SiteWhereException */ protected DeviceAssignment assureBucket(UUID assignmentId) throws SiteWhereException { DeviceAssignment cached = assignmentsById.get(assignmentId); if (cached != null) { return cached; } IDeviceAssignment assignment = getDeviceManagement().getDeviceAssignment(assignmentId); if (assignment == null) { throw new SiteWhereException("Assignment not found."); } DeviceAssignmentMarshalHelper helper = new DeviceAssignmentMarshalHelper(getDeviceManagement()); helper.setIncludeAsset(false); helper.setIncludeDevice(true); MarshaledDeviceAssignment converted = helper.convert(assignment, null); createBucket(converted.getToken(), converted.getAssetName() + " (" + converted.getDevice().getToken() + ")"); assignmentsById.put(assignmentId, converted); return converted; }
deviceTokens.add(match.getToken());
@Override public void handleDeviceRegistration(IDeviceRegistrationPayload registration) throws SiteWhereException { Authentication previous = SecurityContextHolder.getContext().getAuthentication(); try { Authentication system = getMicroservice().getSystemUser() .getAuthenticationForTenant(getTenantEngine().getTenant()); SecurityContextHolder.getContext().setAuthentication(system); IDevice device = getOrCreateDevice(registration); // Find assignment metadata that should be associated. ICustomer customer = getCustomerFor(registration); IArea area = getAreaFor(registration); // Make sure device is assigned. if (device.getDeviceAssignmentId() == null) { getLogger().debug("Handling unassigned device for registration."); DeviceAssignmentCreateRequest assnCreate = new DeviceAssignmentCreateRequest(); assnCreate.setDeviceToken(device.getToken()); if (customer != null) { assnCreate.setCustomerToken(customer.getToken()); } if (area != null) { assnCreate.setAreaToken(area.getToken()); } getDeviceManagement().createDeviceAssignment(assnCreate); } boolean isNewRegistration = (device != null); sendRegistrationAck(registration.getDeviceToken(), isNewRegistration); } finally { SecurityContextHolder.getContext().setAuthentication(previous); } }
getLogger().debug("Built context: Device: " + context.getDevice().getToken() + " Device type: " + context.getDeviceType().getToken());
nested.setParentDeviceToken(device.getToken()); management.updateDevice(mapped.getId(), nested);
IDeviceElementMapping mapping = DeviceUtils.findMappingFor(parent, target.getToken());