Hello,
I’m working on an app that retreives data from a mysql database and display a chart. The problem is the cache data is growing really fast and i don’t know why! Here is my code
import de.bezier.data.sql.*;
import java.util.Calendar;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.content.Context;
import android.app.Activity;
PShape ansamblu;
PShape prop;
MySQL msql;
String msg = "SELECT temp1,temp2,pump_stove,pump_house from temps order by time desc limit 1";
String msg_for_7_day_avg = "select avg(temp2) from temps where date_format(time, '%j') =";
int day_of_week; // Sunday = 0 , Monday = 1 , Tuesday = 2, Wednesday = 3 , Thursday = 4 , Friday = 5 , Saturday = 6
String[] days_name = {"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
int noOfBubbles = 800;
Bubbles[] bubble = new Bubbles[noOfBubbles];
int temp_in_stove = 0;
int temp_out_stove = 0;
int pump_stove = 0;
int pump_house = 0;
float [] temps_days = new float [7];
int current_day_of_year;
int p_current_day_of_year;
boolean refresh_avg = true;
String temps_days_s; // this is temps_days converter to string so it can be stored in shared memory on device
String temps_days_from_dev; // this is temps_days from phone memory
void setup()
{
orientation(PORTRAIT); // only portrait screen
background(0);
size( displayWidth, displayHeight, P2D );
smooth(2);
Calendar c = Calendar.getInstance();
day_of_week = c.get(Calendar.DAY_OF_WEEK);
ansamblu = loadShape("drw1.svg");
prop = loadShape("prop.svg");
// this example assumes that you are running the
// mysql server locally (on "localhost").
//
// replace --username--, --password-- with your mysql-account.
//
String user = "mardan";
String pass = "12345";
String database = "mardan";
msql = new MySQL( this, "192.168.10.109", database, user, pass );
msql.connect();
shape(ansamblu, 45, 501);
get_current_day_of_year();
//get_average_for_7_days();
p_current_day_of_year = loadInt("p_current_day_of_year"); //get the last value saved on device regarding current_day_of_year
//current_day_of_year = current_day_of_year - 5;
temps_days_from_dev = loadString("p_avgString"); //get string from shared
temps_days = float(split(temps_days_from_dev, ',')); // convert string to array of floats
display_chart(temps_days);
if (msql.connect())
{
fill(255);
textSize(35);
text("Database connection....OK!", 15, displayHeight - 1730);
} else
{
fill(255);
textSize(35);
text("Database connection.... NO!", 15, displayHeight - 1730);
draw();
}
fill(255);
textSize(35);
text("Temp start pump 1 ...... 28°", 15, displayHeight - 1690);
text("Temp start pump 2 ...... 32°", 15, displayHeight - 1650);
/* ------------------------Cosmetics--------------------------- */
fill(#008000);
rect(0, 0, displayWidth, 10);
rect(0, 470, displayWidth, 10);
rect(0, 1020, displayWidth, 10);
rect(0, 1020, displayWidth, 10);
rect(0, displayHeight-10, displayWidth, 10);
/* --------------------------------------------------------------*/
println( "from DB = " +current_day_of_year);
println("from shared = " + p_current_day_of_year);
println(temps_days_from_dev);
println(day_of_week);
for (int i = 0; i < noOfBubbles; i++)
{
bubble[i] = new Bubbles();
}
}
void draw()
{
/*
println( "from DB = " +current_day_of_year);
println("from shared = " + p_current_day_of_year);
println(temps_days_from_dev);
*/
fill(0);
rect(80, displayHeight - 100, displayWidth - 80, -255); // rect for refresh display_chart
//background(0);
if (refresh_avg && p_current_day_of_year != current_day_of_year )
{
println("intrat in blucla");
get_average_for_7_days();
display_chart(temps_days);
temps_days_s = join(nfs(temps_days, 2, 1), ","); //converts float array to string
saveString(temps_days_s, "p_avgString"); //save to shared on device
refresh_avg = false;
}
shape(ansamblu, 45, 501);
acquire_data();
if (pump_stove == 1)
{
prop_animation (421, 756, 0); //prop retur centrala
} else
{
shape(prop, 421, 756);
}
if (pump_house == 1)
prop_animation(893, 680, 1); //prop retur centrala
else
shape(prop, 893, 680);
fill(255);
textSize(40);
text(temp_in_stove + "°", 550, 600); // Temp_in_Stove
fill(#FF00FF);
ellipse(530, 587, 15, 15);
fill(255);
text(temp_out_stove + "°", 550, 750); // Temp_out_Stove
fill(#FF00FF);
ellipse(530, 737, 15, 15);
fill(255);
textSize(40);
text("00°", 987, 650+30); // Temp_in_House
fill(#FF00FF);
ellipse(967, 637+30, 15, 15);
fill(255);
text("00°", 985, 800+30); // Temp_out_House
fill(#FF00FF);
ellipse(967, 785+30, 15, 15);
msql.query(msg_for_7_day_avg + current_day_of_year);
msql.next();
temps_days[6] = msql.getFloat("avg(temp2)");
display_chart(temps_days);
for (int i = 0; i < noOfBubbles; i++)
{
bubble[i].display();
bubble[i].step();
}
}
void prop_animation(int x, int y, int sens)
{
pushMatrix();
translate(x, y);
translate(prop.width/2, prop.height/2);
if (sens == 1)
rotate(TWO_PI * (millis()%6000/6000.0));
else
rotate(-TWO_PI * (millis()%6000/6000.0));
translate(-prop.width/2, -prop.height/2);
shape(prop, 0, 0);
popMatrix();
}
void mousePressed ()
{
println("PRESSED x:" + mouseX + " y: " + mouseY);
println( "display width =" + displayWidth + "display height= " + displayHeight );
}
void get_average_for_7_days()
{
int y = 0;
for ( int i = current_day_of_year - 6; i <= current_day_of_year; i++ )
{
msql.query(msg_for_7_day_avg + i);
msql.next();
temps_days[y] = msql.getFloat("avg(temp2)");
println(temps_days[y]);
y++;
}
}
void get_current_day_of_year()
{
/* --------------- get the current day of year ----------------------------*/
if ( msql.connect() )
{
msql.query("select date_format(curdate(), '%j') as cur_day_year ");
msql.next();
current_day_of_year = msql.getInt("cur_day_year");
//println(current_day_of_year);
}
}
void display_chart(float[] avgP)
{ // 1 2 3 4 5 6 7 from device
String[] days_name = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
// 0 1 2 3 4 5 6 from string array
int no_col = 7;
int space_b_col = 20;
int x_pos_rec = 80;
int work_area = displayWidth - ( no_col * space_b_col + 2 * x_pos_rec);
int width_col = work_area / no_col;
int counter_i_days = day_of_week - 1;
for (int i = 0; i < 7; i++)
{
stroke(255);
strokeWeight(1);
fill(map(int(avgP[i]), 0, 100, 150, 255), 0, 0);
rect(x_pos_rec, displayHeight - 100, width_col, -map(int(temps_days[i]), -10, 110, 50, 300));
fill(255);
textSize(35);
text(nf(temps_days[i], 2, 1), x_pos_rec + 12, (displayHeight - map(int(temps_days[i]), -10, 110, 50, 300)) - 130 );
if (counter_i_days == 6)
{
counter_i_days = 0;
text(days_name[counter_i_days], x_pos_rec + 30, displayHeight - 25);
} else
{
counter_i_days++;
text(days_name[counter_i_days], x_pos_rec + 30, displayHeight - 25);
//counter_i_days++;
}
x_pos_rec += width_col + space_b_col;
line( 30, displayHeight - 70, displayWidth - 30, displayHeight - 70);
line( 30, displayHeight - 70, 30, displayHeight - 350);
for (int k = 1; k <= 5; k++)
{
int m = k * 56;
line(20, displayHeight - 70 - m, 40, displayHeight - 70 - m);
}
noStroke();
}
}
void acquire_data()
{
msql.query(msg);
while (msql.next()) //msg = "SELECT temp1,temp2,pump_stove,pump_house from temps order by time desc limit 1";
{
temp_in_stove = msql.getInt("temp1");
temp_out_stove = msql.getInt("temp2");
pump_stove = msql.getInt("pump_stove");
pump_house = msql.getInt("pump_house");
}
}
void saveInt(int doy, String name) {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Activity act;
act = this.getActivity();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(act.getApplicationContext());
editor = sharedPreferences.edit();
editor.putInt(name, doy);
editor.commit();
}
int loadInt(String name) {
SharedPreferences sharedPreferences;
Activity act;
act = this.getActivity();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(act.getApplicationContext());
int getDay = sharedPreferences.getInt(name, 0);
return getDay;
}
void saveString(String avg, String name) {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Activity act;
act = this.getActivity();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(act.getApplicationContext());
editor = sharedPreferences.edit();
editor.putString(name, avg);
editor.commit();
}
String loadString(String name) {
SharedPreferences sharedPreferences;
Activity act;
act = this.getActivity();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(act.getApplicationContext());
String getStr = sharedPreferences.getString(name, null);
return getStr;
}
void onPause() {
super.onPause();
saveInt(current_day_of_year, "p_current_day_of_year");
}