/** * Logic applied before deleting a device assignment. * * @param assignment * @throws SiteWhereException */ public static void deviceAssignmentDeleteLogic(IDeviceAssignment assignment) throws SiteWhereException { if (assignment.getReleasedDate() == null) { throw new SiteWhereSystemException(ErrorCode.CanNotDeleteActiveAssignment, ErrorLevel.ERROR); } }
@Override public byte[] encodeDeviceAssignment(IDeviceAssignment assignment) throws SiteWhereException { ProtobufMarshaler.DeviceAssignment.Builder builder = ProtobufMarshaler.DeviceAssignment.newBuilder(); builder.setToken(assignment.getToken()); builder.setDeviceHardwareId(assignment.getDeviceHardwareId()); builder.setSiteToken(assignment.getSiteToken()); builder.setAssignmentType(marshalAssignmentType(assignment.getAssignmentType())); if (assignment.getAssetModuleId() != null) { builder.setAssetModuleId(assignment.getAssetModuleId()); } if (assignment.getAssetId() != null) { builder.setAssetId(assignment.getAssetId()); } builder.setStatus(assignment.getStatus().name()); if (assignment.getActiveDate() != null) { builder.setActiveDate(assignment.getActiveDate().getTime()); } if (assignment.getReleasedDate() != null) { builder.setReleasedDate(assignment.getReleasedDate().getTime()); } builder.setEntityData(createEntityData(assignment)); try { ByteArrayOutputStream out = new ByteArrayOutputStream(); builder.build().writeTo(out); return out.toByteArray(); } catch (IOException e) { throw new SiteWhereException("Unable to marshal device assignment.", e); } }
/** * Copy information from SPI into Mongo {@link Document}. * * @param source * @param target */ public static void toDocument(IDeviceAssignment source, Document target) { target.append(PROP_DEVICE_ID, source.getDeviceId()); target.append(PROP_DEVICE_TYPE_ID, source.getDeviceTypeId()); target.append(PROP_CUSTOMER_ID, source.getCustomerId()); target.append(PROP_AREA_ID, source.getAreaId()); target.append(PROP_ASSET_ID, source.getAssetId()); if (source.getActiveDate() != null) { target.append(PROP_ACTIVE_DATE, source.getActiveDate()); } if (source.getReleasedDate() != null) { target.append(PROP_RELEASED_DATE, source.getReleasedDate()); } if (source.getStatus() != null) { target.append(PROP_STATUS, source.getStatus().name()); } MongoPersistentEntity.toDocument(source, target); }
/** * Copy information from SPI into Mongo {@link Document}. * * @param source * @param target */ public static void toDocument(IDeviceAssignment source, Document target) { if (source.getActiveDate() != null) { target.append(PROP_ACTIVE_DATE, source.getActiveDate()); } target.append(PROP_ASSET_MODULE_ID, source.getAssetModuleId()); target.append(PROP_ASSET_ID, source.getAssetId()); if (source.getAssignmentType() != null) { target.append(PROP_ASSIGNMENT_TYPE, source.getAssignmentType().name()); } if (source.getReleasedDate() != null) { target.append(PROP_RELEASED_DATE, source.getReleasedDate()); } if (source.getStatus() != null) { target.append(PROP_STATUS, source.getStatus().name()); } target.append(PROP_TOKEN, source.getToken()); target.append(PROP_DEVICE_HARDWARE_ID, source.getDeviceHardwareId()); target.append(PROP_SITE_TOKEN, source.getSiteToken()); if (source.getState() != null) { setState(source.getState(), target); } MongoSiteWhereEntity.toDocument(source, target); MongoMetadataProvider.toDocument(source, target); }
/** * Convert a device assignment from API to GRPC. * * @param api * @return * @throws SiteWhereException */ public static GDeviceAssignment asGrpcDeviceAssignment(IDeviceAssignment api) throws SiteWhereException { GDeviceAssignment.Builder grpc = GDeviceAssignment.newBuilder(); grpc.setStatus(CommonModelConverter.asGrpcDeviceAssignmentStatus(api.getStatus())); grpc.setDeviceId(CommonModelConverter.asGrpcUuid(api.getDeviceId())); grpc.setDeviceTypeId(CommonModelConverter.asGrpcUuid(api.getDeviceTypeId())); if (api.getAreaId() != null) { grpc.setAreaId(CommonModelConverter.asGrpcUuid(api.getAreaId())); } if (api.getAssetId() != null) { grpc.setAssetId(CommonModelConverter.asGrpcUuid(api.getAssetId())); } grpc.setActiveDate(CommonModelConverter.asGrpcDate(api.getActiveDate())); grpc.setReleasedDate(CommonModelConverter.asGrpcDate(api.getReleasedDate())); grpc.setEntityInformation(CommonModelConverter.asGrpcEntityInformation(api)); return grpc.build(); }
MarshaledDeviceAssignment result = new MarshaledDeviceAssignment(); result.setActiveDate(source.getActiveDate()); result.setReleasedDate(source.getReleasedDate()); result.setStatus(source.getStatus()); PersistentEntity.copy(source, result);
result.setToken(source.getToken()); result.setActiveDate(source.getActiveDate()); result.setReleasedDate(source.getReleasedDate()); result.setStatus(source.getStatus()); result.setAssignmentType(source.getAssignmentType());