@Override public IDevice createDevice(IDeviceCreateRequest request) throws SiteWhereException { Device newDevice = SiteWherePersistence.deviceCreateLogic(request); // Convert and save device data. MongoCollection<Document> devices = getMongoClient().getDevicesCollection(getTenant()); Document created = MongoDevice.toDocument(newDevice); MongoPersistence.insert(devices, created, ErrorCode.DuplicateHardwareId); // Update cache with new data. if (getCacheProvider() != null) { getCacheProvider().getDeviceCache().put(request.getHardwareId(), newDevice); } return newDevice; }
/** * Create a new device. * * @param context * @param request * @return * @throws SiteWhereException */ public static IDevice createDevice(IHBaseContext context, IDeviceCreateRequest request) throws SiteWhereException { Tracer.push(TracerCategory.DeviceManagementApiCall, "createDevice (HBase)", LOGGER); try { Long existing = context.getDeviceIdManager().getDeviceKeys().getValue(request.getHardwareId()); if (existing != null) { throw new SiteWhereSystemException(ErrorCode.DuplicateHardwareId, ErrorLevel.ERROR, HttpServletResponse.SC_CONFLICT); } Long value = context.getDeviceIdManager().getDeviceKeys().getNextCounterValue(); Long inverse = Long.MAX_VALUE - value; context.getDeviceIdManager().getDeviceKeys().create(request.getHardwareId(), inverse); Device device = SiteWherePersistence.deviceCreateLogic(request); return putDevicePayload(context, device); } finally { Tracer.pop(LOGGER); } }