2026-01-25 09:45:38 +08:00

182 lines
6.6 KiB
C#

using Microsoft.AspNetCore.Mvc;
using RIZO.Model.PLC;
using RIZO.Service.PLC.IService;
namespace RIZO.Admin.WebApi.Controllers.Plc
{
[ApiController]
[Route("api/[controller]")]
public class PlcDataController : ControllerBase
{
private readonly IPlcDataService _plcDataService;
private readonly ILogger<PlcDataController> _logger;
public PlcDataController(
IPlcDataService plcDataService,
ILogger<PlcDataController> logger)
{
_plcDataService = plcDataService;
_logger = logger;
}
/// <summary>
/// 获取所有工站数据概览
/// </summary>
[HttpGet]
public ActionResult<AllStationsDataDto> GetAllStationsData()
{
try
{
var allData = _plcDataService.GetAllStationData();
var connectedCount = allData.Count(s => s.Value.IsConnected);
var result = new AllStationsDataDto
{
Stations = allData.ToDictionary(
kvp => kvp.Key,
kvp => new StationDataDto
{
StationName = kvp.Value.StationName,
IsConnected = kvp.Value.IsConnected,
LastUpdateTime = kvp.Value.LastReadTime,
LastConnectTime = kvp.Value.LastConnectTime,
ReadFailureCount = kvp.Value.ReadFailureCount,
CurrentValues = kvp.Value.DataItems.ToDictionary(
di => di.Key,
di => di.Value.Value
)
}
),
ServerTime = DateTime.Now,
TotalStations = allData.Count,
ConnectedStations = connectedCount
};
return Ok(result);
}
catch (Exception ex)
{
_logger.LogError(ex, "获取所有工站数据失败");
return StatusCode(500, new { error = "获取数据失败" });
}
}
/// <summary>
/// 获取指定工站详细数据
/// </summary>
[HttpGet("{stationName}")]
public ActionResult<StationDataDto> GetStationData(string stationName)
{
try
{
var stationData = _plcDataService.GetStationData(stationName);
if (stationData == null)
return NotFound(new { error = $"未找到工位: {stationName}" });
var result = new StationDataDto
{
StationName = stationData.StationName,
IsConnected = stationData.IsConnected,
LastUpdateTime = stationData.LastReadTime,
LastConnectTime = stationData.LastConnectTime,
ReadFailureCount = stationData.ReadFailureCount,
CurrentValues = stationData.DataItems.ToDictionary(
di => di.Key,
di => di.Value.Value
)
};
return Ok(result);
}
catch (Exception ex)
{
_logger.LogError(ex, $"获取工位 {stationName} 数据失败");
return StatusCode(500, new { error = "获取数据失败" });
}
}
/// <summary>
/// 获取指定数据项的详细信息
/// </summary>
[HttpGet("{stationName}/data/{dataItemName}")]
public ActionResult<object> GetDataItemDetail(string stationName, string dataItemName)
{
try
{
var stationData = _plcDataService.GetStationData(stationName);
if (stationData == null)
return NotFound(new { error = $"未找到工位: {stationName}" });
if (!stationData.DataItems.ContainsKey(dataItemName))
return NotFound(new { error = $"未找到数据项: {dataItemName}" });
var dataItem = stationData.DataItems[dataItemName];
return Ok(new
{
Name = dataItem.Name,
Value = dataItem.Value,
LastUpdateTime = dataItem.LastUpdateTime,
IsSuccess = dataItem.IsSuccess,
ErrorMessage = dataItem.ErrorMessage
});
}
catch (Exception ex)
{
_logger.LogError(ex, $"获取工位 {stationName} 数据项 {dataItemName} 失败");
return StatusCode(500, new { error = "获取数据失败" });
}
}
/// <summary>
/// 手动重连指定工站
/// </summary>
[HttpPost("{stationName}/reconnect")]
public async Task<IActionResult> ReconnectStation(string stationName)
{
try
{
var success = await _plcDataService.ReconnectStationAsync(stationName);
if (success)
return Ok(new { message = $"工位 {stationName} 重连成功" });
else
return BadRequest(new { error = $"工位 {stationName} 重连失败" });
}
catch (Exception ex)
{
_logger.LogError(ex, $"重连工位 {stationName} 失败");
return StatusCode(500, new { error = "重连失败" });
}
}
/// <summary>
/// 写入数据到PLC
/// </summary>
[HttpPost("{stationName}/write")]
public async Task<IActionResult> WriteData(string stationName, [FromBody] WriteDataRequest request)
{
try
{
if (string.IsNullOrEmpty(request.DataItemName))
return BadRequest(new { error = "数据项名称不能为空" });
var success = await _plcDataService.WriteDataAsync(stationName, request.DataItemName, request.Value);
if (success)
return Ok(new { message = "写入成功" });
else
return BadRequest(new { error = "写入失败" });
}
catch (Exception ex)
{
_logger.LogError(ex, $"写入工位 {stationName} 数据失败");
return StatusCode(500, new { error = "写入失败" });
}
}
}
public class WriteDataRequest
{
public string DataItemName { get; set; } = string.Empty;
public object Value { get; set; } = new();
}
}