|
|
@@ -11,9 +11,8 @@ using System.Threading;
|
|
|
|
|
|
public static class AppUtil
|
|
|
{
|
|
|
-
|
|
|
- //private static List<IoModal> newConfig;
|
|
|
public static List<DicIO> newsConfig;
|
|
|
+ //private static List<IoModal> newConfig;
|
|
|
|
|
|
private static string apppath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config", "Router");
|
|
|
|
|
|
@@ -21,17 +20,46 @@ public static class AppUtil
|
|
|
|
|
|
public static List<DataItem> DataPointCheckList = new List<DataItem>();
|
|
|
|
|
|
- public static IConfiguration configuration;
|
|
|
+
|
|
|
public static void InitAllDirectoryCfg()
|
|
|
{
|
|
|
if (Directory.Exists(apppath))
|
|
|
{
|
|
|
+ //newConfig = GetFiles(apppath, "*.json");
|
|
|
newsConfig = GetFilesGo(apppath, "*.json");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static List<string> CheckPointConfig { get { return newsConfig.SelectMany(a => a.Data).SelectMany(a => a.Params).SelectMany(a => a.keys).Select(a => a.CheckTagName).Distinct().ToList(); } }
|
|
|
|
|
|
+
|
|
|
+ //初始化获取有配置文件
|
|
|
+ //public static void InitAllDirectoryCfg()
|
|
|
+ //{
|
|
|
+ // if (Directory.Exists(apppath))
|
|
|
+ // {
|
|
|
+ // newConfig = GetFiles(apppath, "*.json");
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+
|
|
|
+ static List<DicIO> GetFilesGo(string directory, string pattern = "*.*")
|
|
|
+ {
|
|
|
+ List<DicIO> files = new List<DicIO>();
|
|
|
+ foreach (var item in Directory.GetFiles(directory, pattern))
|
|
|
+ {
|
|
|
+ files.Add(new DicIO { path = item, Data = JsonConvert.DeserializeObject<List<IoModal>>(File.ReadAllText(item)) });
|
|
|
+ }
|
|
|
+ foreach (var item in Directory.GetDirectories(directory))
|
|
|
+ {
|
|
|
+ //files.AddRange(GetFiles(item, pattern));
|
|
|
+
|
|
|
+ files.AddRange(GetFilesGo(item, pattern));
|
|
|
+ }
|
|
|
+ return files;
|
|
|
+ }
|
|
|
+
|
|
|
+ //public static List<string> CheckPointConfig { get { return newConfig.SelectMany(a => a.Params).SelectMany(a => a.keys).Select(a => a.CheckTagName).Distinct().ToList(); } }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 读取文件夹下所有json,并转化为ioModal
|
|
|
/// </summary>
|
|
|
@@ -51,22 +79,69 @@ public static class AppUtil
|
|
|
return files;
|
|
|
}
|
|
|
|
|
|
- static List<DicIO> GetFilesGo(string directory, string pattern = "*.*")
|
|
|
+ public static PLCResultAll TryFunc_AIRun(string action, string method, string param = null)
|
|
|
{
|
|
|
- List<DicIO> files = new List<DicIO>();
|
|
|
- foreach (var item in Directory.GetFiles(directory, pattern))
|
|
|
+ try
|
|
|
{
|
|
|
- files.Add(new DicIO { path = item, Data = JsonConvert.DeserializeObject<List<IoModal>>(File.ReadAllText(item)) });
|
|
|
+ return Func_AIRun(action, method, param);
|
|
|
}
|
|
|
- foreach (var item in Directory.GetDirectories(directory))
|
|
|
+ catch (Exception ext)
|
|
|
{
|
|
|
- //files.AddRange(GetFiles(item, pattern));
|
|
|
|
|
|
- files.AddRange(GetFilesGo(item, pattern));
|
|
|
+ LogUtil.WriteError($"AI:{action}异常", ext.Message);
|
|
|
+ return new PLCResultAll { Sucessful = false, Message = "AI接口失败" };
|
|
|
}
|
|
|
- return files;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// AI模型的执行操作,依据不同的检查值,分别调用
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="action"></param>
|
|
|
+ /// <param name="method"></param>
|
|
|
+ /// <param name="param"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static PLCResultAll Func_AIRun(string action, string method, string param = null)
|
|
|
+ {
|
|
|
+ //判断不同的API调用
|
|
|
+ //依据不同的API名称的标识区分不同的检测点,后期可封装到配置文件中. along202305
|
|
|
+
|
|
|
+ float rlt = 0;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (action.Contains("WQ2116_AIFuc")) //回潮机
|
|
|
+ {
|
|
|
+ rlt = Read<float>("THISNODE.JK_YT6022_L5KYPHC_MODE.F_CV"); //读取模式状态点
|
|
|
+ }
|
|
|
+ if (action.Contains("SJ2147_AIFuc")) //加料机
|
|
|
+ {
|
|
|
+ rlt = Read<float>("THISNODE.JK_YT6022_L5KYPJL_MODE.F_CV"); //读取模式状态点
|
|
|
+ }
|
|
|
+
|
|
|
+ //Console.WriteLine("时间:{0},调用API:{1},参数:{2},模式:{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"),action,param,rlt);
|
|
|
+ LogUtil.Write(",调用API:" + action + ",参数:" + param + ", 模式:" + rlt);
|
|
|
+ string MyAction = ""; //转换后的调用API名称
|
|
|
+
|
|
|
+ switch (rlt)
|
|
|
+ {
|
|
|
+ case 1: //AI模型控制
|
|
|
+ MyAction = action + "_AI";
|
|
|
+ return ConfigRouter(MyAction, method, param);
|
|
|
+ case 2:
|
|
|
+ MyAction = action + "_Test";
|
|
|
+ return ConfigRouter(MyAction, method, param);
|
|
|
+ default:
|
|
|
+ return new PLCResultAll { Sucessful = false, Message = "AI控制模式错误、请重新选择" };
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Console.WriteLine(ex.Message);
|
|
|
+ return new PLCResultAll { Sucessful = false, Message = "程序异常!" };
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
public static PLCResultAll ConfigRouter(string action, string method, string param = null)
|
|
|
{
|
|
|
var first = newsConfig.SelectMany(a => a.Data).FirstOrDefault(a => "api/" + a.APIName == action && a.Method == method);
|
|
|
@@ -89,6 +164,8 @@ public static class AppUtil
|
|
|
return new PLCResultAll { Sucessful = false, Message = "文档错误,访问失败" };
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
public static PLCResultAll Test()
|
|
|
{
|
|
|
var first = newsConfig.SelectMany(a => a.Data).FirstOrDefault(a => a.APIName == "Ifix_Status" && a.Method == "get");
|
|
|
@@ -97,9 +174,10 @@ public static class AppUtil
|
|
|
//LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:fff") + "--PING IFX返回结果:",JsonConvert.SerializeObject(data));
|
|
|
return data;
|
|
|
}
|
|
|
+
|
|
|
private static PLCResultAll WrNewCheckList(IoModal iovar, string pras = null)
|
|
|
{
|
|
|
- //Console.WriteLine("API调用时间 :{0}", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
|
|
|
+ //Console.WriteLine("API调用时间: {0}", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
|
|
|
bool IsCheck = iovar.IsCheck; //获取是否要校验读写结果
|
|
|
|
|
|
try
|
|
|
@@ -204,8 +282,8 @@ public static class AppUtil
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- LogUtil.WriteLog("错误信息", iovar.APIName + ex.Message);
|
|
|
- LogUtil.WriteError("错误信息", iovar.APIName + ex.Message);
|
|
|
+ LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "错误信息", iovar.APIName + ex.Message);
|
|
|
+
|
|
|
//Console.WriteLine("错误信息:{0}", ex.Message);
|
|
|
return new PLCResultAll
|
|
|
{
|
|
|
@@ -220,12 +298,6 @@ public static class AppUtil
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
//写入批操作,加入是否检查标志(为1时检查,默认0为 不检查状态)
|
|
|
public static PLCResultAll WriteListIsCheck(string strWrite, string strRead, int duration, bool bCheck) //写入标签LIST
|
|
|
{
|
|
|
@@ -236,22 +308,14 @@ public static class AppUtil
|
|
|
|
|
|
if (list.Count > 20) //判断写入的数量,超过20不进行操作
|
|
|
{
|
|
|
- LogUtil.WriteLog("错误信息", "写入数据过多,执行失败");
|
|
|
+ LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "错误信息", "写入数据过多,执行失败");
|
|
|
rlt_All.Sucessful = false;
|
|
|
rlt_All.Message = "写入数据过多,执行失败";
|
|
|
return rlt_All;
|
|
|
}
|
|
|
foreach (var item in list)
|
|
|
{
|
|
|
- if (item.Value == "" || item.Value == null)
|
|
|
- {
|
|
|
- OPCHelper.daServerMgr.Write(new List<DataItem> { new DataItem { TagAddress = item.TagAddress, Value = null } });
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- OPCHelper.daServerMgr.Write(new List<DataItem> { new DataItem { TagAddress = item.TagAddress, Value = item.Value } });
|
|
|
- }
|
|
|
-
|
|
|
+ OPCHelper.daServerMgr.Write(new List<DataItem> { new DataItem { TagAddress = item.TagAddress, Value = item.Value } });
|
|
|
Thread.Sleep(5); //间隔 时间
|
|
|
|
|
|
}
|
|
|
@@ -259,7 +323,7 @@ public static class AppUtil
|
|
|
/// 1:在不检查状态下,直接返回结果信息
|
|
|
if (!bCheck) //在不检查状态下
|
|
|
{
|
|
|
- LogUtil.WriteLog("不检查", "不检查");
|
|
|
+ LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "不检查", "不检查");
|
|
|
rlt_All.Sucessful = true;
|
|
|
rlt_All.Message = "不检查";
|
|
|
rlt_All.PlcResults = new List<DataItem>() { new DataItem { TagAddress = "", IsSetSuccessful = false } };
|
|
|
@@ -275,28 +339,109 @@ public static class AppUtil
|
|
|
|
|
|
//间隔等待,写入后数据更新再读出
|
|
|
//Thread.Sleep(duration);
|
|
|
- int delatTime = 300;
|
|
|
- bool listerner = false;
|
|
|
+ int delatTime = 500;
|
|
|
+ bool listerner = false; //True为正确读取值
|
|
|
List<DataItem> Rlst = null;
|
|
|
- while (delatTime <= duration)
|
|
|
+ while (delatTime <= duration) //循环增时,判断缓存的数据是否更新
|
|
|
{
|
|
|
- var resultbool = GetPointFromListerner(strRead);
|
|
|
- if (resultbool.Count > 0 && !resultbool.Exists(a => a == false))
|
|
|
+ //var resultbool = GetPointFromListerner(strRead); //读取缓存记录
|
|
|
+ //if (resultbool.Count > 0 && ! resultbool.Exists(a => a == false)) //结果大于0 或 结果为false
|
|
|
+ //{
|
|
|
+ // listerner = true;
|
|
|
+ // break;
|
|
|
+ //}
|
|
|
+
|
|
|
+ Thread.Sleep(500);
|
|
|
+ delatTime += 500;
|
|
|
+ //定时主动读取 along 202305
|
|
|
+ PLCResultAll Rall = TimeReadList(list, strRead);
|
|
|
+ if (Rall.Sucessful == true)
|
|
|
{
|
|
|
- listerner = true;
|
|
|
- break;
|
|
|
+ //Console.WriteLine("*************主动读取,获取全部正确值!************");
|
|
|
+ LogUtil.Write(",动作:" + "主动读取,获取正确值!");
|
|
|
+ return Rall;
|
|
|
}
|
|
|
- Thread.Sleep(300);
|
|
|
- delatTime += 300;
|
|
|
- }
|
|
|
- if (listerner)
|
|
|
- {
|
|
|
- Rlst = DataPointCheckList; //根据监听数据更新结果
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Rlst = ReadList(strRead); //进行写入值的回读操作
|
|
|
+ //else
|
|
|
+ //{
|
|
|
+ // LogUtil.Write(",结果:" + "数据不匹配"+",读取结果:"+ JsonUtil.SerializeObject(Rall));
|
|
|
+ //}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ return TimeReadList(list, strRead);
|
|
|
+
|
|
|
+ //if (listerner)
|
|
|
+ //{
|
|
|
+ // Rlst = DataPointCheckList; //根据监听数据更新结果
|
|
|
+ //}
|
|
|
+ //else
|
|
|
+ //{
|
|
|
+ // Rlst = ReadList(strRead); //进行写入值的回读操作
|
|
|
+ //}
|
|
|
+ //rlt_All.Sucessful = true; //默认成功,在下方的比较中只要其中有一个不正确,置为false。
|
|
|
+ //rlt_All.Message = "批量写入---成功";
|
|
|
+ //for (int i = 0; i < list.Count; i++)
|
|
|
+ //{
|
|
|
+ // var write = list[i].TagAddress;
|
|
|
+ // var check = Rlst[i].TagAddress;
|
|
|
+
|
|
|
+ // //if (write == check) //标签名相同时,进行两个值的比较
|
|
|
+ // //{
|
|
|
+ // //if (write.ToString() == check.ToString()) allen 2023-04 检查代码出错,进行屏蔽,修改
|
|
|
+ // if (Rlst[i].Value == null) //判断是否读取值,没有读取到(一般是标签值不存在情况), 跳出返回:写入失败
|
|
|
+ // {
|
|
|
+ // LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "错误信息", "批量写入---失败");
|
|
|
+ // Rlst[i].IsSetSuccessful = false;
|
|
|
+ // Rlst[i].Message = "写入失败";
|
|
|
+ // rlt_All.Sucessful = false;
|
|
|
+ // rlt_All.Message = "批量写入---失败";
|
|
|
+ // continue;
|
|
|
+ // //break;
|
|
|
+ // }
|
|
|
+ // var ValWrite = list[i].Value.ToString();
|
|
|
+ // var ValRead = Rlst[i].Value.ToString();
|
|
|
+ // if (ValWrite == ValRead) //进行两者值的比较
|
|
|
+ // {
|
|
|
+ // Rlst[i].IsSetSuccessful = true;
|
|
|
+ // Rlst[i].Message = "写入成功";
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "错误信息", "批量写入---失败");
|
|
|
+ // Rlst[i].IsSetSuccessful = false;
|
|
|
+ // Rlst[i].Message = "写入失败";
|
|
|
+ // rlt_All.Sucessful = false;
|
|
|
+ // rlt_All.Message = "批量写入---失败";
|
|
|
+
|
|
|
+ // }
|
|
|
+ // rlt_All.PlcResults = Rlst;
|
|
|
+ //}
|
|
|
+
|
|
|
+ //if (bCheck == false) //在不检查状态下
|
|
|
+ //{
|
|
|
+ // rlt_All.Sucessful = true;
|
|
|
+ // rlt_All.Message = "不检查写入值";
|
|
|
+ // //return rlt_All;
|
|
|
+ // //还要读取,时间调整为1S时间
|
|
|
+ //}
|
|
|
+ //return rlt_All;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 定时读取OPC标签,获取结果集 along 202305
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="strList"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static PLCResultAll TimeReadList(List<DataItem> list, string strRead)
|
|
|
+ {
|
|
|
+ PLCResultAll rlt_All = new PLCResultAll();
|
|
|
+ rlt_All.PlcResults = new List<DataItem>();
|
|
|
+ List<DataItem> Rlst = null;
|
|
|
+
|
|
|
+ Rlst = ReadList(strRead); //进行写入值的回读操作
|
|
|
rlt_All.Sucessful = true; //默认成功,在下方的比较中只要其中有一个不正确,置为false。
|
|
|
rlt_All.Message = "批量写入---成功";
|
|
|
for (int i = 0; i < list.Count; i++)
|
|
|
@@ -309,7 +454,7 @@ public static class AppUtil
|
|
|
//if (write.ToString() == check.ToString()) allen 2023-04 检查代码出错,进行屏蔽,修改
|
|
|
if (Rlst[i].Value == null) //判断是否读取值,没有读取到(一般是标签值不存在情况), 跳出返回:写入失败
|
|
|
{
|
|
|
- LogUtil.WriteLog("错误信息", "批量写入---失败");
|
|
|
+ //LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "错误信息", "批量写入---读取后比较1");
|
|
|
Rlst[i].IsSetSuccessful = false;
|
|
|
Rlst[i].Message = "写入失败";
|
|
|
rlt_All.Sucessful = false;
|
|
|
@@ -326,7 +471,7 @@ public static class AppUtil
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- LogUtil.WriteLog("错误信息", "批量写入---失败");
|
|
|
+ //LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "错误信息", "批量写入---读取后比较2");
|
|
|
Rlst[i].IsSetSuccessful = false;
|
|
|
Rlst[i].Message = "写入失败";
|
|
|
rlt_All.Sucessful = false;
|
|
|
@@ -336,17 +481,17 @@ public static class AppUtil
|
|
|
rlt_All.PlcResults = Rlst;
|
|
|
}
|
|
|
|
|
|
- if (bCheck == false) //在不检查状态下
|
|
|
- {
|
|
|
- rlt_All.Sucessful = true;
|
|
|
- rlt_All.Message = "不检查写入值";
|
|
|
- //return rlt_All;
|
|
|
- //还要读取,时间调整为1S时间
|
|
|
- }
|
|
|
+
|
|
|
return rlt_All;
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 从回调的更新缓存读取结果值
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="str"></param>
|
|
|
+ /// <returns></returns>
|
|
|
private static List<bool> GetPointFromListerner(string str)
|
|
|
{
|
|
|
DataPointCheckList = new List<DataItem>();
|
|
|
@@ -354,7 +499,7 @@ public static class AppUtil
|
|
|
List<DataItem> list = JsonUtil.DeserializeObject<List<DataItem>>(str);
|
|
|
foreach (var item in list)
|
|
|
{
|
|
|
- if (item.TagAddress.ToUpper().EndsWith(".F_CV"))
|
|
|
+ if (item.TagAddress.EndsWith(".F_CV"))
|
|
|
{
|
|
|
DataItem dataItem = null;
|
|
|
if (AppUtil.DataItemQueue.TryGetValue(item.TagAddress, out dataItem))
|
|
|
@@ -373,15 +518,11 @@ public static class AppUtil
|
|
|
}
|
|
|
|
|
|
}
|
|
|
- else if (item.TagAddress.ToUpper().EndsWith(".A_CV"))
|
|
|
+ else if (item.TagAddress.EndsWith(".A_CV"))
|
|
|
{
|
|
|
DataItem dataItem = null;
|
|
|
if (AppUtil.DataItemQueue.TryGetValue(item.TagAddress, out dataItem))
|
|
|
{
|
|
|
- if (item.Value == null)
|
|
|
- {
|
|
|
- item.Value = "";
|
|
|
- }
|
|
|
if (item.Value.ToString() == dataItem.Value.ToString())
|
|
|
{
|
|
|
result.Add(true);
|
|
|
@@ -406,7 +547,7 @@ public static class AppUtil
|
|
|
List<DataItem> rlt = new List<DataItem>();
|
|
|
foreach (var item in list)
|
|
|
{
|
|
|
- if (item.TagAddress.ToUpper().EndsWith(".F_CV"))
|
|
|
+ if (item.TagAddress.EndsWith(".F_CV"))
|
|
|
{
|
|
|
rlt.Add(new DataItem()
|
|
|
{
|
|
|
@@ -415,7 +556,7 @@ public static class AppUtil
|
|
|
|
|
|
});
|
|
|
}
|
|
|
- else if (item.TagAddress.ToUpper().EndsWith(".A_CV"))
|
|
|
+ else if (item.TagAddress.EndsWith(".A_CV"))
|
|
|
{
|
|
|
rlt.Add(new DataItem()
|
|
|
{
|