~tristan957/tllt-cp

db549b5d28adac1ea0ddb28210515e15ade8b0a1 — Tristan Partin 1 year, 6 months ago 0df42a0
Ping the neural network for recipe details
M driver/tllt-toaster.c => driver/tllt-toaster.c +4 -5
@@ 208,8 208,7 @@ update_toaster(gpointer user_data)

	const int delta = args->total_time_seconds - g_timer_elapsed(priv->timer, NULL);
	if (delta > 0 && !g_cancellable_is_cancelled(priv->cancellable)) {
		args->update(delta / 60, delta % 60, 1 - ((double) delta) / args->total_time_seconds,
					 args->user_data);
		args->update(delta, 1 - ((double) delta) / args->total_time_seconds, args->user_data);

		return TRUE;
	}


@@ 345,8 344,8 @@ prepare_toaster(gpointer user_data)
}

void
tllt_toaster_start(TlltToaster *self, const unsigned int minutes, const unsigned int seconds,
				   const int temperature, const TlltToasterUpdateFunc update, gpointer user_data)
tllt_toaster_start(TlltToaster *self, const unsigned int time, const int temperature,
				   const TlltToasterUpdateFunc update, gpointer user_data)
{
	TlltToasterPrivate *priv = tllt_toaster_get_instance_private(self);



@@ 354,7 353,7 @@ tllt_toaster_start(TlltToaster *self, const unsigned int minutes, const unsigned

	TlltToasterOperationArgs *toaster_op_args = g_malloc(sizeof(TlltToasterOperationArgs));
	g_return_if_fail(toaster_op_args != NULL);
	toaster_op_args->total_time_seconds = minutes * 60 + seconds;
	toaster_op_args->total_time_seconds = time;
	toaster_op_args->update				= update;
	toaster_op_args->user_data			= user_data;
	toaster_op_args->toaster			= self;

M driver/tllt-toaster.h => driver/tllt-toaster.h +3 -5
@@ 19,13 19,11 @@ struct _TlltToaster
#define TLLT_TYPE_TOASTER (tllt_toaster_get_type())
G_DECLARE_FINAL_TYPE(TlltToaster, tllt_toaster, TLLT, TOASTER, GObject)

typedef void (*TlltToasterUpdateFunc)(unsigned int minutes, unsigned int seconds, double progress,
									  gpointer user_data);
typedef void (*TlltToasterUpdateFunc)(unsigned int time, double progress, gpointer user_data);

TlltToaster *tllt_toaster_new_from_config_file(const char *file_path, GError **err);
void tllt_toaster_start(TlltToaster *self, const unsigned int minutes, const unsigned int seconds,
						const int temperature, const TlltToasterUpdateFunc update,
						gpointer user_data);
void tllt_toaster_start(TlltToaster *self, const unsigned int time, const int temperature,
						const TlltToasterUpdateFunc update, gpointer user_data);
void tllt_toaster_stop(TlltToaster *self);
gboolean tllt_toaster_is_running(TlltToaster *self);


A lib/dto/tllt-cp-cooking-details-dto.c => lib/dto/tllt-cp-cooking-details-dto.c +75 -0
@@ 0,0 1,75 @@
#include <limits.h>

#include <glib-object.h>
#include <glib/gi18n.h>

#include "tllt-cp-cooking-details-dto.h"

G_DEFINE_TYPE(TlltCpCookingDetailsDto, tllt_cp_cooking_details_dto, G_TYPE_OBJECT)

typedef enum TlltCpCookingDetailsDtoProps
{
	PROP_TIME = 1,
	PROP_TEMPERATURE,
	N_PROPS,
} TlltCpCookingDetailsDtoProps;

static GParamSpec *obj_properties[N_PROPS];

static void
tllt_cp_cooking_details_dto_get_property(GObject *obj, guint prop_id, GValue *val,
										 GParamSpec *pspec)
{
	TlltCpCookingDetailsDto *self = TLLT_CP_COOKING_DETAILS_DTO(obj);

	switch (prop_id) {
	case PROP_TIME:
		g_value_set_uint(val, self->time);
		break;
	case PROP_TEMPERATURE:
		g_value_set_uint(val, self->time);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void
tllt_cp_cooking_details_dto_set_property(GObject *obj, guint prop_id, const GValue *val,
										 GParamSpec *pspec)
{
	TlltCpCookingDetailsDto *self = TLLT_CP_COOKING_DETAILS_DTO(obj);

	switch (prop_id) {
	case PROP_TIME:
		self->time = g_value_get_uint(val);
		break;
	case PROP_TEMPERATURE:
		self->temperature = g_value_get_uint(val);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void
tllt_cp_cooking_details_dto_class_init(TlltCpCookingDetailsDtoClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->get_property = tllt_cp_cooking_details_dto_get_property;
	obj_class->set_property = tllt_cp_cooking_details_dto_set_property;

	obj_properties[PROP_TIME] =
		g_param_spec_uint("time", _("Time"), _("Time to cook the recipe for"), 0, UINT_MAX, 0,
						  G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);
	obj_properties[PROP_TEMPERATURE] =
		g_param_spec_uint("temperature", _("Temperature"), _("Temperature to cook the recipe at"),
						  250, UINT_MAX, 250, G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);

	g_object_class_install_properties(obj_class, N_PROPS, obj_properties);
}

static void
tllt_cp_cooking_details_dto_init(G_GNUC_UNUSED TlltCpCookingDetailsDto *self)
{}

A lib/dto/tllt-cp-cooking-details-dto.h => lib/dto/tllt-cp-cooking-details-dto.h +23 -0
@@ 0,0 1,23 @@
#pragma once

#if !defined(__TLLT_CP_HEADER_INTERNAL__) && !defined(TLLT_CP_COMPILATION)
#	error "Only <tllt-cp.h> can be included directly."
#endif

#include <glib-object.h>

G_BEGIN_DECLS

struct _TlltCpCookingDetailsDto
{
	GObject parent_instance;

	unsigned int time;
	unsigned int temperature;
};

#define TLLT_CP_TYPE_COOKING_DETAILS_DTO (tllt_cp_cooking_details_dto_get_type())
G_DECLARE_FINAL_TYPE(TlltCpCookingDetailsDto, tllt_cp_cooking_details_dto, TLLT_CP,
					 COOKING_DETAILS_DTO, GObject)

G_END_DECLS

M lib/meson.build => lib/meson.build +2 -0
@@ 25,6 25,7 @@ configure_file(

sources = [
	'dto/tllt-cp-authentication-dto.c',
	'dto/tllt-cp-cooking-details-dto.c',
	'dto/tllt-cp-create-recipe-dto.c',
	'dto/tllt-cp-create-user-dto.c',
	'tllt-cp-client.c',


@@ 35,6 36,7 @@ sources = [

public_headers = [
	'dto/tllt-cp-authentication-dto.h',
	'dto/tllt-cp-cooking-details-dto.h',
	'dto/tllt-cp-create-recipe-dto.h',
	'dto/tllt-cp-create-user-dto.h',
	'tllt-cp.h',

M lib/tllt-cp-recipe.c => lib/tllt-cp-recipe.c +9 -1
@@ 1,10 1,10 @@
#include <glib-object.h>
#include <glib/gi18n.h>

#include "dto/tllt-cp-cooking-details-dto.h"
#include "dto/tllt-cp-create-recipe-dto.h"
#include "tllt-cp-client.h"
#include "tllt-cp-recipe.h"
#include "tllt-cp-user.h"

struct _TlltCpRecipe
{


@@ 138,3 138,11 @@ tllt_cp_recipe_get_rtype(TlltCpRecipe *self)

	return priv->type;
}

unsigned int
tllt_cp_recipe_get_id(TlltCpRecipe *self)
{
	TlltCpRecipePrivate *priv = tllt_cp_recipe_get_instance_private(self);

	return priv->id;
}

M lib/tllt-cp-recipe.h => lib/tllt-cp-recipe.h +2 -0
@@ 6,6 6,7 @@

#include <glib-object.h>

#include "dto/tllt-cp-cooking-details-dto.h"
#include "tllt-cp-client.h"

G_BEGIN_DECLS


@@ 26,5 27,6 @@ TlltCpRecipe *tllt_cp_recipe_create(TlltCpClient *client, const char *name,
const char *tllt_cp_recipe_get_name(TlltCpRecipe *self);
void tllt_cp_recipe_set_name(TlltCpRecipe *self, const char *name);
TlltCpRecipeType tllt_cp_recipe_get_rtype(TlltCpRecipe *self);
unsigned int tllt_cp_recipe_get_id(TlltCpRecipe *self);

G_END_DECLS

M lib/tllt-cp-user.c => lib/tllt-cp-user.c +19 -0
@@ 287,3 287,22 @@ tllt_cp_user_get_recipes(TlltCpUser *self)

	return priv->recipes;
}

TlltCpCookingDetailsDto *
tllt_cp_user_get_cooking_details_for_recipe(TlltCpUser *self, TlltCpClient *client,
											TlltCpRecipe *recipe, GError **err)
{
	g_return_val_if_fail(err == NULL || *err == NULL, NULL);

	TlltCpUserPrivate *priv = tllt_cp_user_get_instance_private(self);

	g_autoptr(GString) endpoint = g_string_new(client->server);
	g_string_append_printf(endpoint, "/users/%u/recipes/%u/cook", priv->id,
						   tllt_cp_recipe_get_id(recipe));
	GObject *obj =
		tllt_cp_client_get_request(client, TLLT_CP_TYPE_COOKING_DETAILS_DTO, endpoint->str, err);

	g_return_val_if_fail(err == NULL || *err == NULL, NULL);

	return TLLT_CP_COOKING_DETAILS_DTO(obj);
}

M lib/tllt-cp-user.h => lib/tllt-cp-user.h +4 -0
@@ 32,5 32,9 @@ GList *tllt_cp_user_get_recipes(TlltCpUser *self);
TlltCpRecipe *tllt_cp_user_add_recipe(TlltCpUser *self, TlltCpClient *client, const char *name,
									  const TlltCpRecipeType type, GError **err);
void tllt_cp_user_remove_recipe(TlltCpUser *self, TlltCpRecipe *recipe, GError **err);
TlltCpCookingDetailsDto *tllt_cp_user_get_cooking_details_for_recipe(TlltCpUser *self,
																	 TlltCpClient *client,
																	 TlltCpRecipe *recipe,
																	 GError **err);

G_END_DECLS

M lib/tllt-cp.h => lib/tllt-cp.h +4 -0
@@ 2,6 2,10 @@

#define __TLLT_CP_HEADER_INTERNAL__

#include "dto/tllt-cp-authentication-dto.h"
#include "dto/tllt-cp-cooking-details-dto.h"
#include "dto/tllt-cp-create-recipe-dto.h"
#include "dto/tllt-cp-create-user-dto.h"
#include "tllt-cp-client.h"
#include "tllt-cp-error.h"
#include "tllt-cp-recipe.h"

M src/tllt-cp-window.c => src/tllt-cp-window.c +35 -12
@@ 41,6 41,8 @@ typedef struct TlltCpWindowPrivate
	GSList *logged_in_users;
	TlltCpClient *client;
	TlltCpUser *selected_user;
	TlltCpUser *toaster_user;
	TlltCpRecipe *currently_running_recipe;
} TlltCpWindowPrivate;

G_DEFINE_TYPE_WITH_PRIVATE(TlltCpWindow, tllt_cp_window, GTK_TYPE_APPLICATION_WINDOW)


@@ 170,30 172,43 @@ show_leading_zeros(GtkSpinButton *widget, G_GNUC_UNUSED gpointer user_data)
}

static void
update_timer(const unsigned int minutes, const unsigned int seconds, const double progress,
			 gpointer user_data)
update_timer(const unsigned int time, const double progress, gpointer user_data)
{
	TlltCpWindow *self		  = TLLT_CP_WINDOW(user_data);
	TlltCpWindowPrivate *priv = tllt_cp_window_get_instance_private(self);

	char buffer[3];
	sprintf(buffer, "%02d", minutes);
	const unsigned int minutes = time / 60;
	const unsigned int seconds = time % 60;

	char buffer[9];
	sprintf(buffer, "%02u", minutes);
	gtk_label_set_label(priv->timer_minutes_label, buffer);
	sprintf(buffer, "%02d", seconds);
	sprintf(buffer, "%02u", seconds);
	gtk_label_set_label(priv->timer_seconds_label, buffer);
	gtk_progress_bar_set_fraction(priv->toasting_progress_bar, progress);
}

static void
on_recipe_started(G_GNUC_UNUSED TlltCpRecipeListItem *list_item, G_GNUC_UNUSED TlltCpRecipe *recipe,
on_recipe_started(G_GNUC_UNUSED TlltCpRecipeListItem *list_item, TlltCpRecipe *recipe,
				  gpointer user_data)
{
	TlltCpWindow *self		  = TLLT_CP_WINDOW(user_data);
	TlltCpWindowPrivate *priv = tllt_cp_window_get_instance_private(self);

	// TODO: Get info from recipe
	g_object_ref(self);
	tllt_toaster_start(priv->toaster, 2, 0, 425, update_timer, self);
	priv->toaster_user			   = priv->selected_user;
	priv->currently_running_recipe = recipe;

	g_autoptr(GError) err				   = NULL;
	g_autoptr(TlltCpCookingDetailsDto) dto = tllt_cp_user_get_cooking_details_for_recipe(
		priv->toaster_user, priv->client, priv->currently_running_recipe, &err);

	if (dto != NULL) {
		g_object_ref(self);
		tllt_toaster_start(priv->toaster, dto->time, dto->temperature, update_timer, self);
	} else {
		priv->toaster_user			   = NULL;
		priv->currently_running_recipe = NULL;
	}
}

static void


@@ 315,10 330,11 @@ on_timer_start_button_clicked(G_GNUC_UNUSED GtkButton *widget, gpointer user_dat
		gtk_switch_set_active(priv->top_heating_element_switch, TRUE);
	}

	const int minutes = gtk_spin_button_get_value_as_int(priv->timer_minutes_spin_button);
	const int seconds = gtk_spin_button_get_value_as_int(priv->timer_seconds_spin_button);

	g_object_ref(self);
	tllt_toaster_start(priv->toaster,
					   gtk_spin_button_get_value_as_int(priv->timer_minutes_spin_button),
					   gtk_spin_button_get_value_as_int(priv->timer_seconds_spin_button),
	tllt_toaster_start(priv->toaster, minutes * 60 + seconds,
					   gtk_range_get_value(GTK_RANGE(priv->temperature_scale)), update_timer, self);
}



@@ 346,6 362,13 @@ on_toaster_stopped(G_GNUC_UNUSED TlltToaster *toaster, gpointer user_data)
	TlltCpWindow *self		  = TLLT_CP_WINDOW(user_data);
	TlltCpWindowPrivate *priv = tllt_cp_window_get_instance_private(self);

	priv->toaster_user			   = NULL;
	priv->currently_running_recipe = NULL;

	if (priv->toaster_user != NULL && priv->currently_running_recipe != NULL) {
		g_print("Present user opinion window\n");
	}

	g_object_unref(self);	// Refers to ref when toaster is started

	gtk_widget_set_visible(GTK_WIDGET(priv->toasting_status_box), FALSE);