mirror of
https://github.com/woheller69/solxpect.git
synced 2025-09-22 08:50:41 +02:00
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:
parent
6492fc241f
commit
b8b37deacf
7 changed files with 68 additions and 47 deletions
|
@ -75,8 +75,12 @@ public class SolarPowerPlant {
|
||||||
|
|
||||||
double totalRadiationOnCell = solarPowerNormal * efficiency + solarPowerDiffuse * diffuseEfficiency; //flat plate equivalent of the solar irradiance
|
double totalRadiationOnCell = solarPowerNormal * efficiency + solarPowerDiffuse * diffuseEfficiency; //flat plate equivalent of the solar irradiance
|
||||||
double cellTemperature = calcCellTemperature(ambientTemperature,totalRadiationOnCell);
|
double cellTemperature = calcCellTemperature(ambientTemperature,totalRadiationOnCell);
|
||||||
|
double dcPower;
|
||||||
double dcPower = totalRadiationOnCell * cellsEfficiency * (1+(cellTemperature - 25)*cellsTempCoeff) * cellsArea;
|
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);
|
double acPower = Math.min(dcPower * inverterEfficiency, inverterPowerLimit);
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,11 @@ import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.List;
|
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
|
* 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
|
@Override
|
||||||
protected void onHandleWork(Intent intent) {
|
protected void onHandleWork(Intent intent) {
|
||||||
if (!isOnline()) {
|
if (!isOnline(2000)) {
|
||||||
Handler h = new Handler(getApplicationContext().getMainLooper());
|
Handler h = new Handler(getApplicationContext().getMainLooper());
|
||||||
h.post(new Runnable() {
|
h.post(new Runnable() {
|
||||||
@Override
|
@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 {
|
try {
|
||||||
URL url = new URL(BuildConfig.BASE_URL);
|
Future<InetAddress> future = Executors.newSingleThreadExecutor().submit(() -> {
|
||||||
InetAddress inetAddress = InetAddress.getByName(url.getHost());
|
try {
|
||||||
return inetAddress.isReachable(2000);
|
URL url = new URL(BuildConfig.BASE_URL);
|
||||||
} catch (IOException | IllegalArgumentException e) {
|
return InetAddress.getByName(url.getHost());
|
||||||
return false;
|
} 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) {
|
private void handleUpdateForecastAction(Intent intent) {
|
||||||
|
|
|
@ -37,7 +37,7 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
|
||||||
|
|
||||||
private int[] dataSetTypes;
|
private int[] dataSetTypes;
|
||||||
private List<HourlyForecast> courseDayList;
|
private List<HourlyForecast> courseDayList;
|
||||||
private float[][] forecastData;
|
private List<WeekForecast> weekForecastList;
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
private ViewGroup mParent;
|
private ViewGroup mParent;
|
||||||
|
@ -142,24 +142,8 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SQLiteHelper dbHelper = SQLiteHelper.getInstance(context.getApplicationContext());
|
weekForecastList = forecasts;
|
||||||
int zonemilliseconds = dbHelper.getGeneralDataByCityId(cityId).getTimeZoneSeconds() * 1000;
|
|
||||||
|
|
||||||
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();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
@ -307,7 +291,7 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
|
||||||
holder.recyclerView.setLayoutManager(layoutManager);
|
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.setAdapter(adapter);
|
||||||
holder.recyclerView.setFocusable(false);
|
holder.recyclerView.setFocusable(false);
|
||||||
|
|
||||||
|
@ -384,7 +368,7 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
|
||||||
} else if (viewHolder.getItemViewType() == CHART) {
|
} else if (viewHolder.getItemViewType() == CHART) {
|
||||||
ChartViewHolder holder = (ChartViewHolder) viewHolder;
|
ChartViewHolder holder = (ChartViewHolder) viewHolder;
|
||||||
|
|
||||||
if(forecastData==null || forecastData.length==0 || forecastData[0]==null) return;
|
if(weekForecastList.isEmpty()) return;
|
||||||
|
|
||||||
float energyMax=0;
|
float energyMax=0;
|
||||||
|
|
||||||
|
@ -393,13 +377,13 @@ public class CityWeatherAdapter extends RecyclerView.Adapter<CityWeatherAdapter.
|
||||||
Calendar c = Calendar.getInstance();
|
Calendar c = Calendar.getInstance();
|
||||||
c.setTimeZone(TimeZone.getTimeZone("GMT"));
|
c.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
|
|
||||||
for (int i=0 ; i< forecastData.length;i++) {
|
for (int i = 0 ; i < weekForecastList.size(); i++) {
|
||||||
c.setTimeInMillis((long) forecastData[i][8]);
|
c.setTimeInMillis(weekForecastList.get(i).getLocalForecastTime(context));
|
||||||
int day = c.get(Calendar.DAY_OF_WEEK);
|
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));
|
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);
|
energyDataset.addBar(dayString, energyDay);
|
||||||
if (energyDay>energyMax) energyMax=energyDay;
|
if (energyDay>energyMax) energyMax=energyDay;
|
||||||
|
|
|
@ -8,6 +8,8 @@ import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
|
|
||||||
import org.woheller69.weather.R;
|
import org.woheller69.weather.R;
|
||||||
import org.woheller69.weather.database.CityToWatch;
|
import org.woheller69.weather.database.CityToWatch;
|
||||||
import org.woheller69.weather.database.SQLiteHelper;
|
import org.woheller69.weather.database.SQLiteHelper;
|
||||||
|
@ -28,6 +30,7 @@ public class RecyclerOverviewListAdapter extends RecyclerView.Adapter<ItemViewHo
|
||||||
*/
|
*/
|
||||||
private Context context;
|
private Context context;
|
||||||
private final List<CityToWatch> cities;
|
private final List<CityToWatch> cities;
|
||||||
|
private RecyclerView rv;
|
||||||
|
|
||||||
SQLiteHelper database;
|
SQLiteHelper database;
|
||||||
|
|
||||||
|
@ -49,6 +52,7 @@ public class RecyclerOverviewListAdapter extends RecyclerView.Adapter<ItemViewHo
|
||||||
@Override
|
@Override
|
||||||
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_city_list, parent, false);
|
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_city_list, parent, false);
|
||||||
|
rv = (RecyclerView) parent;
|
||||||
return new ItemViewHolder(view);
|
return new ItemViewHolder(view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +94,17 @@ public class RecyclerOverviewListAdapter extends RecyclerView.Adapter<ItemViewHo
|
||||||
database.deleteCityToWatch(city);
|
database.deleteCityToWatch(city);
|
||||||
cities.remove(position);
|
cities.remove(position);
|
||||||
notifyItemRemoved(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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -14,11 +14,13 @@ import android.widget.TextView;
|
||||||
import org.woheller69.weather.R;
|
import org.woheller69.weather.R;
|
||||||
import org.woheller69.weather.database.GeneralData;
|
import org.woheller69.weather.database.GeneralData;
|
||||||
import org.woheller69.weather.database.SQLiteHelper;
|
import org.woheller69.weather.database.SQLiteHelper;
|
||||||
|
import org.woheller69.weather.database.WeekForecast;
|
||||||
import org.woheller69.weather.ui.Help.StringFormatUtils;
|
import org.woheller69.weather.ui.Help.StringFormatUtils;
|
||||||
import org.woheller69.weather.ui.UiResourceProvider;
|
import org.woheller69.weather.ui.UiResourceProvider;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,16 +30,16 @@ import java.util.TimeZone;
|
||||||
public class WeekWeatherAdapter extends RecyclerView.Adapter<WeekWeatherAdapter.WeekForecastViewHolder> {
|
public class WeekWeatherAdapter extends RecyclerView.Adapter<WeekWeatherAdapter.WeekForecastViewHolder> {
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
private float[][] forecastData;
|
private List<WeekForecast> weekForecastList;
|
||||||
private int cityID;
|
private int cityID;
|
||||||
private Date courseOfDayHeaderDate;
|
private Date courseOfDayHeaderDate;
|
||||||
|
|
||||||
WeekWeatherAdapter(Context context, float[][] forecastData, int cityID) {
|
WeekWeatherAdapter(Context context, List<WeekForecast> weekForecastList, int cityID) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.cityID = cityID;
|
this.cityID = cityID;
|
||||||
this.forecastData = forecastData;
|
this.weekForecastList = weekForecastList;
|
||||||
if (forecastData!=null && forecastData.length!=0 && forecastData[0]!=null) {
|
if (!weekForecastList.isEmpty()) {
|
||||||
this.courseOfDayHeaderDate = new Date((long) forecastData[0][8]); //init with date of first weekday
|
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
|
} else this.courseOfDayHeaderDate = new Date(); //fallback if no data available
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,15 +67,14 @@ public class WeekWeatherAdapter extends RecyclerView.Adapter<WeekWeatherAdapter.
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(WeekForecastViewHolder holder, int position) {
|
public void onBindViewHolder(WeekForecastViewHolder holder, int position) {
|
||||||
float[] dayValues = forecastData[position];
|
WeekForecast weekForecast = weekForecastList.get(position);
|
||||||
if (dayValues.length!=11) return; //Fixes app crash if forecastData not yet ready.
|
|
||||||
|
|
||||||
SQLiteHelper dbHelper = SQLiteHelper.getInstance(context);
|
SQLiteHelper dbHelper = SQLiteHelper.getInstance(context);
|
||||||
GeneralData generalData = dbHelper.getGeneralDataByCityId(cityID);
|
GeneralData generalData = dbHelper.getGeneralDataByCityId(cityID);
|
||||||
|
|
||||||
Calendar forecastTime = Calendar.getInstance();
|
Calendar forecastTime = Calendar.getInstance();
|
||||||
forecastTime.setTimeZone(TimeZone.getTimeZone("GMT"));
|
forecastTime.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
forecastTime.setTimeInMillis((long) dayValues[8]);
|
forecastTime.setTimeInMillis(weekForecast.getLocalForecastTime(context));
|
||||||
|
|
||||||
boolean isDay;
|
boolean isDay;
|
||||||
|
|
||||||
|
@ -87,15 +88,15 @@ public class WeekWeatherAdapter extends RecyclerView.Adapter<WeekWeatherAdapter.
|
||||||
isDay = true;
|
isDay = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
setIcon((int) dayValues[9], holder.weather, isDay);
|
setIcon(weekForecast.getWeatherID(), holder.weather, isDay);
|
||||||
if (dayValues[4] == 0)
|
if (weekForecast.getEnergyDay() == 0)
|
||||||
holder.power.setText("-");
|
holder.power.setText("-");
|
||||||
else
|
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();
|
Calendar c = Calendar.getInstance();
|
||||||
c.setTimeZone(TimeZone.getTimeZone("GMT"));
|
c.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
c.setTimeInMillis((long) dayValues[8]);
|
c.setTimeInMillis(weekForecast.getLocalForecastTime(context));
|
||||||
int day = c.get(Calendar.DAY_OF_WEEK);
|
int day = c.get(Calendar.DAY_OF_WEEK);
|
||||||
|
|
||||||
holder.day.setText(StringFormatUtils.getDayShort(day));
|
holder.day.setText(StringFormatUtils.getDayShort(day));
|
||||||
|
@ -113,8 +114,8 @@ public class WeekWeatherAdapter extends RecyclerView.Adapter<WeekWeatherAdapter.
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
if (forecastData!=null)
|
if (!weekForecastList.isEmpty())
|
||||||
return forecastData.length;
|
return weekForecastList.size();
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,6 +105,8 @@
|
||||||
<string name="settings_summarize">Summe anzeigen</string>
|
<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="summary_summarize">Summiere die Werte aller Module mit gleichem Längen- und Breitengrad.</string>
|
||||||
<string name="dialog_add_clone_button">Klonen</string>
|
<string name="dialog_add_clone_button">Klonen</string>
|
||||||
|
<string name="itemRemoved">ENTFERNT:\u0020\u0020%s</string>
|
||||||
|
<string name="undo">WIEDERHERSTELLEN</string>
|
||||||
|
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
|
@ -108,4 +108,6 @@
|
||||||
<string name="settings_summarize">Show sum</string>
|
<string name="settings_summarize">Show sum</string>
|
||||||
<string name="summary_summarize">Summarize values of modules at the same latitude and longitude.</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="dialog_add_clone_button">Clone</string>
|
||||||
|
<string name="itemRemoved">REMOVED:\u0020\u0020%s</string>
|
||||||
|
<string name="undo">UNDO</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue