Ejemplo n.º 1
0
        public async Task <JsonResult> GetStatus(long ticks, string adapterSetupId)
        {
            var timestamp = new DateTime(ticks);

            _logService.LogInfo($"GetStatus[{timestamp.ToLongTimeString()}]");
            var adapters    = _adapterSettingService.GetAdapters(adapterSetupId);
            var tokenSource = new CancellationTokenSource(new TimeSpan(0, 1, 0));

            var contracts = await Task.Run(() =>
            {
                while (!tokenSource.IsCancellationRequested)
                {
                    var messages = _adapterService.GetUpdates(timestamp, adapters);
                    if (messages.Any())
                    {
                        return(messages);
                    }

                    Task.Delay(50);
                }

                return(new List <AdapterStatusMessage>());
            }, tokenSource.Token
                                           );

            if (tokenSource.IsCancellationRequested)
            {
                return(new JsonResult(new { context = "Exception", value = "timeout" }));
            }

            var returnValue = new
            {
                context   = "success",
                timestamp = contracts.Any()
                    ? contracts.OrderByDescending(contract => contract.Timestamp).First().Timestamp.Ticks.ToString()
                    : timestamp.Ticks.ToString(),
                result = contracts.ToArray()
            };

            return(new JsonResult(returnValue));
        }