Ejemplo n.º 1
0
        public async Task<ActionResult> Index(string from, string body)
        {
            var request = new LightsRequest
            {
                Source = "twilio",
                From = from,
                Text = body
            };

            var lightsResponse = await _lightsService.HandleRequestAsync(request);

            var response = new TwilioResponse();

            if (_smsSettings.AllowResponses && lightsResponse.IsSuccess)
            {
                if (lightsResponse.IsScheduled && lightsResponse.ScheduledForUtc.HasValue)
                {
                    response.Sms(string.Format("Ooops, you're in a queue! Don't worry, your lights have been scheduled for {0}. Merry Christmas from #157!",
                        lightsResponse.ScheduledForUtc.Value.ToString("HH:mm")));
                }
                else
                {
                    response.Sms("Thanks! Your lights will be shown shortly. Merry Christmas from #157!");                    
                }
            }

            return new TwiMLResult(response);
        }
Ejemplo n.º 2
0
        public async Task<LightsResponse> HandleRequestAsync(LightsRequest request)
        {
            var now = _dateTimeProvider.UtcNow();

            var metric = new Metric
            {
                TimestampUtc = now,
                RequestText = request.Text,
                Source = request.Source,
                From = request.From
            };

            var lightDisplay = await _lightDisplayParser.ParseAsync(request.Text);

            if (lightDisplay.Colours.Count == 0)
            {
                await _metricWriter.SendAsync(metric);

                return LightsResponse.Failure();
            }

            var scheduledEnqueueTimeUtc = _lastRequestUtc.AddMinutes(1);
            
            if (now > scheduledEnqueueTimeUtc || (request.From == _lastRequestUser && !_isScheduleActive))
            {
                _lastRequestUtc = now;
                _isScheduleActive = false;
                
                await _busPublisher.PublishAsync(lightDisplay);
            }
            else
            {
                _isScheduleActive = true;
                _lastRequestUtc = scheduledEnqueueTimeUtc;

                await _busPublisher.PublishAsync(lightDisplay, scheduledEnqueueTimeUtc);
            }

            _lastRequestUser = request.From;

            metric.IsValidCommand = true;
            metric.DisplayType = lightDisplay.GetType().Name;
            metric.Command = lightDisplay;
            metric.IsScheduled = _isScheduleActive;
            metric.ScheduledAtUtc = _isScheduleActive ? (DateTime?) scheduledEnqueueTimeUtc : null;
            metric.ScheduledDelaySecs = _isScheduleActive ? (double?)(scheduledEnqueueTimeUtc - now).TotalSeconds : null;

            await _metricWriter.SendAsync(metric);

            return _isScheduleActive ? LightsResponse.Scheduled(scheduledEnqueueTimeUtc) : LightsResponse.Success();
        }