From fbfde769b1b3e4de023c59111105886eb7a9aecd Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 13 Nov 2024 03:31:43 +0400 Subject: [PATCH 01/14] =?UTF-8?q?add:=20=D1=81=D1=83=D1=89=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D1=82=D0=B5=D0=BF=D0=BB=D0=B8=D1=86=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud/ApplicationContext.cs | 1 + Cloud/Models/Farm.cs | 1 + Cloud/Models/Greenhouse.cs | 14 ++++++++++++++ Cloud/Models/Support/HeatingMode.cs | 8 ++++++++ Cloud/Models/Support/WateringMode.cs | 8 ++++++++ 5 files changed, 32 insertions(+) create mode 100644 Cloud/Models/Greenhouse.cs create mode 100644 Cloud/Models/Support/HeatingMode.cs create mode 100644 Cloud/Models/Support/WateringMode.cs diff --git a/Cloud/ApplicationContext.cs b/Cloud/ApplicationContext.cs index 6a60cf1..681040d 100644 --- a/Cloud/ApplicationContext.cs +++ b/Cloud/ApplicationContext.cs @@ -6,6 +6,7 @@ public class ApplicationContext : DbContext { public DbSet Users { get; set; } = null!; public DbSet Farms { get; set; } = null!; + public DbSet Greenhouses { get; set; } = null!; public ApplicationContext(DbContextOptions options) : base(options) diff --git a/Cloud/Models/Farm.cs b/Cloud/Models/Farm.cs index 5cc1ac8..48fd855 100644 --- a/Cloud/Models/Farm.cs +++ b/Cloud/Models/Farm.cs @@ -7,5 +7,6 @@ public int UserId { get; set; } public User? User { get; set; } public string RaspberryIP { get; set; } + List Greenhouses { get; set; } = new(); } } \ No newline at end of file diff --git a/Cloud/Models/Greenhouse.cs b/Cloud/Models/Greenhouse.cs new file mode 100644 index 0000000..ec2f54a --- /dev/null +++ b/Cloud/Models/Greenhouse.cs @@ -0,0 +1,14 @@ +using Cloud.Models.Support; + +namespace Cloud.Models +{ + public class Greenhouse + { + public int Id { get; set; } + public int RecomendedTemperature { get; set; } + public WateringMode WateringMode { get; set; } + public HeatingMode HeatingMode { get; set; } + public int FarmId { get; set; } + public Farm? Farm { get; set; } + } +} \ No newline at end of file diff --git a/Cloud/Models/Support/HeatingMode.cs b/Cloud/Models/Support/HeatingMode.cs new file mode 100644 index 0000000..81f082e --- /dev/null +++ b/Cloud/Models/Support/HeatingMode.cs @@ -0,0 +1,8 @@ +namespace Cloud.Models.Support +{ + public enum HeatingMode + { + Manual, + Auto + } +} \ No newline at end of file diff --git a/Cloud/Models/Support/WateringMode.cs b/Cloud/Models/Support/WateringMode.cs new file mode 100644 index 0000000..3e58fe4 --- /dev/null +++ b/Cloud/Models/Support/WateringMode.cs @@ -0,0 +1,8 @@ +namespace Cloud.Models.Support +{ + public enum WateringMode + { + Manual, + Auto + } +} \ No newline at end of file -- 2.25.1 From 1e2bd0566769ffdbed36dd2831a4131f64fba0fb Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 13 Nov 2024 03:32:30 +0400 Subject: [PATCH 02/14] =?UTF-8?q?add:=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D1=84=D0=B5=D0=B9=D1=81=D1=8B=20=D0=B1=D1=80=D0=BE=D0=BA=D0=B5?= =?UTF-8?q?=D1=80=D0=B0,=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D0=B8=20=D1=82=D0=BE=D0=BF=D0=B8=D0=BA=D0=BE=D0=B2=20=D0=B1?= =?UTF-8?q?=D1=80=D0=BE=D0=BA=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud/Services/Broker/IBrokerConsumer.cs | 7 +++++++ Cloud/Services/Broker/IBrokerProdurcer.cs | 9 +++++++++ Cloud/Services/Broker/IBrokerService.cs | 10 ++++++++++ .../Broker/Implement/Kafka/KafkaService.cs | 17 +++++++++++++++++ Cloud/Services/Broker/Support/Command.cs | 9 +++++++++ Cloud/Services/Broker/Support/CommandResult.cs | 9 +++++++++ Cloud/Services/Broker/Support/GreenhouseInfo.cs | 12 ++++++++++++ 7 files changed, 73 insertions(+) create mode 100644 Cloud/Services/Broker/IBrokerConsumer.cs create mode 100644 Cloud/Services/Broker/IBrokerProdurcer.cs create mode 100644 Cloud/Services/Broker/IBrokerService.cs create mode 100644 Cloud/Services/Broker/Implement/Kafka/KafkaService.cs create mode 100644 Cloud/Services/Broker/Support/Command.cs create mode 100644 Cloud/Services/Broker/Support/CommandResult.cs create mode 100644 Cloud/Services/Broker/Support/GreenhouseInfo.cs diff --git a/Cloud/Services/Broker/IBrokerConsumer.cs b/Cloud/Services/Broker/IBrokerConsumer.cs new file mode 100644 index 0000000..a421f6d --- /dev/null +++ b/Cloud/Services/Broker/IBrokerConsumer.cs @@ -0,0 +1,7 @@ +namespace Cloud.Services.Broker +{ + public interface IBrokerConsumer + { + // TODO: добавить методы для получения данных + } +} \ No newline at end of file diff --git a/Cloud/Services/Broker/IBrokerProdurcer.cs b/Cloud/Services/Broker/IBrokerProdurcer.cs new file mode 100644 index 0000000..ac3b974 --- /dev/null +++ b/Cloud/Services/Broker/IBrokerProdurcer.cs @@ -0,0 +1,9 @@ +using Cloud.Services.Broker.Support; + +namespace Cloud.Services.Broker +{ + public interface IBrokerProdurcer + { + Task ProduceAsync(string topic, Command command); + } +} \ No newline at end of file diff --git a/Cloud/Services/Broker/IBrokerService.cs b/Cloud/Services/Broker/IBrokerService.cs new file mode 100644 index 0000000..910a25e --- /dev/null +++ b/Cloud/Services/Broker/IBrokerService.cs @@ -0,0 +1,10 @@ +using Cloud.Services.Broker.Support; + +namespace Cloud.Services.Broker +{ + public interface IBrokerService + { + Task Produce(Command command); + Task Consume(string topic); + } +} \ No newline at end of file diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaService.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaService.cs new file mode 100644 index 0000000..44e099b --- /dev/null +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaService.cs @@ -0,0 +1,17 @@ +using Cloud.Services.Broker.Support; + +namespace Cloud.Services.Broker.Implement.Kafka +{ + public class KafkaService : IBrokerService + { + public Task Consume(string topic) + { + throw new NotImplementedException(); + } + + public Task Produce(Command command) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/Cloud/Services/Broker/Support/Command.cs b/Cloud/Services/Broker/Support/Command.cs new file mode 100644 index 0000000..ef2a44b --- /dev/null +++ b/Cloud/Services/Broker/Support/Command.cs @@ -0,0 +1,9 @@ +namespace Cloud.Services.Broker.Support +{ + public class Command + { + public int Id { get; set; } + public int GreenhouseId { get; set; } + public string CommandName { get; set; } = null!; + } +} \ No newline at end of file diff --git a/Cloud/Services/Broker/Support/CommandResult.cs b/Cloud/Services/Broker/Support/CommandResult.cs new file mode 100644 index 0000000..06d997d --- /dev/null +++ b/Cloud/Services/Broker/Support/CommandResult.cs @@ -0,0 +1,9 @@ +namespace Cloud.Services.Broker.Support +{ + public class CommandResult + { + public int CommandId { get; set; } + public int GreenhouseId { get; set; } + public string ResultMessage { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/Cloud/Services/Broker/Support/GreenhouseInfo.cs b/Cloud/Services/Broker/Support/GreenhouseInfo.cs new file mode 100644 index 0000000..c5a2140 --- /dev/null +++ b/Cloud/Services/Broker/Support/GreenhouseInfo.cs @@ -0,0 +1,12 @@ +namespace Cloud.Services.Broker.Support +{ + public class GreenhouseInfo + { + public int Id { get; set; } + public int PercentWater { get; set; } + public int SoilTemperature { get; set; } + public bool PumpStatus { get; set; } + public bool HeatingStatus { get; set; } + public bool AutoWateringStatus { get; set; } + } +} \ No newline at end of file -- 2.25.1 From 2a9508f737adb387a2772a4152dda48fb91159b3 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 13 Nov 2024 03:33:03 +0400 Subject: [PATCH 03/14] =?UTF-8?q?add:=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80?= =?UTF-8?q?=D0=BE=D0=BB=D0=BB=D0=B5=D1=80=20=D1=82=D0=B5=D0=BF=D0=BB=D0=B8?= =?UTF-8?q?=D1=86=D1=8B,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=20=D0=B1=D1=80?= =?UTF-8?q?=D0=BE=D0=BA=D0=B5=D1=80=D0=B0=20=D0=B2=20DI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud/Controllers/GreengouseController.cs | 79 +++++++++++++++++++++++ Cloud/Program.cs | 4 +- Cloud/Requests/GreenhouseRequest.cs | 11 ++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 Cloud/Controllers/GreengouseController.cs create mode 100644 Cloud/Requests/GreenhouseRequest.cs diff --git a/Cloud/Controllers/GreengouseController.cs b/Cloud/Controllers/GreengouseController.cs new file mode 100644 index 0000000..473f292 --- /dev/null +++ b/Cloud/Controllers/GreengouseController.cs @@ -0,0 +1,79 @@ +using Cloud.Models; +using Cloud.Requests; +using Cloud.Services.Broker; +using Cloud.Services.Broker.Support; +using Microsoft.AspNetCore.Mvc; + +namespace Cloud.Controllers +{ + [ApiController] + [Route("api/user/{userId}/farm/{farmId}/greenhouse")] + public class GreenhouseController : ControllerBase + { + private readonly IBrokerService _brokerService; + private readonly ApplicationContext _context; + private readonly IConfiguration _config; + public GreenhouseController(IConfiguration config, ApplicationContext context, + IBrokerService brokerService) + { + _brokerService = brokerService; + _context = context; + _config = config; + } + + /** + * Возвращает текущую информацию о всех теплицах пользователя + */ + [HttpGet] + public async Task>> GetAll(int userId, int farmId) + { + throw new NotImplementedException(); + } + + /** + * Возвращает текущую информацию о конкретной теплице + */ + [HttpGet("{greenhouseId}")] + public async Task> Get(int userId, int farmId, int greenhouseId) + { + throw new NotImplementedException(); + } + + /** + * Возвращает сохраненные данные для автоматизации теплицы + */ + [HttpGet("{greenhouseId}/settings")] + public async Task> GetGreenhouse(int userId, int farmId, int greenhouseId) + { + throw new NotImplementedException(); + } + + /** + * Сохраняет в базе данных API данные для автоматизации теплицы + */ + [HttpPost] + public async Task> SaveToDatabase(int userId, int farmId, GreenhouseRequest greenhouse) + { + throw new NotImplementedException(); + } + + /** + * Обновляет в базе данных API данные для автоматизации теплицы + */ + [HttpPut("{greenhouseId}/settings")] + public async Task> Update(int userId, int farmId, int greenhouseId, GreenhouseRequest greenhouse) + { + throw new NotImplementedException(); + } + + /** + * Удаляет из базы данных API запись настроек автоматизации теплицы + */ + [HttpDelete("{greenhouseId}")] + public async Task Delete(int userId, int farmId, int greenhouseId) + { + + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/Cloud/Program.cs b/Cloud/Program.cs index c02b6fb..7ab00f6 100644 --- a/Cloud/Program.cs +++ b/Cloud/Program.cs @@ -7,11 +7,13 @@ using FluentValidation; using FluentValidation.AspNetCore; using Cloud.Validation; using StackExchange.Redis; +using Cloud.Services.Broker.Implement.Kafka; +using Cloud.Services.Broker; var builder = WebApplication.CreateBuilder(args); // Add services to the container. - +builder.Services.AddSingleton(); //Redis configuration builder.Services.AddSingleton(sp => { diff --git a/Cloud/Requests/GreenhouseRequest.cs b/Cloud/Requests/GreenhouseRequest.cs new file mode 100644 index 0000000..7f2fe80 --- /dev/null +++ b/Cloud/Requests/GreenhouseRequest.cs @@ -0,0 +1,11 @@ +using Cloud.Models.Support; + +namespace Cloud.Requests +{ + public class GreenhouseRequest + { + public int RecomendedTemperature { get; set; } + public WateringMode WateringMode { get; set; } + public HeatingMode HeatingMode { get; set; } + } +} \ No newline at end of file -- 2.25.1 From 5687949f96e5e7642a30ab8aeabab748bec0fc12 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 13 Nov 2024 03:33:49 +0400 Subject: [PATCH 04/14] =?UTF-8?q?todo:=20=D0=B2=20=D0=B4=D0=BE=D0=BA=D0=B5?= =?UTF-8?q?=D1=80=D1=84=D0=B0=D0=B9=D0=BB=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8E=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud/Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Cloud/Dockerfile b/Cloud/Dockerfile index 084ab62..7a518b4 100644 --- a/Cloud/Dockerfile +++ b/Cloud/Dockerfile @@ -25,5 +25,6 @@ RUN dotnet publish "./Cloud.csproj" -c $configuration -o /app/publish /p:UseAppH FROM base AS final WORKDIR /app COPY --from=publish /app/publish . +# TODO: Добавить инициализацию базы данных с помощью миграции ENTRYPOINT ["dotnet", "Cloud.dll"] -- 2.25.1 From c230d86404a3e07eb5c2ac3a89df1cb5f6c7edcd Mon Sep 17 00:00:00 2001 From: mfnefd Date: Tue, 19 Nov 2024 23:23:21 +0400 Subject: [PATCH 05/14] =?UTF-8?q?add:=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8?= =?UTF-8?q?=D1=81=20=D0=B1=D1=80=D0=BE=D0=BA=D0=B5=D1=80=D0=B0,=20=D1=81?= =?UTF-8?q?=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20=D0=B1=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud/Cloud.csproj | 1 + Cloud/Services/Broker/IBrokerConsumer.cs | 4 +- ...IBrokerProdurcer.cs => IBrokerProducer.cs} | 2 +- Cloud/Services/Broker/IBrokerService.cs | 4 +- .../Broker/Implement/Kafka/KafkaConsumer.cs | 51 +++++++++++++++++++ .../Broker/Implement/Kafka/KafkaProducer.cs | 28 ++++++++++ .../Broker/Implement/Kafka/KafkaService.cs | 17 ++++--- Cloud/Services/Broker/Support/Command.cs | 7 +-- .../Services/Broker/Support/CommandResult.cs | 2 +- .../Services/Broker/Support/GreenhouseInfo.cs | 2 +- .../Broker/Support/IBrokerResponse.cs | 6 +++ 11 files changed, 109 insertions(+), 15 deletions(-) rename Cloud/Services/Broker/{IBrokerProdurcer.cs => IBrokerProducer.cs} (78%) create mode 100644 Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs create mode 100644 Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs create mode 100644 Cloud/Services/Broker/Support/IBrokerResponse.cs diff --git a/Cloud/Cloud.csproj b/Cloud/Cloud.csproj index 6a5fc81..042333f 100644 --- a/Cloud/Cloud.csproj +++ b/Cloud/Cloud.csproj @@ -7,6 +7,7 @@ + diff --git a/Cloud/Services/Broker/IBrokerConsumer.cs b/Cloud/Services/Broker/IBrokerConsumer.cs index a421f6d..7997ad0 100644 --- a/Cloud/Services/Broker/IBrokerConsumer.cs +++ b/Cloud/Services/Broker/IBrokerConsumer.cs @@ -1,7 +1,9 @@ +using Cloud.Services.Broker.Support; + namespace Cloud.Services.Broker { public interface IBrokerConsumer { - // TODO: добавить методы для получения данных + T? WaitMessage(string topic) where T : IBrokerResponse; } } \ No newline at end of file diff --git a/Cloud/Services/Broker/IBrokerProdurcer.cs b/Cloud/Services/Broker/IBrokerProducer.cs similarity index 78% rename from Cloud/Services/Broker/IBrokerProdurcer.cs rename to Cloud/Services/Broker/IBrokerProducer.cs index ac3b974..abed579 100644 --- a/Cloud/Services/Broker/IBrokerProdurcer.cs +++ b/Cloud/Services/Broker/IBrokerProducer.cs @@ -2,7 +2,7 @@ using Cloud.Services.Broker.Support; namespace Cloud.Services.Broker { - public interface IBrokerProdurcer + public interface IBrokerProducer { Task ProduceAsync(string topic, Command command); } diff --git a/Cloud/Services/Broker/IBrokerService.cs b/Cloud/Services/Broker/IBrokerService.cs index 910a25e..f19aeb4 100644 --- a/Cloud/Services/Broker/IBrokerService.cs +++ b/Cloud/Services/Broker/IBrokerService.cs @@ -4,7 +4,7 @@ namespace Cloud.Services.Broker { public interface IBrokerService { - Task Produce(Command command); - Task Consume(string topic); + Task Produce(Command command); + T? Consume(string topic) where T : IBrokerResponse; } } \ No newline at end of file diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs new file mode 100644 index 0000000..2b31499 --- /dev/null +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs @@ -0,0 +1,51 @@ + +using Cloud.Services.Broker.Support; +using Confluent.Kafka; +using System.Text.Json; + +namespace Cloud.Services.Broker.Implement.Kafka +{ + public class KafkaConsumer : IBrokerConsumer + { + private readonly IConsumer _consumer; + + public KafkaConsumer(IConfiguration config) + { + var consumerConfig = new ConsumerConfig() + { + BootstrapServers = config["Kafka:BootstrapServers"], + GroupId = config["Kafka:GroupId"], + AutoOffsetReset = AutoOffsetReset.Earliest, + }; + + _consumer = new ConsumerBuilder(consumerConfig).Build(); + } + + public T? WaitMessage(string topic) + where T : IBrokerResponse + { + try + { + _consumer.Subscribe(topic); + + var consumeResult = _consumer.Consume(TimeSpan.FromMilliseconds(1000)); + + if (consumeResult == null) + { + // No message received from Kafka within the specified timeout. + return default; + } + return JsonSerializer.Deserialize(consumeResult.Message.Value); + + } + catch (Exception ex) + { + throw; + } + finally + { + _consumer.Close(); + } + } + } +} \ No newline at end of file diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs new file mode 100644 index 0000000..b301084 --- /dev/null +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs @@ -0,0 +1,28 @@ +using Cloud.Services.Broker.Support; +using Confluent.Kafka; + +namespace Cloud.Services.Broker.Implement.Kafka +{ + public class KafkaProducer : IBrokerProducer + { + private readonly IProducer _producer; + + public KafkaProducer(IConfiguration configuration) + { + var producerConfig = new ProducerConfig + { + BootstrapServers = configuration["Kafka:BootstrapServers"] + }; + + //Build the Producer + _producer = new ProducerBuilder(producerConfig).Build(); + } + public async Task ProduceAsync(string topic, Command command) + { + var message = new Message { Key = Guid.NewGuid(), Value = command }; + + //Produce the Message + await _producer.ProduceAsync(topic, message); + } + } +} \ No newline at end of file diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaService.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaService.cs index 44e099b..c0b90ba 100644 --- a/Cloud/Services/Broker/Implement/Kafka/KafkaService.cs +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaService.cs @@ -4,14 +4,19 @@ namespace Cloud.Services.Broker.Implement.Kafka { public class KafkaService : IBrokerService { - public Task Consume(string topic) + private readonly KafkaProducer _producer; + private readonly KafkaConsumer _consumer; + + public KafkaService(IConfiguration configuration) { - throw new NotImplementedException(); + _producer = new KafkaProducer(configuration); + _consumer = new KafkaConsumer(configuration); } - public Task Produce(Command command) - { - throw new NotImplementedException(); - } + public T? Consume(string topic) + where T : IBrokerResponse => _consumer.WaitMessage(topic); + + public async Task Produce(Command command) + => await _producer.ProduceAsync("commands", command); } } \ No newline at end of file diff --git a/Cloud/Services/Broker/Support/Command.cs b/Cloud/Services/Broker/Support/Command.cs index ef2a44b..2debae3 100644 --- a/Cloud/Services/Broker/Support/Command.cs +++ b/Cloud/Services/Broker/Support/Command.cs @@ -1,9 +1,10 @@ +using System.Text.Json; + namespace Cloud.Services.Broker.Support { - public class Command + public class Command { - public int Id { get; set; } - public int GreenhouseId { get; set; } + public Guid GreenhouseId { get; set; } public string CommandName { get; set; } = null!; } } \ No newline at end of file diff --git a/Cloud/Services/Broker/Support/CommandResult.cs b/Cloud/Services/Broker/Support/CommandResult.cs index 06d997d..e66004d 100644 --- a/Cloud/Services/Broker/Support/CommandResult.cs +++ b/Cloud/Services/Broker/Support/CommandResult.cs @@ -1,6 +1,6 @@ namespace Cloud.Services.Broker.Support { - public class CommandResult + public class CommandResult : IBrokerResponse { public int CommandId { get; set; } public int GreenhouseId { get; set; } diff --git a/Cloud/Services/Broker/Support/GreenhouseInfo.cs b/Cloud/Services/Broker/Support/GreenhouseInfo.cs index c5a2140..bea028b 100644 --- a/Cloud/Services/Broker/Support/GreenhouseInfo.cs +++ b/Cloud/Services/Broker/Support/GreenhouseInfo.cs @@ -1,6 +1,6 @@ namespace Cloud.Services.Broker.Support { - public class GreenhouseInfo + public class GreenhouseInfo : IBrokerResponse { public int Id { get; set; } public int PercentWater { get; set; } diff --git a/Cloud/Services/Broker/Support/IBrokerResponse.cs b/Cloud/Services/Broker/Support/IBrokerResponse.cs new file mode 100644 index 0000000..e13b65f --- /dev/null +++ b/Cloud/Services/Broker/Support/IBrokerResponse.cs @@ -0,0 +1,6 @@ +namespace Cloud.Services.Broker.Support +{ + public interface IBrokerResponse + { + } +} \ No newline at end of file -- 2.25.1 From fed96d5b86b42399c3921a1193e85f60a2d35b0c Mon Sep 17 00:00:00 2001 From: mfnefd Date: Tue, 19 Nov 2024 23:42:12 +0400 Subject: [PATCH 06/14] =?UTF-8?q?del:=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8?= =?UTF-8?q?=D1=81=D1=8B=20=D0=B1=D1=80=D0=BE=D0=BA=D0=B5=D1=80=D0=B0=20?= =?UTF-8?q?=D0=BE=D0=BD=D0=B8=20=D1=83=D0=B6=D0=B5=20=D0=B5=D1=81=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud/Services/BackgroundWorkerService.cs | 39 ----------------- Cloud/Services/ConsumerService.cs | 51 ----------------------- Cloud/Services/ProducerService.cs | 33 --------------- 3 files changed, 123 deletions(-) delete mode 100644 Cloud/Services/BackgroundWorkerService.cs delete mode 100644 Cloud/Services/ConsumerService.cs delete mode 100644 Cloud/Services/ProducerService.cs diff --git a/Cloud/Services/BackgroundWorkerService.cs b/Cloud/Services/BackgroundWorkerService.cs deleted file mode 100644 index f7fbf34..0000000 --- a/Cloud/Services/BackgroundWorkerService.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace Cloud.Services -{ - public class BackgroundWorkerService : BackgroundService - { - public readonly ILogger _logger; - private readonly ConsumerService _consumerService; - - public BackgroundWorkerService(ILogger logger, ConsumerService consumer) - { - _logger = logger; - _consumerService = consumer; - } - - //Backghround Service, This will run continuously - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - try - { - while (!stoppingToken.IsCancellationRequested) - { - //_logger.LogInformation("Background Service is Runing at : {time}", DateTimeOffset.Now); - - string request = await _consumerService.WaitMessage("ValvesHeatersRequest"); //Consume the Kafka Message - - //After Consume the Order Request Can process the order - if (!string.IsNullOrEmpty(request)) - _logger.LogInformation("Valves-Heaters Request : {value}", request); - - - await Task.Delay(1000, stoppingToken); - } - } - catch (Exception ex) - { - _logger.LogError($"BackgroundWorkerService - Exception {ex}"); - } - } - } -} diff --git a/Cloud/Services/ConsumerService.cs b/Cloud/Services/ConsumerService.cs deleted file mode 100644 index 82d5bb2..0000000 --- a/Cloud/Services/ConsumerService.cs +++ /dev/null @@ -1,51 +0,0 @@ -using Confluent.Kafka; - -namespace Cloud.Services -{ - public class ConsumerService - { - private IConsumer _consumer; - private ConsumerConfig consumerConfig; - public ConsumerService(IConfiguration configuration) - { - consumerConfig = new ConsumerConfig - { - BootstrapServers = configuration["Kafka:BootstrapServers"], - GroupId = configuration["Kafka:GroupId"], - AutoOffsetReset = AutoOffsetReset.Earliest, - }; - - _consumer = new ConsumerBuilder(consumerConfig).Build(); - } - - //Consume Method - public async TaskWaitMessage(string topic) - { - try - { - _consumer.Subscribe(topic); - - var consumeResult = _consumer.Consume(TimeSpan.FromMilliseconds(1000)); - - if (consumeResult != null) - { - return consumeResult.Message.Value; - } - else - { - //No message received from Kafka within the specified timeout. - } - return ""; - - } - catch (Exception ex) - { - return ""; - } - finally - { - _consumer.Close(); - } - } - } -} diff --git a/Cloud/Services/ProducerService.cs b/Cloud/Services/ProducerService.cs deleted file mode 100644 index 073bdc6..0000000 --- a/Cloud/Services/ProducerService.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Confluent.Kafka; - -namespace Cloud.Services -{ - public class ProducerService - { - private readonly IProducer _producer; - - public ProducerService(IConfiguration configuration) - { - var producerConfig = new ProducerConfig - { - BootstrapServers = configuration["Kafka:BootstrapServers"] - }; - - //Build the Producer - _producer = new ProducerBuilder(producerConfig).Build(); - } - - //Method for Produce the Message to Kafka Topic - public async Task ProduceMessageAsync(string topic, string value) - { - var kafkaMessage = new Message - { - Key = Guid.NewGuid().ToString(), - Value = value - }; - - //Produce the Message - await _producer.ProduceAsync(topic, kafkaMessage); - } - } -} -- 2.25.1 From 9c1720e1316d6bcfb14a36f6ccfd2b3dc433f3a5 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 4 Dec 2024 01:57:10 +0400 Subject: [PATCH 07/14] fix: kafka service --- Cloud/Controllers/ValveController.cs | 9 ++--- Cloud/Services/Broker/IBrokerConsumer.cs | 2 +- Cloud/Services/Broker/IBrokerService.cs | 5 ++- .../Broker/Implement/Kafka/KafkaConsumer.cs | 34 ++++++++++++------- .../Broker/Implement/Kafka/KafkaProducer.cs | 22 ++++++++---- .../Broker/Implement/Kafka/KafkaService.cs | 14 ++++++-- 6 files changed, 55 insertions(+), 31 deletions(-) diff --git a/Cloud/Controllers/ValveController.cs b/Cloud/Controllers/ValveController.cs index dbc081a..9fc973a 100644 --- a/Cloud/Controllers/ValveController.cs +++ b/Cloud/Controllers/ValveController.cs @@ -1,5 +1,6 @@ using Cloud.Requests; using Cloud.Services; +using Cloud.Services.Broker; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.ComponentModel.DataAnnotations; @@ -14,11 +15,11 @@ namespace Cloud.Controllers { //Контроллер вентиля - private readonly ProducerService _producerService; + private readonly IBrokerService _kafkaService; - public ValveController(ProducerService producerService) + public ValveController(IBrokerService kafkaService) { - _producerService = producerService; + _kafkaService = kafkaService; } [HttpPost("farm/{farmId}/greenhouse/{ghId}/watering")] @@ -34,7 +35,7 @@ namespace Cloud.Controllers var message = JsonSerializer.Serialize(kafkaRequest); return Ok(kafkaRequest); - /*await _producerService.ProduceMessageAsync("ValvesHeatersRequest", message); + /*await _kafkaService.ProduceAsync("ValvesHeatersRequest", message); return Ok($"Valve status is {request.Action}");*/ } diff --git a/Cloud/Services/Broker/IBrokerConsumer.cs b/Cloud/Services/Broker/IBrokerConsumer.cs index 7997ad0..c152981 100644 --- a/Cloud/Services/Broker/IBrokerConsumer.cs +++ b/Cloud/Services/Broker/IBrokerConsumer.cs @@ -4,6 +4,6 @@ namespace Cloud.Services.Broker { public interface IBrokerConsumer { - T? WaitMessage(string topic) where T : IBrokerResponse; + IEnumerable? WaitMessages(string topic) where T : IBrokerResponse; } } \ No newline at end of file diff --git a/Cloud/Services/Broker/IBrokerService.cs b/Cloud/Services/Broker/IBrokerService.cs index f19aeb4..356157b 100644 --- a/Cloud/Services/Broker/IBrokerService.cs +++ b/Cloud/Services/Broker/IBrokerService.cs @@ -2,9 +2,8 @@ using Cloud.Services.Broker.Support; namespace Cloud.Services.Broker { - public interface IBrokerService + public interface IBrokerService : IBrokerProducer, IBrokerConsumer { - Task Produce(Command command); - T? Consume(string topic) where T : IBrokerResponse; + void ChangeBrokerIp(string ip); } } \ No newline at end of file diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs index 2b31499..d2fe34c 100644 --- a/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs @@ -7,35 +7,31 @@ namespace Cloud.Services.Broker.Implement.Kafka { public class KafkaConsumer : IBrokerConsumer { - private readonly IConsumer _consumer; + private IConsumer _consumer; + private readonly IConfiguration _config; public KafkaConsumer(IConfiguration config) { - var consumerConfig = new ConsumerConfig() - { - BootstrapServers = config["Kafka:BootstrapServers"], - GroupId = config["Kafka:GroupId"], - AutoOffsetReset = AutoOffsetReset.Earliest, - }; - - _consumer = new ConsumerBuilder(consumerConfig).Build(); + _config = config; + ChangeBrokerIp(_config["Kafka:BootstrapServers"]); } - public T? WaitMessage(string topic) + public IEnumerable? WaitMessages(string topic) where T : IBrokerResponse - { + { try { _consumer.Subscribe(topic); var consumeResult = _consumer.Consume(TimeSpan.FromMilliseconds(1000)); - + Console.WriteLine($"================ Received message: {consumeResult?.Message.Value}"); if (consumeResult == null) { // No message received from Kafka within the specified timeout. return default; } - return JsonSerializer.Deserialize(consumeResult.Message.Value); + + return JsonSerializer.Deserialize>(consumeResult.Message.Value); } catch (Exception ex) @@ -47,5 +43,17 @@ namespace Cloud.Services.Broker.Implement.Kafka _consumer.Close(); } } + + public void ChangeBrokerIp(string ip) + { + var consumerConfig = new ConsumerConfig() + { + BootstrapServers = ip, + GroupId = _config["Kafka:GroupId"], + AutoOffsetReset = AutoOffsetReset.Earliest, + }; + + _consumer = new ConsumerBuilder(consumerConfig).Build(); + } } } \ No newline at end of file diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs index b301084..0391cc4 100644 --- a/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs @@ -5,17 +5,14 @@ namespace Cloud.Services.Broker.Implement.Kafka { public class KafkaProducer : IBrokerProducer { - private readonly IProducer _producer; + private IProducer _producer; + private readonly IConfiguration _config; public KafkaProducer(IConfiguration configuration) { - var producerConfig = new ProducerConfig - { - BootstrapServers = configuration["Kafka:BootstrapServers"] - }; + _config = configuration; - //Build the Producer - _producer = new ProducerBuilder(producerConfig).Build(); + ChangeBrokerIp(_config["Kafka:BootstrapServers"]); } public async Task ProduceAsync(string topic, Command command) { @@ -24,5 +21,16 @@ namespace Cloud.Services.Broker.Implement.Kafka //Produce the Message await _producer.ProduceAsync(topic, message); } + + public void ChangeBrokerIp(string ip) + { + var producerConfig = new ProducerConfig + { + BootstrapServers = ip + }; + + //Build the Producer + _producer = new ProducerBuilder(producerConfig).Build(); + } } } \ No newline at end of file diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaService.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaService.cs index c0b90ba..8fc9174 100644 --- a/Cloud/Services/Broker/Implement/Kafka/KafkaService.cs +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaService.cs @@ -13,10 +13,18 @@ namespace Cloud.Services.Broker.Implement.Kafka _consumer = new KafkaConsumer(configuration); } - public T? Consume(string topic) - where T : IBrokerResponse => _consumer.WaitMessage(topic); + public IEnumerable? WaitMessages(string topic) + where T : IBrokerResponse + => _consumer.WaitMessages(topic); - public async Task Produce(Command command) + + public async Task ProduceAsync(string topic, Command command) => await _producer.ProduceAsync("commands", command); + + public void ChangeBrokerIp(string ip) + { + _consumer.ChangeBrokerIp(ip); + _producer.ChangeBrokerIp(ip); + } } } \ No newline at end of file -- 2.25.1 From 8478bf1a471009f8d66ec9e83d95728d928b4057 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 4 Dec 2024 01:57:29 +0400 Subject: [PATCH 08/14] add: greenhouse service --- Cloud/Controllers/GreengouseController.cs | 101 ++++++++++++------ Cloud/Program.cs | 14 +-- Cloud/Services/Domain/IGreenhouseService.cs | 45 ++++++++ .../Domain/Implement/GreenhouseService.cs | 67 ++++++++++++ 4 files changed, 182 insertions(+), 45 deletions(-) create mode 100644 Cloud/Services/Domain/IGreenhouseService.cs create mode 100644 Cloud/Services/Domain/Implement/GreenhouseService.cs diff --git a/Cloud/Controllers/GreengouseController.cs b/Cloud/Controllers/GreengouseController.cs index 473f292..6f77c60 100644 --- a/Cloud/Controllers/GreengouseController.cs +++ b/Cloud/Controllers/GreengouseController.cs @@ -2,75 +2,106 @@ using Cloud.Models; using Cloud.Requests; using Cloud.Services.Broker; using Cloud.Services.Broker.Support; +using Cloud.Services.Domain; using Microsoft.AspNetCore.Mvc; namespace Cloud.Controllers { [ApiController] - [Route("api/user/{userId}/farm/{farmId}/greenhouse")] + [Route("api/farm/{farmId}/greenhouse")] public class GreenhouseController : ControllerBase { - private readonly IBrokerService _brokerService; - private readonly ApplicationContext _context; - private readonly IConfiguration _config; - public GreenhouseController(IConfiguration config, ApplicationContext context, - IBrokerService brokerService) + private readonly IGreenhouseService _greenhouseService; + public GreenhouseController(IGreenhouseService greenhouseService) { - _brokerService = brokerService; - _context = context; - _config = config; + _greenhouseService = greenhouseService; } - /** - * Возвращает текущую информацию о всех теплицах пользователя - */ + /// + /// Возвращает текущую информацию о всех теплицах пользователя + /// + /// + /// [HttpGet] - public async Task>> GetAll(int userId, int farmId) + public async Task>> GetAll(int farmId) { - throw new NotImplementedException(); + try + { + var greenhouses = _greenhouseService.GetAll(farmId); + return Ok(greenhouses); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } } - /** - * Возвращает текущую информацию о конкретной теплице - */ + /// + /// Возвращает текущую информацию о конкретной теплице + /// + /// + /// + /// [HttpGet("{greenhouseId}")] - public async Task> Get(int userId, int farmId, int greenhouseId) + public async Task> Get(int farmId, int greenhouseId) { - throw new NotImplementedException(); + try + { + var greenhouses = _greenhouseService.GetGreenhouseInfo(greenhouseId, farmId); + return Ok(greenhouses); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } } - /** - * Возвращает сохраненные данные для автоматизации теплицы - */ + /// + /// Возвращает сохраненные данные для автоматизации теплицы + /// + /// + /// + /// [HttpGet("{greenhouseId}/settings")] - public async Task> GetGreenhouse(int userId, int farmId, int greenhouseId) + public async Task> GetGreenhouse(int farmId, int greenhouseId) { throw new NotImplementedException(); } - /** - * Сохраняет в базе данных API данные для автоматизации теплицы - */ + /// + /// Сохраняет в базе данных API данные для автоматизации теплицы + /// + /// + /// + /// [HttpPost] - public async Task> SaveToDatabase(int userId, int farmId, GreenhouseRequest greenhouse) + public async Task> SaveToDatabase(int farmId, GreenhouseRequest greenhouse) { throw new NotImplementedException(); } - /** - * Обновляет в базе данных API данные для автоматизации теплицы - */ + + /// + /// Обновляет в базе данных API данные для автоматизации теплицы + /// + /// ID фермы + /// ID теплицы + /// Данные для обновления + /// Обновленный объект Greenhouse [HttpPut("{greenhouseId}/settings")] - public async Task> Update(int userId, int farmId, int greenhouseId, GreenhouseRequest greenhouse) + public async Task> Update(int farmId, int greenhouseId, GreenhouseRequest greenhouse) { throw new NotImplementedException(); } - /** - * Удаляет из базы данных API запись настроек автоматизации теплицы - */ + /// + /// Удаляет из базы данных API запись настроек автоматизации теплицы + /// + /// + /// + /// [HttpDelete("{greenhouseId}")] - public async Task Delete(int userId, int farmId, int greenhouseId) + public async Task Delete(int farmId, int greenhouseId) { throw new NotImplementedException(); diff --git a/Cloud/Program.cs b/Cloud/Program.cs index 6e22ecd..9cec515 100644 --- a/Cloud/Program.cs +++ b/Cloud/Program.cs @@ -10,11 +10,14 @@ using StackExchange.Redis; using Cloud.Services.Broker.Implement.Kafka; using Cloud.Services.Broker; using Cloud.Services; +using Cloud.Services.Domain.Implement; +using Cloud.Services.Domain; var builder = WebApplication.CreateBuilder(args); // Add services to the container. -builder.Services.AddSingleton(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); //Redis configuration builder.Services.AddSingleton(sp => { @@ -22,15 +25,6 @@ builder.Services.AddSingleton(sp => return ConnectionMultiplexer.Connect(configuration); }); -//Kafka producer service -builder.Services.AddSingleton(); - -//Kafka consumer service -builder.Services.AddSingleton(); - -//Add the BackgroundWorkerService -builder.Services.AddHostedService(); - //Jwt configuration var jwtIssuer = builder.Configuration.GetSection("Jwt:Issuer").Get(); var jwtKey = builder.Configuration.GetSection("Jwt:Key").Get(); diff --git a/Cloud/Services/Domain/IGreenhouseService.cs b/Cloud/Services/Domain/IGreenhouseService.cs new file mode 100644 index 0000000..523af87 --- /dev/null +++ b/Cloud/Services/Domain/IGreenhouseService.cs @@ -0,0 +1,45 @@ +using Cloud.Models; +using Cloud.Services.Broker.Support; + +namespace Cloud.Services.Domain; + +public interface IGreenhouseService +{ + /// + /// Возвращает текущую информацию о конкретной теплице из брокера + /// + /// ID теплицы + /// ID фермы, то есть брокера + /// Текущие данные о теплице от менеджера теплицы + public Task GetGreenhouseInfo(int id, int farmId); + /// + /// Возвращает сохраненные данные для автоматизации теплицы из базы данных + /// + /// ID теплицы + /// Данные для автоматизации теплицы + public Task GetGreenhouse(int id); + /// + /// Возвращает список данных о всех теплицах пользователя из брокера + /// + /// ID фермы + /// Список текущих данных о теплицах + public Task?> GetAll(int farmId); + /// + /// Сохраняет данные об автоматизации теплицы в базу данных + /// + /// Данные автоматизации теплицы + /// Созданную сущность из базы данных + public Task Create(Greenhouse greenhouse); + /// + /// Обновляет данные автоматизации теплицы в базе данных + /// + /// Новая информация об автоматизации теплицы + /// Обновленную сущность из базы данных + public Task Update(Greenhouse greenhouse); + /// + /// Удаляет данные об автоматизации теплицы из базы данных + /// + /// ID данных автоматизации теплицы + /// Возвращает удаленную сущность + public Task Delete(int id); +} \ No newline at end of file diff --git a/Cloud/Services/Domain/Implement/GreenhouseService.cs b/Cloud/Services/Domain/Implement/GreenhouseService.cs new file mode 100644 index 0000000..8c2d8bc --- /dev/null +++ b/Cloud/Services/Domain/Implement/GreenhouseService.cs @@ -0,0 +1,67 @@ +using Cloud.Models; +using Cloud.Services.Broker; +using Cloud.Services.Broker.Support; +using Microsoft.EntityFrameworkCore; + +namespace Cloud.Services.Domain.Implement; + +public class GreenhouseService : IGreenhouseService +{ + private readonly IBrokerService _brokerService; + private readonly ApplicationContext _context; + + public GreenhouseService(IBrokerService brokerService, ApplicationContext context) + { + _context = context; + _brokerService = brokerService; + } + + public async Task Create(Greenhouse greenhouse) + { + var res = await _context.Greenhouses.AddAsync(greenhouse); + await _context.SaveChangesAsync(); + return res.Entity; + } + + public async Task Delete(int id) + { + var greenhouse = await _context.Greenhouses.FirstOrDefaultAsync(x => x.Id == id); + + _context.Greenhouses.Remove(greenhouse); + await _context.SaveChangesAsync(); + return greenhouse; + } + + + public async Task GetGreenhouse(int id) + { + return await _context.Greenhouses.FirstOrDefaultAsync(x => x.Id == id); + } + + public async Task Update(Greenhouse greenhouse) + { + var res = _context.Greenhouses.Update(greenhouse); + await _context.SaveChangesAsync(); + return res.Entity; + } + + public async Task?> GetAll(int farmId) + { + await _changeBrokerIp(farmId); + return _brokerService.WaitMessages("data"); + } + + public async Task GetGreenhouseInfo(int id, int farmId) + { + await _changeBrokerIp(farmId); + var infos = _brokerService.WaitMessages("data"); + return infos?.FirstOrDefault(x => x.Id == id); + } + + private async Task _changeBrokerIp(int farmId) + { + var farm = await _context.Farms.FirstOrDefaultAsync(x => x.Id == farmId); + _brokerService.ChangeBrokerIp(farm.RaspberryIP); + + } +} -- 2.25.1 From 2e20e9d7cc98d79d47501f4b38ec8bbc71d6b333 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 4 Dec 2024 04:06:56 +0400 Subject: [PATCH 09/14] =?UTF-8?q?fix:=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B2=D0=B0=D0=B6=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=D1=8B=20=D0=B2?= =?UTF-8?q?=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D1=8B=D0=B5?= =?UTF-8?q?=20=D0=BE=D0=BA=D1=80=D1=83=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 4 ++++ Cloud/Dockerfile | 4 ++-- Cloud/Program.cs | 15 ++++++++++++--- Cloud/Support/NetworkSupport.cs | 26 ++++++++++++++++++++++++++ docker-compose.yml | 13 ++++++++++--- 5 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 .env create mode 100644 Cloud/Support/NetworkSupport.cs diff --git a/.env b/.env new file mode 100644 index 0000000..bff77a7 --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +POSTGRES_USER="postgres" +POSTGRES_PASSWORD="12345" +POSTGRES_DB="main_database" +DB_CONNECTION_STRING="Host=postgres:5438;Database=${POSTGRES_DB};Username=${POSTGRES_USER};Password=${POSTGRES_PASSWORD}" \ No newline at end of file diff --git a/Cloud/Dockerfile b/Cloud/Dockerfile index 7a518b4..2f52ad4 100644 --- a/Cloud/Dockerfile +++ b/Cloud/Dockerfile @@ -10,7 +10,7 @@ RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser / USER appuser FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build -ARG configuration=Release +ARG configuration=Debug WORKDIR /src COPY ["Cloud.csproj", "."] RUN dotnet restore "./Cloud.csproj" @@ -25,6 +25,6 @@ RUN dotnet publish "./Cloud.csproj" -c $configuration -o /app/publish /p:UseAppH FROM base AS final WORKDIR /app COPY --from=publish /app/publish . -# TODO: Добавить инициализацию базы данных с помощью миграции + ENTRYPOINT ["dotnet", "Cloud.dll"] diff --git a/Cloud/Program.cs b/Cloud/Program.cs index 98799f2..e8eac1c 100644 --- a/Cloud/Program.cs +++ b/Cloud/Program.cs @@ -13,6 +13,8 @@ using Cloud.Services; using Cloud.Services.Domain.Implement; using Cloud.Services.Domain; using Cloud.Services.Cache; +using Cloud.Support; +using System.Text.RegularExpressions; var builder = WebApplication.CreateBuilder(args); @@ -21,9 +23,10 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); //Redis configuration +string redisUrl = Environment.GetEnvironmentVariable("REDIS_URL") ?? "localhost:6379"; builder.Services.AddSingleton(sp => { - var configuration = ConfigurationOptions.Parse("localhost:6379"); + var configuration = ConfigurationOptions.Parse(redisUrl); return ConnectionMultiplexer.Connect(configuration); }); builder.Services.AddSingleton(); @@ -47,15 +50,21 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) }; }); +string connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING") + ?? "Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345"; +string dbUrl = "http://" + Regex.Match(connectionString, @"(?<=Host=)([^;]+)").Groups[1].Value; +await NetworkSupport.CheckConnectionAsync(dbUrl); + builder.Services.AddDbContext(options => - options.UseNpgsql("Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345")); + options.UseNpgsql(connectionString)); // Настройка CORS +string frontUrl = Environment.GetEnvironmentVariable("FRONT_URL") ?? "http://localhost:3000"; builder.Services.AddCors(options => { options.AddPolicy("AllowFrontendLocalhost", builder => { - builder.WithOrigins("http://localhost:3000") // фронтенд + builder.WithOrigins(frontUrl) // фронтенд .AllowAnyHeader() .AllowAnyMethod(); }); diff --git a/Cloud/Support/NetworkSupport.cs b/Cloud/Support/NetworkSupport.cs new file mode 100644 index 0000000..752d9a6 --- /dev/null +++ b/Cloud/Support/NetworkSupport.cs @@ -0,0 +1,26 @@ +namespace Cloud.Support; + +public static class NetworkSupport +{ + public static async Task CheckConnectionAsync(string address) + { + using var client = new HttpClient(); + try + { + var response = await client.GetAsync(address); + + if (response.IsSuccessStatusCode) + { + Console.WriteLine($"Соединение успешно проверено. Статус-код: {response.StatusCode}"); + } + else + { + Console.WriteLine($"Соединение не удалось проверить. Статус-код: {response.StatusCode}. URL: {address}"); + } + } + catch (HttpRequestException ex) + { + Console.WriteLine($"Ошибка при проверке соединения: {ex.Message}. URL: {address}"); + } + } +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index ede4fce..8608800 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,13 @@ services: build: ./Cloud/ ports: - "5124:5124" + environment: + DB_CONNECTION_STRING: ${DB_CONNECTION_STRING} + REDDIS_URL: redis:6379 + # На всякий случай, если будет больно + # KAFKA_URL: kafka:9092 + # Добавить, когда будет фронт! + # FRONT_URL: front:3000 depends_on: - postgres - redis @@ -19,9 +26,9 @@ services: image: postgres:14 container_name: cucumber_database environment: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: 12345 - POSTGRES_DB: main_database + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB} ports: - "5438:5432" volumes: -- 2.25.1 From 739d434f53d0fc4e0c5be5ed87f5ede553604b6d Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 4 Dec 2024 06:27:39 +0400 Subject: [PATCH 10/14] =?UTF-8?q?fix:=20=D0=B5=D1=81=D1=82=D1=8C=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=82=D0=B0=D0=BA=D1=82!=20=D0=BC=D0=B5=D0=B6?= =?UTF-8?q?=D0=B4=D1=83=20=D0=BE=D0=B1=D0=BB=D0=B0=D0=BA=D0=BE=D0=BC=20?= =?UTF-8?q?=D0=B8=20=D0=BA=D0=B0=D1=84=D0=BA=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 2 +- Cloud/Controllers/GreengouseController.cs | 1 + Cloud/Dockerfile | 4 +-- Cloud/Middlewares/DatabaseMiddleware.cs | 30 +++++++++++++++++++ Cloud/Program.cs | 16 +++++----- .../Broker/Implement/Kafka/KafkaConsumer.cs | 3 +- .../Broker/Implement/Kafka/KafkaProducer.cs | 12 ++++---- .../Domain/Implement/GreenhouseService.cs | 4 +-- docker-compose.yml | 6 ++-- 9 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 Cloud/Middlewares/DatabaseMiddleware.cs diff --git a/.env b/.env index bff77a7..eafc6c7 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ POSTGRES_USER="postgres" POSTGRES_PASSWORD="12345" POSTGRES_DB="main_database" -DB_CONNECTION_STRING="Host=postgres:5438;Database=${POSTGRES_DB};Username=${POSTGRES_USER};Password=${POSTGRES_PASSWORD}" \ No newline at end of file +DB_CONNECTION_STRING="Host=postgres:5432;Database=${POSTGRES_DB};Username=${POSTGRES_USER};Password=${POSTGRES_PASSWORD}" \ No newline at end of file diff --git a/Cloud/Controllers/GreengouseController.cs b/Cloud/Controllers/GreengouseController.cs index 6f77c60..57448ea 100644 --- a/Cloud/Controllers/GreengouseController.cs +++ b/Cloud/Controllers/GreengouseController.cs @@ -28,6 +28,7 @@ namespace Cloud.Controllers try { var greenhouses = _greenhouseService.GetAll(farmId); + if (greenhouses == null) return NotFound("Greenhouses is not found"); return Ok(greenhouses); } catch (Exception ex) diff --git a/Cloud/Dockerfile b/Cloud/Dockerfile index 2f52ad4..b4c2152 100644 --- a/Cloud/Dockerfile +++ b/Cloud/Dockerfile @@ -10,7 +10,7 @@ RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser / USER appuser FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build -ARG configuration=Debug +ARG configuration=Development WORKDIR /src COPY ["Cloud.csproj", "."] RUN dotnet restore "./Cloud.csproj" @@ -19,7 +19,7 @@ WORKDIR "/src/." RUN dotnet build "./Cloud.csproj" -c $configuration -o /app/build FROM build AS publish -ARG configuration=Release +ARG configuration=Development RUN dotnet publish "./Cloud.csproj" -c $configuration -o /app/publish /p:UseAppHost=false FROM base AS final diff --git a/Cloud/Middlewares/DatabaseMiddleware.cs b/Cloud/Middlewares/DatabaseMiddleware.cs new file mode 100644 index 0000000..88091bf --- /dev/null +++ b/Cloud/Middlewares/DatabaseMiddleware.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; + +namespace Cloud.Middlewares; + +public static class DatabaseMiddleware +{ + public static void AddDbConnectionService(this IServiceCollection services) + { + string connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING") + ?? "Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345"; + + services.AddDbContext(options => + options.UseNpgsql(connectionString)); + + } + public static void MigrateDb(this IApplicationBuilder app) + { + try + { + using var scope = app.ApplicationServices.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); + + context.Database.Migrate(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } +} \ No newline at end of file diff --git a/Cloud/Program.cs b/Cloud/Program.cs index e8eac1c..5daec1f 100644 --- a/Cloud/Program.cs +++ b/Cloud/Program.cs @@ -15,6 +15,7 @@ using Cloud.Services.Domain; using Cloud.Services.Cache; using Cloud.Support; using System.Text.RegularExpressions; +using Cloud.Middlewares; var builder = WebApplication.CreateBuilder(args); @@ -49,15 +50,8 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)) }; }); - -string connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING") - ?? "Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345"; -string dbUrl = "http://" + Regex.Match(connectionString, @"(?<=Host=)([^;]+)").Groups[1].Value; -await NetworkSupport.CheckConnectionAsync(dbUrl); - -builder.Services.AddDbContext(options => - options.UseNpgsql(connectionString)); - +// Настройка подключения к БД +builder.Services.AddDbConnectionService(); // Настройка CORS string frontUrl = Environment.GetEnvironmentVariable("FRONT_URL") ?? "http://localhost:3000"; builder.Services.AddCors(options => @@ -113,6 +107,7 @@ var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { + Console.WriteLine("Swagger enabled"); app.UseSwagger(); app.UseSwaggerUI(c => { @@ -126,6 +121,9 @@ app.UseHttpsRedirection(); // Включение CORS app.UseCors("AllowFrontendLocalhost"); +// Применение миграций +app.MigrateDb(); + app.UseAuthentication(); app.UseAuthorization(); diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs index d2fe34c..c6b0b30 100644 --- a/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs @@ -13,7 +13,8 @@ namespace Cloud.Services.Broker.Implement.Kafka public KafkaConsumer(IConfiguration config) { _config = config; - ChangeBrokerIp(_config["Kafka:BootstrapServers"]); + Console.WriteLine($"KafkaConsumer created. IP:" + _config["KAFKA_URL"]); + ChangeBrokerIp(_config["KAFKA_URL"]); } public IEnumerable? WaitMessages(string topic) diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs index 0391cc4..2a0f8c8 100644 --- a/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using Cloud.Services.Broker.Support; using Confluent.Kafka; @@ -5,18 +6,19 @@ namespace Cloud.Services.Broker.Implement.Kafka { public class KafkaProducer : IBrokerProducer { - private IProducer _producer; + private IProducer _producer; private readonly IConfiguration _config; public KafkaProducer(IConfiguration configuration) { _config = configuration; - - ChangeBrokerIp(_config["Kafka:BootstrapServers"]); + Console.WriteLine($"KafkaConsumer created. IP:" + _config["KAFKA_URL"]); + ChangeBrokerIp(_config["KAFKA_URL"]); } public async Task ProduceAsync(string topic, Command command) { - var message = new Message { Key = Guid.NewGuid(), Value = command }; + var commandSerialized = JsonSerializer.Serialize(command); + var message = new Message { Key = Guid.NewGuid().ToString(), Value = commandSerialized }; //Produce the Message await _producer.ProduceAsync(topic, message); @@ -30,7 +32,7 @@ namespace Cloud.Services.Broker.Implement.Kafka }; //Build the Producer - _producer = new ProducerBuilder(producerConfig).Build(); + _producer = new ProducerBuilder(producerConfig).Build(); } } } \ No newline at end of file diff --git a/Cloud/Services/Domain/Implement/GreenhouseService.cs b/Cloud/Services/Domain/Implement/GreenhouseService.cs index 8c2d8bc..f398a39 100644 --- a/Cloud/Services/Domain/Implement/GreenhouseService.cs +++ b/Cloud/Services/Domain/Implement/GreenhouseService.cs @@ -47,13 +47,13 @@ public class GreenhouseService : IGreenhouseService public async Task?> GetAll(int farmId) { - await _changeBrokerIp(farmId); + // await _changeBrokerIp(farmId); return _brokerService.WaitMessages("data"); } public async Task GetGreenhouseInfo(int id, int farmId) { - await _changeBrokerIp(farmId); + // await _changeBrokerIp(farmId); var infos = _brokerService.WaitMessages("data"); return infos?.FirstOrDefault(x => x.Id == id); } diff --git a/docker-compose.yml b/docker-compose.yml index 8608800..11c4650 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,14 +9,16 @@ networks: services: cloud: + networks: + - vpn build: ./Cloud/ ports: - "5124:5124" environment: + ASPNETCORE_ENVIRONMENT: Development DB_CONNECTION_STRING: ${DB_CONNECTION_STRING} REDDIS_URL: redis:6379 - # На всякий случай, если будет больно - # KAFKA_URL: kafka:9092 + KAFKA_URL: kafka:29092 # Добавить, когда будет фронт! # FRONT_URL: front:3000 depends_on: -- 2.25.1 From 7b751b707293984732e03cf1d93a21a323a725bf Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 4 Dec 2024 11:32:34 +0400 Subject: [PATCH 11/14] =?UTF-8?q?fix:=20=D0=BD=D0=B8=D1=87=D0=B5=D0=B3?= =?UTF-8?q?=D0=BE=20=D0=BD=D0=B5=20=D0=BF=D0=BE=D0=BC=D0=BE=D0=B3=D0=B0?= =?UTF-8?q?=D0=B5=D1=82=20=D0=BD=D0=BE=D1=80=D0=BC=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=B8=D1=82=D1=8C?= =?UTF-8?q?=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5...=20=D1=8D=D1=82=D0=BE?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BD=D0=B5=D1=86...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud/Program.cs | 2 +- .../Broker/Implement/Kafka/KafkaConsumer.cs | 75 +++++++++++++------ .../Broker/Implement/Kafka/KafkaProducer.cs | 2 +- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/Cloud/Program.cs b/Cloud/Program.cs index 5daec1f..16d0c36 100644 --- a/Cloud/Program.cs +++ b/Cloud/Program.cs @@ -20,7 +20,7 @@ using Cloud.Middlewares; var builder = WebApplication.CreateBuilder(args); // Add services to the container. -builder.Services.AddTransient(); +builder.Services.AddSingleton(); builder.Services.AddTransient(); //Redis configuration diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs index c6b0b30..f29a4b8 100644 --- a/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs @@ -1,6 +1,7 @@ using Cloud.Services.Broker.Support; using Confluent.Kafka; +using System.Diagnostics; using System.Text.Json; namespace Cloud.Services.Broker.Implement.Kafka @@ -20,29 +21,61 @@ namespace Cloud.Services.Broker.Implement.Kafka public IEnumerable? WaitMessages(string topic) where T : IBrokerResponse { - try - { - _consumer.Subscribe(topic); + List res = new(); + List partitions; - var consumeResult = _consumer.Consume(TimeSpan.FromMilliseconds(1000)); - Console.WriteLine($"================ Received message: {consumeResult?.Message.Value}"); - if (consumeResult == null) - { - // No message received from Kafka within the specified timeout. - return default; - } + using var adminClient = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = _config["KAFKA_URL"] }).Build(); + var meta = adminClient.GetMetadata(TimeSpan.FromSeconds(20)); + var currentTopic = meta.Topics.SingleOrDefault(t => t.Topic == topic) + ?? throw new Exception($"Topic {topic} not found"); + partitions = currentTopic.Partitions; + + _consumer.Subscribe(topic); + foreach (var partition in partitions) + { + var topicPartition = new TopicPartition(topic, partition.PartitionId); + _consumer.Assign(topicPartition); + + T? message = _consume(); + if (message == null) return null; + res.Add(message); - return JsonSerializer.Deserialize>(consumeResult.Message.Value); + } + _consumer.Unassign(); + _consumer.Unsubscribe(); + return res; + } - } - catch (Exception ex) - { - throw; - } - finally - { - _consumer.Close(); - } + private T? _consume() where T : IBrokerResponse + { + var sw = new Stopwatch(); + sw.Start(); + try + { + while (true) + { + var consumeResult = _consumer.Consume(TimeSpan.FromMinutes(1)); + + if (consumeResult?.Message?.Value == null) + { + // Предел по времени + if (sw.Elapsed > TimeSpan.FromMinutes(1)) + { + return default; + } + continue; + } + + string jsonObj = consumeResult.Message.Value; + return JsonSerializer.Deserialize(jsonObj); + + } + } + catch (Exception ex) + { + _consumer.Close(); + throw; + } } public void ChangeBrokerIp(string ip) @@ -53,7 +86,7 @@ namespace Cloud.Services.Broker.Implement.Kafka GroupId = _config["Kafka:GroupId"], AutoOffsetReset = AutoOffsetReset.Earliest, }; - + _consumer?.Close(); _consumer = new ConsumerBuilder(consumerConfig).Build(); } } diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs index 2a0f8c8..6a916bb 100644 --- a/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs @@ -12,7 +12,7 @@ namespace Cloud.Services.Broker.Implement.Kafka public KafkaProducer(IConfiguration configuration) { _config = configuration; - Console.WriteLine($"KafkaConsumer created. IP:" + _config["KAFKA_URL"]); + Console.WriteLine($"KafkaProducer created. IP:" + _config["KAFKA_URL"]); ChangeBrokerIp(_config["KAFKA_URL"]); } public async Task ProduceAsync(string topic, Command command) -- 2.25.1 From db3cbde0af2bbb27228e7a92deac3c94bfa18921 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 4 Dec 2024 11:41:08 +0400 Subject: [PATCH 12/14] =?UTF-8?q?add:=20=D0=BE=D1=81=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9=20=D0=BA=D1=80=D1=83=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud/Controllers/GreengouseController.cs | 59 +++++++++++++++++++++-- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/Cloud/Controllers/GreengouseController.cs b/Cloud/Controllers/GreengouseController.cs index 57448ea..9f4c025 100644 --- a/Cloud/Controllers/GreengouseController.cs +++ b/Cloud/Controllers/GreengouseController.cs @@ -66,7 +66,16 @@ namespace Cloud.Controllers [HttpGet("{greenhouseId}/settings")] public async Task> GetGreenhouse(int farmId, int greenhouseId) { - throw new NotImplementedException(); + try + { + var greenhouse = await _greenhouseService.GetGreenhouse(greenhouseId); + if (greenhouse == null) return NotFound("Greenhouses is not found"); + return Ok(greenhouse); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } } /// @@ -78,7 +87,23 @@ namespace Cloud.Controllers [HttpPost] public async Task> SaveToDatabase(int farmId, GreenhouseRequest greenhouse) { - throw new NotImplementedException(); + try + { + var greenhouseEntity = new Greenhouse() + { + RecomendedTemperature = greenhouse.RecomendedTemperature, + WateringMode = greenhouse.WateringMode, + HeatingMode = greenhouse.HeatingMode + }; + + var result = await _greenhouseService.Create(greenhouseEntity); + return Ok(result); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } @@ -92,7 +117,24 @@ namespace Cloud.Controllers [HttpPut("{greenhouseId}/settings")] public async Task> Update(int farmId, int greenhouseId, GreenhouseRequest greenhouse) { - throw new NotImplementedException(); + try + { + var greenhouseEntity = new Greenhouse() + { + Id = greenhouseId, + FarmId = farmId, + WateringMode = greenhouse.WateringMode, + HeatingMode = greenhouse.HeatingMode, + RecomendedTemperature = greenhouse.RecomendedTemperature + }; + + var result = await _greenhouseService.Update(greenhouseEntity); + return Ok(result); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } } /// @@ -104,8 +146,15 @@ namespace Cloud.Controllers [HttpDelete("{greenhouseId}")] public async Task Delete(int farmId, int greenhouseId) { - - throw new NotImplementedException(); + try + { + _ = await _greenhouseService.Delete(greenhouseId); + return Ok(); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } } } } \ No newline at end of file -- 2.25.1 From 71bc31d0bd7952616025aa820f087b8e203e169b Mon Sep 17 00:00:00 2001 From: the Date: Wed, 4 Dec 2024 21:33:02 +0400 Subject: [PATCH 13/14] containers test --- GreenhouseDetector/Dockerfile | 11 +++++++++++ GreenhouseManager/Dockerfile | 11 +++++++++++ docker-compose.yml | 18 ++++++++++++++++++ requirements.txt | 3 +++ 4 files changed, 43 insertions(+) create mode 100644 GreenhouseDetector/Dockerfile create mode 100644 GreenhouseManager/Dockerfile create mode 100644 requirements.txt diff --git a/GreenhouseDetector/Dockerfile b/GreenhouseDetector/Dockerfile new file mode 100644 index 0000000..cb846ca --- /dev/null +++ b/GreenhouseDetector/Dockerfile @@ -0,0 +1,11 @@ +FROM python:latest + +WORKDIR /app + +COPY requirements.txt . + +RUN pip install --no-cache-dir -r requirements.txt + +COPY GreenhouseDetector/detector.py . + +CMD ["python", "detector.py"] \ No newline at end of file diff --git a/GreenhouseManager/Dockerfile b/GreenhouseManager/Dockerfile new file mode 100644 index 0000000..efc49af --- /dev/null +++ b/GreenhouseManager/Dockerfile @@ -0,0 +1,11 @@ +FROM python:latest + +WORKDIR /app + +COPY requirements.txt . + +RUN pip install --no-cache-dir -r requirements.txt + +COPY GreenhouseManager/manager.py . + +CMD ["python", "manager.py"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index a1e5c42..665e246 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -78,3 +78,21 @@ services: KAFKA_CLUSTERS_0_NAME: local KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:29092 KAFKA_CLUSTERS_0_METRICS_PORT: 9997 + + #manager_py_service: + # container_name: manager_py + # build: + # context: . + # dockerfile: ./GreenhouseManager/Dockerfile + # depends_on: + # - kafka + + #detector_py_service: + # container_name: detector_py + # build: + # context: . + # dockerfile: ./GreenhouseDetector/Dockerfile + # depends_on: + # - kafka + # expose: + # - 20002 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0145355 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +kafka-python~=2.0.2 +Flask~=3.0.3 +requests~=2.31.0 \ No newline at end of file -- 2.25.1 From aace337052e96e822ec98e956047a72de44ffc01 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 4 Dec 2024 23:20:09 +0400 Subject: [PATCH 14/14] =?UTF-8?q?fix:=20=D1=85=D0=B7,=20=D0=B2=D1=80=D0=BE?= =?UTF-8?q?=D0=B4=D0=B5=20=D0=B1=D1=8B=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=B5=D1=82,=20=D0=BD=D0=BE=20=D0=BF=D0=BE=D1=87=D0=B5?= =?UTF-8?q?=D0=BC=D1=83-=D1=82=D0=BE=20=D0=BD=D0=B5=20=D0=BF=D0=BE=D0=B4?= =?UTF-8?q?=D0=BA=D0=BB=D1=8E=D1=87=D0=B0=D0=B5=D1=82=D1=81=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GreenhouseDetector/Dockerfile | 2 +- GreenhouseDetector/detector.py | 6 +- GreenhouseManager/Dockerfile | 2 +- GreenhouseManager/manager.py | 10 +- docker-compose.yml | 165 +++++++++++++++++---------------- 5 files changed, 100 insertions(+), 85 deletions(-) diff --git a/GreenhouseDetector/Dockerfile b/GreenhouseDetector/Dockerfile index cb846ca..7be5c76 100644 --- a/GreenhouseDetector/Dockerfile +++ b/GreenhouseDetector/Dockerfile @@ -1,4 +1,4 @@ -FROM python:latest +FROM python:3.9-slim WORKDIR /app diff --git a/GreenhouseDetector/detector.py b/GreenhouseDetector/detector.py index d092373..4921dfe 100644 --- a/GreenhouseDetector/detector.py +++ b/GreenhouseDetector/detector.py @@ -1,3 +1,4 @@ +import os import time import random as rnd @@ -5,10 +6,13 @@ from flask import Flask import requests import threading + app = Flask(__name__) class Detector: def __init__(self, id, moistureThresholdUpper, moistureThresholdLower, tempThresholdUpper, tempThresholdLower): + self.MANAGER_URL = os.environ.get('MANAGER_URL') + print("MANAGER_URL=", self.MANAGER_URL) self.id = id self.moistureThresholdUpper = moistureThresholdUpper self.moistureThresholdLower = moistureThresholdLower @@ -24,7 +28,7 @@ class Detector: def sendData(self): data = {"moisture": self.moisture, "temp": self.temp} - requests.post(f"http://127.0.0.1:20002/webhook?id={self.id}", json=data) + requests.post(f"{self.MANAGER_URL}/webhook?id={self.id}", json=data) detector1 = Detector(1, 0.6, 0.2, 40, 20) diff --git a/GreenhouseManager/Dockerfile b/GreenhouseManager/Dockerfile index efc49af..ab31b66 100644 --- a/GreenhouseManager/Dockerfile +++ b/GreenhouseManager/Dockerfile @@ -1,4 +1,4 @@ -FROM python:latest +FROM python:3.9-slim WORKDIR /app diff --git a/GreenhouseManager/manager.py b/GreenhouseManager/manager.py index d9772d1..8366baf 100644 --- a/GreenhouseManager/manager.py +++ b/GreenhouseManager/manager.py @@ -1,3 +1,4 @@ +import os from kafka import KafkaProducer, KafkaConsumer import kafka import socket @@ -7,6 +8,7 @@ import time from enum import Enum import threading + app = Flask(__name__) def start_manager(): @@ -15,6 +17,8 @@ def start_manager(): class Manager: def __init__(self, _id: int, moisture: float = 0, temp: float = 20, isAutoOn: bool = False, valve_state: str = "closed", heater_state: str = "off"): + KAFKA_URL = os.environ.get('KAFKA_URL') + print("KAFKA_URL=", KAFKA_URL) self._id = _id self.moisture = moisture self.temp = temp @@ -23,14 +27,14 @@ class Manager: self.heater_state = heater_state self.dataPublisher = KafkaProducer( - bootstrap_servers=['localhost:9092'], + bootstrap_servers=[KAFKA_URL], client_id=f'manager{self._id}_producer', value_serializer=lambda v: dumps(v).encode('utf-8') ) self.controllerConsumer = KafkaConsumer( 'commands', - bootstrap_servers=['localhost:9092'], + bootstrap_servers=[KAFKA_URL], auto_offset_reset='earliest', enable_auto_commit=True, consumer_timeout_ms=2000, @@ -38,7 +42,7 @@ class Manager: value_deserializer=lambda x: loads(x.decode('utf-8')) ) self.controllerConsumerResponse = KafkaProducer( - bootstrap_servers=['localhost:9092'], + bootstrap_servers=[KAFKA_URL], client_id=f'manager{self._id}_producer', value_serializer=lambda v: dumps(v).encode('utf-8') ) diff --git a/docker-compose.yml b/docker-compose.yml index 665e246..75795b6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,91 +8,98 @@ networks: gateway: "192.168.2.1" services: - zookeeper: - networks: - - vpn - image: confluentinc/cp-zookeeper:7.4.0 - environment: - ZOOKEEPER_CLIENT_PORT: 2181 - ZOOKEEPER_TICK_TIME: 2000 - ports: - - 2181:2181 + zookeeper: + networks: + - vpn + image: confluentinc/cp-zookeeper:7.4.0 + environment: + ZOOKEEPER_CLIENT_PORT: 2181 + ZOOKEEPER_TICK_TIME: 2000 + ports: + - 2181:2181 - kafka: - networks: - vpn: - ipv4_address: 192.168.2.10 - image: confluentinc/cp-kafka:7.4.0 - ports: - - 9092:9092 - - 9997:9997 + kafka: + networks: + vpn: + ipv4_address: 192.168.2.10 + image: confluentinc/cp-kafka:7.4.0 + ports: + - 9092:9092 + - 9997:9997 - expose: - - 29092:29092 + expose: + - 29092:29092 - environment: - KAFKA_BROKER_ID: 1 - KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 - KAFKA_LISTENERS: HOST://0.0.0.0:9092,DOCKER://0.0.0.0:29092 - KAFKA_ADVERTISED_LISTENERS: HOST://192.168.1.5:9092,DOCKER://kafka:29092 - KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: DOCKER:PLAINTEXT,HOST:PLAINTEXT - KAFKA_INTER_BROKER_LISTENER_NAME: DOCKER - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 - KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1 - KAFKA_LOG_FLUSH_INTERVAL_MESSAGES: 10000 - KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0 - depends_on: - - zookeeper + environment: + KAFKA_BROKER_ID: 1 + KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 + KAFKA_LISTENERS: HOST://0.0.0.0:9092,DOCKER://0.0.0.0:29092 + KAFKA_ADVERTISED_LISTENERS: HOST://192.168.1.5:9092,DOCKER://kafka:29092 + KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: DOCKER:PLAINTEXT,HOST:PLAINTEXT + KAFKA_INTER_BROKER_LISTENER_NAME: DOCKER + KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 + KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1 + KAFKA_LOG_FLUSH_INTERVAL_MESSAGES: 10000 + KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0 + depends_on: + - zookeeper - init-kafka: - networks: - - vpn - image: confluentinc/cp-kafka:7.4.0 - depends_on: - - kafka - entrypoint: [ '/bin/sh', '-c' ] - command: | - " - # blocks until kafka is reachable - kafka-topics --bootstrap-server kafka:29092 --list + init-kafka: + networks: + - vpn + image: confluentinc/cp-kafka:7.4.0 + depends_on: + - kafka + entrypoint: [ '/bin/sh', '-c' ] + command: | + " + # blocks until kafka is reachable + kafka-topics --bootstrap-server kafka:29092 --list - echo -e 'Creating kafka topics' - kafka-topics --bootstrap-server kafka:29092 --create --if-not-exists --topic commands --replication-factor 1 --partitions 1 - kafka-topics --bootstrap-server kafka:29092 --create --if-not-exists --topic data --replication-factor 1 --partitions 1 - kafka-topics --bootstrap-server kafka:29092 --create --if-not-exists --topic response --replication-factor 1 --partitions 1 + echo -e 'Creating kafka topics' + kafka-topics --bootstrap-server kafka:29092 --create --if-not-exists --topic commands --replication-factor 1 --partitions 1 + kafka-topics --bootstrap-server kafka:29092 --create --if-not-exists --topic data --replication-factor 1 --partitions 1 + kafka-topics --bootstrap-server kafka:29092 --create --if-not-exists --topic response --replication-factor 1 --partitions 1 - echo -e 'Successfully created the following topics:' - kafka-topics --bootstrap-server kafka:29092 --list - " + echo -e 'Successfully created the following topics:' + kafka-topics --bootstrap-server kafka:29092 --list + " - kafka-ui: - networks: - - vpn - container_name: kafka-ui - image: provectuslabs/kafka-ui:latest - ports: - - 8080:8080 - depends_on: - - kafka - environment: - KAFKA_CLUSTERS_0_NAME: local - KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:29092 - KAFKA_CLUSTERS_0_METRICS_PORT: 9997 + kafka-ui: + networks: + - vpn + container_name: kafka-ui + image: provectuslabs/kafka-ui:latest + ports: + - 8080:8080 + depends_on: + - kafka + environment: + KAFKA_CLUSTERS_0_NAME: local + KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:29092 + KAFKA_CLUSTERS_0_METRICS_PORT: 9997 - #manager_py_service: - # container_name: manager_py - # build: - # context: . - # dockerfile: ./GreenhouseManager/Dockerfile - # depends_on: - # - kafka + manager: + networks: + - vpn + build: + context: . + dockerfile: ./GreenhouseManager/Dockerfile + environment: + KAFKA_URL: kafka:29092 + depends_on: + - kafka + expose: + - 20002 - #detector_py_service: - # container_name: detector_py - # build: - # context: . - # dockerfile: ./GreenhouseDetector/Dockerfile - # depends_on: - # - kafka - # expose: - # - 20002 \ No newline at end of file + detector: + networks: + - vpn + build: + context: . + dockerfile: ./GreenhouseDetector/Dockerfile + environment: + MANAGER_URL: http://manager:20002 + depends_on: + - manager + \ No newline at end of file -- 2.25.1