# Class RelayService

Namespace
  adas_core.Infrastructure.Services

Assembly
  adas-core.Infrastructure.dll

Provides a concrete implementation of the IRelayService contract, delivering relay-related functionality to consumers of the service.

    public class RelayService : IRelayService

Inheritance
  object
  RelayService

Implements
  IRelayService

Inherited Members
  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?)

## Remarks

This class is the default implementation of IRelayService, and can be substituted via dependency injection where the interface is required.

## Constructors

### RelayService(ILogger<RelayService>, IHttpClientFactory, IOptions<RelaySettings>, IPointOfCareService, IRelayRepository)

    public RelayService(ILogger<RelayService> logger, IHttpClientFactory httpClientFactory, IOptions<RelaySettings> relaySettings, IPointOfCareService pointOfCareService, IRelayRepository relayRepository)

#### Parameters

`logger` ILogger<RelayService>
`httpClientFactory` IHttpClientFactory
`relaySettings` IOptions<RelaySettings>
`pointOfCareService` IPointOfCareService
`relayRepository` IRelayRepository

## Methods

### CheckRelayStatus(ObjectId)

Checks the status of a relay identified by its unique ID. Returns Unknown when the relay cannot be found, otherwise delegates to the relay-based overload to resolve the current status.

    public Task<RelayEnum.Status> CheckRelayStatus(ObjectId relayId)

#### Parameters

`relayId` ObjectId

  The unique identifier of the relay whose status should be checked.

#### Returns

Task<RelayEnum.Status>

  The current RelayEnum.Status of the relay, or Unknown if no relay with the given ID exists.

### CheckRelayStatus(Relay)

Asynchronously determines the current status of a relay, preferring a cached value when available and falling back to a direct device read or an external endpoint when the device is unreachable. Returns the cached status if caching is enabled, the cached value is present and not NotInitialized; otherwise queries the relay device, or invokes the configured URL endpoint if the device cannot be obtained. On failure, logs the error and resolves the status to Unknown.

    public Task<RelayEnum.Status> CheckRelayStatus(Relay relay)

#### Parameters

`relay` Relay

  The relay to check, identified by its IP, port, and relay number used both as the cache key and as the target of the status request.

#### Returns

Task<RelayEnum.Status>

  A task that yields the resolved RelayEnum.Status of the relay, or Unknown when the status cannot be determined.

### GetById(ObjectId)

Retrieves a relay entity by its unique identifier from the repository. Returns `null` when no matching relay is found.

    public Task<Relay?> GetById(ObjectId relay)

#### Parameters

`relay` ObjectId

  The unique identifier of the relay to look up.

#### Returns

Task<Relay>

  A task that resolves to the Relay if found, or `null` if no relay matches the supplied identifier.

### GetPaginatedRelays(PaginationFilter)

Retrieves a paginated list of relays based on the specified pagination filter.

    public Task<PaginationResponse<Relay>> GetPaginatedRelays(PaginationFilter filter)

#### Parameters

`filter` PaginationFilter

#### Returns

Task<PaginationResponse<Relay>>

  A task that represents the asynchronous operation, containing the paginated response with the requested relays.

### GetRelayByTypeInList(List<ObjectId>?, Type)

Retrieves the relays of a specified type from the provided list of configuration relay identifiers. Returns an empty list when the input list is `null`, otherwise delegates the lookup to the relay repository.

    public List<Relay> GetRelayByTypeInList(List<ObjectId>? configurationRelayList, RelayEnum.Type type)

#### Parameters

`configurationRelayList` List<ObjectId>

  The optional list of MongoDB.Bson.ObjectId values identifying the configuration relays to filter; when `null`, the method short-circuits and returns an empty list.

`type` RelayEnum.Type

  The relay type used to filter the matching relays within the supplied list.

#### Returns

List<Relay>

  A List<T> containing the relays matching the specified `type`, or an empty list if `configurationRelayList` is `null`.

### GetRelayInList(List<ObjectId>?)

Retrieves a list of relays matching the specified list of relay identifiers. Returns an empty list when the provided identifier list is null.

    public List<Relay> GetRelayInList(List<ObjectId>? relayList)

#### Parameters

`relayList` List<ObjectId>

  The list of relay identifiers to look up. Can be null.

#### Returns

List<Relay>

  A list of Relay objects corresponding to the provided identifiers, or an empty list if the input is null.

### InsertRelay(Relay)

Inserts a new relay into the repository after verifying that no relay with the same name already exists.

    public Task<Relay?> InsertRelay(Relay request)

#### Parameters

`request` Relay

  The relay entity to insert, whose name is checked for duplicates prior to persistence.

#### Returns

Task<Relay>

  The inserted Relay entity returned by the repository, or `null` if the repository yields no result.

#### Exceptions

Exception

  Thrown when a relay with the same name as `request` already exists in the repository.

### PowerOff(Relay)

Asynchronously powers off the specified relay. Uses a cache to avoid redundant operations when the relay is already off, falls back to an HTTP request when no local relay device is available, and updates the cache after a successful power off.

    public Task PowerOff(Relay relay)

#### Parameters

`relay` Relay

  The relay to power off, including its network address and relay number.

#### Returns

Task

### PowerOn(Relay)

Powers on the specified relay. If the relay is configured with a URL, sends a remote power-on request; otherwise, drives the relay device directly. Honors a caching layer to avoid redundant power-on commands when the relay is already reported as on.

    public Task PowerOn(Relay relay)

#### Parameters

`relay` Relay

  The relay to power on, including driver, IP, port, relay number, and cache settings.

#### Returns

Task

### SetManualRelay(Status, ObjectId, Type)

Sets the manual relay status for a point of care device. If the point of care exists and has a configured relay list, the relay matching the specified type is updated with the new status; otherwise, the operation is silently skipped.

    public Task SetManualRelay(RelayEnum.Status status, ObjectId pocId, RelayEnum.Type type)

#### Parameters

`status` RelayEnum.Status

  The manual relay status to apply.

`pocId` ObjectId

  The identifier of the point of care device whose relay configuration should be updated.

`type` RelayEnum.Type

  The relay type used to look up the target relay within the point of care's relay configuration.

#### Returns

Task

### UpdateRelayById(ObjectId, Relay)

Updates an existing relay identified by its unique ObjectId by delegating to the relay repository.

    public Task<Relay?> UpdateRelayById(ObjectId objectId, Relay relay)

#### Parameters

`objectId` ObjectId

  The unique identifier of the relay to update.

`relay` Relay

  The relay object containing the updated information.

#### Returns

Task<Relay>

  A task representing the asynchronous operation, containing the updated Relay, or `null` if no relay with the specified identifier was found.

## Events

### RelayStatusChanged

    public event EventHandler<Tuple<RelayDevice, int>>? RelayStatusChanged

#### Event Type

EventHandler<Tuple<RelayDevice, int>>