Click here to Skip to main content
12,250,675 members (48,381 online)
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads

Stats

36.6K views
767 downloads
22 bookmarked
Posted

CooksMate

, 21 Jan 2008 GPL3
A simple program to help get the timing of a roast dinner
cooksmate-bin-mono
CooksMate
mono
cooksmate.exe
cooksmate.exe.mdb
cooksmate.sh
log4net.dll
uk.org.aspellclark.common.dll
uk.org.aspellclark.common.gui.dll
cooksmate-bin-net
CooksMate
bin
Debug
cooksmate.exe
cooksmate.log
log4net.dll
uk.org.aspellclark.common.dll
uk.org.aspellclark.common.gui.dll
cooksmate-src
Common
common.build
Properties
src
main
csharp
NotImplementedDlg.resources
CommonFile
common.file.build
CommonGui
common.gui.build
CooksMate
cooksmate.build
cooksmate.sh
cooksmate_norm.png
DlgAction.resources
DlgEditRecipe.resources
MainForm.resources
libs
mono
log4net.dll
Mono.GetOptions.dll
sqlite-3.5.4.so
net
log4net.dll
Mono.GetOptions.dll
sqlite3.def
sqlite3.dll
/*
 * Created by SharpDevelop.
 * User: andy
 * Date: 26/12/2007
 * Time: 15:19
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Data;
using System.Collections;
using System.Xml;

namespace uk.org.aspellclark.cooksmate.engine
{
	/// <summary>
	/// Description of DataEngine.
	/// </summary>
	public class DataEngine
	{
        #region members

        // Create a logger for use in this class
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private string filename = "recipes.xml";

		/// <summary>
		/// this hashtable contains the Recipes.
		/// the key into the table is the recipe name
		/// which means that each recipe needs a unique name to identify it
		/// the value of the table is an array list of action steps for the recipe
		/// </summary>
		private Hashtable recipes = new Hashtable();
		
		#endregion
		
		public string Filename
		{
			set {this.filename = value;}
			get {return this.filename;}
		}
		
		public DataEngine()
		{
		}
		
		public void AddRecipe(string name, Recipe recipe)
		{
			if (recipes[name]!=null)
			{
				throw new RecipeAlreadyExistsException(name);
			}
			recipes.Add(name, recipe);
		}
		
		public ArrayList GetRecipes()
		{
			return new ArrayList(recipes.Values);
		}
		
		public Recipe GetRecipe(string name)
		{
			return recipes[name] as Recipe;
		}
		
		public void AddStepToRecipe(string name, ActionStep step)
		{
			ArrayList steps = recipes[name] as ArrayList;
			steps.Add(step);
			recipes.Add(name, steps);
		}
		
		 
	 	/// <summary>
	 	/// return the steps that need to be taken for a recipe
	 	/// </summary>
	 	/// <param name="recipeName"></param>
	 	/// <returns></returns>
		public ArrayList GetActionSteps(string recipeName)
		{
			if (recipeName.Length == 0 || recipeName==null)
			{
				return new ArrayList();
			}
			return (recipes[recipeName] as Recipe).Steps as ArrayList;
		}
		
		public DateTime GetServeTime(string recipeName)
		{
			if (recipeName.Length == 0 || recipeName==null)
			{
				return new DateTime();
			}
			return (recipes[recipeName] as Recipe).ServeTime;
		}
		
		public void SetServeTime(string recipeName, DateTime serveAt)
		{
			if (recipeName.Length == 0 || recipeName==null)
			{
				return;
			}
			(recipes[recipeName] as Recipe).ServeTime = serveAt;
		}
		
		public void ActionDone(string recipeName, ActionStep step)
		{
			Recipe recp = recipes[recipeName] as Recipe;
			recp.ActionDone(step);
		}
		public ActionStep GetAction(string recipeName,string actionName)
		{
			log.Debug("recipeName : " + recipeName);
			log.Debug("actionName : " + actionName);
			Recipe recp = recipes[recipeName] as Recipe;
			return recp.GetAction(actionName);
		}
		
		public ActionStep GetNextAction(string recipeName, DateTime currTime)
		{
			ActionStep nxtStep = null;
			//DateTime maxTime = new DateTime(1,1,1,0,0,0);
			
			//need to sort the action list
				
			Recipe recipe = (recipes[recipeName] as Recipe);
			foreach (ActionStep step in recipe.Steps)
			{
				if (!step.Done)
				{
					if (nxtStep == null)
					{
						nxtStep = step;	
					}
					else
					{
						if ((step.StartTime(recipe.ServeTime).CompareTo(nxtStep.StartTime(recipe.ServeTime)) < 0) &&
						    (step.StartTime(recipe.ServeTime).CompareTo(recipe.ServeTime) <= 0 ))
						{
							nxtStep = step;							
						}
					}
				}
			}
			
			return nxtStep;
		}


		public Recipe CreateNewRecipe(string aName)
		{
			Recipe recipe = new Recipe(aName);
			recipe.ServeTime = new DateTime(DateTime.Now.Year,DateTime.Now.Month, DateTime.Now.Day, 12, 00, 0, 0);
			recipes.Add(recipe.Title, recipe);
			return recipe;
		}


		public void DummyData()
		{
			Recipe recipe = new Recipe("Roast Chicken Dinner");
			recipe.ServeTime = new DateTime(DateTime.Now.Year,DateTime.Now.Month, DateTime.Now.Day, 12, 00, 0, 0);
			
			ActionStep newStep = new ActionStep();
			newStep.Name="Chicken in";
			newStep.MinsPerKilo=60;
			newStep.MinsStanding=30;
			newStep.WeightGrams=1500; //1.5 Kg
			newStep.Type= ActionStepType.Oven;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Chicken foil off";
			newStep.Type= ActionStepType.Oven;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Roast Potatoes";
			newStep.Type= ActionStepType.Oven;
			newStep.MinsDuration=135;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Roast Sweet Potatoes";
			newStep.Type= ActionStepType.Oven;
			newStep.MinsDuration=75;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Chicken Out";
			newStep.Type= ActionStepType.Oven;
			newStep.MinsDuration=30;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Swede";
			newStep.Type= ActionStepType.Hob;
			newStep.MinsDuration=30;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Stuffing";
			newStep.Type= ActionStepType.Oven;
			newStep.MinsDuration=25;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Brussles";
			newStep.Type= ActionStepType.Hob;
			newStep.MinsDuration=15;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Potatoes";
			newStep.Type= ActionStepType.Steamer;
			newStep.MinsDuration=40;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Cauliflower";
			newStep.Type= ActionStepType.Steamer;
			newStep.MinsDuration=35;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Carrots";
			newStep.Type= ActionStepType.Steamer;
			newStep.MinsDuration=30;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Broccoli";
			newStep.Type= ActionStepType.Steamer;
			newStep.MinsDuration=30;
			recipe.Steps.Add(newStep);

			newStep = new ActionStep();
			newStep.Name="Gravy";
			newStep.Type= ActionStepType.Hob;
			newStep.MinsDuration=5;
			recipe.Steps.Add(newStep);

			recipes.Add(recipe.Title, recipe);
		}


		#region XMLCode
		
		/// <summary>
		/// This method reads in the recipe's
		/// from an xml file
		/// </summary>
		/// <param name="filename">the name of the xml file to read</param>
		public void ReadFromXml()
		{
			log.Info("Reading file ["+filename+"]");
			XmlTextReader reader = null;
			try
			{
				// Load the file with an XmlTextReader
				log.Info("Reading file ["+ filename +"]");
				reader = new XmlTextReader (filename);
				log.Info("File  ["+ filename +"] read sucessfully");

				// Process the supplied XML file
				FormatXml(reader, filename);
			}
			catch (Exception e)
			{
                string sMsg = e.Message;
				log.Info ("Failed to read the file ["+filename+"]");
				log.Info ("Exception: ["+sMsg+"]");
			}
			finally
			{
				if (reader != null)
				{
					reader.Close();
				}
			}//finally
		}

		private void FormatXml (XmlReader reader, String filename)
		{
			while (reader.Read())
			{
				switch (reader.NodeType)
				{
					case XmlNodeType.XmlDeclaration:
						break;
					case XmlNodeType.ProcessingInstruction:
						break;
					case XmlNodeType.DocumentType:
						break;
					case XmlNodeType.Comment:
						break;
					case XmlNodeType.Element:
						if (reader.Name == Constants.XML_TAG_RECIPE)
						{
							Recipe recp = new Recipe(reader);
							recipes.Add(recp.Title, recp);
						}
						break;
					case XmlNodeType.Text:
						break;
					case XmlNodeType.Whitespace:
						break;
				}
			}
		}



		/// <summary>
		/// This method writes out the recipes to an xml file
		/// </summary>
		/// <param name="filename">the name of the xml file to write</param>
		public void writeToFile()
        {
//            if (File.Exists(CommonFunctions.GetApplicationPath() + System.IO.Path.DirectorySeparatorChar + m_sFileName))
//            {
//                if (File.Exists(CommonFunctions.GetApplicationPath() + System.IO.Path.DirectorySeparatorChar + m_sFileName + ".bak"))
//                {
//                    File.Delete(CommonFunctions.GetApplicationPath() + System.IO.Path.DirectorySeparatorChar + m_sFileName + ".bak");
//                }
//                File.Move(CommonFunctions.GetApplicationPath() + System.IO.Path.DirectorySeparatorChar + m_sFileName, CommonFunctions.GetApplicationPath() + System.IO.Path.DirectorySeparatorChar + m_sFileName + ".bak");
//            }
            XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
            xmlWriter.Formatting = Formatting.Indented;
            xmlWriter.Indentation = 4;
            xmlWriter.IndentChar = ' ';
            
            xmlWriter.WriteStartDocument();

            //write out the root element - Project
            xmlWriter.WriteStartElement(Constants.XML_TAG_RECIPES);

            //xmlWriter.WriteAttributeString("", "Recipes", "", m_sFileName);

            foreach (Recipe obj in recipes.Values)
            {
            	obj.WriteToXml(xmlWriter);
            }

            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            xmlWriter.Flush();
            xmlWriter.Close();
		}

		#endregion


	}//class
}//namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Andy Aspell-Clark
Software Developer (Senior) Airbus Defense and Space
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160426.1 | Last Updated 21 Jan 2008
Article Copyright 2007 by Andy Aspell-Clark
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid