prj03-group06/src/weather/WeatherStation.java
@SuppressWarnings("rawtypes")
	private void updateSensors() {
		if (API_ENABLED) {
			StringBuilder theUrl = new StringBuilder("http://api.openweathermap.org/data/2.5/weather?q=");
			theUrl.append(theLocation);
			theUrl.append("&APPID=");
			theUrl.append(APIKEY);
			
			/*
			 * Project 3, Deliveries, Q4: Refactored code lines 275-308 below to output
			 *   temperature and wind speed without unnecessary conversions.
			 *   Removed some code complexity and added comments to make things
			 *   a bit easier to understand.
			 */
			theUrl.append("&units=imperial");
			
			try {
				// Grab json from OpenWeatherMap API
				URL url = new URL(theUrl.toString());
				JSONObject jo = (JSONObject) new JSONParser().parse(new BufferedReader(new InputStreamReader(url.openStream())));
				
				// Fetch current forecast data
				Map forecast = (Map) jo.get("main");
				temp = (int) (10 * (double) forecast.get("temp"));
				humid = ((Long) forecast.get("humidity")).intValue();
				pressure = (int) (1000 * (long)forecast.get("pressure") * HPA_INHG_COEF + 0.5);
				
				// Fetch wind speed and direction data
				Map wind = (Map)jo.get("wind");
				windspd = ((Double) wind.get("speed")).intValue();
				if (wind.get("deg") != null) {
					winddir = (int)((long) wind.get("deg"));
				} else {
					winddir = 0;
				}
				
				// Fetch rain data
				Map rainObj = (Map) jo.get("rain");
				if (rainObj != null) {
					rain = (int)(10 * (double) rainObj.get("1h") * MM_IN_COEF + 0.5);
				} else {
					rain = 0;
				}
				
				// Fetch sunrise/sunset data
				Map sys = (Map)jo.get("sys");
				sunrise = unixToVantageTime((long) (sys.get("sunrise")));
				sunset = unixToVantageTime((long) (sys.get("sunset")));
			} catch (IOException | ParseException e) {
				e.printStackTrace();
			}
		} else {
			temp = getTemp() + (int)(RANDOM.nextGaussian() * TEMP_DEV + 0.5);
			humid = getHumid() + (int)(RANDOM.nextGaussian() * HUMID_DEV + 0.5);
			pressure = getPressure() + (int)(RANDOM.nextGaussian() * PRESSURE_DEV + 0.5);
			windspd = getWindspd() + (int)(RANDOM.nextGaussian() * WINDSPD_DEV + 0.5);
			winddir += (int)(RANDOM.nextGaussian() * WINDDIR_DEV + 0.5);
			rain = DISABLE_RAIN ? 0 : rain + (int)(RANDOM.nextGaussian() * RAIN_DEV + 0.5);
			
			temp = Math.max(Math.min(getTemp(), MAX_TEMP), MIN_TEMP);
			humid = Math.max(Math.min(getHumid(), MAX_HUMID), MIN_HUMID);
			pressure = Math.max(Math.min(getPressure(), MAX_PRESSURE), MIN_PRESSURE);
			windspd = Math.max(Math.min(getWindspd(), MAX_WINDSPD), MIN_WINDSPD);
			winddir = (winddir + WINDDIR_MOD)%WINDDIR_MOD;
			rain = Math.max(Math.min(rain, MAX_RAIN), MIN_RAIN);
		}
	}
/**
	 * Updates all the data values from the OpenWeatherMap API
	 */
Updates all the data values from the OpenWeatherMap API
Updates all the data values from the OpenWeatherMap API
@SuppressWarnings("rawtypes")
	private void updateSensors() {
		if (API_ENABLED) {
			StringBuilder theUrl = new StringBuilder("http://api.openweathermap.org/data/2.5/weather?q=");
			theUrl.append(theLocation);
			theUrl.append("&APPID=");
			theUrl.append(APIKEY);
			/*
			 * Project 3, Deliveries, Q4: Refactored code lines 275-308 below to output
			 *   temperature and wind speed without unnecessary conversions.
			 *   Removed some code complexity and added comments to make things
			 *   a bit easier to understand.
			 */
			theUrl.append("&units=imperial");
			try {
				URL url = new URL(theUrl.toString());
				JSONObject jo = (JSONObject) new JSONParser().parse(new BufferedReader(new InputStreamReader(url.openStream())));
				Map forecast = (Map) jo.get("main");
				temp = (int) (10 * (double) forecast.get("temp"));
				humid = ((Long) forecast.get("humidity")).intValue();
				pressure = (int) (1000 * (long)forecast.get("pressure") * HPA_INHG_COEF + 0.5);
				Map wind = (Map)jo.get("wind");
				windspd = ((Double) wind.get("speed")).intValue();
				if (wind.get("deg") != null) {
					winddir = (int)((long) wind.get("deg"));
				} else {
					winddir = 0;
				}
				Map rainObj = (Map) jo.get("rain");
				if (rainObj != null) {
					rain = (int)(10 * (double) rainObj.get("1h") * MM_IN_COEF + 0.5);
				} else {
					rain = 0;
				}
				Map sys = (Map)jo.get("sys");
				sunrise = unixToVantageTime((long) (sys.get("sunrise")));
				sunset = unixToVantageTime((long) (sys.get("sunset")));
			} catch (IOException | ParseException e) {
				e.printStackTrace();
			}
		} else {
			temp = getTemp() + (int)(RANDOM.nextGaussian() * TEMP_DEV + 0.5);
			humid = getHumid() + (int)(RANDOM.nextGaussian() * HUMID_DEV + 0.5);
			pressure = getPressure() + (int)(RANDOM.nextGaussian() * PRESSURE_DEV + 0.5);
			windspd = getWindspd() + (int)(RANDOM.nextGaussian() * WINDSPD_DEV + 0.5);
			winddir += (int)(RANDOM.nextGaussian() * WINDDIR_DEV + 0.5);
			rain = DISABLE_RAIN ? 0 : rain + (int)(RANDOM.nextGaussian() * RAIN_DEV + 0.5);
			temp = Math.max(Math.min(getTemp(), MAX_TEMP), MIN_TEMP);
			humid = Math.max(Math.min(getHumid(), MAX_HUMID), MIN_HUMID);
			pressure = Math.max(Math.min(getPressure(), MAX_PRESSURE), MIN_PRESSURE);
			windspd = Math.max(Math.min(getWindspd(), MAX_WINDSPD), MIN_WINDSPD);
			winddir = (winddir + WINDDIR_MOD)%WINDDIR_MOD;
			rain = Math.max(Math.min(rain, MAX_RAIN), MIN_RAIN);
		}
	}
