|
|
@@ -0,0 +1,600 @@
|
|
|
+using Fine.OPCDaClient9000;
|
|
|
+using Fine.OPCDaClient9000.App;
|
|
|
+using Fine.OPCDaClient9000.Util;
|
|
|
+using Fine.Util;
|
|
|
+using Newtonsoft.Json;
|
|
|
+using Newtonsoft.Json.Linq;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Configuration;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace Fine.OPCDaClient9000.Util
|
|
|
+{
|
|
|
+ public static class AppUtil
|
|
|
+ {
|
|
|
+
|
|
|
+ private static List<IoModal> newConfig;
|
|
|
+
|
|
|
+ private static string apppath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config", "Router");
|
|
|
+
|
|
|
+ public static Dictionary<string, DataItem> DataItemQueue = new Dictionary<string, DataItem>();
|
|
|
+
|
|
|
+ public static List<DataItem> DataPointCheckList = new List<DataItem>();
|
|
|
+
|
|
|
+ //初始化获取有配置文件
|
|
|
+ public static void InitAllDirectoryCfg()
|
|
|
+ {
|
|
|
+ if (Directory.Exists(apppath))
|
|
|
+ {
|
|
|
+ newConfig = GetFiles(apppath, "*.json");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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>
|
|
|
+ /// <param name="apppath"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ static List<IoModal> GetFiles(string directory, string pattern = "*.*")
|
|
|
+ {
|
|
|
+ List<IoModal> files = new List<IoModal>();
|
|
|
+ foreach (var item in Directory.GetFiles(directory, pattern))
|
|
|
+ {
|
|
|
+ files.AddRange(JsonConvert.DeserializeObject<List<IoModal>>(File.ReadAllText(item)));
|
|
|
+ }
|
|
|
+ foreach (var item in Directory.GetDirectories(directory))
|
|
|
+ {
|
|
|
+ files.AddRange(GetFiles(item, pattern));
|
|
|
+ }
|
|
|
+ return files;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static PLCResultAll TryFunc_AIRun(string action, string method, string param = null)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ return Func_AIRun(action, method, param);
|
|
|
+ }
|
|
|
+ catch (Exception ext)
|
|
|
+ {
|
|
|
+
|
|
|
+ LogUtil.WriteError($"AI:{action}异常",ext.Message);
|
|
|
+ return new PLCResultAll { Sucessful = false, Message = "AI接口失败" };
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <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 = "程序异常!" };
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 配置加载路由信息、调用检查
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="action"></param>
|
|
|
+ /// <param name="method"></param>
|
|
|
+ /// <param name="param"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static PLCResultAll ConfigRouter(string action, string method, string param = null)
|
|
|
+ {
|
|
|
+ var first = newConfig.FirstOrDefault(a => "api/" + a.APIName == action && a.Method == method);
|
|
|
+ if (first != null)
|
|
|
+ {
|
|
|
+ //LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + first.APIName, "进入查询!");
|
|
|
+ if (param != null)
|
|
|
+ {
|
|
|
+ return WrNewCheckList(first, param);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return WrNewCheckList(first);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "访问失败", "无此接口,访问无效");
|
|
|
+ return new PLCResultAll { Sucessful = false, Message = "文档错误,访问失败" };
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static PLCResultAll Test()
|
|
|
+ {
|
|
|
+ var first = newConfig.FirstOrDefault(a => a.APIName == "Ifix_Status" && a.Method == "get");
|
|
|
+ //LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:fff") + "--PING IFX:", $"【Get动作】--- Ifix_Status 【Params参数】");
|
|
|
+ var data = WrNewCheckList(first);
|
|
|
+ //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"));
|
|
|
+ bool IsCheck = iovar.IsCheck; //获取是否要校验读写结果
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //情况1:GET方法,无参数
|
|
|
+ if (iovar.PType == EAPIparType.NoParam && string.IsNullOrEmpty(pras))
|
|
|
+ {
|
|
|
+ var writestr = JsonUtil.SerializeObject(iovar.Params.SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.WriteTagName, Value = a.StrValue }));
|
|
|
+ var readstr = JsonUtil.SerializeObject(iovar.Params.SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.CheckTagName, Value = a.StrValue }));
|
|
|
+ return WriteListIsCheck(writestr, readstr, iovar.DelayTime, IsCheck);
|
|
|
+ }
|
|
|
+
|
|
|
+ //情况2:传入单值,如预热温度值下发
|
|
|
+ if (iovar.PType == EAPIparType.InOneVal)
|
|
|
+ {
|
|
|
+ if (!string.IsNullOrEmpty(pras)) //判断是否有传入值
|
|
|
+ {
|
|
|
+ //iovar.StrValue = pras; //更新配置的写入值为:调用时传入值
|
|
|
+
|
|
|
+ var writestr = JsonUtil.SerializeObject(iovar.Params.SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.WriteTagName, Value = pras }));
|
|
|
+ var readstr = JsonUtil.SerializeObject(iovar.Params.SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.CheckTagName, Value = pras }));
|
|
|
+ return WriteListIsCheck(writestr, readstr, iovar.DelayTime, IsCheck);
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return new PLCResultAll { Sucessful = false, Message = "传入参数为空", PlcResults = new List<DataItem> { new DataItem { TagAddress = "", IsSetSuccessful = false } } };
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //情况3:传入多值(Json方式),如工单号、批次号、牌号信息下发
|
|
|
+ if (iovar.PType == EAPIparType.inMultiValObj) //检查配置中的:InputName对应关系
|
|
|
+ {
|
|
|
+ JObject data = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(pras);
|
|
|
+ var strWrite = JsonUtil.SerializeObject(iovar.Params.Where(a => a.Param == "").SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.WriteTagName, Value = data.GetValue(a.InputName) }));
|
|
|
+ var strRead = JsonUtil.SerializeObject(iovar.Params.Where(a => a.Param == "").SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.CheckTagName, Value = data.GetValue(a.InputName) }));
|
|
|
+ return WriteListIsCheck(strWrite, strRead, iovar.DelayTime, IsCheck);
|
|
|
+ }
|
|
|
+
|
|
|
+ //情况4:传入查询条件,如用柜号校验路径
|
|
|
+ if (iovar.PType == EAPIparType.InSel)
|
|
|
+ {
|
|
|
+ //JObject data = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(pras);
|
|
|
+ var strWrite = JsonUtil.SerializeObject(iovar.Params.Where(a => a.Param == pras).SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.WriteTagName, Value = a.StrValue }));
|
|
|
+ var strRead = JsonUtil.SerializeObject(iovar.Params.Where(a => a.Param == pras).SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.CheckTagName, Value = a.StrValue }));
|
|
|
+ return WriteListIsCheck(strWrite, strRead, iovar.DelayTime, IsCheck);
|
|
|
+ }
|
|
|
+ //情况5:传入查询条件后传入多个值
|
|
|
+ if (iovar.PType == EAPIparType.inSelAndMultiVal)
|
|
|
+ {
|
|
|
+ JObject data = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(pras);
|
|
|
+ SelYPno data1 = Newtonsoft.Json.JsonConvert.DeserializeObject<SelYPno>(pras);
|
|
|
+ if (!string.IsNullOrEmpty(data1.GDNum)) //输入参数判断
|
|
|
+ {
|
|
|
+ var strWrite = JsonUtil.SerializeObject(iovar.Params.Where(a => a.Param == data1.GDNum).SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.WriteTagName, Value = data.GetValue(a.InputName) }));
|
|
|
+ var strRead = JsonUtil.SerializeObject(iovar.Params.Where(a => a.Param == data1.GDNum).SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.CheckTagName, Value = data.GetValue(a.InputName) }));
|
|
|
+ return WriteListIsCheck(strWrite, strRead, iovar.DelayTime, IsCheck);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if (iovar.PType == EAPIparType.OnlyRead)
|
|
|
+ {
|
|
|
+ // var writestr = JsonUtil.SerializeObject(iovar.Params.SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.WriteTagName, Value = a.StrValue }));
|
|
|
+ var readstr = JsonUtil.SerializeObject(iovar.Params.SelectMany(a => a.keys).Select(a => new DataItem { TagAddress = a.CheckTagName, Value = a.CheckValue }));
|
|
|
+ List<DataItem> list = JsonUtil.DeserializeObject<List<DataItem>>(readstr);
|
|
|
+ List<DataItem> rlt = OPCHelper.daServerMgr.ReadListForServer(list);
|
|
|
+ foreach (var item in rlt)
|
|
|
+ {
|
|
|
+ var listdata = list.FirstOrDefault(a => a.TagAddress == item.TagAddress);
|
|
|
+ if (listdata != null)
|
|
|
+ {
|
|
|
+ if (listdata.Value.ToString() == item.Value.ToString())
|
|
|
+ {
|
|
|
+ item.IsSetSuccessful = true;
|
|
|
+ item.Message = "写入成功";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ item.IsSetSuccessful = false;
|
|
|
+ item.Message = "写入失败";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (rlt.Count == 0)
|
|
|
+ {
|
|
|
+ throw new Exception("异常");
|
|
|
+ }
|
|
|
+ if (rlt.Exists(a => a.IsSetSuccessful == false))
|
|
|
+ {
|
|
|
+ return new PLCResultAll { Sucessful = false, PlcResults = rlt, Message = "读取失败" };
|
|
|
+ }
|
|
|
+ return new PLCResultAll { Sucessful = true, PlcResults = rlt, Message = "读取成功" };
|
|
|
+ }
|
|
|
+ return new PLCResultAll
|
|
|
+ {
|
|
|
+ Sucessful = false,
|
|
|
+ Message = "传入参数异常",
|
|
|
+ PlcResults = new List<DataItem> { new DataItem { TagAddress = "", IsSetSuccessful = false }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "错误信息", iovar.APIName + ex.Message);
|
|
|
+
|
|
|
+ //Console.WriteLine("错误信息:{0}", ex.Message);
|
|
|
+ return new PLCResultAll
|
|
|
+ {
|
|
|
+ Sucessful = false,
|
|
|
+ Message = "接口程序执行错误,需检查!",
|
|
|
+ PlcResults = new List<DataItem> { new DataItem { TagAddress = "", IsSetSuccessful = false }
|
|
|
+ //throw;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //写入批操作,加入是否检查标志(为1时检查,默认0为 不检查状态)
|
|
|
+ public static PLCResultAll WriteListIsCheck(string strWrite, string strRead, int duration, bool bCheck) //写入标签LIST
|
|
|
+ {
|
|
|
+
|
|
|
+ List<DataItem> list = JsonConvert.DeserializeObject<List<DataItem>>(strWrite, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
|
|
|
+ PLCResultAll rlt_All = new PLCResultAll();
|
|
|
+ rlt_All.PlcResults = new List<DataItem>();
|
|
|
+
|
|
|
+ if (list.Count > 20) //判断写入的数量,超过20不进行操作
|
|
|
+ {
|
|
|
+ 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)
|
|
|
+ {
|
|
|
+ OPCHelper.daServerMgr.Write(new List<DataItem> { new DataItem { TagAddress = item.TagAddress, Value = item.Value } });
|
|
|
+ Thread.Sleep(5); //间隔 时间
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 1:在不检查状态下,直接返回结果信息
|
|
|
+ if (!bCheck) //在不检查状态下
|
|
|
+ {
|
|
|
+ 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 } };
|
|
|
+ return rlt_All;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //2:在检查状态,读回原有标签的数据
|
|
|
+ if (duration < 1000)///避免没有配置
|
|
|
+ {
|
|
|
+ duration = 2000;
|
|
|
+ }
|
|
|
+
|
|
|
+ //间隔等待,写入后数据更新再读出
|
|
|
+ //Thread.Sleep(duration);
|
|
|
+ int delatTime = 500;
|
|
|
+ bool listerner = false; //True为正确读取值
|
|
|
+ List<DataItem> Rlst = null;
|
|
|
+ while (delatTime <= duration) //循环增时,判断缓存的数据是否更新
|
|
|
+ {
|
|
|
+ //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)
|
|
|
+ {
|
|
|
+ //Console.WriteLine("*************主动读取,获取全部正确值!************");
|
|
|
+ LogUtil.Write(",动作:"+"主动读取,获取正确值!");
|
|
|
+ return Rall;
|
|
|
+ }
|
|
|
+ //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++)
|
|
|
+ {
|
|
|
+ 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") + "错误信息", "批量写入---读取后比较1");
|
|
|
+ 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") + "错误信息", "批量写入---读取后比较2");
|
|
|
+ Rlst[i].IsSetSuccessful = false;
|
|
|
+ Rlst[i].Message = "写入失败";
|
|
|
+ rlt_All.Sucessful = false;
|
|
|
+ rlt_All.Message = "批量写入---失败";
|
|
|
+
|
|
|
+ }
|
|
|
+ rlt_All.PlcResults = Rlst;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return rlt_All;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 从回调的更新缓存读取结果值
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="str"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private static List<bool> GetPointFromListerner(string str)
|
|
|
+ {
|
|
|
+ DataPointCheckList = new List<DataItem>();
|
|
|
+ List<bool> result = new List<bool>();
|
|
|
+ List<DataItem> list = JsonUtil.DeserializeObject<List<DataItem>>(str);
|
|
|
+ foreach (var item in list)
|
|
|
+ {
|
|
|
+ if (item.TagAddress.EndsWith(".F_CV"))
|
|
|
+ {
|
|
|
+ DataItem dataItem = null;
|
|
|
+ if (AppUtil.DataItemQueue.TryGetValue(item.TagAddress, out dataItem))
|
|
|
+ {
|
|
|
+ if (float.Parse( item.Value.ToString()) == (float)dataItem.Value)
|
|
|
+ {
|
|
|
+ result.Add(true);
|
|
|
+ dataItem.IsSetSuccessful = true;
|
|
|
+ dataItem.Message = "写入成功!";
|
|
|
+ DataPointCheckList.Add(dataItem);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result.Add(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else if (item.TagAddress.EndsWith(".A_CV"))
|
|
|
+ {
|
|
|
+ DataItem dataItem = null;
|
|
|
+ if (AppUtil.DataItemQueue.TryGetValue(item.TagAddress, out dataItem))
|
|
|
+ {
|
|
|
+ if (item.Value.ToString() == dataItem.Value.ToString())
|
|
|
+ {
|
|
|
+ result.Add(true);
|
|
|
+ dataItem.IsSetSuccessful = true;
|
|
|
+ dataItem.Message = "写入成功!";
|
|
|
+ DataPointCheckList.Add(dataItem);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result.Add(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<DataItem> ReadList(string str) //读取多个标签
|
|
|
+ {
|
|
|
+ //List<string> tags = JsonUtil.DeserializeObject<List<string>>(str);
|
|
|
+ List<DataItem> list = JsonUtil.DeserializeObject<List<DataItem>>(str);
|
|
|
+ List<DataItem> rlt = new List<DataItem>();
|
|
|
+ foreach (var item in list)
|
|
|
+ {
|
|
|
+ if (item.TagAddress.EndsWith(".F_CV"))
|
|
|
+ {
|
|
|
+ rlt.Add(new DataItem()
|
|
|
+ {
|
|
|
+ TagAddress = item.TagAddress,
|
|
|
+ Value = Read<float>(item.TagAddress),
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ else if (item.TagAddress.EndsWith(".A_CV"))
|
|
|
+ {
|
|
|
+ rlt.Add(new DataItem()
|
|
|
+ {
|
|
|
+ TagAddress = item.TagAddress,
|
|
|
+ Value = Read<string>(item.TagAddress),
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ Thread.Sleep(5);
|
|
|
+ }
|
|
|
+ return rlt;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 写入OPC
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="itemName"></param>
|
|
|
+ /// <param name="value"></param>
|
|
|
+ public static void Write(string itemName, object value)
|
|
|
+ {
|
|
|
+ List<DataItem> items = new List<DataItem>();
|
|
|
+ items.Add(new DataItem() { TagAddress = itemName, Value = value });
|
|
|
+ OPCHelper.daServerMgr.Write(items);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 读取RPC
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T"></typeparam>
|
|
|
+ /// <param name="itemName"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static T Read<T>(string itemName)
|
|
|
+ {
|
|
|
+ List<DataItem> items = OPCHelper.daServerMgr.ReadForServer(new string[] { itemName });
|
|
|
+
|
|
|
+ DataItem item = null;
|
|
|
+ if (items == null || items.Count == 0)
|
|
|
+ item = new DataItem();
|
|
|
+ else
|
|
|
+ item = items[0];
|
|
|
+ return (T)item.Value;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|