Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ar.utn.ba.ddsi.mailing.clients;

import ar.utn.ba.ddsi.mailing.models.entities.Clima;
import reactor.core.publisher.Mono;

public interface IClimaClient {
Mono<Clima> obtenerClimaPorCiudad(String ciudad);
}
37 changes: 37 additions & 0 deletions src/main/java/ar/utn/ba/ddsi/mailing/clients/impl/ClimaClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ar.utn.ba.ddsi.mailing.clients.impl;

import ar.utn.ba.ddsi.mailing.clients.IClimaClient;
import ar.utn.ba.ddsi.mailing.models.dto.external.weatherapi.WeatherResponse;
import ar.utn.ba.ddsi.mailing.models.entities.Clima;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Component
public class ClimaClient implements IClimaClient {
private final WebClient webClient;
private final String apiKey;

public ClimaClient(
@Value("${weather.api.key}") String apiKey,
@Value("${weather.api.base-url}") String baseUrl) {
this.webClient = WebClient.builder()
.baseUrl(baseUrl)
.build();
this.apiKey = apiKey;
}

public Mono<Clima> obtenerClimaPorCiudad(String ciudad) {
return webClient.get()
.uri(uriBuilder -> uriBuilder
.path("/current.json")
.queryParam("key", apiKey)
.queryParam("q", ciudad)
.queryParam("aqi", "no")
.build())
.retrieve()
.bodyToMono(WeatherResponse.class)
.map(WeatherResponse::toDomain);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ar.utn.ba.ddsi.mailing.controllers;

import ar.utn.ba.ddsi.mailing.models.entities.Email;
import ar.utn.ba.ddsi.mailing.models.entities.notificaciones.Email;
import ar.utn.ba.ddsi.mailing.services.IEmailService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ar.utn.ba.ddsi.mailing.models.dto.external.weatherapi;

import ar.utn.ba.ddsi.mailing.models.entities.Clima;
import ar.utn.ba.ddsi.mailing.models.entities.Temperatura;
import ar.utn.ba.ddsi.mailing.models.entities.Ubicacion;
import ar.utn.ba.ddsi.mailing.models.entities.Viento;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -8,4 +12,16 @@
public class WeatherResponse {
private Location location;
private Current current;

public Clima toDomain(){
Clima clima = new Clima();
clima.setCondicion(this.getCurrent().getCondition().getText());
clima.setHumedad(this.getCurrent().getHumidity());

clima.setUbicacion(new Ubicacion(this.getLocation().getName(),this.getLocation().getRegion(), this.getLocation().getCountry()));
clima.setTemperatura(new Temperatura(this.getCurrent().getTemp_c(), this.getCurrent().getTemp_f()));
clima.setViento(new Viento(this.getCurrent().getWind_kph()));

return clima;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ar.utn.ba.ddsi.mailing.models.entities;

import org.springframework.stereotype.Component;

@Component
public class AlertaPorTemperaturaYHumedad implements CondicionDeAlerta {
private static final double TEMPERATURA_ALERTA = 35.0;
private static final int HUMEDAD_ALERTA = 60;

@Override
public boolean evaluar(Clima clima) {
return clima.getTemperatura().getTemperaturaCelsius() > TEMPERATURA_ALERTA &&
clima.getHumedad() > HUMEDAD_ALERTA;
}
}
10 changes: 4 additions & 6 deletions src/main/java/ar/utn/ba/ddsi/mailing/models/entities/Clima.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
@Setter
public class Clima {
private Long id;
private String ciudad;
private String region;
private String pais;
private Double temperaturaCelsius;
private Double temperaturaFahrenheit;
private String condicion;
private Double velocidadVientoKmh;
private Integer humedad;
private LocalDateTime fechaActualizacion;
private boolean procesado;

private Ubicacion ubicacion;
private Temperatura temperatura;
private Viento viento;

public Clima() {
this.fechaActualizacion = LocalDateTime.now();
this.procesado = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ar.utn.ba.ddsi.mailing.models.entities;

public interface CondicionDeAlerta {
boolean evaluar(Clima clima);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ar.utn.ba.ddsi.mailing.models.entities;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@AllArgsConstructor
public class Temperatura {
private Double temperaturaCelsius;
private Double temperaturaFahrenheit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ar.utn.ba.ddsi.mailing.models.entities;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class Ubicacion {
private String ciudad;
private String region;
private String pais;
}
10 changes: 10 additions & 0 deletions src/main/java/ar/utn/ba/ddsi/mailing/models/entities/Viento.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ar.utn.ba.ddsi.mailing.models.entities;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class Viento {
private Double velocidadVientoKmh;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package ar.utn.ba.ddsi.mailing.models.entities;
package ar.utn.ba.ddsi.mailing.models.entities.notificaciones;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Email {
private Long id;
public class Email extends Notificacion {
private String destinatario;
private String remitente;
private String asunto;
private String contenido;
private boolean enviado;

public Email(String destinatario, String remitente, String asunto, String contenido) {
validateMailFormat(destinatario);
validateMailFormat(remitente);

this.destinatario = destinatario;
this.remitente = remitente;
this.asunto = asunto;
this.contenido = contenido;
this.enviado = false;
}

private void validateMailFormat(String mail) {
if (!mail.contains("@")) // TODO Mejorar
throw new IllegalArgumentException("El formato del email es incorrecto");
}

public void enviar() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ar.utn.ba.ddsi.mailing.models.entities.notificaciones;

import ar.utn.ba.ddsi.mailing.models.entities.notificaciones.enums.EstadoNotificacion;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Notificacion {
private Long id;
protected EstadoNotificacion estado;

public Notificacion() {
this.estado = EstadoNotificacion.PENDIENTE;
}

public boolean fueEnviada() {
return estado == EstadoNotificacion.ENVIADA;
}

public void marcarComoEnviada() {
estado = EstadoNotificacion.ENVIADA;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ar.utn.ba.ddsi.mailing.models.entities.notificaciones.enums;

public enum EstadoNotificacion {
PENDIENTE("Pendiente"),
ENVIADA("Enviada"),
ERROR("Error");

private final String estado;

EstadoNotificacion(String estado) {
this.estado = estado;
}

public String getEstado() {
return estado;
}

@Override
public String toString() {
return estado;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ar.utn.ba.ddsi.mailing.models.repositories;

import ar.utn.ba.ddsi.mailing.models.entities.Email;
import ar.utn.ba.ddsi.mailing.models.entities.notificaciones.Email;
import java.util.List;
import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public Clima save(Clima clima) {
Long id = idGenerator.getAndIncrement();
clima.setId(id);
climas.put(id, clima);
ciudadToId.put(clima.getCiudad(), id);
ciudadToId.put(clima.getUbicacion().getCiudad(), id);
} else {
climas.put(clima.getId(), clima);
ciudadToId.put(clima.getCiudad(), clima.getId());
ciudadToId.put(clima.getUbicacion().getCiudad(), clima.getId());
}
return clima;
}
Expand Down Expand Up @@ -53,7 +53,7 @@ public List<Clima> findByProcesado(boolean procesado) {
public void delete(Clima clima) {
if (clima.getId() != null) {
climas.remove(clima.getId());
ciudadToId.remove(clima.getCiudad());
ciudadToId.remove(clima.getUbicacion().getCiudad());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ar.utn.ba.ddsi.mailing.models.repositories.impl;

import ar.utn.ba.ddsi.mailing.models.entities.Email;
import ar.utn.ba.ddsi.mailing.models.entities.notificaciones.Email;
import ar.utn.ba.ddsi.mailing.models.entities.notificaciones.Notificacion;
import ar.utn.ba.ddsi.mailing.models.repositories.IEmailRepository;
import org.springframework.stereotype.Repository;
import java.util.*;
Expand Down Expand Up @@ -33,7 +34,7 @@ public List<Email> findAll() {
@Override
public List<Email> findByEnviado(boolean enviado) {
return emails.values().stream()
.filter(email -> email.isEnviado() == enviado)
.filter(Notificacion::fueEnviada)
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public AlertasScheduler(IAlertasService alertasService) {

@Scheduled(fixedRate = 60000) // Cada 1 minuto
public void procesarAlertas() {
alertasService.generarAlertasYAvisar()
.doOnSuccess(v -> logger.info("Procesamiento de alertas completado"))
.doOnError(e -> logger.error("Error en el procesamiento de alertas: {}", e.getMessage()))
.subscribe();
alertasService.generarAlertasPorClima()
.doOnSuccess(v -> logger.info("Procesamiento de alertas completado"))
.doOnError(e -> logger.error("Error en el procesamiento de alertas: {}", e.getMessage()))
.subscribe();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import reactor.core.publisher.Mono;

public interface IAlertasService {
Mono<Void> generarAlertasYAvisar();
Mono<Void> generarAlertasPorClima();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ar.utn.ba.ddsi.mailing.services;

import ar.utn.ba.ddsi.mailing.models.entities.Email;
import ar.utn.ba.ddsi.mailing.models.entities.notificaciones.Email;
import java.util.List;

public interface IEmailService {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ar.utn.ba.ddsi.mailing.services.alertas;

import ar.utn.ba.ddsi.mailing.models.entities.Clima;

public interface ICanalAlerta {
boolean evaluar(Clima clima);

void guardarParaEnviar(Clima clima);
}
Loading