using CommonLib.Components;
using CommonLib.Modules.ParamsFileReader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCSPro.Model;
namespace WCSPro.IDAL
{
public interface ILibIODAL : IParamsFileRW<List<IOItem>>
{
List<IOItem> QueryIOList(string queryKey);
List<IOItem> QueryIOList(string queryKey, string ioType);
Dictionary<string, IOItem> GetComponentIOItems(params ComponentName[] components);
IOItem GetIOItemByName(string ioName);
bool AddIOItem(IOItem item);
bool ModifyIO(IOItem item);
bool RemoveIO(IOItem item);
}
}
using CommonLib.Components;
using CommonLib.SerializerUtils;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCSPro.IDAL;
using WCSPro.Model;
namespace WCSPro.DAL
{
public class LibIODAL : ILibIODAL
{
private string IOListFilePath { get; } = Path.Combine(AppContext.BaseDirectory, "Config", "IOList.JFANA");
public LibIODAL()
{
IOList = ReadParamsObjFromFile(this.IOListFilePath);
}
private List<IOItem> IOList { get; set; }
public List<IOItem> ReadParamsObjFromFile(string path)
{
if (!File.Exists(path)) return new List<IOItem>();
var paramJson = File.ReadAllText(path);
return JsonHelper.Deserialize<List<IOItem>>(paramJson);
}
public bool WriteParamsObjToFile(List<IOItem> obj, string path)
{
var paramJson = JsonHelper.Serialize(obj);
File.WriteAllText(path, paramJson);
return true;
}
public List<IOItem> QueryIOList(string queryKey)
{
List<IOItem> lRet = IOList;
if (!string.IsNullOrWhiteSpace(queryKey))
{
lRet = IOList.Where(t =>
t.IOCode.ToLower().Contains(queryKey.ToLower()) ||
t.IOName.ToLower().Contains(queryKey.ToLower()) ||
t.PointName.ToLower().Contains(queryKey.ToLower())
).ToList();
}
return JsonHelper.ObjCopy(lRet).OrderBy(t => t.IOCode).ThenBy(t => t.IOName).ToList();
}
public List<IOItem> QueryIOList(string queryKey, string ioType)
{
List<IOItem> lRet = IOList;
if (!string.IsNullOrWhiteSpace(ioType))
{
lRet = lRet.Where(t => t.IOType.ToString().ToUpper() == ioType.ToUpper()).ToList();
}
if (!string.IsNullOrWhiteSpace(queryKey))
{
lRet = lRet
.WhereIF(!string.IsNullOrWhiteSpace(ioType), t => t.IOType.ToString().ToUpper() == ioType.ToUpper())
.Where(t =>
t.IOCode.ToLower().Contains(queryKey.ToLower()) ||
t.IOName.ToLower().Contains(queryKey.ToLower()) ||
t.PointName.ToLower().Contains(queryKey.ToLower())
)
.ToList();
}
return JsonHelper.ObjCopy(lRet).OrderBy(t => t.IOCode).ThenBy(t => t.IOName).ToList();
}
public Dictionary<string, IOItem> GetComponentIOItems(params ComponentName[] components)
{
List<IOItem> ls = new List<IOItem>();
if (components == null || components.Length == 0) return new Dictionary<string, IOItem>();
foreach (var component in components)
{
var temp = IOList.Where(t => t.CmpName == component);
if (temp != null && temp.Count() > 0) ls.AddRange(temp);
}
return JsonHelper.ObjCopy(ls.ToDictionary(t => t.IOName));
}
public IOItem GetIOItemByName(string ioName)
{
if (this.IOList == null || this.IOList.Count == 0) return null;
var item = this.IOList.FirstOrDefault(t => t.IOName == ioName);
if (item == null) return null;
return JsonHelper.ObjCopy(item);
}
public bool AddIOItem(IOItem item)
{
if (IOList.Exists(t => t.IOCode == item.IOCode))
{
throw new Exception("IO already exists");
}
IOList.Add(item);
return WriteParamsObjToFile(IOList, this.IOListFilePath);
}
public bool ModifyIO(IOItem item)
{
var obj = IOList.Single(t => t.IOCode == item.IOCode);
obj.IOAddr = item.IOAddr;
obj.CanRead = item.CanRead;
obj.CanWrite = item.CanWrite;
obj.CmpName = item.CmpName;
obj.IONote = item.IONote;
obj.IOType = item.IOType;
obj.IOName = item.IOName;
obj.PointName = item.PointName;
obj.DataLen = item.DataLen;
obj.ValueType = item.ValueType;
return WriteParamsObjToFile(IOList, this.IOListFilePath);
}
public bool RemoveIO(IOItem item)
{
IOList.RemoveAll(t => t.IOCode == item.IOCode);
return WriteParamsObjToFile(IOList, this.IOListFilePath);
}
}
}
using Newtonsoft.Json;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommonLib.IO;
using CommonLib.Components;
namespace WCSPro.Model
{
[AddINotifyPropertyChangedInterface]
public class IOItem
{
public string IOCode { get; set; } = string.Empty;
public IO_Type IOType { get; set; }
public string IOName { get; set; } = string.Empty;
public string PointName { get; set; } = string.Empty;
public string IOAddr { get; set; } = string.Empty;
public string IONote { get; set; } = string.Empty;
public ComponentName CmpName { get; set; }
public bool CanRead { get; set; } = true;
public bool CanWrite { get; set; } = false;
public int DataLen { get; set; }
[JsonIgnore]
public byte[] Data { get; set; }
public string ValueType { get; set; } = "bool";
private string sValue;
[JsonIgnore]
public string Value
{
get { return sValue; }
set
{
if (ValueType == "bool" && DataLen == 1)
{
switch (value)
{
default:
sValue = value;
break;
case "1":
sValue = "true";
break;
case "0":
sValue = "false";
break;
}
}
else
{
sValue = value;
}
}
}
[JsonIgnore]
public string Message { get; set; } = string.Empty;
public IOItem Clone()
{
IOItem item = new IOItem();
item.IOCode = this.IOCode;
item.IOType = this.IOType;
item.IOName = this.IOName;
item.PointName = this.PointName;
item.IOAddr = this.IOAddr;
item.IONote = this.IONote;
item.CmpName = this.CmpName;
item.CanRead = this.CanRead;
item.CanWrite = this.CanWrite;
item.DataLen = this.DataLen;
item.Data = new byte[this.Data.Length];
Array.Copy(this.Data, item.Data, this.Data.Length);
item.ValueType = this.ValueType;
item.Value = this.Value;
item.Message = this.Message;
return item;
}
}
}