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
{
///
/// 参数列表!!!!!
///
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(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;
}
///
/// 应用 & 保存
///
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();
}
///
/// 获取产品列表, 返回类型为 List<string>
///
///
///
public void GetList(AsyncCBHandler AsyncDelegate, object AsyncState)
{
IEnumerable names = from p in profileDB.ParamList select p.PName;
AsyncDelegate(AsyncState, names);
}
///
/// 删除指定产品
///
///
public void Del(string productname)
{
profileDB.ParamList.RemoveAll((p) => p.PName == productname);
}
///
/// 读取指定产品,返回类型为 ProfileParam
///
///
///
///
public void Read(string productname, AsyncCBHandler AsyncDelegate, object AsyncState)
{
var p = profileDB.ParamList.Find((_p) => _p.PName == productname);
AsyncDelegate(AsyncState, p);
}
}
public class BlowingProfileDBJson
{
///
/// 当前正在使用的产品参数
///
public string CurrentPName { get; set; }
///
/// 参数列表!!!!!
///
public List ParamList { get; set; } = new List();
}
}