mirror of
https://gitlab.freedesktop.org/upower/upower.git
synced 2026-05-09 04:48:05 +02:00
Add a GetStatistics() method on org.freedesktop.DeviceKit.Power.Source so we can print some funky graphs
This commit is contained in:
parent
dfd1103067
commit
0e617b862c
3 changed files with 132 additions and 7 deletions
|
|
@ -41,6 +41,7 @@
|
|||
#include "dkp-object.h"
|
||||
#include "dkp-source.h"
|
||||
#include "dkp-history.h"
|
||||
#include "dkp-history-obj.h"
|
||||
#include "dkp-marshal.h"
|
||||
#include "dkp-source-glue.h"
|
||||
|
||||
|
|
@ -108,6 +109,44 @@ static const char *dkp_source_get_object_path (DkpDevice *device);
|
|||
static void dkp_source_removed (DkpDevice *device);
|
||||
static gboolean dkp_source_changed (DkpDevice *device, DevkitDevice *d, gboolean synthesized);
|
||||
|
||||
/**
|
||||
* dkp_source_error_quark:
|
||||
**/
|
||||
GQuark
|
||||
dkp_source_error_quark (void)
|
||||
{
|
||||
static GQuark ret = 0;
|
||||
|
||||
if (ret == 0) {
|
||||
ret = g_quark_from_static_string ("dkp_source_error");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
|
||||
|
||||
/**
|
||||
* dkp_source_error_get_type:
|
||||
**/
|
||||
GType
|
||||
dkp_source_error_get_type (void)
|
||||
{
|
||||
static GType etype = 0;
|
||||
|
||||
if (etype == 0)
|
||||
{
|
||||
static const GEnumValue values[] =
|
||||
{
|
||||
ENUM_ENTRY (DKP_SOURCE_ERROR_GENERAL, "GeneralError"),
|
||||
{ 0, 0, 0 }
|
||||
};
|
||||
g_assert (DKP_SOURCE_NUM_ERRORS == G_N_ELEMENTS (values) - 1);
|
||||
etype = g_enum_register_static ("DkpSourceError", values);
|
||||
}
|
||||
return etype;
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_source_get_property:
|
||||
**/
|
||||
|
|
@ -299,6 +338,8 @@ dkp_source_class_init (DkpSourceClass *klass)
|
|||
object_class,
|
||||
PROP_BATTERY_TECHNOLOGY,
|
||||
g_param_spec_string ("battery-technology", NULL, NULL, NULL, G_PARAM_READABLE));
|
||||
|
||||
dbus_g_error_domain_register (DKP_SOURCE_ERROR, NULL, DKP_SOURCE_TYPE_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -376,10 +417,10 @@ dkp_source_compute_object_path (const char *native_path)
|
|||
}
|
||||
|
||||
/**
|
||||
* dkp_source_register_power_source:
|
||||
* dkp_source_register_source:
|
||||
**/
|
||||
static gboolean
|
||||
dkp_source_register_power_source (DkpSource *source)
|
||||
dkp_source_register_source (DkpSource *source)
|
||||
{
|
||||
DBusConnection *connection;
|
||||
GError *error = NULL;
|
||||
|
|
@ -441,7 +482,7 @@ dkp_source_new (DkpDaemon *daemon, DevkitDevice *d)
|
|||
goto out;
|
||||
}
|
||||
|
||||
if (!dkp_source_register_power_source (DKP_SOURCE (source))) {
|
||||
if (!dkp_source_register_source (DKP_SOURCE (source))) {
|
||||
g_object_unref (source);
|
||||
source = NULL;
|
||||
goto out;
|
||||
|
|
@ -836,13 +877,58 @@ dkp_source_update (DkpSource *source)
|
|||
return ret;
|
||||
}
|
||||
|
||||
#define DKP_DBUS_STRUCT_UINT_DOUBLE_STRING (dbus_g_type_get_struct ("GValueArray", \
|
||||
G_TYPE_UINT, G_TYPE_DOUBLE, G_TYPE_STRING, G_TYPE_INVALID))
|
||||
|
||||
/**
|
||||
* dkp_source_get_statistics:
|
||||
**/
|
||||
gboolean
|
||||
dkp_source_get_statistics (DkpSource *source, const gchar *type, guint timespan, DBusGMethodInvocation *context)
|
||||
{
|
||||
GError *error;
|
||||
GPtrArray *array;
|
||||
GPtrArray *complex;
|
||||
const DkpHistoryObj *obj;
|
||||
GValue *value;
|
||||
guint i;
|
||||
|
||||
/* get the correct data */
|
||||
if (strcmp (type, "rate") == 0)
|
||||
array = dkp_history_get_rate_data (source->priv->history, timespan);
|
||||
else if (strcmp (type, "charge") == 0)
|
||||
array = dkp_history_get_charge_data (source->priv->history, timespan);
|
||||
else {
|
||||
error = g_error_new (DKP_DAEMON_ERROR, DKP_DAEMON_ERROR_GENERAL, "type '%s' not recognised", type);
|
||||
dbus_g_method_return_error (context, error);
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* copy data to dbus struct */
|
||||
complex = g_ptr_array_sized_new (array->len);
|
||||
for (i=0; i<array->len; i++) {
|
||||
obj = (const DkpHistoryObj *) g_ptr_array_index (array, i);
|
||||
value = g_new0 (GValue, 1);
|
||||
g_value_init (value, DKP_DBUS_STRUCT_UINT_DOUBLE_STRING);
|
||||
g_value_take_boxed (value, dbus_g_type_specialized_construct (DKP_DBUS_STRUCT_UINT_DOUBLE_STRING));
|
||||
dbus_g_type_struct_set (value, 0, obj->time, 1, obj->value, 2, dkp_source_state_to_text (obj->state), -1);
|
||||
g_ptr_array_add (complex, g_value_get_boxed (value));
|
||||
g_free (value);
|
||||
}
|
||||
|
||||
g_ptr_array_free (array, TRUE);
|
||||
dbus_g_method_return (context, complex);
|
||||
out:
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_source_refresh:
|
||||
**/
|
||||
gboolean
|
||||
dkp_source_refresh (DkpSource *power_source, DBusGMethodInvocation *context)
|
||||
dkp_source_refresh (DkpSource *source, DBusGMethodInvocation *context)
|
||||
{
|
||||
dkp_source_update (power_source);
|
||||
dkp_source_update (source);
|
||||
dbus_g_method_return (context);
|
||||
return TRUE;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,14 +50,30 @@ typedef struct
|
|||
DkpDeviceClass parent_class;
|
||||
} DkpSourceClass;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
DKP_SOURCE_ERROR_GENERAL,
|
||||
DKP_SOURCE_NUM_ERRORS
|
||||
} DkpSourceError;
|
||||
|
||||
#define DKP_SOURCE_ERROR dkp_source_error_quark ()
|
||||
|
||||
GType dkp_source_error_get_type (void);
|
||||
#define DKP_SOURCE_TYPE_ERROR (dkp_source_error_get_type ())
|
||||
|
||||
GQuark dkp_source_error_quark (void);
|
||||
GType dkp_source_get_type (void);
|
||||
DkpSource *dkp_source_new (DkpDaemon *daemon,
|
||||
DevkitDevice *d);
|
||||
|
||||
/* exported methods */
|
||||
gboolean dkp_source_refresh (DkpSource *power_source,
|
||||
gboolean dkp_source_refresh (DkpSource *source,
|
||||
DBusGMethodInvocation *context);
|
||||
gchar *dkp_source_get_id (DkpSource *source);
|
||||
gboolean dkp_source_get_statistics (DkpSource *source,
|
||||
const gchar *type,
|
||||
guint timespan,
|
||||
DBusGMethodInvocation *context);
|
||||
gchar *dkp_source_get_id (DkpSource *power_source);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
|
|
|
|||
|
|
@ -48,6 +48,29 @@
|
|||
</doc:doc>
|
||||
</signal>
|
||||
|
||||
<!-- ************************************************************ -->
|
||||
<method name="GetStatistics">
|
||||
<annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
|
||||
<arg name="type" direction="in" type="s">
|
||||
<doc:doc><doc:summary>The type of statistics.
|
||||
Valid types are <literal>rate</literal> or <literal>charge</literal>.</doc:summary></doc:doc>
|
||||
</arg>
|
||||
<arg name="timespan" direction="in" type="u">
|
||||
<doc:doc><doc:summary>The amount of data to return in seconds, or 0 for all.</doc:summary></doc:doc>
|
||||
</arg>
|
||||
<arg name="data" direction="out" type="a(uds)">
|
||||
<doc:doc><doc:summary>The data for the power device.</doc:summary></doc:doc>
|
||||
</arg>
|
||||
<doc:doc>
|
||||
<doc:description>
|
||||
<doc:para>
|
||||
Gets statistics for the power device that may be interesting
|
||||
to show on a graph in the session.
|
||||
</doc:para>
|
||||
</doc:description>
|
||||
</doc:doc>
|
||||
</method>
|
||||
|
||||
<!-- ************************************************************ -->
|
||||
<property name="native-path" type="s" access="read">
|
||||
<doc:doc><doc:description><doc:para>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue