private void checkAddCity() { if (WeatherData.getCityInfo(0).mShowType == com.moji.mjweather.data.CityWeatherInfo.ShowType.ST_NOSET) { Gl.setHandler(MainHandler); Intent intent = new Intent(this, AddCityActivity.class); Bundle bundle = new Bundle(); bundle.putInt("cityindex", 0); bundle.putBoolean("isNoDisplayLocation", true); intent.putExtras(bundle); startActivity(intent); } }
private void initWDwithSensorDefaults(WeatherData data) { data.setTemp(TEMPDEFAULTVALUE); data.setPressure(PRESDEFAULTVALUE); data.setHumidity(HUMDEFAULTVALUE); data.setLight(LIGHTDEFAULTVALUE); data.setSensorWindSpeed(WSPEEDDEFAULTVALUE); }
public void update(Observable observable, Object arg) { if (observable instanceof WeatherData) { WeatherData weatherData = (WeatherData) observable; float t = weatherData.getTemperature(); float rh = weatherData.getHumidity(); heatIndex = (float) ((16.923 + (0.185212 * t)) + (5.37941 * rh) - (0.100254 * t * rh) + (0.00941695 * (t * t)) + (0.00728898 * (rh * rh)) + (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) + (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 * (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) + (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) + (0.000000000843296 * (t * t * rh * rh * rh)) - (0.0000000000481975 * (t * t * t * rh * rh * rh))); display(); } }
public static void main(String[] args) { WeatherData weatherData = new WeatherData(); DisplayCurrentConditions currentConditions = new DisplayCurrentConditions(weatherData); DisplayForecast forecast = new DisplayForecast(weatherData); DisplayStatistics statistics = new DisplayStatistics(weatherData); System.out.println("=======第一次更新======="); weatherData.setMeasurements(80, 65, 30.4f); currentConditions.update(weatherData, null); forecast.update(weatherData, null); statistics.update(weatherData, null); System.out.println("=======第二次更新======="); weatherData.setMeasurements(82, 70, 29.2f); currentConditions.update(weatherData, null); forecast.update(weatherData, null); statistics.update(weatherData, null); System.out.println("=======第三次更新======="); weatherData.setMeasurements(78, 90, 29.2f); currentConditions.update(weatherData, null); forecast.update(weatherData, null); statistics.update(weatherData, null); }
public static void main(String[] args) { WeatherData data = new WeatherData(); data.addObserver(new CurrentConditionDisplay()); data.addObserver(new StatisticsDisplay()); data.addObserver(new ForecastDisplay()); data.setMeasurements(30, 0.7f, 1024); }
public void update(Observable obs, Object arg) { if (obs instanceof WeatherData) { WeatherData weatherData = (WeatherData) obs; this.temperature = weatherData.getTemperature(); this.humidity = weatherData.getHumidity(); display(); } }
public static void main(String[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData); weatherData.setMeasurements(80, 65, 30.4f); weatherData.setMeasurements(82, 70, 29.2f); weatherData.setMeasurements(78, 90, 29.2f); }
public static void main(String[] args) { WeatherData weather = new WeatherData(); CurrentWeather cw = new CurrentWeather(weather); ForecastWeather fw = new ForecastWeather(weather); weather.setMeasurement(1, 2, 3); weather.setMeasurement(4, 5, 6); weather.setMeasurement(7, 8, 9); weather.setMeasurement(10, 11, 12); }
public static void main(String[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentConditions = new CurrentConditionsDisplay(weatherData); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData); weatherData.setMeasurements(80, 65, 30.4f); weatherData.setMeasurements(82, 70, 29.2f); weatherData.setMeasurements(78, 90, 29.2f); }
/** * Insert data into database * * @param weatherData object which you want to insert */ void addWeather(WeatherData weatherData) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(NAME, weatherData.getCity()); // Name values.put(LONG_NAME, weatherData.getCoordLon()); // LON values.put(LAT_NAME, weatherData.getCoordLat()); // LON // Inserting Row db.insert(TABLE_WEATHER, null, values); db.close(); // Closing database connection }
/** * Creates a string that represents the values in the top 5 Weather Results * * @return */ public String toStringValue() { return "ResultsTracker [wd1=" + wd1.getValue() + ", wd2=" + wd2.getValue() + ", wd3=" + wd3.getValue() + ", wd4=" + wd4.getValue() + ", wd5=" + wd5.getValue() + "]"; }
/** * Tests if the WeatherData is bigger than any of the results in the current top 5, and adds if it * does Automatically pushes down the other results * * @param wd WeatherData to test and add */ public void add(WeatherData wd) { if (wd.getValue() > wd1.getValue()) { replace1(wd); } else if (wd.getValue() > wd2.getValue()) { replace2(wd); } else if (wd.getValue() > wd3.getValue()) { replace3(wd); } else if (wd.getValue() > wd4.getValue()) { replace4(wd); } else if (wd.getValue() > wd5.getValue()) { replace5(wd); } }
public static void main(String[] args) { WeatherData weatherData = new WeatherData(); WeatherStation1 ws1 = new WeatherStation1(weatherData); WeatherStation2 ws2 = new WeatherStation2(weatherData); System.out.println("----------1st Set Data: ----------"); weatherData.setData(25, 80, 15); System.out.println("----------2nd Set Data: ----------"); weatherData.setData(30, 70, 25); System.out.println("----------3rd Set Data: ----------"); weatherData.setData(20, 50, 5); }
private void doIntentCityManager() { if (WeatherData.getCityInfo(Gl.getCurrentCityIndex()).mShowType == com.moji.mjweather.data.CityWeatherInfo.ShowType.ST_UPDATING) { Toast.makeText(this, 0x7f0b0041, 0).show(); } else { Gl.setChangedCity(false); startActivityForResult(new Intent(this, CityManagerActivity.class), 99); } }
private void doBroadcast() { if (WeatherData.getCityInfo(Gl.getCurrentCityIndex()).mShowType == com.moji.mjweather.data.CityWeatherInfo.ShowType.ST_NOSET) { mCDialogManager.showNotifyAddCityDialog(); } else { // PlayerUtil.manualBroadcast(mCDialogManager, MainHandler); if (PlayerUtil.isBroadcasting) mBroadcast.setImageResource(0x7f02014b); else mBroadcast.setImageResource(0x7f02014a); } }
private void initWDwithOwmDefaults(WeatherData data) { data.setOwmTemp(TEMPDEFAULTVALUE); data.setOwmPressure(PRESDEFAULTVALUE); data.setOwmHumidity(HUMDEFAULTVALUE); data.setOwmDesc("N/A"); data.setOwmName("N/A"); data.setOwmWindSpeed(WSPEEDDEFAULTVALUE); data.setOwmWindDegree(WDEGREEDEFAULTVALUE); }
private void doAddCityResult(Message message) { int i = 0; while (i < 9) { if (WeatherData.getCityInfo(i).m_cityID == message.arg2) { mCDialogManager.CancelCurrentDialog(); if (message.arg2 != -1) mCDialogManager.ShowMsgOKDialog(0x7f0b0035); else mCDialogManager.ShowMsgOKDialog(0x7f0b003f); } i++; } CityWeatherInfo cityweatherinfo = WeatherData.getCityInfo(message.arg1); cityweatherinfo.Clean(); cityweatherinfo.m_cityID = message.arg2; cityweatherinfo.mCityName = message.obj.toString(); cityweatherinfo.mShowType = com.moji.mjweather.data.CityWeatherInfo.ShowType.ST_NEED_BE_UPDATE; cityweatherinfo.mWeatherMainInfo.mWeatherDescription = ""; cityweatherinfo.m_lastUpdateTime = ""; Gl.saveCityInfo(message.arg1, cityweatherinfo); Gl.saveCurrentCityIndex(message.arg1); }
/** * Get Single object from the database * * @param id Primary key * @return WeatherData object */ public WeatherData getweather(int id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query( TABLE_WEATHER, new String[] {"id", NAME, LONG_NAME, LONG_NAME}, "id" + "=?", new String[] {String.valueOf(id)}, null, null, null, null); if (cursor != null) cursor.moveToFirst(); WeatherData weatherData = new WeatherData(); weatherData.setCity(cursor.getString(0)); weatherData.setCoordLon(Double.parseDouble(cursor.getString(1))); weatherData.setCoordLon(Double.parseDouble(cursor.getString(2))); return weatherData; }
private void startUpdateWeather() { mUpdateWeather.setImageResource(0x7f02014f); isUpdating = true; if (mUpdateCallbackImpl == null) mUpdateCallbackImpl = new UpdateCallbackImpl(); WeatherPublisher.getInstance().unSubscribe(mUpdateCallbackImpl); WeatherPublisher.getInstance().subscribe(mUpdateCallbackImpl); if (mWeatherUpdater == null) mWeatherUpdater = AbstractWeatherUpdater.createInstance(); mWeatherUpdater.update(Gl.getCurrentCityIndex()); WeatherData.getCityInfo(Gl.getCurrentCityIndex()).mShowType = com.moji.mjweather.data.CityWeatherInfo.ShowType.ST_UPDATING; mUpdateLayout.setVisibility(0); if (mPublicTextView != null) mPublicTextView.setVisibility(8); }
/** * Initializes data with all the values which the connected sensor array provides. WindDegree, * WeatherDescription and WeatherName are only provided by OpenWeatherMap and therefore missing. * * @param data WeatherData object to be initialized. */ private void initWDwithSensorData(WeatherData data) { try { data.setTemp(curSensData.getDouble("temperature")); System.out.println("set temp"); } catch (Exception e) { data.setTemp(TEMPDEFAULTVALUE); } try { data.setPressure(curSensData.getDouble("pressure")); System.out.println("set pres"); } catch (Exception e) { data.setPressure(PRESDEFAULTVALUE); } try { data.setHumidity(curSensData.getDouble("humidity")); System.out.println("set hum"); } catch (Exception e) { data.setHumidity(HUMDEFAULTVALUE); } try { data.setSensorWindSpeed(curSensWind); } catch (Exception e1) { data.setSensorWindSpeed(WSPEEDDEFAULTVALUE); } // data.setSensorWindSpeed(WSPEEDDEFAULTVALUE); System.out.println("set ws"); try { data.setLight(curSensData.getDouble("luminosity")); System.out.println("set light"); } catch (Exception e) { data.setLight(LIGHTDEFAULTVALUE); } }
private void doUpdateResult(com.moji.mjweather.common.WeatherUpdater.Result result) { if (AbstractWeatherUpdater.isSucceed(result)) { CityWeatherInfo cityweatherinfo = WeatherData.getCityInfo(result.cityIndex); Gl.Ct() .deleteFile( (new StringBuilder()).append(cityweatherinfo.m_cityID).append(".txt").toString()); mWorkSpace.replaceCity(Gl.getCurrentCityIndex(), false); Gl.setNeedNotifyTrendState(true); } else { mCDialogManager.CancelCurrentDialog(); mCDialogManager.ShowMsgOKDialogFromString((String) result.errMsg); } cancelUpdateWeather(null, false); }
/** * Return List of All records of WeatherData from DB * * @return List<WeatherData> */ public List<WeatherData> getAllWeather() { List<WeatherData> weatherList = new ArrayList<WeatherData>(); // Select All Query String selectQuery = "SELECT * FROM " + TABLE_WEATHER; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToFirst()) { do { WeatherData weatherData = new WeatherData(); weatherData.setID(cursor.getString(0)); weatherData.setCity(cursor.getString(1)); weatherData.setCoordLon(Double.parseDouble(cursor.getString(2))); weatherData.setCoordLat(Double.parseDouble(cursor.getString(3))); weatherList.add(weatherData); } while (cursor.moveToNext()); } // return weather list return weatherList; }
public void parse(long cityId, ContentProviderClient provider, SyncResult result) { try { WeatherData data = new Gson().fromJson(new InputStreamReader(mInputStream), WeatherData.class); result.stats.numDeletes += provider.delete( WeatherProvider.URI, WeatherProvider.Columns.CITY_ID + "=?", new String[] {String.valueOf(cityId)}); ContentValues cityValues = new ContentValues(); cityValues.put(CitiesProvider.Columns.NAME, data.getCityName()); result.stats.numUpdates += provider.update( CitiesProvider.URI, cityValues, CitiesProvider.Columns._ID + "=?", new String[] {String.valueOf(cityId)}); List<ContentValues> insertData = new ArrayList<>(); for (WeatherData.Measurement measurement : data.getMeasurements()) { ContentValues cv = new ContentValues(); cv.put(WeatherProvider.Columns.CITY_ID, cityId); cv.put(WeatherProvider.Columns.TEMP, measurement.getTemp()); cv.put(WeatherProvider.Columns.DATE, measurement.getDate()); cv.put(WeatherProvider.Columns.WIND_SPEED, measurement.getWindSpeed()); insertData.add(cv); } result.stats.numInserts += provider.bulkInsert( WeatherProvider.URI, insertData.toArray(new ContentValues[insertData.size()])); } catch (Exception e) { ++result.stats.numParseExceptions; } }
private void cancelUpdateWeather(CityWeatherInfo cityweatherinfo, boolean flag) { if (cityweatherinfo == null) cityweatherinfo = WeatherData.getCityInfo(Gl.getCurrentCityIndex()); mUpdateWeather.setImageResource(0x7f02014e); isUpdating = false; Gl.setIsLocationbyGPS(false); if (mWeatherUpdater != null) mWeatherUpdater.cancel(); if (mUpdateCallbackImpl != null) WeatherPublisher.getInstance().unSubscribe(mUpdateCallbackImpl); mUpdateLayout.setVisibility(8); if (mPublicTextView != null) mPublicTextView.setVisibility(0); if (cityweatherinfo.mShowType == com.moji.mjweather.data.CityWeatherInfo.ShowType.ST_UPDATING) if (cityweatherinfo.m_lastUpdateTime.length() <= 0) cityweatherinfo.mShowType = com.moji.mjweather.data.CityWeatherInfo.ShowType.ST_NEED_BE_UPDATE; else cityweatherinfo.mShowType = com.moji.mjweather.data.CityWeatherInfo.ShowType.ST_OK; if (flag) Toast.makeText(this, 0x7f0b0039, 0).show(); }
private void doUpdateCityWeather(boolean flag) { CityWeatherInfo cityweatherinfo = WeatherData.getCityInfo(Gl.getCurrentCityIndex()); if ((!flag || cityweatherinfo.mShowType == com.moji.mjweather.data.CityWeatherInfo.ShowType.ST_NEED_BE_UPDATE) && cityweatherinfo.mShowType != com.moji.mjweather.data.CityWeatherInfo.ShowType.ST_NOSET) if (!Util.isConnectInternet(this)) { mCDialogManager.CancelCurrentDialog(); mCDialogManager.ShowMsgOKDialog(0x7f0b0032); } else { mUpdateLayout = (LinearLayout) mWorkSpace.getChildAt(mWorkSpace.getCurrentScreen()).findViewById(0x7f0d0106); if (cityweatherinfo.mShowType == com.moji.mjweather.data.CityWeatherInfo.ShowType.ST_OK) mPublicTextView = (TextView) mWorkSpace.getChildAt(mWorkSpace.getCurrentScreen()).findViewById(0x7f0d0117); if (isUpdating) cancelUpdateWeather(cityweatherinfo, true); else startUpdateWeather(); } }
public void setForecastWeatherData(JSONArray owmAllForecastData) throws WeatherDataServiceException { try { for (int i = 0; i < 3; i++) { JSONObject owmAllData = owmAllForecastData.getJSONObject(i + 1); // start from next day this.forecastDataArr[i] = new WeatherData(); try { this.forecastDataArr[i].setTemperature( owmAllData.getJSONObject("temp").get("day").toString()); this.forecastDataArr[i].setHumidity(owmAllData.get("humidity").toString()); this.forecastDataArr[i].setLastUpdate( WeatherData.setLastUpdateTime(owmAllData.get("dt").toString())); this.forecastDataArr[i].setWeatherMain( owmAllData.getJSONArray("weather").getJSONObject(0).get("main").toString()); this.forecastDataArr[i].setWeatherDesc( owmAllData.getJSONArray("weather").getJSONObject(0).get("description").toString()); } catch (Exception SetWeatherDataException) { throw new WeatherDataServiceException(SetWeatherDataException.getMessage()); } } } catch (Exception SetWeatherDataException) { throw new WeatherDataServiceException(SetWeatherDataException.getMessage()); } }
public ForecastDisplay(WeatherData weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this); }
public StatisticsDisplay(WeatherData weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this); }
/** * Initializes data with all the values which OpenWeatherMap provides. Only value missing is * therefore the current light intensity. * * @param currentWeather CurrentWeather object from which data is taken. * @param data WeatherData object to be initialized. */ private void initWDwithOWMData(CurrentWeather currentWeather, WeatherData data) { try { if (currentWeather.getMainInstance() != null) { data.setOwmTemp(fahrenheitToCelsius(currentWeather.getMainInstance().getTemperature())); data.setOwmPressure(currentWeather.getMainInstance().getPressure()); data.setOwmHumidity(currentWeather.getMainInstance().getHumidity()); } else { data.setOwmTemp(TEMPDEFAULTVALUE); data.setOwmPressure(PRESDEFAULTVALUE); data.setOwmHumidity(HUMDEFAULTVALUE); } if (currentWeather.getWindInstance() != null) { data.setOwmWindSpeed(currentWeather.getWindInstance().getWindSpeed()); data.setOwmWindDegree(currentWeather.getWindInstance().getWindDegree()); } else { data.setOwmWindSpeed(WSPEEDDEFAULTVALUE); data.setOwmWindDegree(WDEGREEDEFAULTVALUE); } if ((currentWeather.getWeatherCount() > 0) && (currentWeather.getWeatherInstance(0) != null)) { data.setOwmDesc(currentWeather.getWeatherInstance(0).getWeatherDescription()); data.setOwmName(currentWeather.getWeatherInstance(0).getWeatherName()); } else { data.setOwmDesc("N/A"); data.setOwmName("N/A"); } if (currentWeather.getRainInstance() != null) { if (currentWeather.getRainInstance().hasRain1h()) System.out.println(currentWeather.getRainInstance().getRain1h()); if (currentWeather.getRainInstance().hasRain3h()) System.out.println(currentWeather.getRainInstance().getRain3h()); } if (currentWeather.getCloudsInstance() != null) { if (currentWeather.getCloudsInstance().hasPercentageOfClouds()) System.out.println(currentWeather.getCloudsInstance().getPercentageOfClouds()); } } catch (NullPointerException e) { e.printStackTrace(); initWDwithOwmDefaults(data); } }
public void run() { registerDriver(); dbConnection = establishConnection(); // #1 security EU. Plz no stealerino. String apiKey = "26ef2b98aa2077410020408feb29cde8"; owmap = new OpenWeatherMap(apiKey); System.out.println("created OpenWeatherMap instance"); while (true) { if (client != null) { client.requestAllData(); try { client.requestWindSpeed(); } catch (Exception e1) { System.out.println("Failed to request wind speed."); e1.printStackTrace(); } } // Not sure if needed // else { // System.out.println("SensorClient unreachable."); // if (!reconnectorRunning) { // SCReconnector reconnector = new SCReconnector(); // reconnector.start(); // } // } System.out.println("---------------------------------------"); Date date = new Date(); System.out.println(date.toString()); CurrentWeather currentWeather = null; try { weathermapAccessible = true; currentWeather = getCurrentWeatherWithTimeout(currentWeather); System.out.println("Received current Weather from OpenWeatherMap."); } catch (ExecutionException e) { System.out.println("OpenWeatherMap appears to be inaccessible."); weathermapAccessible = false; // e.printStackTrace(); } catch (InterruptedException e) { weathermapAccessible = false; System.out.println("Request interrupted while waiting for response."); // e.printStackTrace(); } catch (TimeoutException e) { weathermapAccessible = false; System.out.println( "Did not receive response from openweathermap within " + WEATHER_REQUEST_TIMEOUT + " seconds."); // e.printStackTrace(); } sensorServerAvailable = true; System.out.println("Creating WeatherData object..."); WeatherData data = initializeWeatherDataObject( currentWeather, sensorServerAvailable, weathermapAccessible); System.out.println("Created WeatherData object"); String query = "INSERT INTO weatherdatalog (temperature, pressure, humidity, owmtemperature, owmpressure, owmhumidity, sensorwindspeed, owmwindspeed, owmwinddegree, light, owmweathername, owmweatherdesc)" + " VALUES (" + data.getTemp() + ", " + data.getPressure() + ", " + data.getHumidity() + ", " + data.getOwmTemp() + ", " + data.getOwmPressure() + ", " + data.getOwmHumidity() + ", " + data.getSensorWindSpeed() + ", " + data.getOwmWindSpeed() + ", " + data.getOwmWindDegree() + ", " + data.getLight() + ", '" + data.getOwmName() + "', '" + data.getOwmDesc() + "');"; try { Statement statement = dbConnection.createStatement(); System.out.println("Executing query..."); statement.executeUpdate(query); System.out.println("Inserted data"); } catch (SQLException e) { e.printStackTrace(); } try { Thread.sleep(ACQUIREDATAINTERVAL); } catch (InterruptedException e) { System.out.println("Sleep was interrupted"); e.printStackTrace(); } } }