@ SuppressWarnings ( "rawtypes" ) private void updateSensors ( ) { if ( API_ENABLED ) { StringBuilder theUrl = new StringBuilder ( "http://api.openweathermap.org/data/2.5/weather?q=" ) ; theUrl . append ( theLocation ) ; theUrl . append ( "&APPID=" ) ; theUrl . append ( APIKEY ) ; /*
			 * Project 3, Deliveries, Q4: Refactored code lines 275-308 below to output
			 *   temperature and wind speed without unnecessary conversions.
			 *   Removed some code complexity and added comments to make things
			 *   a bit easier to understand.
			 */ theUrl . append ( "&units=imperial" ) ; try { URL url = new URL ( theUrl . toString ( ) ) ; JSONObject jo = ( JSONObject ) new JSONParser ( ) . parse ( new BufferedReader ( new InputStreamReader ( url . openStream ( ) ) ) ) ; Map forecast = ( Map ) jo . get ( "main" ) ; temp = ( int ) ( 10 * ( double ) forecast . get ( "temp" ) ) ; humid = ( ( Long ) forecast . get ( "humidity" ) ) . intValue ( ) ; pressure = ( int ) ( 1000 * ( long ) forecast . get ( "pressure" ) * HPA_INHG_COEF + 0.5 ) ; Map wind = ( Map ) jo . get ( "wind" ) ; windspd = ( ( Double ) wind . get ( "speed" ) ) . intValue ( ) ; if ( wind . get ( "deg" ) != null ) { winddir = ( int ) ( ( long ) wind . get ( "deg" ) ) ; } else { winddir = 0 ; } Map rainObj = ( Map ) jo . get ( "rain" ) ; if ( rainObj != null ) { rain = ( int ) ( 10 * ( double ) rainObj . get ( "1h" ) * MM_IN_COEF + 0.5 ) ; } else { rain = 0 ; } Map sys = ( Map ) jo . get ( "sys" ) ; sunrise = unixToVantageTime ( ( long ) ( sys . get ( "sunrise" ) ) ) ; sunset = unixToVantageTime ( ( long ) ( sys . get ( "sunset" ) ) ) ; } catch ( IOException | ParseException e ) { e . printStackTrace ( ) ; } } else { temp = getTemp ( ) + ( int ) ( RANDOM . nextGaussian ( ) * TEMP_DEV + 0.5 ) ; humid = getHumid ( ) + ( int ) ( RANDOM . nextGaussian ( ) * HUMID_DEV + 0.5 ) ; pressure = getPressure ( ) + ( int ) ( RANDOM . nextGaussian ( ) * PRESSURE_DEV + 0.5 ) ; windspd = getWindspd ( ) + ( int ) ( RANDOM . nextGaussian ( ) * WINDSPD_DEV + 0.5 ) ; winddir += ( int ) ( RANDOM . nextGaussian ( ) * WINDDIR_DEV + 0.5 ) ; rain = DISABLE_RAIN ? 0 : rain + ( int ) ( RANDOM . nextGaussian ( ) * RAIN_DEV + 0.5 ) ; temp = Math . max ( Math . min ( getTemp ( ) , MAX_TEMP ) , MIN_TEMP ) ; humid = Math . max ( Math . min ( getHumid ( ) , MAX_HUMID ) , MIN_HUMID ) ; pressure = Math . max ( Math . min ( getPressure ( ) , MAX_PRESSURE ) , MIN_PRESSURE ) ; windspd = Math . max ( Math . min ( getWindspd ( ) , MAX_WINDSPD ) , MIN_WINDSPD ) ; winddir = ( winddir + WINDDIR_MOD ) % WINDDIR_MOD ; rain = Math . max ( Math . min ( rain , MAX_RAIN ) , MIN_RAIN ) ; } }
Updates all the data values from the OpenWeatherMap API
Updates all the data values from the OpenWeatherMap API
// Grab json from OpenWeatherMap API // Fetch current forecast data // Fetch wind speed and direction data // Fetch rain data // Fetch sunrise/sunset data