Weatherservice
Weatherservice
weather;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.database.DailyWeatherSummary;
import com.example.database.DatabaseService;
import org.json.JSONObject;
import com.example.weather.WeatherAlert;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Instant;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Service
public class WeatherService {
private static final String[] CITIES = {"Delhi", "Mumbai", "Chennai",
"Bangalore", "Kolkata", "Hyderabad"};
@Autowired
public WeatherService(WeatherAlert weatherAlert, DatabaseService
databaseService) {
this.weatherAlert = weatherAlert;
this.databaseService = databaseService;
}
parseWeatherResponse(response.toString(), city);
}
} else {
System.err.println("GET request failed for city: " + city + " with
response code: " + responseCode);
}
}
weatherAlert.checkTemperatureAlert(temperature, city);
weatherAlert.checkWeatherConditionAlert(description, city);
}
// Processes the weather data and saves the daily summary to the database
public void processAndSaveDailySummary() {
for (String city : CITIES) {
List<WeatherData> data = dailyWeatherData.get(city);
if (data != null && !data.isEmpty()) {
double sumTemp = 0, maxTemp = Double.MIN_VALUE, minTemp =
Double.MAX_VALUE;
Map<String, Integer> conditionCount = new HashMap<>();