# 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
  object
  MongoRepository<Device>
  DeviceRepository

Implements
  IDeviceRepository
  IMongoRepository<Device>

Inherited Members
  MongoRepository<Device>.Db
  MongoRepository<Device>.Collection
  MongoRepository<Device>.InsertOneAsync(Device)
  MongoRepository<Device>.UpdateOneAsync(ObjectId, Device)
  MongoRepository<Device>.DeleteAsync(ObjectId)
  MongoRepository<Device>.InsertInitialLoad()
  MongoRepository<Device>.InsertManyAsync(List<Device>)
  MongoRepository<Device>.UpdateManyObjectIdAsync(string, ObjectId, ObjectId)
  MongoRepository<Device>.DeleteAsync(string)
  MongoRepository<Device>.CollectionExists(string)
  object.Equals(object)
  object.Equals(object, object)
  object.GetHashCode()
  object.GetType()
  object.MemberwiseClone()
  object.ReferenceEquals(object, object)
  object.ToString()

Extension Methods
  NumberUtils.IsNumber(object)
  NumberUtils.ToDouble(object)
  ObjectUtils.ToStr(object?)

## 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

`apiSettings` ApiSettings

  The API settings.

`database` IMongoDatabase

  The 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

`deviceDtoKey` string

  The key of the device to find.

#### Returns

Task<Device>

  The device with the specified key, or null if not found.

### 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

`deviceDtoMacAddr` string

  The MAC address of the device to find.

#### Returns

Task<Device>

  The device with the specified MAC address, or null if not found.

### 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

`deviceDtoSerialNumber` string

  The serial number of the device to find.

#### Returns

Task<Device>

  The device with the specified serial number, or null if not found.

### 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

`deviceDtoUuid` string

  The UUID of the device to find.

#### Returns

Task<Device>

  The device with the specified UUID, or null if not found.

### 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

`id` ObjectId

  The ID of the device to update.

`deviceExist` DeviceDto

  The existing DeviceDto object containing the updated statistics.

#### Returns

Task

  A task representing the asynchronous operation.