public Builder(IDeviceSpecification api) { request.setToken(api.getToken()); request.setName(api.getName()); request.setAssetModuleId(api.getAssetModuleId()); request.setAssetId(api.getAssetId()); request.setContainerPolicy(api.getContainerPolicy()); request.setDeviceElementSchema((DeviceElementSchema) api.getDeviceElementSchema()); request.setMetadata(new HashMap<String, String>()); request.getMetadata().putAll(api.getMetadata()); }
/** * Copy information from SPI into Mongo DBObject. * * @param source * @param target */ public static void toDocument(IDeviceSpecification source, Document target) { target.append(PROP_TOKEN, source.getToken()); target.append(PROP_NAME, source.getName()); target.append(PROP_ASSET_MODULE_ID, source.getAssetModuleId()); target.append(PROP_ASSET_ID, source.getAssetId()); target.append(PROP_CONTAINER_POLICY, source.getContainerPolicy().name()); MongoSiteWhereEntity.toDocument(source, target); MongoMetadataProvider.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 byte[] encodeDeviceSpecification(IDeviceSpecification spec) throws SiteWhereException { ProtobufMarshaler.DeviceSpecification.Builder builder = ProtobufMarshaler.DeviceSpecification.newBuilder(); builder.setToken(spec.getToken()); builder.setName(spec.getName()); builder.setAssetModuleId(spec.getAssetModuleId()); builder.setAssetId(spec.getAssetId()); builder.setContainerPolicy(spec.getContainerPolicy().name()); if (spec.getDeviceElementSchema() != null) { builder.setDeviceElementSchema(encodeDeviceElementSchema(spec.getDeviceElementSchema())); } builder.setEntityData(createEntityData(spec)); try { ByteArrayOutputStream out = new ByteArrayOutputStream(); builder.build().writeTo(out); return out.toByteArray(); } catch (IOException e) { throw new SiteWhereException("Unable to marshal device specification.", e); } }
/** * Convert a device specification for marshaling. * * @param source * @param manager * @return * @throws SiteWhereException */ public DeviceSpecification convert(IDeviceSpecification source, IAssetModuleManager manager) throws SiteWhereException { DeviceSpecification spec = new DeviceSpecification(); MetadataProviderEntity.copy(source, spec); spec.setToken(source.getToken()); spec.setName(source.getName()); HardwareAsset asset = (HardwareAsset) manager.getAssetById(source.getAssetModuleId(), source.getAssetId()); if (asset == null) { LOGGER.warn("Device specification has reference to non-existent asset."); throw new SiteWhereSystemException(ErrorCode.InvalidAssetReferenceId, ErrorLevel.ERROR); } spec.setAssetModuleId(source.getAssetModuleId()); spec.setAssetId(asset.getId()); spec.setAssetName(asset.getName()); spec.setAssetImageUrl(asset.getImageUrl()); if (isIncludeAsset()) { spec.setAsset(asset); } spec.setContainerPolicy(source.getContainerPolicy()); spec.setDeviceElementSchema((DeviceElementSchema) source.getDeviceElementSchema()); return spec; }