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
-
RelayService
- Implements
- Inherited Members
- Extension Methods
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
loggerILogger<RelayService>httpClientFactoryIHttpClientFactoryrelaySettingsIOptions<RelaySettings>pointOfCareServiceIPointOfCareServicerelayRepositoryIRelayRepository
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
relayIdObjectIdThe 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
relayRelayThe 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
relayObjectIdThe unique identifier of the relay to look up.
Returns
- Task<Relay>
A task that resolves to the Relay if found, or
nullif 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
filterPaginationFilter
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
configurationRelayListList<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.typeRelayEnum.TypeThe 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 ifconfigurationRelayListisnull.
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
relayListList<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
requestRelayThe 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
nullif the repository yields no result.
Exceptions
- Exception
Thrown when a relay with the same name as
requestalready 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
relayRelayThe relay to power off, including its network address and relay number.
Returns
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
relayRelayThe relay to power on, including driver, IP, port, relay number, and cache settings.
Returns
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
statusRelayEnum.StatusThe manual relay status to apply.
pocIdObjectIdThe identifier of the point of care device whose relay configuration should be updated.
typeRelayEnum.TypeThe relay type used to look up the target relay within the point of care's relay configuration.
Returns
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
objectIdObjectIdThe unique identifier of the relay to update.
relayRelayThe relay object containing the updated information.
Returns
- Task<Relay>
A task representing the asynchronous operation, containing the updated Relay, or
nullif no relay with the specified identifier was found.
Events
RelayStatusChanged
public event EventHandler<Tuple<RelayDevice, int>>? RelayStatusChanged