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;
        }
        public async Task <int> SetLightState(LightStateViewModel lightState, CancellationToken cancellationToken)
        {
            var switchedLight = await _context.Lights.FindAsync(lightState.LightId);

            switchedLight.State = lightState.State;
            return(await _context.SaveChangesAsync(cancellationToken));
        }
Exemple #3
0
 public async Task SetLightState(LightStateViewModel lightState)
 {
     await _lightSwitchService.SetLightState(lightState);
 }
        public bool SendLightState(LightStateViewModel lightState)
        {
            var message = $"{(int) lightState.State}, {lightState.LightId}";

            return(SendMessage(message));
        }