Пример #1
0
        public async Task ReadAllTeamStatus(PerformContext context)
        {
            foreach (var team in db.Teams.ToList())
            {
                var number = await exclusiveTaskFactory.StartNew(() =>
                {
                    using (var helper = new ATHelper(team.ComPort)) return(helper.GetForwardedPhoneNumber());
                });

                var state = await db.ForwardingStates.OrderByDescending(s => s.When).FirstOrDefaultAsync();

                if (state?.RawPhoneNumber == number)
                {
                    continue;
                }

                var newState = new ForwardingState
                {
                    Team                = team,
                    When                = DateTimeOffset.Now,
                    RawPhoneNumber      = number,
                    DetectedPhoneNumber = db.UserPhoneNumbers
                                          .FirstOrDefault(p => p.Value == number)
                };

                db.ForwardingStates.Add(newState);
                db.SaveChanges();
            }
        }
Пример #2
0
        private async Task ReadTeamStatus(SupportTeam team, PerformContext context)
        {
            var number = await TaskQueue.Enqueue(async() =>
            {
                using (var helper = new ATHelper(team.ConnectionString))
                {
                    await helper.OpenAsync();
                    return(await helper.GetForwardedPhoneNumber());
                }
            });

            context.WriteLine($"Team {team.Name} is forwarding to '{number}'.");

            var state = await db.ForwardingStates.Where(s => s.TeamId == team.Id).OrderByDescending(s => s.When)
                        .FirstOrDefaultAsync();

            if (state?.RawPhoneNumber == number)
            {
                context.WriteLine("No state change, completed check.");
                return;
            }

            context.WriteLine($"Change detected from '{state?.RawPhoneNumber}' to '{number}'.");
            var newState = new ForwardingState
            {
                Team                = team,
                When                = DateTimeOffset.Now,
                RawPhoneNumber      = number,
                DetectedPhoneNumber = db.UserPhoneNumbers.FirstOrDefault(p => p.Value == number)
            };

            db.ForwardingStates.Add(newState);
            await db.SaveChangesAsync();

            var prevStateId = state?.Id;
            await db.Teams.Where(t => t.Id == team.Id).SelectMany(t => t.Members).Select(m => m.User)
            .SelectMany(u => u.ApiKeys).Where(a => a.CallbackUrl != null).ForEachAsync(apiKey =>
            {
                context.WriteLine($"Scheduling notification to {apiKey.CallbackUrl}.");
                BackgroundJob.Enqueue <IPublisher>(p =>
                                                   p.NotifyStateChange(prevStateId, newState.Id, apiKey.CallbackUrl, null));
            });
        }