Esempio n. 1
0
        private async Task ShowMeasurePoints(User user, string[] arguments)
        {
            if (user.Context == null)
            {
                throw new UnauthorizedCommandException(GetMeasurePointsCommand);
            }

            var client        = new Lers.Rest.MeasurePointsClient(user.Context.BaseUri.ToString(), user.Context.RestClient);
            var measurePoints = await client.GetMeasurePoints(arguments);

            long chatId = user.ChatId;

            if (!measurePoints.Any())
            {
                await _bot.SendText(chatId, "Ничего не найдено");

                return;
            }

            await SendListMessage(chatId, measurePoints.OrderBy(x => x.FullTitle, new NaturalSortComparer()), x => x.FullTitle);
        }
Esempio n. 2
0
        private async System.Threading.Tasks.Task ShowCurrents(User user, string[] arguments)
        {
            if (user.Context == null)
            {
                throw new UnauthorizedCommandException(GetCurrentsCommand);
            }

            var measurePointsClient = new Lers.Rest.MeasurePointsClient(user.Context.BaseUri.ToString(),
                                                                        user.Context.RestClient);

            var manualPollClient = new Lers.Rest.ManualPollClient(user.Context.BaseUri.ToString(),
                                                                  user.Context.RestClient);

            long chatId = user.ChatId;

            var measurePoint = (await measurePointsClient.GetMeasurePoints(arguments))
                               .FirstOrDefault();

            if (measurePoint == null)
            {
                await _bot.SendText(chatId, "Точка учёта не найдена");

                return;
            }

            await _bot.SendText(chatId, $"Точка учёта {measurePoint.FullTitle}");

            var result = await manualPollClient.PollArchiveAsync(DateTimeOffset.Now, DateTimeOffset.Now, new Lers.Rest.PollArchiveRequestParameters
            {
                AbsentDataOnly    = false,
                RequestedDataMask = Lers.Rest.DeviceDataType.Current,
                MeasurePoints     = new int[] { measurePoint.Id },
                StartMode         = Lers.Rest.PollManualStartMode.Force
            });

            if (result.Result != Lers.Rest.PollManualStartResult.Success)
            {
                throw new BotException("Не удалось запустить опрос текущих. " + result.Result);
            }

            await _bot.SendText(chatId, "Запущен опрос");

            var tcs = new TaskCompletionSource <bool>();

            // Подписываемся на событие о чтении текущих.

            await using var hubClient = new NotificationsClient(user.Context.BaseUri + "api/v0.1/rpc/serverHub",
                                                                user.Context.Token);

            await hubClient.Connect();

            using var subscription = hubClient.Subscribe <Lers.Rest.CurrentConsumptionDataRead>(Lers.Rest.Operation.SAVE_CURRENT_DATA,
                                                                                                Lers.Rest.EntityType.PollSession,
                                                                                                result.PollSessionId,
                                                                                                async(data) =>
            {
                await SendCurrents(chatId, data.Consumption);
                tcs.SetResult(true);
            });

            var timeoutTask = Task.Delay(TimeSpan.FromMinutes(2));

            var completed = await Task.WhenAny(tcs.Task, timeoutTask);

            if (completed == timeoutTask)
            {
                throw new BotException("Не удалось считать текущие данные за 2 минуты");
            }
        }