From d9700aea015e20e76d6a736b0a291cb855f382dc Mon Sep 17 00:00:00 2001 From: dell Date: Fri, 29 Aug 2025 17:17:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BF=83=E8=B7=B3=E4=B8=93?= =?UTF-8?q?=E7=94=A8=E5=86=99=E5=85=A5=E6=96=B9=E6=B3=95=EF=BC=8C=E9=98=B2?= =?UTF-8?q?=E5=BE=A1=E6=80=A7=E6=9B=B4=E6=94=B9=E4=B8=8D=E5=81=9Adebug?= =?UTF-8?q?=EF=BC=8C=E6=9C=89=E9=97=AE=E9=A2=98=E5=9B=9E=E9=80=80=E6=AD=A4?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HTCoreServiceApp/Program.cs | 4 + HTCoreServiceApp/WebApis/DeviceController.cs | 113 +++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/HTCoreServiceApp/Program.cs b/HTCoreServiceApp/Program.cs index 02c5d74..32e2f72 100644 --- a/HTCoreServiceApp/Program.cs +++ b/HTCoreServiceApp/Program.cs @@ -151,6 +151,10 @@ namespace HTCoreServiceApp async (string deviceCode, string plcTag, string value, HttpContext context, DeviceControl dc) => Results.Content(await dc.SetDevicePlcTagData(deviceCode, plcTag, value, context))); + deviceControl.MapGet("/SetDevicePlcTagData/deviceCode={deviceCode}&plcTag={plcTag}&value={value}", + async (string deviceCode, string plcTag, string value, HttpContext context, DeviceControl dc) + => Results.Content(await dc.SetDevicePlcTagDataHeartbeat(deviceCode, plcTag, value, context))); + deviceControl.MapGet("/SetDeviceBlockData/driverName={driverName}&address={address}&value={value}", async (string driverName, string address, string value, DeviceControl dc) => Results.Content(await dc.SetDeviceBlockData(driverName, address, value))); diff --git a/HTCoreServiceApp/WebApis/DeviceController.cs b/HTCoreServiceApp/WebApis/DeviceController.cs index bb8c571..7e001a2 100644 --- a/HTCoreServiceApp/WebApis/DeviceController.cs +++ b/HTCoreServiceApp/WebApis/DeviceController.cs @@ -355,6 +355,119 @@ namespace HTCoreServiceApp.WebApi Console.WriteLine($"写入耗时:{timediff}"); return reCode.ToString(); } + + public async Task SetDevicePlcTagDataHeartbeat(string deviceCode, string plcTag, string value, HttpContext context) + { + var time1 = DateTime.Now; + //var htsetlog = new HTLogSetDriver(); + //htsetlog.EventTime = DateTime.Now; + //var t1 = DateTime.Now; + //htsetlog.RequestIp = context.Connection.RemoteIpAddress.ToString();// "192.168.110.247"; + //var cache = StaticLibrary.MemoryCacheHelper.Get>(driverName).Where(x=>x.GroupName == groupName).First(); + //var val = new List(); + //StaticLibrary.MemoryCacheHelper.TryGetValue(driverName,out val); + //return LiveJsonIntegrate.MetaValues2Json(val.FirstOrDefault()); + + //var userAgent = HttpContext. + + int reCode = 0; + //直接返回,取消后续写入步骤 + //return reCode; + try + { + var tagName = metaTags.Where(x => x.PLCAddress == $"{deviceCode}_{plcTag}").FirstOrDefault().TagName; + if (!string.IsNullOrEmpty(tagName) && !string.IsNullOrEmpty(value)) + { + //var context = StaticLibrary.DBContext; + //return StaticLibrary.MemoryCacheHelper.GetString(groupName); + //var dataType = metaTags.Where(x => x.TagName == tagName && x.GroupId == groupID).First().DataType; + //var writeAddress = metaTags.Where(x => x.TagName == tagName && x.GroupId == groupID).First().PLCAddress;//231113 ,CDL QWZ 写入地址字段由WriteAddress修改为PLCAddress + var tag = metaTags.Where(x => x.TagName == tagName).First(); + var dataType = tag.DataType; + var groupID = tag.GroupId; + var driverID = metaGroups.Where(x => x.GroupId == groupID).FirstOrDefault().DriverId; + var driverName = metaDrivers.Where(x => x.DriverId == driverID).FirstOrDefault().DriverName; + var writeAddress = tag.WriteAddress;//231113 ,CDL QWZ 写入地址字段由WriteAddress修改为PLCAddress + //htsetlog.WriteDriverName = driverName; + //htsetlog.WriteTagName = tag.TagName; + //htsetlog.WriteAddress = writeAddress; + //htsetlog.WriteValue = value; + + if (writeAddress == "" || writeAddress == null) + { + reCode = 3; + //htsetlog.WriteResult = reCode.ToString(); + //LogHelper.SetDriverLogTask(htsetlog); + return reCode.ToString(); + } + //var windex = writeAddress.IndexOf("."); + //writeAddress = writeAddress.Remove(windex, 1).Insert(windex, ","); + //var valueHandle = 0; + if (dataType == (short)DataType.BOOL) + { + if (value.Equals("True") || value.Equals("true") || value.Equals("TRUE") || value.Equals("1")) + { + value = "1"; + } + else if (value.Equals("False") || value.Equals("false") || value.Equals("FALSE") || value.Equals("0")) + { + value = "0"; + } + } + if (dataType == (short)DataType.IFLOAT) + { + var iFloatPlaces = (int)tag.IFloatPlaces; + value = ((int)(float.Parse(value) * (int)Math.Pow(10.0f, iFloatPlaces))).ToString(); + } + var writePara = new WritePara + { + TagName = tagName, + //GroupName = groupName, + WriteAddress = writeAddress, + DataType = dataType, + Value = value + }; + //string serJson = JsonSerializer.Serialize(writePara); //JsonConvert.SerializeObject(writePara); + + //var t2 = DateTime.Now; + //var timediff = t2 - t1; + //Console.WriteLine($"写入参数初始化耗时:{timediff}"); + + foreach (var driverThread in _driverService.DriverThreads) + { + if (driverThread.driverName == driverName) + { + reCode = driverThread.DriverWrite(writePara); + //htsetlog.WriteResult = reCode.ToString(); + } + } + } + else + { + reCode = 2; + //htsetlog.WriteResult = reCode.ToString(); + } + } + catch (Exception ex) + { + reCode = 1; + //htsetlog.WriteResult = reCode.ToString(); + //htsetlog.WriteErrInformation = ex.ToString(); + + //LogHelper.SetDriverLogTask(htsetlog); + + GC.Collect(); + LogHelper.LogWrite("S7写入数据出错,PLCAddresss:" + deviceCode.ToString() + "_" + plcTag.ToString() + ",ERROR:" + ex.ToString()); + return reCode.ToString() + "/t" + ex.ToString(); + } + //LogHelper.SetDriverLogTask(htsetlog); + GC.Collect(); + var time2 = DateTime.Now; + var timediff = time2 - time1; + Console.WriteLine($"写入耗时:{timediff}"); + return reCode.ToString(); + } + public async Task SetDevicePlcTagDataPost(string deviceCode, string plcTag, string value, HttpContext context) { var time1 = DateTime.Now;