Improve code in WeekWeatherAdapter

Improve isOnline check #4
Offer UNDO operation when location is deleted #5
Use cells peak power if cell area or efficiency = 0
This commit is contained in:
woheller69 2023-04-26 09:46:50 +02:00
parent 6492fc241f
commit b8b37deacf
7 changed files with 68 additions and 47 deletions

View file

@ -75,8 +75,12 @@ public class SolarPowerPlant {
double totalRadiationOnCell = solarPowerNormal * efficiency + solarPowerDiffuse * diffuseEfficiency; //flat plate equivalent of the solar irradiance
double cellTemperature = calcCellTemperature(ambientTemperature,totalRadiationOnCell);
double dcPower = totalRadiationOnCell * cellsEfficiency * (1+(cellTemperature - 25)*cellsTempCoeff) * cellsArea;
double dcPower;
if (cellsEfficiency!=0 && cellsArea!=0){
dcPower = totalRadiationOnCell * (1+(cellTemperature - 25)*cellsTempCoeff) * cellsEfficiency * cellsArea;
} else { //assume cellMaxPower is defined at 1000W/sqm
dcPower = totalRadiationOnCell/1000 * (1+(cellTemperature - 25)*cellsTempCoeff) * cellsMaxPower;
}
double acPower = Math.min(dcPower * inverterEfficiency, inverterPowerLimit);

View file

@ -20,6 +20,11 @@ import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* This class provides the functionality to fetch forecast data for a given city as a background
@ -54,7 +59,7 @@ public class UpdateDataService extends JobIntentService {
@Override
protected void onHandleWork(Intent intent) {
if (!isOnline()) {
if (!isOnline(2000)) {
Handler h = new Handler(getApplicationContext().getMainLooper());
h.post(new Runnable() {
@Override
@ -116,14 +121,22 @@ public class UpdateDataService extends JobIntentService {
}
}
private boolean isOnline() {
private boolean isOnline(int timeOut) { //https://stackoverflow.com/questions/9570237/android-check-internet-connection
InetAddress inetAddress = null;
try {
Future<InetAddress> future = Executors.newSingleThreadExecutor().submit(() -> {
try {
URL url = new URL(BuildConfig.BASE_URL);
InetAddress inetAddress = InetAddress.getByName(url.getHost());
return inetAddress.isReachable(2000);
} catch (IOException | IllegalArgumentException e) {
return false;
return InetAddress.getByName(url.getHost());
} catch ( IOException e) {
return null;
}
});
inetAddress = future.get(timeOut, TimeUnit.MILLISECONDS);
future.cancel(true);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
}
return inetAddress!=null && !inetAddress.toString().isEmpty();
}
private void handleUpdateForecastAction(Intent intent) {

View file

@ -37,7 +37,7 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
private int[] dataSetTypes;
private List<HourlyForecast> courseDayList;
private float[][] forecastData;
private List<WeekForecast> weekForecastList;
private Context context;
private ViewGroup mParent;
@ -142,24 +142,8 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
}
}
SQLiteHelper dbHelper = SQLiteHelper.getInstance(context.getApplicationContext());
int zonemilliseconds = dbHelper.getGeneralDataByCityId(cityId).getTimeZoneSeconds() * 1000;
weekForecastList = forecasts;
forecastData = new float[forecasts.size()][11];
for (int i=0;i<forecasts.size();i++){
forecastData[i][0]=0;
forecastData[i][1]=0;
forecastData[i][2]=0;
forecastData[i][3]=0;
forecastData[i][4]=forecasts.get(i).getEnergyDay();
forecastData[i][5]=0;
forecastData[i][6]=0;
forecastData[i][7]=0;
forecastData[i][8]=forecasts.get(i).getForecastTime()+zonemilliseconds;
forecastData[i][9]=forecasts.get(i).getWeatherID();
forecastData[i][10]=1;
}
notifyDataSetChanged();
}
@ -307,7 +291,7 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
holder.recyclerView.setLayoutManager(layoutManager);
final WeekWeatherAdapter adapter = new WeekWeatherAdapter(context, forecastData, generalDataList.getCity_id());
final WeekWeatherAdapter adapter = new WeekWeatherAdapter(context, weekForecastList, generalDataList.getCity_id());
holder.recyclerView.setAdapter(adapter);
holder.recyclerView.setFocusable(false);
@ -384,7 +368,7 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
} else if (viewHolder.getItemViewType() == CHART) {
ChartViewHolder holder = (ChartViewHolder) viewHolder;
if(forecastData==null || forecastData.length==0 || forecastData[0]==null) return;
if(weekForecastList.isEmpty()) return;
float energyMax=0;
@ -393,13 +377,13 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT"));
for (int i=0 ; i< forecastData.length;i++) {
c.setTimeInMillis((long) forecastData[i][8]);
for (int i = 0 ; i < weekForecastList.size(); i++) {
c.setTimeInMillis(weekForecastList.get(i).getLocalForecastTime(context));
int day = c.get(Calendar.DAY_OF_WEEK);
float energyDay=forecastData[i][4];
float energyDay=weekForecastList.get(i).getEnergyDay();
String dayString = context.getResources().getString(StringFormatUtils.getDayShort(day));
if (forecastData.length>8) dayString=dayString.substring(0,1); //use first character only if more than 8 days to avoid overlapping text
if (weekForecastList.size()>8) dayString=dayString.substring(0,1); //use first character only if more than 8 days to avoid overlapping text
energyDataset.addBar(dayString, energyDay);
if (energyDay>energyMax) energyMax=energyDay;

View file

@ -8,6 +8,8 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.snackbar.Snackbar;
import org.woheller69.weather.R;
import org.woheller69.weather.database.CityToWatch;
import org.woheller69.weather.database.SQLiteHelper;
@ -28,6 +30,7 @@ public class RecyclerOverviewListAdapter extends RecyclerView.Adapter<ItemViewHo
*/
private Context context;
private final List<CityToWatch> cities;
private RecyclerView rv;
SQLiteHelper database;
@ -49,6 +52,7 @@ public class RecyclerOverviewListAdapter extends RecyclerView.Adapter<ItemViewHo
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_city_list, parent, false);
rv = (RecyclerView) parent;
return new ItemViewHolder(view);
}
@ -90,6 +94,17 @@ public class RecyclerOverviewListAdapter extends RecyclerView.Adapter<ItemViewHo
database.deleteCityToWatch(city);
cities.remove(position);
notifyItemRemoved(position);
Snackbar.make(rv,context.getString(R.string.itemRemoved,city.getCityName()),Snackbar.LENGTH_LONG).setAction(context.getString(R.string.undo), new View.OnClickListener() {
@Override
public void onClick(View view) {
long id=database.addCityToWatch(city);
city.setId((int) id);
city.setCityId((int) id); //use id also instead of city id as unique identifier
cities.add(position,city);
notifyItemInserted(position);
}
}).show();
}
/**

View file

@ -14,11 +14,13 @@ import android.widget.TextView;
import org.woheller69.weather.R;
import org.woheller69.weather.database.GeneralData;
import org.woheller69.weather.database.SQLiteHelper;
import org.woheller69.weather.database.WeekForecast;
import org.woheller69.weather.ui.Help.StringFormatUtils;
import org.woheller69.weather.ui.UiResourceProvider;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
/**
@ -28,16 +30,16 @@ import java.util.TimeZone;
public class WeekWeatherAdapter extends RecyclerView.Adapter<WeekWeatherAdapter.WeekForecastViewHolder> {
private Context context;
private float[][] forecastData;
private List<WeekForecast> weekForecastList;
private int cityID;
private Date courseOfDayHeaderDate;
WeekWeatherAdapter(Context context, float[][] forecastData, int cityID) {
WeekWeatherAdapter(Context context, List<WeekForecast> weekForecastList, int cityID) {
this.context = context;
this.cityID = cityID;
this.forecastData = forecastData;
if (forecastData!=null && forecastData.length!=0 && forecastData[0]!=null) {
this.courseOfDayHeaderDate = new Date((long) forecastData[0][8]); //init with date of first weekday
this.weekForecastList = weekForecastList;
if (!weekForecastList.isEmpty()) {
this.courseOfDayHeaderDate = new Date(weekForecastList.get(0).getLocalForecastTime(context)); //init with date of first weekday
} else this.courseOfDayHeaderDate = new Date(); //fallback if no data available
}
@ -65,15 +67,14 @@ public class WeekWeatherAdapter extends RecyclerView.Adapter<WeekWeatherAdapter.
@Override
public void onBindViewHolder(WeekForecastViewHolder holder, int position) {
float[] dayValues = forecastData[position];
if (dayValues.length!=11) return; //Fixes app crash if forecastData not yet ready.
WeekForecast weekForecast = weekForecastList.get(position);
SQLiteHelper dbHelper = SQLiteHelper.getInstance(context);
GeneralData generalData = dbHelper.getGeneralDataByCityId(cityID);
Calendar forecastTime = Calendar.getInstance();
forecastTime.setTimeZone(TimeZone.getTimeZone("GMT"));
forecastTime.setTimeInMillis((long) dayValues[8]);
forecastTime.setTimeInMillis(weekForecast.getLocalForecastTime(context));
boolean isDay;
@ -87,15 +88,15 @@ public class WeekWeatherAdapter extends RecyclerView.Adapter<WeekWeatherAdapter.
isDay = true;
}
setIcon((int) dayValues[9], holder.weather, isDay);
if (dayValues[4] == 0)
setIcon(weekForecast.getWeatherID(), holder.weather, isDay);
if (weekForecast.getEnergyDay() == 0)
holder.power.setText("-");
else
holder.power.setText(StringFormatUtils.formatDecimal(dayValues[4], context.getString(R.string.units_kWh)));
holder.power.setText(StringFormatUtils.formatDecimal(weekForecast.getEnergyDay(), context.getString(R.string.units_kWh)));
Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT"));
c.setTimeInMillis((long) dayValues[8]);
c.setTimeInMillis(weekForecast.getLocalForecastTime(context));
int day = c.get(Calendar.DAY_OF_WEEK);
holder.day.setText(StringFormatUtils.getDayShort(day));
@ -113,8 +114,8 @@ public class WeekWeatherAdapter extends RecyclerView.Adapter<WeekWeatherAdapter.
@Override
public int getItemCount() {
if (forecastData!=null)
return forecastData.length;
if (!weekForecastList.isEmpty())
return weekForecastList.size();
else
return 0;
}

View file

@ -105,6 +105,8 @@
<string name="settings_summarize">Summe anzeigen</string>
<string name="summary_summarize">Summiere die Werte aller Module mit gleichem Längen- und Breitengrad.</string>
<string name="dialog_add_clone_button">Klonen</string>
<string name="itemRemoved">ENTFERNT:\u0020\u0020%s</string>
<string name="undo">WIEDERHERSTELLEN</string>
</resources>

View file

@ -108,4 +108,6 @@
<string name="settings_summarize">Show sum</string>
<string name="summary_summarize">Summarize values of modules at the same latitude and longitude.</string>
<string name="dialog_add_clone_button">Clone</string>
<string name="itemRemoved">REMOVED:\u0020\u0020%s</string>
<string name="undo">UNDO</string>
</resources>