using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; namespace SHARP_CLAS_UI { public class Vision_Info_Manager { #region Define string str_extension = ".xml"; #endregion #region Property private static readonly Lazy _instatnce = new Lazy(() => new Vision_Info_Manager()); public static Vision_Info_Manager Instance { get { return _instatnce.Value; } } #endregion #region Field private Dictionary Recipes; private Xml_Manager xml_manager = new Xml_Manager(); private string file_path; #endregion #region Construct public Vision_Info_Manager() { Recipes = new Dictionary(); Get_Vision_Info_Recipes(); } #endregion #region Function private void Get_File_Path() { RegistryKey rkk = Registry.CurrentUser.OpenSubKey(Equipment.Registry_Path, true); try { if (rkk == null) { rkk = Registry.CurrentUser.CreateSubKey(Equipment.Registry_Path); rkk.SetValue("VISION_INFO_RECIPE_PATH", @"C:\SHARP_CLAS\Config\VISION_INFO_RECIPE\"); file_path = @"C:\SHARP_CLAS\Config\VISION_INFO_RECIPE\"; } else { object value = rkk.GetValue("VISION_INFO_RECIPE_PATH"); if (value == null) { rkk.SetValue("VISION_INFO_RECIPE_PATH", @"C:\SHARP_CLAS\Config\VISION_INFO_RECIPE\"); file_path = @"C:\SHARP_CLAS\Config\VISION_INFO_RECIPE\"; } else { file_path = value.ToString(); } } } finally { rkk.Close(); } } private void Get_Vision_Info_Recipes() { Get_File_Path(); Recipes.Clear(); if (Directory.Exists(file_path)) { DirectoryInfo di = new DirectoryInfo(file_path); FileInfo[] recipes = di.GetFiles($"*{str_extension}"); foreach (FileInfo recipe in recipes) { Vision_Info_Recipe vision_info = new Vision_Info_Recipe(); if (xml_manager.Try_Read_File(file_path + recipe, out vision_info)) { if (Recipes.ContainsKey(vision_info.Name)) { Recipes[vision_info.Name] = vision_info; } else { Recipes.Add(vision_info.Name, vision_info); } } } } } public void Refresh_Vision_Info() { Get_Vision_Info_Recipes(); } public bool Exist_Recipe(string name) { foreach (Vision_Info_Recipe info in Recipes.Values) { if (name == info.Name) return true; } return false; } public bool Create_Recipe(string name) { if (!Exist_Recipe(name)) { Vision_Info_Recipe info = new Vision_Info_Recipe(); info.Name = name; if (xml_manager.Try_Save_File(file_path + name + str_extension, info)) { Recipes.Add(name, info); return true; } else return false; } else return false; } public bool Delete_Recipe(string name) { if (Exist_Recipe(name)) { Recipes.Remove(name); FileInfo fi = new FileInfo(file_path + name + str_extension); if (fi.Exists) { fi.Delete(); } } return true; } public bool Save_Recipe(string name, Vision_Info_Recipe info) { if (Exist_Recipe(name)) { DateTime dt = Recipes[name].Create_Time; Recipes[name] = info.Clone(); Recipes[name].Name = name; Recipes[name].Create_Time = dt; if (xml_manager.Try_Save_File(file_path + name + str_extension, Recipes[name])) { return true; } else return false; } else { return false; } } public bool Try_Get_Recipe(string name, out Vision_Info_Recipe info) { if (Exist_Recipe(name)) { info = Recipes[name].Clone(); return true; } else { info = null; return false; } } public Vision_Info_Recipe Get_Recipe(string name) { if (Exist_Recipe(name)) { return Recipes[name].Clone(); } else { return null; } } public Vision_Info_Recipe[] Get_Recipes() { List recipes = new List(); recipes.Clear(); foreach (Vision_Info_Recipe info in Recipes.Values) { recipes.Add(info.Clone()); } return recipes.ToArray(); } #endregion } }