From af38410db79e03eb1696e583760e4464af8b6b6e Mon Sep 17 00:00:00 2001 From: cos Date: Sun, 26 Nov 2017 21:31:48 +0100 Subject: Initial addition of energyadder script --- energyadder.pl | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100755 energyadder.pl diff --git a/energyadder.pl b/energyadder.pl new file mode 100755 index 0000000..730a953 --- /dev/null +++ b/energyadder.pl @@ -0,0 +1,119 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Config::Simple; +use DBI; +use DateTime; +use YAML; + +use common; +use utf8; + +binmode STDOUT, 'utf8'; + +use constant GOAL_ENERGY => 900; + +tie my %Config, "Config::Simple", '/etc/mat.conf'; + +my $db = DBI->connect($Config{'database'}, "", "", {RaiseError => 1, + AutoCommit => 1, sqlite_unicode => 1}); + +my $recipe_id = $ARGV[0]; +$recipe_id =~ s/(.*\/)*([0-9]*)-.*/$2/; + + +#if ( length(@ingredients) == 1 ) { +#if ( 0 ) { +if ( $recipe_id eq $ARGV[0] ) { + print "original_scale_reading: 0\n"; + print "already_eaten: 0\n"; + print "scale_reading: 0\n"; + print "container_weight: 0\n"; + print "\n"; + print "ingredients:\n"; + + my $sql = "SELECT * FROM contents WHERE recipe_id=$recipe_id"; + my $sth = $db->prepare($sql); + $sth->execute(); +# my $first_line = 1; + my $ingredient_row; + while ( my $content_row = $sth->fetchrow_hashref() ) { +# if ( $first_line ) { +# print 'my @ingredients = ( '; +# $first_line = 0; +# } else { +# printf ", # %s\n%20s", $last_ingredient->{name}, " "; +# } + $sql = "SELECT * FROM ingredients WHERE id=$content_row->{ingredient_id}"; + ( $ingredient_row ) = $db->selectrow_hashref($sql); +# printf '{ ingredient => %4d, quantity => 0 }', $content_row->{ingredient_id}; + print " - description: $ingredient_row->{'name'}\n"; + print " ingredient: $content_row->{ingredient_id}\n"; + print " # $content_row->{quantity} $content_row->{unit}\n"; + print " quantity : 0\n\n"; + } +# printf " ); # %s\n", $last_ingredient->{name}; + exit 1; +} + +open my $cooking_file, '<', $ARGV[0]; +my $cooking = YAML::LoadFile($cooking_file); +my @ingredients = @{ $cooking->{'ingredients'} }; + +print "Goal energy: ", GOAL_ENERGY, " kJ\n\n"; + +my $energy = 0; + +for my $ingredient ( @ingredients ) { + + my $sql = "SELECT * FROM LivsmedelsDatabas WHERE ". + "ingredient_id=$ingredient->{ingredient}"; + my ( $row ) = $db->selectrow_hashref($sql); + if ($row->{'Livsmedelsnamn'}) { +# print $row->{'Livsmedelsnamn'}; + $energy += $ingredient->{quantity} * $row->{'Energi'} / 100; + } else { + $sql = "SELECT * FROM ingredients WHERE id=$ingredient->{ingredient}"; + ( $row ) = $db->selectrow_hashref($sql); + print "Could not find ingredient $ingredient->{ingredient} ". + "($row->{'name'}) in LivsmedelsDatabas.\n"; + } + printf "%5d%5d g|%-40s%10d kJ\n", $ingredient->{ingredient}, + $ingredient->{quantity}, $row->{'Livsmedelsnamn'}, + $ingredient->{quantity} * $row->{'Energi'} / 100; +# printf "%5d|%5d g|%-50s%10s kJ|%10s kJ\n", $ingredient->{ingredient}, +# $ingredient->{quantity}, $row->{'Livsmedelsnamn'}, +# $ingredient->{quantity} * $row->{'Energi'} / 100, $energy; +} + +#my $total_food_weight = SCALE_READING - CONTAINER_WEIGHT; +my $total_food_weight = $cooking->{'scale_reading'} - $cooking->{'container_weight'} + + $cooking->{'scale_reading'} * (1.0 * $cooking->{'already_eaten'} / $cooking->{'original_scale_reading'}); +my $specific_energy = $energy / $total_food_weight * 100; # kJ/100 g +#my $left = ( $total_food_weight - 198 ) * $specific_energy; + +#my $reduced = $left / 2.980; + +print "\n"; +printf "Total energy (all %d g): %d kJ\n", $total_food_weight, $energy; +printf "Specific energi: %d kJ/100 g\n", $specific_energy; + +print "\n"; + +my $portions = int($energy/GOAL_ENERGY); +print $portions, " matlådor på ", int($total_food_weight/$portions), " g ger ", + int($total_food_weight/$portions/100 * $specific_energy) ," kJ/matlåda.\n"; +$portions++; +print $portions, " matlådor på ", int($total_food_weight/$portions), " g ger ", + int($total_food_weight/$portions/100 * $specific_energy) ," kJ/matlåda.\n"; + +print "\n"; + +my $database_path = (split(":", $Config{'database'}))[2]; +add_to_cooking($recipe_id, $specific_energy, $database_path); +print "echo \"INSERT INTO cookings VALUES ($recipe_id, '". + DateTime->now()->ymd()."', ".int($specific_energy+0.5)."); \" | sqlite3 ". + $database_path."\n"; + -- cgit v1.2.3