174 lines
6.0 KiB
C#
174 lines
6.0 KiB
C#
|
|
using RIZO.Admin.WebApi.Filters;
|
|||
|
|
using RIZO.Common;
|
|||
|
|
using RIZO.Admin.WebApi.PLC;
|
|||
|
|
using Infrastructure;
|
|||
|
|
using Infrastructure.Attribute;
|
|||
|
|
using Infrastructure.Controllers;
|
|||
|
|
using Infrastructure.Enums;
|
|||
|
|
using Infrastructure.Model;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
|
|||
|
|
// 创建时间:2026-01-16
|
|||
|
|
namespace RIZO.Admin.WebApi.Controllers
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// PLC通讯测试接口
|
|||
|
|
/// </summary>
|
|||
|
|
[AllowAnonymous]
|
|||
|
|
[Route("PLC")]
|
|||
|
|
public class PlcController : BaseController
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// PLC通讯服务
|
|||
|
|
/// </summary>
|
|||
|
|
private readonly PlcService _plcService;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 构造函数注入PLC服务
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="plcService">PLC通讯服务实例</param>
|
|||
|
|
public PlcController(PlcService plcService)
|
|||
|
|
{
|
|||
|
|
_plcService = plcService;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 批量测试所有PLC的连接、读写功能(核心接口)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>批量测试结果</returns>
|
|||
|
|
[HttpPost("batch-test")]
|
|||
|
|
[ActionPermissionFilter(Permission = "business:plc:batchtest")]
|
|||
|
|
[Log(Title = "PLC测试", BusinessType = BusinessType.OTHER)]
|
|||
|
|
public async Task<IActionResult> BatchTestAllPlc()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var results = await _plcService.BatchTestAllPlcAsync();
|
|||
|
|
return SUCCESS(results);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error($"批量测试失败:{ex.Message}"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 测试单个PLC的连接、读写功能(调试用)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="config">单个PLC配置参数</param>
|
|||
|
|
/// <returns>单PLC测试结果</returns>
|
|||
|
|
[HttpPost("single-test")]
|
|||
|
|
[ActionPermissionFilter(Permission = "business:plc:singletest")]
|
|||
|
|
[Log(Title = "PLC测试", BusinessType = BusinessType.OTHER)]
|
|||
|
|
public async Task<IActionResult> TestSinglePlc([FromBody] PlcConfig config)
|
|||
|
|
{
|
|||
|
|
if (config == null || string.IsNullOrEmpty(config.Ip))
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error("PLC配置不能为空,IP地址为必填项"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var result = await _plcService.TestSinglePlcAsync(config);
|
|||
|
|
return SUCCESS(result);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error($"单PLC测试失败:{ex.Message}"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 单独读取指定PLC的地址数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="ip">PLC IP地址</param>
|
|||
|
|
/// <param name="address">读取地址(如DB1.DBD0)</param>
|
|||
|
|
/// <param name="rack">机架号(默认0)</param>
|
|||
|
|
/// <param name="slot">槽位号(默认1)</param>
|
|||
|
|
/// <returns>读取结果</returns>
|
|||
|
|
[HttpGet("read")]
|
|||
|
|
[ActionPermissionFilter(Permission = "business:plc:read")]
|
|||
|
|
public async Task<IActionResult> ReadPlcData(
|
|||
|
|
[FromQuery] string ip,
|
|||
|
|
[FromQuery] string address,
|
|||
|
|
[FromQuery] short rack = 0,
|
|||
|
|
[FromQuery] short slot = 1)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(ip))
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error("PLC IP地址不能为空"));
|
|||
|
|
}
|
|||
|
|
if (string.IsNullOrEmpty(address))
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error("PLC读取地址不能为空"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var (success, value, message) = await _plcService.ReadPlcDataAsync(ip, rack, slot, address);
|
|||
|
|
if (success)
|
|||
|
|
{
|
|||
|
|
return SUCCESS(new { Value = value, Message = message });
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error(message));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error($"读取PLC数据失败:{ex.Message}"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 单独写入数据到指定PLC地址
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="ip">PLC IP地址</param>
|
|||
|
|
/// <param name="address">写入地址(如DB1.DBD0)</param>
|
|||
|
|
/// <param name="value">写入值(字符串格式)</param>
|
|||
|
|
/// <param name="rack">机架号(默认0)</param>
|
|||
|
|
/// <param name="slot">槽位号(默认1)</param>
|
|||
|
|
/// <returns>写入结果</returns>
|
|||
|
|
[HttpPost("write")]
|
|||
|
|
[ActionPermissionFilter(Permission = "business:plc:write")]
|
|||
|
|
[Log(Title = "PLC数据写入", BusinessType = BusinessType.UPDATE)]
|
|||
|
|
public async Task<IActionResult> WritePlcData(
|
|||
|
|
[FromQuery] string ip,
|
|||
|
|
[FromQuery] string address,
|
|||
|
|
[FromQuery] string value,
|
|||
|
|
[FromQuery] short rack = 0,
|
|||
|
|
[FromQuery] short slot = 1)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(ip))
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error("PLC IP地址不能为空"));
|
|||
|
|
}
|
|||
|
|
if (string.IsNullOrEmpty(address))
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error("PLC写入地址不能为空"));
|
|||
|
|
}
|
|||
|
|
if (string.IsNullOrEmpty(value))
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error("PLC写入值不能为空"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var (success, message) = await _plcService.WritePlcDataAsync(ip, rack, slot, address, value);
|
|||
|
|
if (success)
|
|||
|
|
{
|
|||
|
|
return SUCCESS(message);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error(message));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return ToResponse(ApiResult.Error($"写入PLC数据失败:{ex.Message}"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|