/** * Generate a '.proto' file for a device type. * * @param deviceType * @return * @throws SiteWhereException */ public static String getProtoForDeviceType(IDeviceType deviceType, IDeviceManagement deviceManagement) throws SiteWhereException { StringBuffer buffer = new StringBuffer(); DeviceCommandSearchCriteria criteria = new DeviceCommandSearchCriteria(1, 0); criteria.setDeviceTypeId(deviceType.getId()); ISearchResults<IDeviceCommand> commands = deviceManagement.listDeviceCommands(criteria); generateProto(deviceType, commands.getResults(), buffer); return buffer.toString(); }
@Override public SearchResults<IDevice> listDevices(IDeviceSearchCriteria criteria) throws SiteWhereException { MongoCollection<Document> devices = getMongoClient().getDevicesCollection(); Document dbCriteria = new Document(); if (criteria.isExcludeAssigned()) { dbCriteria.put(MongoDevice.PROP_ASSIGNMENT_ID, null); } MongoPersistence.addDateSearchCriteria(dbCriteria, MongoPersistentEntity.PROP_CREATED_DATE, criteria); // Add device type filter if specified. if (!StringUtils.isEmpty(criteria.getDeviceTypeToken())) { IDeviceType deviceType = getDeviceTypeByToken(criteria.getDeviceTypeToken()); dbCriteria.put(MongoDevice.PROP_DEVICE_TYPE_ID, deviceType.getId()); } Document sort = new Document(MongoPersistentEntity.PROP_CREATED_DATE, -1); return MongoPersistence.search(IDevice.class, devices, dbCriteria, sort, criteria, LOOKUP); }
if (!device.getDeviceTypeId().equals(deviceType.getId())) { continue;
/** * Common logic for creating new device object and populating it from request. * * @param request * @return * @throws SiteWhereException */ public static Device deviceCreateLogic(IDeviceCreateRequest request, IDeviceType deviceType) throws SiteWhereException { Device device = new Device(); Persistence.entityCreateLogic(request, device); Matcher matcher = HARDWARE_ID_REGEX.matcher(request.getToken()); if (!matcher.matches()) { throw new SiteWhereSystemException(ErrorCode.MalformedHardwareId, ErrorLevel.ERROR); } device.setToken(request.getToken()); device.setDeviceTypeId(deviceType.getId()); device.setComments(request.getComments()); device.setStatus(request.getStatus()); return device; }
target.setDeviceTypeId(deviceType.getId());
target.setDeviceTypeId(deviceType.getId());
/** * Common logic for creating new device command and populating it from request. * * @param deviceType * @param request * @param existing * @return * @throws SiteWhereException */ public static DeviceCommand deviceCommandCreateLogic(IDeviceType deviceType, IDeviceCommandCreateRequest request, List<IDeviceCommand> existing) throws SiteWhereException { DeviceCommand command = new DeviceCommand(); Persistence.entityCreateLogic(request, command); command.setDeviceTypeId(deviceType.getId()); // Name is required. require("Name", request.getName()); command.setName(request.getName()); command.setNamespace(request.getNamespace()); command.setDescription(request.getDescription()); command.getParameters().addAll(request.getParameters()); checkDuplicateCommand(command, existing); return command; }
@Override public IDeviceCommand updateDeviceCommand(UUID id, IDeviceCommandCreateRequest request) throws SiteWhereException { Document match = assertDeviceCommand(id); DeviceCommand command = MongoDeviceCommand.fromDocument(match); // Validate device type token passed. IDeviceType deviceType = null; if (request.getDeviceTypeToken() != null) { deviceType = getDeviceTypeByToken(request.getDeviceTypeToken()); if (deviceType == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceTypeToken, ErrorLevel.ERROR); } } else { deviceType = getDeviceType(command.getDeviceTypeId()); } DeviceCommandSearchCriteria criteria = new DeviceCommandSearchCriteria(1, 0); criteria.setDeviceTypeId(deviceType.getId()); ISearchResults<IDeviceCommand> existing = listDeviceCommands(criteria); // Use common update logic. DeviceManagementPersistence.deviceCommandUpdateLogic(deviceType, request, command, existing.getResults()); Document updated = MongoDeviceCommand.toDocument(command); Document query = new Document(MongoPersistentEntity.PROP_ID, id); MongoCollection<Document> commands = getMongoClient().getDeviceCommandsCollection(); MongoPersistence.update(commands, query, updated); return MongoDeviceCommand.fromDocument(updated); }
@Override public IDeviceStatus updateDeviceStatus(UUID id, IDeviceStatusCreateRequest request) throws SiteWhereException { Document match = assertDeviceStatus(id); DeviceStatus status = MongoDeviceStatus.fromDocument(match); // Validate device type token passed. IDeviceType deviceType = null; if (request.getDeviceTypeToken() != null) { deviceType = getDeviceTypeByToken(request.getDeviceTypeToken()); if (deviceType == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceTypeToken, ErrorLevel.ERROR); } } else { deviceType = getDeviceType(status.getDeviceTypeId()); } DeviceStatusSearchCriteria criteria = new DeviceStatusSearchCriteria(1, 0); criteria.setDeviceTypeId(deviceType.getId()); ISearchResults<IDeviceStatus> existing = listDeviceStatuses(criteria); // Use common update logic. DeviceManagementPersistence.deviceStatusUpdateLogic(deviceType, request, status, existing.getResults()); Document updated = MongoDeviceStatus.toDocument(status); Document query = new Document(MongoPersistentEntity.PROP_ID, id); MongoCollection<Document> statuses = getMongoClient().getDeviceStatusesCollection(); MongoPersistence.update(statuses, query, updated); return MongoDeviceStatus.fromDocument(updated); }
@Override public IDeviceStatus createDeviceStatus(IDeviceStatusCreateRequest request) throws SiteWhereException { // Validate device type token passed. if (request.getDeviceTypeToken() == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceTypeToken, ErrorLevel.ERROR); } IDeviceType deviceType = getDeviceTypeByToken(request.getDeviceTypeToken()); if (deviceType == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceTypeToken, ErrorLevel.ERROR); } DeviceStatusSearchCriteria criteria = new DeviceStatusSearchCriteria(1, 0); criteria.setDeviceTypeId(deviceType.getId()); ISearchResults<IDeviceStatus> existing = listDeviceStatuses(criteria); // Use common logic so all backend implementations work the same. DeviceStatus status = DeviceManagementPersistence.deviceStatusCreateLogic(deviceType, request, existing.getResults()); MongoCollection<Document> statuses = getMongoClient().getDeviceStatusesCollection(); Document created = MongoDeviceStatus.toDocument(status); MongoPersistence.insert(statuses, created, ErrorCode.DeviceStatusExists); return MongoDeviceStatus.fromDocument(created); }
@Override public IDeviceCommand createDeviceCommand(IDeviceCommandCreateRequest request) throws SiteWhereException { // Validate device type token passed. if (request.getDeviceTypeToken() == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceTypeToken, ErrorLevel.ERROR); } IDeviceType deviceType = getDeviceTypeByToken(request.getDeviceTypeToken()); if (deviceType == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceTypeToken, ErrorLevel.ERROR); } DeviceCommandSearchCriteria criteria = new DeviceCommandSearchCriteria(1, 0); criteria.setDeviceTypeId(deviceType.getId()); ISearchResults<IDeviceCommand> existing = listDeviceCommands(criteria); // Use common logic so all backend implementations work the same. DeviceCommand command = DeviceManagementPersistence.deviceCommandCreateLogic(deviceType, request, existing.getResults()); MongoCollection<Document> commands = getMongoClient().getDeviceCommandsCollection(); Document created = MongoDeviceCommand.toDocument(command); MongoPersistence.insert(commands, created, ErrorCode.DeviceCommandExists); return MongoDeviceCommand.fromDocument(created); }
/** * Create the message for a device type. * * @param deviceType * @param tenant * @param deviceManagement * @return * @throws SiteWhereException */ public static DescriptorProtos.DescriptorProto createDeviceTypeMessage(IDeviceType deviceType, ITenant tenant, IDeviceManagement deviceManagement) throws SiteWhereException { DeviceCommandSearchCriteria criteria = new DeviceCommandSearchCriteria(1, 0); criteria.setDeviceTypeId(deviceType.getId()); ISearchResults<IDeviceCommand> commands = deviceManagement.listDeviceCommands(criteria); DescriptorProtos.DescriptorProto.Builder builder = DescriptorProtos.DescriptorProto.newBuilder(); builder.setName(ProtobufNaming.getDeviceTypeIdentifier(deviceType)); builder.addEnumType(createCommandsEnum(commands.getResults())); builder.addNestedType(createUuidMessage()); builder.addNestedType(createHeaderMessage()); for (IDeviceCommand command : commands.getResults()) { builder.addNestedType(createCommandMessage(command)).build(); } return builder.build(); }
target.setDeviceTypeId(deviceType.getId());
Persistence.entityCreateLogic(request, status); status.setDeviceTypeId(deviceType.getId());