设备驱动修改

This commit is contained in:
“hsc”
2026-06-10 10:45:51 +08:00
parent b0a7742b8f
commit 5d14afcb66
19 changed files with 764 additions and 122 deletions

View File

@@ -0,0 +1,44 @@
using DeviceCommand.Base;
using Microsoft.AspNetCore.Mvc;
using Model.Model;
[ApiController]
[Route("api/enova")] // 路由可以自己定,定好后把完整的 URL 提供给上位机配置
public class EnovaDataController : ControllerBase
{
/// <summary>
/// 接收下位机 POST 上报的通道数据,并联动分发到所有匹配的 EnovaDataReporter 实例(如 CTS3
/// </summary>
[HttpPost("upload-status")]
public IActionResult ReceiveChannelStatus([FromBody] List<EnovaChannelData> rawDataList)
{
// 1. 基础校验
if (rawDataList == null || rawDataList.Count == 0)
{
return Ok(new ApiResponse
{
Success = false,
ErrorInfo = "接收到的数据为空"
});
}
try
{
// 2. 联动设备:将数据分发到所有已注册的 EnovaDataReporter 实例
// (包括 CTS3 等派生类,由它们自行通过事件处理)
ApiResponse dispatchResult = EnovaDataReporter.Dispatch(rawDataList);
// 3. 严格按照文档 Page 6 的格式返回响应
return Ok(dispatchResult);
}
catch (Exception ex)
{
// 系统内部异常处理
return Ok(new ApiResponse
{
Success = false,
ErrorInfo = $"服务器内部错误: {ex.Message}"
});
}
}
}

47
WebAPI/Program.cs Normal file
View File

@@ -0,0 +1,47 @@
using System.Text.Json;
namespace WebAPI
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// ======= 核心配置开始 =======
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString;
// 3. 属性命名策略保持原样(不强制转为驼峰)
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
// ======= 核心配置结束 =======
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// 注意:如果上位机只支持 http 请求,现场部署时你可能需要根据实际情况注释掉下面这行 Https 重定向
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}

View File

@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:18581",
"sslPort": 44392
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5287",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7240;http://localhost:5287",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

18
WebAPI/WebAPI.csproj Normal file
View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\DeviceCommand\DeviceCommand.csproj" />
</ItemGroup>
</Project>

6
WebAPI/WebAPI.http Normal file
View File

@@ -0,0 +1,6 @@
@WebAPI_HostAddress = http://localhost:5287
GET {{WebAPI_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

9
WebAPI/appsettings.json Normal file
View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}