Пример #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));
            });
        }
Пример #3
0
        private async Task ForwardImpl(PerformContext context, string comPort, string phoneNumber)
        {
            await exclusiveTaskFactory.StartNew(() =>
            {
                context.WriteLine(Debug, "Forwarding ...");
                using (var helper = new ATHelper(comPort))
                {
                    helper.ForwardTo(phoneNumber);
                    context.WriteLine(Info, "Applied forward");

                    context.WriteLine(Debug, "Verifying ...");
                    var res = helper.GetForwardedPhoneNumber();
                    if (res != phoneNumber)
                    {
                        context.WriteLine(Error, $"Verification returned {res}, expected {phoneNumber}");
                        throw new Exception("Verification failed after applying forward");
                    }
                    context.WriteLine(Info, $"Verified forward to {phoneNumber}");
                }
            }).ConfigureAwait(false);
        }
Пример #4
0
        private async Task ForwardImpl(PerformContext context, string connectionString, string phoneNumber)
        {
            await TaskQueue.Enqueue(async() =>
            {
                context.WriteLine(Debug, "Forwarding ...");
                using (var helper = new ATHelper(connectionString))
                {
                    await helper.OpenAsync();
                    await helper.ForwardTo(phoneNumber);
                    context.WriteLine(Info, "Applied forward");

                    context.WriteLine(Debug, "Verifying ...");
                    var res = await helper.GetForwardedPhoneNumber();
                    if (res != phoneNumber)
                    {
                        context.WriteLine(Error, $"Verification returned {res}, expected {phoneNumber}");
                        throw new Exception("Verification failed after applying forward");
                    }
                    context.WriteLine(Info, $"Verified forward to {phoneNumber}");
                }
            }).ConfigureAwait(false);
        }