//event binding private void InitializeComponents() { // 2.When the power switch is switched on, the kettle controller must activate the heating element and the lamp. _powerSwitch.SwitchedOn += async delegate { // 7.When having a HeatingElementException when switching on, // the controller must ensure that all the components are off. try { await _powerLamp.SwitchOnAsync(); await _heatingElement.SwitchOnAsync(); } catch (HeatingElementException ex) { await _powerSwitch.SwitchOffAsync(); await _powerLamp.SwitchOffAsync(); } }; // 3.When the power switch is switched off, all components must be deactivated. _powerSwitch.SwitchedOff += async delegate { await _powerLamp.SwitchOffAsync(); await _heatingElement.SwitchOffAsync(); }; // 5.When the water temperature reaches 100 degrees Celsius, the power must be switched off. _temperatureSensor.ValueChanged += async delegate(object sender, ValueChangedEventArgs <int> e) { if (e.Value >= 100) { await _powerSwitch.SwitchOffAsync(); await _powerLamp.SwitchOffAsync(); await _heatingElement.SwitchOffAsync(); } }; // 6... The kettle controller should switch off if the water is removed during heating. _waterSensor.ValueChanged += async delegate(object sender, ValueChangedEventArgs <bool> e) { if (!e.Value) { await _powerSwitch.SwitchOffAsync(); } }; _isComponentsInitialized = true; }
private async Task TurnOffKettle() { await powerSwitch.SwitchOffAsync(); await heatingElement.SwitchOffAsync(); await powerLamp.SwitchOffAsync(); }