From 04e651cdee1c1deda1e745b39f8b5361c85d1762 Mon Sep 17 00:00:00 2001
From: SimonPan <panruising@163.com>
Date: Wed, 4 Sep 2019 10:09:58 +0800
Subject: [PATCH] =?UTF-8?q?=E8=BF=90=E8=A1=8C=E5=8F=82=E6=95=B0=20?=
 =?UTF-8?q?=E4=BD=BF=E7=94=A8=20json=E4=BF=9D=E5=AD=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Common/BlowingFixProfileParam.cs          |  13 ++-
 .../Server/BlowingFixProfile.cs               | 107 ++++++++++++------
 2 files changed, 87 insertions(+), 33 deletions(-)

diff --git a/Project.FLY.Thick.Blowing/FLY.Thick.Blowing/Common/BlowingFixProfileParam.cs b/Project.FLY.Thick.Blowing/FLY.Thick.Blowing/Common/BlowingFixProfileParam.cs
index 5f49fed0..0e1cd83c 100644
--- a/Project.FLY.Thick.Blowing/FLY.Thick.Blowing/Common/BlowingFixProfileParam.cs
+++ b/Project.FLY.Thick.Blowing/FLY.Thick.Blowing/Common/BlowingFixProfileParam.cs
@@ -8,7 +8,7 @@ using System.Text;
 
 namespace FLY.Thick.Blowing.Common
 {
-    public class BlowingFixProfileParam : INotifyPropertyChanged
+    public class BlowingFixProfileParam : INotifyPropertyChanged,Misc.ICopiable,ICloneable
     {
 
         #region 正常运行参数
@@ -70,6 +70,17 @@ namespace FLY.Thick.Blowing.Common
         #endregion
         public event PropertyChangedEventHandler PropertyChanged;
 
+        public object Clone()
+        {
+            BlowingFixProfileParam p = new BlowingFixProfileParam();
+            p.Copy(this);
+            return p;
+        }
+
+        public void Copy(object src)
+        {
+            Misc.PropertiesManager.CopyTo(src, this);
+        }
     }
 
     /// <summary>
diff --git a/Project.FLY.Thick.Blowing/FLY.Thick.Blowing/Server/BlowingFixProfile.cs b/Project.FLY.Thick.Blowing/FLY.Thick.Blowing/Server/BlowingFixProfile.cs
index 07125fb4..1e7826e3 100644
--- a/Project.FLY.Thick.Blowing/FLY.Thick.Blowing/Server/BlowingFixProfile.cs
+++ b/Project.FLY.Thick.Blowing/FLY.Thick.Blowing/Server/BlowingFixProfile.cs
@@ -1,6 +1,7 @@
 using FLY.Thick.Blowing.Common;
 using FLY.Thick.Blowing.IService;
 using FObjBase;
+using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
 using System.IO;
@@ -11,33 +12,64 @@ namespace FLY.Thick.Blowing.Server
 {
     public class BlowingFixProfile: IBlowingFixProfileService
     {
+        /// <summary>
+        /// 参数列表!!!!!
+        /// </summary>
+        BlowingProfileDBJson profileDB = new BlowingProfileDBJson();
+
         public BlowingFixProfileParam Param { get; } = new BlowingFixProfileParam();
+
+
+        private string file_path = "profile.json";
         public BlowingFixProfile()
         {
 
         }
         public BlowingFixProfile(string path)
         {
-            if (path == null)
-                Load();
-            else
-                Load(path);
+            if (!string.IsNullOrEmpty(path))
+                file_path = path;
+
+            Load();
         }
 
         public bool Load()
         {
-            return Misc.SaveToXmlHepler.Load("profile.xml", this.Param);
-        }
-        bool Load(string path)
-        {
-            return Misc.SaveToXmlHepler.Load(path, this.Param);
+            try
+            {
+                if (File.Exists(file_path))
+                {
+                    string json = File.ReadAllText(file_path);
+                    profileDB = JsonConvert.DeserializeObject<BlowingProfileDBJson>(json);
+                    var param = profileDB.ParamList.Find((p) => p.PName == profileDB.CurrentPName);
+                    if (param != null)
+                    {
+                        Param.Copy(param);
+                    }
+                    return true;
+                }
+            }
+            catch
+            {
+                //异常,没有json 解码失败
+
+            }
+            return false;
         }
 
         bool Save()
         {
-            bool ret2 = Misc.SaveToXmlHepler.Save(@"profile.xml", this.Param);
-            bool ret1 = Misc.SaveToXmlHepler.Save(@"profile\" + Param.PName + ".xml", this.Param);
-            return (ret1 && ret2);
+            try
+            {
+                File.WriteAllText(file_path, JsonConvert.SerializeObject(profileDB, Formatting.Indented));
+                return true;
+            }
+            catch
+            {
+                //异常,没有json 编码失败
+
+            }
+            return false;
         }
 
         /// <summary>
@@ -46,6 +78,18 @@ namespace FLY.Thick.Blowing.Server
         public void Apply()
         {
             Save();
+
+            profileDB.CurrentPName = Param.PName;
+            BlowingFixProfileParam param = profileDB.ParamList.Find((p) => p.PName == Param.PName);
+            if (param == null)
+            {
+                profileDB.ParamList.Add((BlowingFixProfileParam)Param.Clone());
+            }
+            else
+            {
+                param.Copy(Param);
+            }
+            Save();
         }
 
         /// <summary>
@@ -55,20 +99,8 @@ namespace FLY.Thick.Blowing.Server
         /// <param name="AsyncState"></param>
         public void GetList(AsyncCBHandler AsyncDelegate, object AsyncState)
         {
-            DirectoryInfo dinfo = new DirectoryInfo(@"profile");
-            List<string> filenames = new List<string>();
-            if (dinfo.Exists)
-            {
-
-                foreach (FileInfo info in dinfo.GetFiles())
-                {
-                    if (Path.GetExtension(info.Name) == ".xml")
-                    {
-                        filenames.Add(Path.GetFileNameWithoutExtension(info.Name));
-                    }
-                }
-            }
-            AsyncDelegate(AsyncState, filenames);
+            IEnumerable<string> names = from p in profileDB.ParamList select p.PName;
+            AsyncDelegate(AsyncState, names);
         }
 
         /// <summary>
@@ -77,9 +109,7 @@ namespace FLY.Thick.Blowing.Server
         /// <param name="productname"></param>
         public void Del(string productname)
         {
-            string path = @"profile\" + productname + ".xml";
-            if (System.IO.File.Exists(path))
-                System.IO.File.Delete(path);
+            profileDB.ParamList.RemoveAll((p) => p.PName == productname);
 
         }
 
@@ -91,9 +121,22 @@ namespace FLY.Thick.Blowing.Server
         /// <param name="AsyncState"></param>
         public void Read(string productname, AsyncCBHandler AsyncDelegate, object AsyncState)
         {
-            BlowingFixProfile p = new BlowingFixProfile();
-            p.Load(@"profile\" + productname + ".xml");
-            AsyncDelegate(AsyncState, p.Param);
+            var p = profileDB.ParamList.Find((_p) => _p.PName == productname);
+
+            AsyncDelegate(AsyncState, p);
         }
     }
+
+    public class BlowingProfileDBJson
+    {
+        /// <summary>
+        /// 当前正在使用的产品参数
+        /// </summary>
+        public string CurrentPName { get; set; }
+        /// <summary>
+        /// 参数列表!!!!!
+        /// </summary>
+        public List<BlowingFixProfileParam> ParamList { get; set; } = new List<BlowingFixProfileParam>();
+
+    }
 }
-- 
2.18.1