Class DeviceRepository
- Namespace
- adas_core.Infrastructure.Repositories
- Assembly
- adas-core.Infrastructure.dll
Repository for managing Device entities in MongoDB. Provides methods for finding devices by various attributes and updating device statistics.
public class DeviceRepository : MongoRepository<Device>, IDeviceRepository, IMongoRepository<Device>
- Inheritance
-
DeviceRepository
- Implements
- Inherited Members
- Extension Methods
Constructors
DeviceRepository(ApiSettings, IMongoDatabase)
Initializes a new instance of the DeviceRepository class with the specified API settings and MongoDB database.
public DeviceRepository(ApiSettings apiSettings, IMongoDatabase database)
Parameters
apiSettingsApiSettingsThe API settings.
databaseIMongoDatabaseThe MongoDB database.
Methods
CreateIndexes()
Creates indexes for the Device collection in MongoDB. This method ensures that indexes are created for the MacAddr, SerialNumber, Uuid, and Key fields to optimize query performance. The MacAddr index is unique, while the others are non-unique and created in the background.
public override Task CreateIndexes()
Returns
- Task
A task representing the asynchronous operation.
FindByKey(string)
Finds a device by its key. This method queries the MongoDB collection for a device with the specified key and returns the first matching device, or null if no match is found.
public Task<Device?> FindByKey(string deviceDtoKey)
Parameters
deviceDtoKeystringThe key of the device to find.
Returns
FindByMacAddr(string)
Finds a device by its MAC address. This method queries the MongoDB collection for a device with the specified MAC address and returns the first matching device, or null if no match is found.
public Task<Device?> FindByMacAddr(string deviceDtoMacAddr)
Parameters
deviceDtoMacAddrstringThe MAC address of the device to find.
Returns
FindBySerialNumber(string)
Finds a device by its serial number. This method queries the MongoDB collection for a device with the specified serial number and returns the first matching device, or null if no match is found.
public Task<Device?> FindBySerialNumber(string deviceDtoSerialNumber)
Parameters
deviceDtoSerialNumberstringThe serial number of the device to find.
Returns
FindByUuid(string)
Finds a device by its UUID. This method queries the MongoDB collection for a device with the specified UUID and returns the first matching device, or null if no match is found.
public Task<Device?> FindByUuid(string deviceDtoUuid)
Parameters
deviceDtoUuidstringThe UUID of the device to find.
Returns
GetCollectionName()
Gets the name of the MongoDB collection for devices. The collection name is determined by the API settings, with a default value of "devices" if not specified.
public override string GetCollectionName()
Returns
- string
The name of the MongoDB collection for devices.
UpdateDeviceStats(ObjectId, DeviceDto)
Updates the statistics of a device. This method takes the device ID and an existing DeviceDto object, and updates the corresponding fields (Battery, Connected, Ready, Name) in the MongoDB collection for the device with the specified ID. The UpdatedAt field is also set to the current UTC time.
public Task UpdateDeviceStats(ObjectId id, DeviceDto deviceExist)
Parameters
idObjectIdThe ID of the device to update.
deviceExistDeviceDtoThe existing DeviceDto object containing the updated statistics.
Returns
- Task
A task representing the asynchronous operation.