Api修改

This commit is contained in:
dell
2023-11-28 15:34:30 +08:00
parent 13ed7c7ae5
commit 157d56a3fc
14 changed files with 138 additions and 47 deletions

View File

@ -10,7 +10,7 @@ using System.Collections;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Reflection;
using HTCoreServiceApp.Controllers;
using HTCoreServiceApp.WebApi;
namespace HTCoreServiceApp.DataHandle
{

View File

@ -10,7 +10,7 @@ using System.Collections;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Reflection;
using HTCoreServiceApp.Controllers;
using HTCoreServiceApp.WebApi;
using HTCoreServiceApp.Communicate.AllenBradleyEIP;
namespace HTCoreServiceApp.DataHandle

View File

@ -10,7 +10,7 @@ using System.Collections;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Reflection;
using HTCoreServiceApp.Controllers;
using HTCoreServiceApp.WebApi;
namespace HTCoreServiceApp.DataHandle
{

View File

@ -10,7 +10,7 @@ using System.Collections;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Reflection;
using HTCoreServiceApp.Controllers;
using HTCoreServiceApp.WebApi;
namespace HTCoreServiceApp.DataHandle
{

View File

@ -10,7 +10,7 @@ using System.Collections;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Reflection;
using HTCoreServiceApp.Controllers;
using HTCoreServiceApp.WebApi;
using HTCoreServiceApp.Communicate.AllenBradleyEIP;
namespace HTCoreServiceApp.DataHandle

View File

@ -18,6 +18,7 @@
</ItemGroup>
<ItemGroup>
<Compile Remove="WebApis\ValuesController.cs" />
<Compile Remove="DataHandle\DataExtractOpc.cs" />
<Compile Remove="Models\HT_Service_Log.cs" />
<Compile Remove="Models\LogAlarm.cs" />

View File

@ -1,11 +1,47 @@
@HTCoreServiceApp_HostAddress = http://localhost:5162
@HTCoreServiceApp_HostAddress = http://localhost:8040
GET {{HTCoreServiceApp_HostAddress}}/todos/
Accept: application/json
###
GET {{HTCoreServiceApp_HostAddress}}/todos/1
GET {{HTCoreServiceApp_HostAddress}}/todos/2
Accept: application/json
###
GET {{HTCoreServiceApp_HostAddress}}/DataLiveCache/Get
Accept: application/json
###
GET {{HTCoreServiceApp_HostAddress}}/DataLiveCache/1
Accept: application/json
###
GET {{HTCoreServiceApp_HostAddress}}/DataLiveCache/GetLiveGroupData/groupName=MW3200
Accept: application/json
###
GET {{HTCoreServiceApp_HostAddress}}/DataLiveCache/GetLiveTagData/groupName=MW3200&tagName=T2_PT_PV
Accept: application/json
###
GET {{HTCoreServiceApp_HostAddress}}/DeviceControl/Get
Accept: application/json
###
GET {{HTCoreServiceApp_HostAddress}}/DeviceControl/1
Accept: application/json
###
GET {{HTCoreServiceApp_HostAddress}}/DeviceControl/SetDeviceData/groupName=MW3200&tagName=T2_PT_PV&value=88.0
Accept: application/json
###

View File

@ -1,4 +1,7 @@
using HTCoreServiceApp.Common;
using HTCoreServiceApp.WebApi;
using Microsoft.AspNetCore.Http.Headers;
using Microsoft.AspNetCore.Http.HttpResults;
using SqlSugar;
using System.Text.Json.Serialization;
@ -35,10 +38,45 @@ namespace HTCoreServiceApp
options.InstanceName = "test";
});
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddEndpointsApiExplorer();
builder.Services.AddScoped<DataLiveCache>();
builder.Services.AddScoped<DeviceControl>();
var app = builder.Build();
var dataLiveCacheApi = app.MapGroup("/DataLiveCache");
var deviceControl = app.MapGroup("/DeviceControl");
dataLiveCacheApi.MapGet("/Get",
async(DataLiveCache dlc)
=> Results.Content(await dlc.Get()));
dataLiveCacheApi.MapGet("/{Name}",
async (string Name, DataLiveCache dlc)
=> Results.Content(await dlc.GetName(Name)));
//tagName
dataLiveCacheApi.MapGet("/GetLiveGroupData/groupName={groupName}",
async (string groupName, DataLiveCache dlc)
=> Results.Content(await dlc.GetLiveGroupData(groupName)));
dataLiveCacheApi.MapGet("/GetLiveTagData/groupName={groupName}&tagName={tagName}",
async (string groupName, string tagName, DataLiveCache dlc)
=> Results.Content(await dlc.GetLiveTagData(groupName,tagName)));
deviceControl.MapGet("/Get",
async (DeviceControl dc)
=> Results.Content(await dc.Get()));
deviceControl.MapGet("/{Name}",
async (string Name, DeviceControl dc)
=> Results.Content(await dc.GetName(Name)));
deviceControl.MapGet("/SetDeviceData/groupName={groupName}&tagName={tagName}&value={value}",
async (string groupName, string tagName ,string value,DeviceControl dc)
=> Results.Content(await dc.SetDeviceData(groupName, tagName, value)));
var sampleTodos = new Todo[] {
new(1, "Walk the dog"),
new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)),

