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

运行参数 使用 json保存

parent 05194008
......@@ -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>
......
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>();
}
}
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