public async Task PerformCheck() { try { var doorSettings = _appSettings.DoorSettings; foreach (var door in doorSettings) { var pinStatus = _pinCheckerService.CheckPin(door.CloseSensorPin); var state = _doorStateService.GetState(door.Id); if (pinStatus != (int)state) { var newState = (DoorState)pinStatus; var doorEvent = new DoorEventBody { State = newState, Door = door.Id }; await _doorEventService.PostDoorServer(doorEvent); _doorStateService.SetState(door.Id, newState); } // else - ignore same state } } catch (Exception e) { _logger.LogError(e, "Exception while performing check"); } }
public void ToggleDoorEvent(DoorEventBody body) { var door = _appSettings.DoorSettings.First(_ => _.Id == body.Door); var pin = door.TriggerPin; var state = _pinService.CheckPin(pin); if (state != (int)body.State) { string doorVerb = body.State == DoorState.Open ? "Opening" : "Closing"; _logger.LogInformation(doorVerb + " Door..."); _pinService.WritePin(pin, GpioPinValue.High); // Sleep for 2 sec System.Threading.Thread.Sleep(2000); _pinService.WritePin(pin, GpioPinValue.Low); _logger.LogInformation("Door has been triggered!"); } else { string doorStateStr = body.State == DoorState.Open ? "Opened" : "Closed"; _logger.LogInformation("Door is already " + doorStateStr); } }