View File

@ -8,7 +8,7 @@
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5162"
"applicationUrl": "http://localhost:8040"
},
"Docker": {
"commandName": "Docker",

View File

@ -3,39 +3,48 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
//using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
namespace HTCoreServiceApp.Controllers
namespace HTCoreServiceApp.WebApi
{
[ApiController]
[Route("[controller]/[action]")]
public class DataLiveCache : ControllerBase
//[Route("[controller]/[action]")]
public class DataLiveCache //: ControllerBase
{
[HttpGet]
public string Get() //请求方法行为
//[HttpGet]
public async Task<string?> Get() //请求方法行为
{
return "hello这里是恒拓实时服务";
}
[HttpGet]
public string Getname(string name) //请求方法行为
//[HttpGet]
public async Task<string> GetName(string Name) //请求方法行为
{
return $"服务器收到字符串:{name}";
return $"服务器收到字符串:{Name}";
}
[HttpGet]
public string GetLiveGroupData(string groupName)
//[HttpGet]
public async Task<string> GetLiveGroupData(string groupName)
{
//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());
//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());
try
//public static string StringToJson(string JsonString)
//{ // string 转换成Json JsonDocument jsonDocument = JsonDocument.Parse(JsonString);//这里序列化成json return JsonSerializer.Serialize(jsonDocument.RootElement); }//在main中调用
try
{
//var result = StaticLibrary.MemoryCacheHelper.GetString(groupName);
//Console.WriteLine(result);
//return result;
//return valueJson.ToString();
return StaticLibrary.MemoryCacheHelper.GetString(groupName);
}
catch (Exception ex)
@ -43,13 +52,17 @@ namespace HTCoreServiceApp.Controllers
return ex.Message;
}
}
[HttpGet]
public string GetLiveTagData(string groupName, string tagName)
//[HttpGet]
public async Task<string> GetLiveTagData(string groupName, string tagName)
{
//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());
//!!!!重要校验有无groupName
string value = "";
if (tagName != null && tagName != "")
{

View File

@ -16,11 +16,11 @@ using System.Linq.Expressions;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
namespace HTCoreServiceApp.Controllers
namespace HTCoreServiceApp.WebApi
{
[ApiController]
[Route("[controller]/[action]")]
public class DeviceControl : ControllerBase
//[ApiController]
//[Route("[controller]/[action]")]
public class DeviceControl // : ControllerBase
{
private readonly DriverService _driverService;
private readonly List<MetaGroup> metaGroups;
@ -36,25 +36,28 @@ namespace HTCoreServiceApp.Controllers
metaTags = db.Queryable<MetaTag>().ToList(); //db.MetaTags.ToList();
db.Dispose();
}
[HttpGet]
public string Get() //请求方法行为
//[HttpGet]
public async Task<string> Get() //请求方法行为
{
return "hello这里是恒拓设备实时控制服务";
}
[HttpGet]
public string Getname(string name) //请求方法行为
//[HttpGet]
public async Task<string> GetName(string Name) //请求方法行为
{
return $"服务器收到字符串:{name}";
return $"服务器收到字符串:{Name}";
}
[HttpGet]
public int SetDeviceData(string groupName,string tagName,string value)
//[HttpGet]
public async Task<string> SetDeviceData(string groupName,string tagName,string value)
{
var ip = HttpContext.GetClientUserIp();
//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());
//var userAgent = HttpContext.
int reCode = 0;
//直接返回,取消后续写入步骤
@ -73,7 +76,7 @@ namespace HTCoreServiceApp.Controllers
if(writeAddress == ""|| writeAddress == null)
{
reCode = 1;
return reCode;
return reCode.ToString();
}
var windex = writeAddress.IndexOf(".");
writeAddress = writeAddress.Remove(windex, 1).Insert(windex, ",");
@ -122,7 +125,7 @@ namespace HTCoreServiceApp.Controllers
{
reCode = 1;
}
return reCode;
return reCode.ToString();
}
}
}

View File

@ -3,7 +3,7 @@ using System.Linq;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Collections.Generic;
namespace HTCoreServiceApp.Controllers
namespace HTCoreServiceApp.WebApi
{
public static class Extension
{

View File

@ -3,16 +3,15 @@ using Microsoft.AspNetCore.Mvc;
namespace HTCoreServiceApp.Controllers
{
[ApiController]
[Route("[controller]/[action]")]
public class ValuesController : ControllerBase
{
[HttpGet]
public string Get() //请求方法行为
{
return "hello这里是恒拓实时服务测试";
}
[HttpGet]
public string Getname(string name) //请求方法行为
{
return $"服务器收到字符串:{name}";

View File

@ -1 +1,2 @@
# HTCoreServiceApp
# HTCoreServiceApp
V8