public CreateUnassociatedRequest() throws SiteWhereException { setToken(UUID.randomUUID().toString()); setDeviceHardwareId(ExampleData.TRACKER.getHardwareId()); setAssignmentType(DeviceAssignmentType.Unassociated); Map<String, String> metadata = new HashMap<String, String>(); metadata.put("validUntil", "2016-10-10"); metadata.put("renewable", "true"); setMetadata(metadata); SiteWherePersistence.deviceAssignmentCreateLogic(this, ExampleData.TRACKER); } }
@Override public IDeviceEventBatchResponse addDeviceEventBatch(String assignmentToken, IDeviceEventBatch batch) throws SiteWhereException { return SiteWherePersistence.deviceEventBatchLogic(assignmentToken, batch, this); }
/** * Update an existing asset category. * * @param context * @param id * @param request * @return * @throws SiteWhereException */ public static IAssetCategory updateAssetCategory(IHBaseContext context, String id, IAssetCategoryCreateRequest request) throws SiteWhereException { AssetCategory updated = assertAssetCategory(context, id); SiteWherePersistence.assetCategoryUpdateLogic(request, updated); return HBaseUtils.put(context, context.getPayloadMarshaler(), ISiteWhereHBase.ASSETS_TABLE_NAME, updated, id, KEY_BUILDER); }
/** * Common logic for updating an existing device group. * * @param request * @param target * @throws SiteWhereException */ public static void deviceGroupUpdateLogic(IDeviceGroupCreateRequest request, DeviceGroup target) throws SiteWhereException { target.setName(request.getName()); target.setDescription(request.getDescription()); if (request.getRoles() != null) { target.getRoles().clear(); target.getRoles().addAll(request.getRoles()); } if (request.getMetadata() != null) { target.getMetadata().clear(); MetadataProvider.copy(request.getMetadata(), target); } SiteWherePersistence.setUpdatedEntityMetadata(target); }
SiteWherePersistence.setUpdatedEntityMetadata(existing); byte[] payload = context.getPayloadMarshaler().encodeUser(existing); Table users = null; SiteWherePersistence.userDeleteLogic(username); return existing;
@Override public IBatchOperation createBatchOperation(IBatchOperationCreateRequest request) throws SiteWhereException { String uuid = ((request.getToken() != null) ? request.getToken() : UUID.randomUUID().toString()); BatchOperation batch = SiteWherePersistence.batchOperationCreateLogic(request, uuid); MongoCollection<Document> batches = getMongoClient().getBatchOperationsCollection(getTenant()); Document created = MongoBatchOperation.toDocument(batch); MongoPersistence.insert(batches, created, ErrorCode.DuplicateBatchOperationToken); // Insert element for each hardware id. long index = 0; MongoCollection<Document> elements = getMongoClient().getBatchOperationElementsCollection(getTenant()); for (String hardwareId : request.getHardwareIds()) { BatchElement element = SiteWherePersistence.batchElementCreateLogic(batch.getToken(), hardwareId, ++index); Document dbElement = MongoBatchElement.toDocument(element); MongoPersistence.insert(elements, dbElement, ErrorCode.DuplicateBatchElement); } return MongoBatchOperation.fromDocument(created); }
checkDuplicateCommand(target, existing); MetadataProvider.copy(request.getMetadata(), target); SiteWherePersistence.setUpdatedEntityMetadata(target);
/** * Update an existing batch operation. * * @param context * @param token * @param request * @return * @throws SiteWhereException */ public static IBatchOperation updateBatchOperation(IHBaseContext context, String token, IBatchOperationUpdateRequest request) throws SiteWhereException { BatchOperation updated = assertBatchOperation(context, token); BatchOperationStatus oldProcessingStatus = updated.getProcessingStatus(); SiteWherePersistence.batchOperationUpdateLogic(request, updated); Map<byte[], byte[]> qualifiers = new HashMap<byte[], byte[]>(); if (updated.getProcessingStatus() != oldProcessingStatus) { qualifiers.put(PROCESSING_STATUS, Bytes.toBytes(String.valueOf(updated.getProcessingStatus().getCode()))); } return HBaseUtils.createOrUpdate(context, context.getPayloadMarshaler(), ISiteWhereHBase.DEVICES_TABLE_NAME, updated, token, KEY_BUILDER, qualifiers); }
@Override public IBatchOperation createBatchCommandInvocation(IBatchCommandInvocationRequest request) throws SiteWhereException { String uuid = ((request.getToken() != null) ? request.getToken() : UUID.randomUUID().toString()); IBatchOperationCreateRequest generic = SiteWherePersistence.batchCommandInvocationCreateLogic(request, uuid); return createBatchOperation(generic); }
@Override public IBatchElement updateBatchElement(String operationToken, long index, IBatchElementUpdateRequest request) throws SiteWhereException { MongoCollection<Document> elements = getMongoClient().getBatchOperationElementsCollection(getTenant()); Document dbElement = assertBatchElement(operationToken, index); BatchElement element = MongoBatchElement.fromDocument(dbElement); SiteWherePersistence.batchElementUpdateLogic(request, element); Document updated = MongoBatchElement.toDocument(element); Document query = new Document(MongoBatchElement.PROP_BATCH_OPERATION_TOKEN, operationToken) .append(MongoBatchElement.PROP_INDEX, index); MongoPersistence.update(elements, query, updated); return MongoBatchElement.fromDocument(updated); }
/** * Create a new asset category. * * @param context * @param request * @return * @throws SiteWhereException */ public static AssetCategory createAssetCategory(IHBaseContext context, IAssetCategoryCreateRequest request) throws SiteWhereException { if (getAssetCategoryById(context, request.getId()) != null) { throw new SiteWhereSystemException(ErrorCode.AssetCategoryIdInUse, ErrorLevel.ERROR); } // Add new key to table. String id = KEY_BUILDER.getMap(context).useExistingId(request.getId()); // Use common logic so all backend implementations work the same. AssetCategory category = SiteWherePersistence.assetCategoryCreateLogic(request); Map<byte[], byte[]> qualifiers = new HashMap<byte[], byte[]>(); return HBaseUtils.createOrUpdate(context, context.getPayloadMarshaler(), ISiteWhereHBase.ASSETS_TABLE_NAME, category, id, KEY_BUILDER, qualifiers); }
/** * Create a batch element row. * * @param context * @param devices * @param request * @return * @throws SiteWhereException */ public static IBatchElement createBatchElement(IHBaseContext context, Table devices, IBatchElement request) throws SiteWhereException { byte[] elementKey = getElementRowKey(context, request.getBatchOperationToken(), request.getIndex()); // Use common processing logic so all backend implementations work the // same. BatchElement element = SiteWherePersistence.batchElementCreateLogic(request.getBatchOperationToken(), request.getHardwareId(), request.getIndex()); // Encode batch element. byte[] payload = context.getPayloadMarshaler().encodeBatchElement(element); try { Put put = new Put(elementKey); HBaseUtils.addPayloadFields(context.getPayloadMarshaler().getEncoding(), put, payload); put.addColumn(ISiteWhereHBase.FAMILY_ID, HARDWARE_ID, Bytes.toBytes(element.getHardwareId())); put.addColumn(ISiteWhereHBase.FAMILY_ID, PROCESSING_STATUS, Bytes.toBytes(String.valueOf(request.getProcessingStatus().getCode()))); devices.put(put); } catch (IOException e) { throw new SiteWhereException("Unable to create device group element.", e); } return element; }
/** * Common code for copying information from an update request to an existing * zone. * * @param request * @param target * @throws SiteWhereException */ public static void zoneUpdateLogic(IZoneCreateRequest request, Zone target) throws SiteWhereException { target.setName(request.getName()); target.setBorderColor(request.getBorderColor()); target.setFillColor(request.getFillColor()); target.setOpacity(request.getOpacity()); target.getCoordinates().clear(); for (ILocation coordinate : request.getCoordinates()) { target.getCoordinates().add(coordinate); } if (request.getMetadata() != null) { target.getMetadata().clear(); MetadataProvider.copy(request.getMetadata(), target); } SiteWherePersistence.setUpdatedEntityMetadata(target); }
BatchOperation batch = SiteWherePersistence.batchOperationCreateLogic(request, uuid); devices = getDeviceTableInterface(context); for (String hardwareId : request.getHardwareIds()) { BatchElement element = SiteWherePersistence.batchElementCreateLogic(batch.getToken(), hardwareId, ++index); HBaseBatchElement.createBatchElement(context, devices, element);
@Override public IBatchOperation updateBatchOperation(String token, IBatchOperationUpdateRequest request) throws SiteWhereException { MongoCollection<Document> batchops = getMongoClient().getBatchOperationsCollection(getTenant()); Document match = assertBatchOperation(token); BatchOperation operation = MongoBatchOperation.fromDocument(match); SiteWherePersistence.batchOperationUpdateLogic(request, operation); Document updated = MongoBatchOperation.toDocument(operation); Document query = new Document(MongoBatchOperation.PROP_TOKEN, token); MongoPersistence.update(batchops, query, updated); return MongoBatchOperation.fromDocument(updated); }
@Override public IBatchOperation createBatchCommandInvocation(IBatchCommandInvocationRequest request) throws SiteWhereException { String uuid = ((request.getToken() != null) ? request.getToken() : UUID.randomUUID().toString()); IBatchOperationCreateRequest generic = SiteWherePersistence.batchCommandInvocationCreateLogic(request, uuid); return createBatchOperation(generic); }
byte[] elementKey = getElementRowKey(context, operationToken, index); SiteWherePersistence.batchElementUpdateLogic(request, element); byte[] payload = context.getPayloadMarshaler().encodeBatchElement(element);
@Override public IAssetCategory createAssetCategory(IAssetCategoryCreateRequest request) throws SiteWhereException { // Use common logic so all backend implementations work the same. AssetCategory category = SiteWherePersistence.assetCategoryCreateLogic(request); MongoCollection<Document> categories = getMongoClient().getAssetCategoriesCollection(getTenant()); Document created = MongoAssetCategory.toDocument(category); MongoPersistence.insert(categories, created, ErrorCode.AssetCategoryIdInUse); // Update cache with new data. if (getCacheProvider() != null) { getCacheProvider().getAssetCategoryCache().put(category.getId(), category); } return MongoAssetCategory.fromDocument(created); }
/** * Handle common logic for updating a scheduled job. * * @param job * @param request * @throws SiteWhereException */ public static void scheduledJobUpdateLogic(ScheduledJob job, IScheduledJobCreateRequest request) throws SiteWhereException { if (request.getScheduleToken() != null) { job.setScheduleToken(request.getScheduleToken()); } if (request.getJobType() != null) { job.setJobType(request.getJobType()); } if (request.getJobConfiguration() != null) { job.getJobConfiguration().clear(); job.getJobConfiguration().putAll(request.getJobConfiguration()); } if (request.getJobState() != null) { job.setJobState(request.getJobState()); } SiteWherePersistence.setUpdatedEntityMetadata(job); }
public CreateAssociatedRequest() throws SiteWhereException { setToken(UUID.randomUUID().toString()); setDeviceHardwareId(ExampleData.TRACKER.getHardwareId()); setAssignmentType(DeviceAssignmentType.Associated); setAssetModuleId(ExampleData.AM_PERSONS.getId()); setAssetId(ExampleData.ASSET_DEREK.getId()); Map<String, String> metadata = new HashMap<String, String>(); metadata.put("validUntil", "2016-10-10"); metadata.put("renewable", "true"); setMetadata(metadata); SiteWherePersistence.deviceAssignmentCreateLogic(this, ExampleData.TRACKER); } }