/** * Copy information from SPI into Mongo {@link Document}. * * @param source * @param target */ public static void toDocument(IDevice source, Document target) { target.append(PROP_DEVICE_TYPE_ID, source.getDeviceTypeId()); target.append(PROP_PARENT_DEVICE_ID, source.getParentDeviceId()); target.append(PROP_STATUS, source.getStatus()); target.append(PROP_COMMENTS, source.getComments()); target.append(PROP_ASSIGNMENT_ID, source.getDeviceAssignmentId()); // Save nested list of mappings. List<Document> mappings = new ArrayList<Document>(); for (IDeviceElementMapping mapping : source.getDeviceElementMappings()) { mappings.add(MongoDeviceElementMapping.toDocument(mapping)); } target.append(PROP_DEVICE_ELEMENT_MAPPINGS, mappings); MongoPersistentEntity.toDocument(source, target); }
@Override public URI getDeviceIdentifier(IDevice device) throws SiteWhereException { return createUri(SITEWHERE_PROTOCOL + "device/" + device.getToken()); }
/** * Common logic executed before a device is deleted. * * @param device * @param deviceManagement * @throws SiteWhereException */ public static void deviceDeleteLogic(IDevice device, IDeviceManagement deviceManagement) throws SiteWhereException { if (device.getDeviceAssignmentId() != null) { throw new SiteWhereSystemException(ErrorCode.DeviceCanNotBeDeletedIfAssigned, ErrorLevel.ERROR); } DeviceAssignmentSearchCriteria criteria = new DeviceAssignmentSearchCriteria(1, 1); criteria.setDeviceId(device.getId()); ISearchResults<IDeviceAssignment> assignments = deviceManagement.listDeviceAssignments(criteria); if (assignments.getNumResults() > 0) { throw new SiteWhereSystemException(ErrorCode.DeviceDeleteHasAssignments, ErrorLevel.ERROR); } }
public Builder(IDevice api) { request.setSiteToken(api.getSiteToken()); request.setSpecificationToken(api.getSpecificationToken()); request.setHardwareId(api.getHardwareId()); request.setStatus(api.getStatus()); request.setComments(api.getComments()); request.setParentHardwareId(api.getParentHardwareId()); if (api.getDeviceElementMappings() != null) { request.setDeviceElementMappings(new ArrayList<DeviceElementMapping>()); request.getDeviceElementMappings().addAll(api.getDeviceElementMappings()); } if (api.getMetadata() != null) { request.setMetadata(new HashMap<String, String>()); request.getMetadata().putAll(api.getMetadata()); } }
/** * Copy information from SPI into Mongo {@link Document}. * * @param source * @param target */ public static void toDocument(IDevice source, Document target) { target.append(PROP_HARDWARE_ID, source.getHardwareId()); target.append(PROP_SITE_TOKEN, source.getSiteToken()); target.append(PROP_SPECIFICATION_TOKEN, source.getSpecificationToken()); target.append(PROP_PARENT_HARDWARE_ID, source.getParentHardwareId()); target.append(PROP_COMMENTS, source.getComments()); target.append(PROP_ASSIGNMENT_TOKEN, source.getAssignmentToken()); // Save nested list of mappings. List<Document> mappings = new ArrayList<Document>(); for (IDeviceElementMapping mapping : source.getDeviceElementMappings()) { mappings.add(MongoDeviceElementMapping.toDocument(mapping)); } target.append(PROP_DEVICE_ELEMENT_MAPPINGS, mappings); MongoSiteWhereEntity.toDocument(source, target); MongoMetadataProvider.toDocument(source, target); }
if (mapped.getParentDeviceId() != null) { throw new SiteWhereSystemException(ErrorCode.DeviceParentMappingExists, ErrorLevel.ERROR); IDeviceType deviceType = management.getDeviceType(device.getDeviceTypeId()); DeviceTypeUtils.getDeviceSlotByPath(deviceType, request.getDeviceElementSchemaPath()); List<IDeviceElementMapping> existing = device.getDeviceElementMappings(); List<DeviceElementMapping> newMappings = new ArrayList<DeviceElementMapping>(); for (IDeviceElementMapping mapping : existing) { nested.setParentDeviceToken(device.getToken()); management.updateDevice(mapped.getId(), nested); IDevice updated = management.updateDevice(device.getId(), update); return updated;
context.setDeviceId(device.getId()); context.setDeviceTypeId(device.getDeviceTypeId()); context.setParentDeviceId(device.getParentDeviceId()); context.setDeviceStatus(device.getStatus()); context.setDeviceMetadata(device.getMetadata()); context.setAssignmentStatus(assignment.getStatus()); context.setAssignmentMetadata(assignment.getMetadata()); getTenantEngine().getEnrichedEventsProducer().send(device.getToken(), message); getTenantEngine().getEnrichedCommandInvocationsProducer().send(device.getToken(), message);
if (mapped.getParentHardwareId() != null) { throw new SiteWhereSystemException(ErrorCode.DeviceParentMappingExists, ErrorLevel.ERROR); IDeviceSpecification specification = management.getDeviceSpecificationByToken(device.getSpecificationToken()); DeviceSpecificationUtils.getDeviceSlotByPath(specification, request.getDeviceElementSchemaPath()); List<IDeviceElementMapping> existing = device.getDeviceElementMappings(); List<DeviceElementMapping> newMappings = new ArrayList<DeviceElementMapping>(); for (IDeviceElementMapping mapping : existing) { management.updateDevice(mapped.getHardwareId(), nested);
if (!device.getDeviceTypeId().equals(deviceType.getId())) { continue; if (criteria.isExcludeAssigned() && (device.getDeviceAssignmentId() != null)) { continue; if ((criteria.getStartDate() != null) && (device.getCreatedDate().before(criteria.getStartDate()))) { continue; if ((criteria.getEndDate() != null) && (device.getCreatedDate().after(criteria.getEndDate()))) { continue;
@Override public IDeviceAssignment getCurrentDeviceAssignment(UUID deviceId) throws SiteWhereException { IDevice device = getApiDeviceById(deviceId); if (device.getDeviceAssignmentId() == null) { return null; } return getDeviceAssignment(device.getDeviceAssignmentId()); }
if (!device.getSpecificationToken().equals(criteria.getSpecificationToken())) { continue; if (!device.getSiteToken().equals(criteria.getSiteToken())) { continue; if (criteria.isExcludeAssigned() && (device.getAssignmentToken() != null)) { continue; if ((criteria.getStartDate() != null) && (device.getCreatedDate().before(criteria.getStartDate()))) { continue; if ((criteria.getEndDate() != null) && (device.getCreatedDate().after(criteria.getEndDate()))) { continue;
/** * Common logic for creating a device assignment from a request. * * @param request * @param customer * @param area * @param asset * @param device * @return * @throws SiteWhereException */ public static DeviceAssignment deviceAssignmentCreateLogic(IDeviceAssignmentCreateRequest request, ICustomer customer, IArea area, IAsset asset, IDevice device) throws SiteWhereException { DeviceAssignment assignment = new DeviceAssignment(); Persistence.entityCreateLogic(request, assignment); assignment.setCustomerId(customer != null ? customer.getId() : null); assignment.setAreaId(area != null ? area.getId() : null); assignment.setAssetId(asset != null ? asset.getId() : null); assignment.setDeviceId(device.getId()); assignment.setDeviceTypeId(device.getDeviceTypeId()); assignment.setActiveDate(new Date()); assignment.setStatus(request.getStatus() != null ? request.getStatus() : DeviceAssignmentStatus.Active); return assignment; }
@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); } }
/** * Get {@link ICommandDestination} for device based on specification token * associated with the device. * * @param nesting * @return * @throws SiteWhereException */ protected ICommandDestination<?, ?> getDestinationForDevice(IDeviceNestingContext nesting) throws SiteWhereException { UUID deviceTypeId = nesting.getGateway().getDeviceTypeId(); String destinationId = mappings.get(deviceTypeId); if (destinationId == null) { if (getDefaultDestination() != null) { destinationId = getDefaultDestination(); } else { throw new SiteWhereException("No command destination mapping for device type: " + deviceTypeId); } } ICommandDestination<?, ?> destination = getCommandDestinationsManager().getCommandDestinations() .get(destinationId); if (destination == null) { throw new SiteWhereException("No destination found for destination id: " + destinationId); } return destination; }
List<IDeviceElementMapping> existing = device.getDeviceElementMappings(); List<DeviceElementMapping> newMappings = new ArrayList<DeviceElementMapping>(); IDeviceElementMapping match = null; DeviceCreateRequest nested = new DeviceCreateRequest(); nested.setRemoveParentHardwareId(true); management.updateDevice(mapped.getId(), nested); IDevice updated = management.updateDevice(device.getId(), update); return updated;
@Override public boolean isFiltered(IDeviceEvent event, IDevice device, IDeviceAssignment assignment) throws SiteWhereException { if (getSpecificationToken().equals(device.getSpecificationToken())) { return (getOperation() != FilterOperation.Include); } return (getOperation() == FilterOperation.Include); }
deviceCreate.setMetadata(request.getMetadata()); device = SiteWhere.getServer().getDeviceManagement(getTenant()).createDevice(deviceCreate); } else if (!device.getSpecificationToken().equals(request.getSpecificationToken())) { LOGGER.info("Found existing device registration, but specification does not match."); sendInvalidSpecification(request.getHardwareId()); if (device.getAssignmentToken() == null) { LOGGER.debug("Handling unassigned device for registration."); DeviceAssignmentCreateRequest assnCreate = new DeviceAssignmentCreateRequest(); assnCreate.setDeviceHardwareId(device.getHardwareId()); assnCreate.setAssignmentType(DeviceAssignmentType.Unassociated); SiteWhere.getServer().getDeviceManagement(getTenant()).createDeviceAssignment(assnCreate);
if (target.getParentDeviceId() == null) { nested.setGateway(target); return nested; IDevice parent = getDeviceManagement(tenant).getDevice(target.getParentDeviceId()); if (parent == null) { throw new SiteWhereException("Parent device reference points to device that does not exist."); IDeviceElementMapping mapping = DeviceUtils.findMappingFor(parent, target.getToken());
/** * Update an existing device. * * @param device * @param builder * @return * @throws SiteWhereException */ public IDevice update(IDevice device, DeviceCreateRequest.Builder builder) throws SiteWhereException { return getDeviceManagement().updateDevice(device.getId(), builder.build()); }
@Override public IDeviceAssignment getCurrentDeviceAssignment(IDevice device) throws SiteWhereException { if (device.getAssignmentToken() == null) { return null; } return assertApiDeviceAssignment(device.getAssignmentToken()); }