# Class AlarmService

Namespace
  adas_core.Application.Services

Assembly
  adas-core.Application.dll

Provides the concrete implementation of the IAlarmService contract, encapsulating the business logic for managing and processing alarms within the application.

    public class AlarmService : IAlarmService, IApiRequestService

Inheritance
  object
  AlarmService

Implements
  IAlarmService
  IApiRequestService

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

## Constructors

### AlarmService(IAlarmRepository, ILogger<AlarmService>, IPatientService, IConfigObservationService, Lazy<IObservationService>, IClientMessageService, ISubscribersService, Lazy<ICalculatedObservationsService>, Lazy<ILightBeaconService>, Lazy<IRecordingService>, Lazy<IRelayService>, IOptions<ApiSettings>, IUnitService, IPointOfCareService, IHttpContextAccessor, ILocalAuditService, bool)

    public AlarmService(IAlarmRepository alarmRepository, ILogger<AlarmService> logger, IPatientService patientService, IConfigObservationService configObservationService, Lazy<IObservationService> observationService, IClientMessageService clientMessageService, ISubscribersService subscribersService, Lazy<ICalculatedObservationsService> calculatedObservationsService, Lazy<ILightBeaconService> lightBeaconService, Lazy<IRecordingService> recordingService, Lazy<IRelayService> relayService, IOptions<ApiSettings> apiSettings, IUnitService unitService, IPointOfCareService pocService, IHttpContextAccessor httpContextAccessor, ILocalAuditService auditService, bool startTimer = true)

#### Parameters

`alarmRepository` IAlarmRepository
`logger` ILogger<AlarmService>
`patientService` IPatientService
`configObservationService` IConfigObservationService
`observationService` Lazy<IObservationService>
`clientMessageService` IClientMessageService
`subscribersService` ISubscribersService
`calculatedObservationsService` Lazy<ICalculatedObservationsService>
`lightBeaconService` Lazy<ILightBeaconService>
`recordingService` Lazy<IRecordingService>
`relayService` Lazy<IRelayService>
`apiSettings` IOptions<ApiSettings>
`unitService` IUnitService
`pocService` IPointOfCareService
`httpContextAccessor` IHttpContextAccessor
`auditService` ILocalAuditService
`startTimer` bool

## Methods

### CalculateAlarmTest(BasePatientObservationValue, string)

Calculates and dispatches test alarms for a patient observation based on its configuration. Supports beacon alerts, recording, and door control when the corresponding alarm components are enabled; if the source is not a PatientObservation, the method returns without action, and recordings fall back to a default Yellow severity when no severity string is provided.

    public Task CalculateAlarmTest(BasePatientObservationValue source, string name)

#### Parameters

`source` BasePatientObservationValue

  The patient observation value used to look up the alarm configuration and identify the target patient.

`name` string

  The name associated with the observation, used for logging and alarm context.

#### Returns

Task

### CheckObservationAlarm(PatientObservation)

Evaluates alarm configuration rules for a patient observation and, when matching conditions and preconditions are satisfied, generates a new alert observation, logs it, and triggers the corresponding alarm notification.

    public Task CheckObservationAlarm(PatientObservation obs)

#### Parameters

`obs` PatientObservation

  The patient observation whose value is evaluated against configured alert rules and preconditions.

#### Returns

Task

### FindLastObservationsByField(ObjectId, List<Field>?)

Retrieves the most recent patient observation alarms for the specified patient, optionally filtered by a set of fields.

    public Task<List<PatientObservationAlarm>> FindLastObservationsByField(ObjectId patientId, List<Field>? filterObservations = null)

#### Parameters

`patientId` ObjectId

  The identifier of the patient whose last observations should be retrieved.

`filterObservations` List<Field>

  An optional list of fields used to restrict which observations are returned; when null, no field filter is applied.

#### Returns

Task<List<PatientObservationAlarm>>

  A task that represents the asynchronous operation, containing a list of the patient's last PatientObservationAlarm entries.

### FindLastValuesNotExpired(ObjectId, List<Field>, List<ConfigObservation>)

Retrieves the most recent non-expired patient observation alarms for the specified patient, filtered by the given observation fields and alarm configurations.

    public Task<List<PatientObservationAlarm>> FindLastValuesNotExpired(ObjectId patientId, List<Field> filterObservations, List<ConfigObservation> configAlarm)

