using FLY.Thick.Blowing.Common; using FLY.Thick.Blowing.IService; using FObjBase; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; 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 (!string.IsNullOrEmpty(path)) file_path = path; Load(); } public bool Load() { 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() { try { File.WriteAllText(file_path, JsonConvert.SerializeObject(profileDB, Formatting.Indented)); return true; } catch { //异常,没有json 编码失败 } return false; } /// <summary> /// 应用 & 保存 /// </summary> 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> /// 获取产品列表, 返回类型为 List<string> /// </summary> /// <param name="AsyncDelegate"></param> /// <param name="AsyncState"></param> public void GetList(AsyncCBHandler AsyncDelegate, object AsyncState) { IEnumerable<string> names = from p in profileDB.ParamList select p.PName; AsyncDelegate(AsyncState, names); } /// <summary> /// 删除指定产品 /// </summary> /// <param name="productname"></param> public void Del(string productname) { profileDB.ParamList.RemoveAll((p) => p.PName == productname); } /// <summary> /// 读取指定产品,返回类型为 ProfileParam /// </summary> /// <param name="productname"></param> /// <param name="AsyncDelegate"></param> /// <param name="AsyncState"></param> public void Read(string productname, AsyncCBHandler AsyncDelegate, object AsyncState) { 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>(); } }