/** * Get the device type identifier. * * @param deviceType * @return */ protected static String getDeviceTypeIdentifier(IDeviceType deviceType) { return "Spec_" + deviceType.getToken(); }
/** * Convert a device type for marshaling. * * @param source * @return * @throws SiteWhereException */ public DeviceType convert(IDeviceType source) throws SiteWhereException { DeviceType deviceType = new DeviceType(); deviceType.setName(source.getName()); deviceType.setDescription(source.getDescription()); deviceType.setContainerPolicy(source.getContainerPolicy()); deviceType.setDeviceElementSchema((DeviceElementSchema) source.getDeviceElementSchema()); BrandedEntity.copy(source, deviceType); return deviceType; }
/** * 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(); }
+ area.getName() + "</a>"; html += " with device type <a href=\"" + relativePath + "/devicetypes/" + deviceType.getToken() + ".html\">" + deviceType.getName() + "</a>"; if (!StringUtils.isEmpty(criteria.getGroupToken())) { IDeviceGroup group = devices.getDeviceGroupByToken(criteria.getGroupToken());
IDeviceUnit unit = deviceType.getDeviceElementSchema(); if (unit == null) { return null;
/** * Generate the '.proto' information. * * @param deviceType * @param commands * @param buffer * @throws SiteWhereException */ protected static void generateProto(IDeviceType deviceType, List<IDeviceCommand> commands, StringBuffer buffer) throws SiteWhereException { int indent = 0; SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z"); String typeName = ProtobufNaming.getDeviceTypeIdentifier(deviceType); println("option optimize_for = LITE_RUNTIME;", indent, buffer); newline(buffer); println("// Google Protocol Buffer for '" + deviceType.getName() + "'.", indent, buffer); println("// Generated on " + formatter.format(new Date()), indent, buffer); println("message " + typeName + " {", indent, buffer); newline(buffer); addCommandsEnum(deviceType, commands, buffer, indent + 1); newline(buffer); addHeader(buffer, indent + 1); newline(buffer); int index = 2; for (IDeviceCommand command : commands) { addCommand(command, index++, buffer, indent + 1); newline(buffer); } println("}", indent, buffer); }
IDeviceType nestedType = deviceManagement.getDeviceType(nested.getNested().getDeviceTypeId()); LOGGER.debug( "Targeting nested device with type: " + nestedType.getName() + " at path " + nested.getPath()); headBuilder.setField(header.findFieldByName(ProtobufNaming.HEADER_NESTED_PATH_FIELD_NAME), nested.getPath()); headBuilder.setField(header.findFieldByName(ProtobufNaming.HEADER_NESTED_TYPE_FIELD_NAME), nestedType.getToken());
/** * Copy information from SPI into Mongo DBObject. * * @param source * @param target */ public static void toDocument(IDeviceType source, Document target) { target.append(PROP_NAME, source.getName()); target.append(PROP_DESCRIPTION, source.getDescription()); target.append(PROP_CONTAINER_POLICY, source.getContainerPolicy().name()); MongoBrandedEntity.toDocument(source, target); // Marshal device element schema as JSON. if (source.getDeviceElementSchema() != null) { ObjectMapper mapper = new ObjectMapper(); try { byte[] schemaJson = mapper.writeValueAsBytes(source.getDeviceElementSchema()); target.append(PROP_DEVICE_ELEMENT_SCHEMA, new Binary(schemaJson)); } catch (JsonProcessingException e) { LOGGER.error("Unable to marshal device element schema for MongoDB persistence.", e); } } }
@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); }
@Override public URI getDeviceTypeIdentifier(IDeviceType deviceType) throws SiteWhereException { return createUri(SITEWHERE_PROTOCOL + "devicetype/" + deviceType.getToken()); }
/** * Convert device type from API to GRPC. * * @param api * @return * @throws SiteWhereException */ public static GDeviceType asGrpcDeviceType(IDeviceType api) throws SiteWhereException { GDeviceType.Builder grpc = GDeviceType.newBuilder(); grpc.setName(api.getName()); grpc.setDescription(api.getDescription()); grpc.setContainerPolicy(DeviceModelConverter.asGrpcDeviceContainerPolicy(api.getContainerPolicy())); if (api.getDeviceElementSchema() != null) { grpc.setDeviceElementSchema(DeviceModelConverter.asGrpcDeviceElementSchema(api.getDeviceElementSchema())); } grpc.setEntityInformation(CommonModelConverter.asGrpcEntityInformation(api)); grpc.setBranding(CommonModelConverter.asGrpcBrandingInformation(api)); return grpc.build(); }
if (!device.getDeviceTypeId().equals(deviceType.getId())) { continue;
@Override public boolean appliesTo(IDeviceContext<T> criteria) { if (criteria.getDeviceType().getToken().equals(getDeviceTypeToken())) { return true; } return false; }
/** * 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; }
/** * Common logic executed before deleting a device type. * * @param deviceType * @param deviceManagement * @throws SiteWhereException */ public static void deviceTypeDeleteLogic(IDeviceType deviceType, IDeviceManagement deviceManagement) throws SiteWhereException { DeviceSearchCriteria criteria = new DeviceSearchCriteria(1, 1, null, null); criteria.setDeviceTypeToken(deviceType.getToken()); ISearchResults<IDevice> devices = deviceManagement.listDevices(criteria); if (devices.getNumResults() > 0) { throw new SiteWhereSystemException(ErrorCode.DeviceTypeInUseByDevices, ErrorLevel.ERROR); } }
target.setDeviceTypeId(deviceType.getId());
IDeviceContext<T> context = buildContext(metadata); getLogger().debug("Built context: Device: " + context.getDevice().getToken() + " Device type: " + context.getDeviceType().getToken());
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 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); }