Commit 04e651cd authored by 潘栩锋's avatar 潘栩锋 🚴

运行参数 使用 json保存

parent 05194008
...@@ -8,7 +8,7 @@ using System.Text; ...@@ -8,7 +8,7 @@ using System.Text;
namespace FLY.Thick.Blowing.Common namespace FLY.Thick.Blowing.Common
{ {
public class BlowingFixProfileParam : INotifyPropertyChanged public class BlowingFixProfileParam : INotifyPropertyChanged,Misc.ICopiable,ICloneable
{ {
#region 正常运行参数 #region 正常运行参数
...@@ -70,6 +70,17 @@ namespace FLY.Thick.Blowing.Common ...@@ -70,6 +70,17 @@ namespace FLY.Thick.Blowing.Common
#endregion #endregion
public event PropertyChangedEventHandler PropertyChanged; 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> /// <summary>
......
using FLY.Thick.Blowing.Common; using FLY.Thick.Blowing.Common;
using FLY.Thick.Blowing.IService; using FLY.Thick.Blowing.IService;
using FObjBase; using FObjBase;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
...@@ -11,33 +12,64 @@ namespace FLY.Thick.Blowing.Server ...@@ -11,33 +12,64 @@ namespace FLY.Thick.Blowing.Server
{ {
public class BlowingFixProfile: IBlowingFixProfileService public class BlowingFixProfile: IBlowingFixProfileService
{ {
/// <summary>
/// 参数列表!!!!!
/// </summary>
BlowingProfileDBJson profileDB = new BlowingProfileDBJson();
public BlowingFixProfileParam Param { get; } = new BlowingFixProfileParam(); public BlowingFixProfileParam Param { get; } = new BlowingFixProfileParam();
private string file_path = "profile.json";
public BlowingFixProfile() public BlowingFixProfile()
{ {
} }
public BlowingFixProfile(string path) public BlowingFixProfile(string path)
{ {
if (path == null) if (!string.IsNullOrEmpty(path))
Load(); file_path = path;
else
Load(path); Load();
} }
public bool Load() public bool Load()
{ {
return Misc.SaveToXmlHepler.Load("profile.xml", this.Param); try
} {
bool Load(string path) if (File.Exists(file_path))
{ {
return Misc.SaveToXmlHepler.Load(path, this.Param); 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 Save()
{ {
bool ret2 = Misc.SaveToXmlHepler.Save(@"profile.xml", this.Param); try
bool ret1 = Misc.SaveToXmlHepler.Save(@"profile\" + Param.PName + ".xml", this.Param); {
return (ret1 && ret2); File.WriteAllText(file_path, JsonConvert.SerializeObject(profileDB, Formatting.Indented));
return true;
}
catch
{
//异常,没有json 编码失败
}
return false;
} }
/// <summary> /// <summary>
...@@ -46,6 +78,18 @@ namespace FLY.Thick.Blowing.Server ...@@ -46,6 +78,18 @@ namespace FLY.Thick.Blowing.Server
public void Apply() public void Apply()
{ {
Save(); 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> /// <summary>
...@@ -55,20 +99,8 @@ namespace FLY.Thick.Blowing.Server ...@@ -55,20 +99,8 @@ namespace FLY.Thick.Blowing.Server
/// <param name="AsyncState"></param> /// <param name="AsyncState"></param>
public void GetList(AsyncCBHandler AsyncDelegate, object AsyncState) public void GetList(AsyncCBHandler AsyncDelegate, object AsyncState)
{ {
DirectoryInfo dinfo = new DirectoryInfo(@"profile"); IEnumerable<string> names = from p in profileDB.ParamList select p.PName;
List<string> filenames = new List<string>(); AsyncDelegate(AsyncState, names);
if (dinfo.Exists)
{
foreach (FileInfo info in dinfo.GetFiles())
{
if (Path.GetExtension(info.Name) == ".xml")
{
filenames.Add(Path.GetFileNameWithoutExtension(info.Name));
}
}
}
AsyncDelegate(AsyncState, filenames);
} }
/// <summary> /// <summary>
...@@ -77,9 +109,7 @@ namespace FLY.Thick.Blowing.Server ...@@ -77,9 +109,7 @@ namespace FLY.Thick.Blowing.Server
/// <param name="productname"></param> /// <param name="productname"></param>
public void Del(string productname) public void Del(string productname)
{ {
string path = @"profile\" + productname + ".xml"; profileDB.ParamList.RemoveAll((p) => p.PName == productname);
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
} }
...@@ -91,9 +121,22 @@ namespace FLY.Thick.Blowing.Server ...@@ -91,9 +121,22 @@ namespace FLY.Thick.Blowing.Server
/// <param name="AsyncState"></param> /// <param name="AsyncState"></param>
public void Read(string productname, AsyncCBHandler AsyncDelegate, object AsyncState) public void Read(string productname, AsyncCBHandler AsyncDelegate, object AsyncState)
{ {
BlowingFixProfile p = new BlowingFixProfile(); var p = profileDB.ParamList.Find((_p) => _p.PName == productname);
p.Load(@"profile\" + productname + ".xml");
AsyncDelegate(AsyncState, p.Param); AsyncDelegate(AsyncState, p);
} }
} }
public class BlowingProfileDBJson
{
/// <summary>
/// 当前正在使用的产品参数
/// </summary>
public string CurrentPName { get; set; }
/// <summary>
/// 参数列表!!!!!
/// </summary>
public List<BlowingFixProfileParam> ParamList { get; set; } = new List<BlowingFixProfileParam>();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment