save the time to full and time to empty too

This commit is contained in:
Richard Hughes 2008-08-28 10:43:08 +01:00
parent 57ae3ac178
commit 0e2c7ef0ec
3 changed files with 134 additions and 0 deletions

View file

@ -42,10 +42,14 @@ struct DkpHistoryPrivate
{
gchar *id;
gdouble rate_last;
gint64 time_full_last;
gint64 time_empty_last;
gdouble percentage_last;
DkpDeviceState state;
GPtrArray *data_rate;
GPtrArray *data_charge;
GPtrArray *data_time_full;
GPtrArray *data_time_empty;
guint save_id;
};
@ -121,6 +125,38 @@ dkp_history_get_rate_data (DkpHistory *history, guint timespan)
return array;
}
/**
* dkp_history_get_time_full_data:
**/
GPtrArray *
dkp_history_get_time_full_data (DkpHistory *history, guint timespan)
{
GPtrArray *array;
g_return_val_if_fail (DKP_IS_HISTORY (history), NULL);
if (history->priv->id == NULL)
return NULL;
array = dkp_history_copy_array_timespan (history->priv->data_time_full, timespan);
return array;
}
/**
* dkp_history_get_time_empty_data:
**/
GPtrArray *
dkp_history_get_time_empty_data (DkpHistory *history, guint timespan)
{
GPtrArray *array;
g_return_val_if_fail (DKP_IS_HISTORY (history), NULL);
if (history->priv->id == NULL)
return NULL;
array = dkp_history_copy_array_timespan (history->priv->data_time_empty, timespan);
return array;
}
/**
* dkp_history_load_data:
**/
@ -212,6 +248,16 @@ dkp_history_save_data (DkpHistory *history)
dkp_history_save_data_array (filename, history->priv->data_charge);
g_free (filename);
/* save charge history to disk */
filename = dkp_history_get_filename (history, "time-full");
dkp_history_save_data_array (filename, history->priv->data_time_full);
g_free (filename);
/* save charge history to disk */
filename = dkp_history_get_filename (history, "time-empty");
dkp_history_save_data_array (filename, history->priv->data_time_empty);
g_free (filename);
return TRUE;
}
@ -362,6 +408,16 @@ dkp_history_load_data (DkpHistory *history)
dkp_history_load_data_array (filename, history->priv->data_charge);
g_free (filename);
/* load charge history from disk */
filename = dkp_history_get_filename (history, "time-full");
dkp_history_load_data_array (filename, history->priv->data_time_full);
g_free (filename);
/* load charge history from disk */
filename = dkp_history_get_filename (history, "time-empty");
dkp_history_load_data_array (filename, history->priv->data_time_empty);
g_free (filename);
return TRUE;
}
@ -457,6 +513,66 @@ dkp_history_set_rate_data (DkpHistory *history, gdouble rate)
return TRUE;
}
/**
* dkp_history_set_time_full_data:
**/
gboolean
dkp_history_set_time_full_data (DkpHistory *history, gint64 time)
{
DkpHistoryObj *obj;
g_return_val_if_fail (DKP_IS_HISTORY (history), FALSE);
if (history->priv->id == NULL)
return FALSE;
if (history->priv->state == DKP_DEVICE_STATE_UNKNOWN)
return FALSE;
if (time < 0)
return FALSE;
if (history->priv->time_full_last == time)
return FALSE;
/* add to array and schedule save file */
obj = dkp_history_obj_create ((gdouble) time, history->priv->state);
g_ptr_array_add (history->priv->data_time_full, obj);
dkp_history_schedule_save (history);
/* save last value */
history->priv->time_full_last = time;
return TRUE;
}
/**
* dkp_history_set_time_empty_data:
**/
gboolean
dkp_history_set_time_empty_data (DkpHistory *history, gint64 time)
{
DkpHistoryObj *obj;
g_return_val_if_fail (DKP_IS_HISTORY (history), FALSE);
if (history->priv->id == NULL)
return FALSE;
if (history->priv->state == DKP_DEVICE_STATE_UNKNOWN)
return FALSE;
if (time < 0)
return FALSE;
if (history->priv->time_empty_last == time)
return FALSE;
/* add to array and schedule save file */
obj = dkp_history_obj_create ((gdouble) time, history->priv->state);
g_ptr_array_add (history->priv->data_time_empty, obj);
dkp_history_schedule_save (history);
/* save last value */
history->priv->time_empty_last = time;
return TRUE;
}
/**
* dkp_history_class_init:
* @klass: The DkpHistoryClass
@ -483,6 +599,8 @@ dkp_history_init (DkpHistory *history)
history->priv->state = DKP_DEVICE_STATE_UNKNOWN;
history->priv->data_rate = g_ptr_array_new ();
history->priv->data_charge = g_ptr_array_new ();
history->priv->data_time_full = g_ptr_array_new ();
history->priv->data_time_empty = g_ptr_array_new ();
history->priv->save_id = 0;
}
@ -504,6 +622,8 @@ dkp_history_finalize (GObject *object)
g_ptr_array_free (history->priv->data_rate, TRUE);
g_ptr_array_free (history->priv->data_charge, TRUE);
g_ptr_array_free (history->priv->data_time_full, TRUE);
g_ptr_array_free (history->priv->data_time_empty, TRUE);
g_free (history->priv->id);
g_return_if_fail (history->priv != NULL);

View file

@ -55,6 +55,10 @@ GPtrArray *dkp_history_get_charge_data (DkpHistory *history,
guint timespan);
GPtrArray *dkp_history_get_rate_data (DkpHistory *history,
guint timespan);
GPtrArray *dkp_history_get_time_full_data (DkpHistory *history,
guint timespan);
GPtrArray *dkp_history_get_time_empty_data (DkpHistory *history,
guint timespan);
gboolean dkp_history_set_id (DkpHistory *history,
const gchar *id);
@ -64,6 +68,10 @@ gboolean dkp_history_set_charge_data (DkpHistory *history,
gdouble percentage);
gboolean dkp_history_set_rate_data (DkpHistory *history,
gdouble rate);
gboolean dkp_history_set_time_full_data (DkpHistory *history,
gint64 time);
gboolean dkp_history_set_time_empty_data (DkpHistory *history,
gint64 time);
G_END_DECLS

View file

@ -331,6 +331,8 @@ out:
dkp_history_set_state (supply->priv->history, obj->state);
dkp_history_set_charge_data (supply->priv->history, obj->percentage);
dkp_history_set_rate_data (supply->priv->history, obj->energy_rate);
dkp_history_set_time_full_data (supply->priv->history, obj->time_to_full);
dkp_history_set_time_empty_data (supply->priv->history, obj->time_to_empty);
g_free (status);
return ret;
@ -371,6 +373,10 @@ dkp_supply_get_history (DkpDevice *device, const gchar *type, guint timespan)
array = dkp_history_get_rate_data (supply->priv->history, timespan);
else if (strcmp (type, "charge") == 0)
array = dkp_history_get_charge_data (supply->priv->history, timespan);
else if (strcmp (type, "time-full") == 0)
array = dkp_history_get_time_full_data (supply->priv->history, timespan);
else if (strcmp (type, "time-empty") == 0)
array = dkp_history_get_time_empty_data (supply->priv->history, timespan);
return array;
}