Files
HTCoreServiceApp/HTCoreServiceApp/WebApis/DeviceController.cs

139 lines
5.0 KiB
C#
Raw Normal View History

2023-11-22 14:16:29 +08:00
using HTCoreServiceApp.Common;
using HTCoreServiceApp.DataHandle;
using HTCoreServiceApp.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
//using Newtonsoft.Json;
2023-11-22 14:16:29 +08:00
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using System.Text.Json.Serialization;
2023-11-22 14:16:29 +08:00
using System.Text.RegularExpressions;
2023-11-28 15:34:30 +08:00
namespace HTCoreServiceApp.WebApi
2023-11-22 14:16:29 +08:00
{
2023-11-28 15:34:30 +08:00
//[ApiController]
//[Route("[controller]/[action]")]
public class DeviceControl // : ControllerBase
2023-11-22 14:16:29 +08:00
{
private readonly DriverService _driverService;
private readonly List<MetaGroup> metaGroups;
private readonly List<MetaDriver> metaDrivers;
private readonly List<MetaTag> metaTags;
public DeviceControl(DriverService driverService)
{
_driverService = driverService;
//string ConnectionStrings = AESKeyEncrypt.DecryptDES(ConfigHelper.ReadAppSettings("ConnectionStrings", "SqlServer"));
var db = new SqlSugarClient(StaticLibrary.DBConfig);
metaGroups = db.Queryable<MetaGroup>().ToList(); //db.MetaGroups.ToList();
metaDrivers = db.Queryable<MetaDriver>().ToList(); //db.MetaDrivers.ToList();
metaTags = db.Queryable<MetaTag>().ToList(); //db.MetaTags.ToList();
db.Dispose();
}
2023-11-28 15:34:30 +08:00
//[HttpGet]
public async Task<string> Get() //请求方法行为
2023-11-22 14:16:29 +08:00
{
return "hello这里是恒拓设备实时控制服务";
}
2023-11-28 15:34:30 +08:00
//[HttpGet]
public async Task<string> GetName(string Name) //请求方法行为
2023-11-22 14:16:29 +08:00
{
2023-11-28 15:34:30 +08:00
return $"服务器收到字符串:{Name}";
2023-11-22 14:16:29 +08:00
}
2023-11-28 15:34:30 +08:00
//[HttpGet]
public async Task<string> SetDeviceData(string groupName,string tagName,string value)
2023-11-22 14:16:29 +08:00
{
//var t1 = DateTime.Now;
//var ip = HttpContext.GetClientUserIp();
//var cache = StaticLibrary.MemoryCacheHelper.Get<List<LiveGroupDictionary>>(driverName).Where(x=>x.GroupName == groupName).First();
//var val = new List<LiveGroupDictionary>();
//StaticLibrary.MemoryCacheHelper.TryGetValue(driverName,out val);
//return LiveJsonIntegrate.MetaValues2Json(val.FirstOrDefault());
2023-11-28 15:34:30 +08:00
//var userAgent = HttpContext.
2023-11-22 14:16:29 +08:00
int reCode = 0;
2023-11-22 14:16:29 +08:00
//直接返回,取消后续写入步骤
//return reCode;
try
{
if (!string.IsNullOrEmpty(groupName) && !string.IsNullOrEmpty(tagName) && !string.IsNullOrEmpty(value))
{
//var context = StaticLibrary.DBContext;
//return StaticLibrary.MemoryCacheHelper.GetString(groupName);
var driverID = metaGroups.Where(x => x.GroupName == groupName).FirstOrDefault().DriverId;
var driverName = metaDrivers.Where(x => x.DriverId == driverID).FirstOrDefault().DriverName;
var groupID = metaGroups.Where(x => x.GroupName == groupName).FirstOrDefault().GroupId;
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
if(writeAddress == ""|| writeAddress == null)
{
reCode = 1;
2023-11-28 15:34:30 +08:00
return reCode.ToString();
2023-11-22 14:16:29 +08:00
}
//var windex = writeAddress.IndexOf(".");
//writeAddress = writeAddress.Remove(windex, 1).Insert(windex, ",");
2023-11-22 14:16:29 +08:00
//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)metaTags.Where(x => x.TagName == tagName && x.GroupId == groupID).First().IFloatPlaces;
value = ((int)(float.Parse(value) * (int)Math.Pow(10.0f, iFloatPlaces))).ToString();
}
var writePara = new WritePara
{
2023-11-22 14:16:29 +08:00
TagName = tagName,
GroupName = groupName,
WriteAddress = writeAddress,
DataType = dataType,
Value = value
};
//string serJson = JsonSerializer.Serialize(writePara); //JsonConvert.SerializeObject(writePara);
2023-11-22 14:16:29 +08:00
//var t2 = DateTime.Now;
//var timediff = t2 - t1;
//Console.WriteLine($"写入参数初始化耗时:{timediff}");
2023-11-22 14:16:29 +08:00
foreach (var driverThread in _driverService.DriverThreads)
2023-11-22 14:16:29 +08:00
{
if (driverThread.driverName == driverName)
{
reCode = driverThread.DriverWrite(writePara);
2023-11-22 14:16:29 +08:00
}
}
}
else
{
reCode = 1;
}
}
catch (Exception ex)
{
reCode = 1;
GC.Collect();
2023-11-22 14:16:29 +08:00
}
GC.Collect();
2023-11-28 15:34:30 +08:00
return reCode.ToString();
2023-11-22 14:16:29 +08:00
}
}
}