コード例 #1
0
        public async Task SetLightState(LightStateViewModel lightState)
        {
            var transition = GetLightSwitchTransition(lightState.State);

            lightState.State = transition[0];

            // trigger light changing on all clients to disable switch
            _controlHub.Clients.All.SetLightState(lightState);

            // save new state to db so new requests fetch updated state
            var cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken       = cancellationTokenSource.Token;
            var saving = _lightSwitchDbService.SetLightState(lightState, cancellationToken);

            // send request to set light
            lightState.State = transition[1];
            var transitionRequestSent = _queueService.SendLightState(lightState);

            if (!transitionRequestSent)
            {
                // could not send transition request, rollback all actions
                cancellationTokenSource.Cancel();
            }

            await saving;
        }