#### Parameters

`patientId` ObjectId

  The unique identifier of the patient whose alarms are being queried.

`filterObservations` List<Field>

  The list of fields used to filter the observations included in the aggregation.

`configAlarm` List<ConfigObservation>

  The list of alarm configurations that define the criteria applied during the aggregation.

#### Returns

Task<List<PatientObservationAlarm>>

  A task that resolves to a list of the latest non-expired PatientObservationAlarm entries matching the criteria.

### MapObservation(PatientObservationAlarm, bool)

Maps a PatientObservationAlarm through the configuration observation service and then through the calculated observations service. If either mapping step returns null, or an exception occurs, the method logs the issue and returns null instead of propagating the error.

    public Task<PatientObservationAlarm?> MapObservation(PatientObservationAlarm obs, bool onlyByName = false)

#### Parameters

`obs` PatientObservationAlarm

  The patient observation alarm to be mapped.

`onlyByName` bool

  If true, mapping is performed by name only; otherwise the full mapping is applied.

#### Returns

Task<PatientObservationAlarm>

  A mapped PatientObservationAlarm if both mapping steps succeed; otherwise, null.

### MapObservationsByName(PatientObservationAlarm)

Maps the provided patient observation alarm to its corresponding configuration by name, returning the mapped alarm if a matching configuration is found.

    public Task<PatientObservationAlarm?> MapObservationsByName(PatientObservationAlarm obs)

#### Parameters

`obs` PatientObservationAlarm

  The patient observation alarm to be mapped by name.

#### Returns

Task<PatientObservationAlarm>

  A task that returns the mapped PatientObservationAlarm if a matching configuration is found; otherwise, `null`.

### ProcessAlarmObservations(List<PatientObservationAlarm>, List<PatientObservation>, Patient, DateTime, ObservationData?)

Processes and persists a list of patient observation alarms, enriching each alarm with patient, timing, and coding metadata. When a matching non-alarm observation is found in the provided list, its coding fields are reused; otherwise defaults derived from the alarm priority (Ph/Pm/Pl) are applied. For alarms that carry source data, related observations are mapped and submitted through the observation service before the alarm itself is inserted.

    public Task ProcessAlarmObservations(List<PatientObservationAlarm> alarmObservations, List<PatientObservation> observations, Patient patient, DateTime messageTime, ObservationData? observationData = null)

#### Parameters

`alarmObservations` List<PatientObservationAlarm>

  The alarm observations to process and insert.

`observations` List<PatientObservation>

  Existing non-alarm observations used to look up and reuse coding information when a value match is found.

`patient` Patient

  The patient the alarms are associated with.

`messageTime` DateTime

  The message time assigned to each alarm.

`observationData` ObservationData

  Optional parent observation metadata applied to all alarms in the batch.

#### Returns

Task

### SaveRequest(ApiRequest)

Processes and saves an incoming API request, handling ORU_R40 unsolicited alert observation messages. Validates that the patient number or location unit name is provided, resolves the patient and unit configuration, and processes any alarm observations; requests are ignored when no patient is found or auto-adt management is disabled.

    public Task SaveRequest(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request containing patient, location, observation, and alarm data to process.

#### Returns

Task

#### Exceptions

InvalidFormatException

  Thrown when both the patient number and location unit name are missing.

ApiRequestException

  Thrown when the API request type is not valid for observations.

### SaveRequestAsync(ApiRequest)

Asynchronously saves the specified API request by delegating to the underlying save operation.

    public Task SaveRequestAsync(ApiRequest apiRequest)

#### Parameters

`apiRequest` ApiRequest

  The API request to be saved.

#### Returns

Task

### SendAlarm(PatientObservation, string, Name?, Severity, Type)

Sends a new alarm

    public Task SendAlarm(PatientObservation obs, string name, AlarmEnum.Name? code, AlarmEnum.Severity severity, AlarmEnum.Type type)

#### Parameters

`obs` PatientObservation

  Observation to generate the alarm

`name` string

  Name of the alarm

`code` AlarmEnum.Name?

  Code of the alarm for the recording

`severity` AlarmEnum.Severity

  Severity of the alarm for the recording

`type` AlarmEnum.Type

#### Returns

Task

  New alarm created

#### Exceptions

ArgumentOutOfRangeException