using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using XP.Common.Logging.Interfaces;
using XP.Scan.Models;
namespace XP.Scan.Services
{
///
/// 重建通知服务 | Reconstruction notification service
/// 通过 TCP Socket 向本地重建软件发送配置路径 + 重建模式码
///
public class ReconstructionNotifyService : IReconstructionNotifyService
{
private readonly ILoggerService _logger;
private readonly string _host;
private readonly int _port;
private readonly ReconstructionMode _mode;
///
/// 构造函数
///
/// 日志服务
/// 重建软件 IP 地址(本机默认 127.0.0.1)
/// 重建软件监听端口
/// CT 重建模式(Planar=2001, Vertical=2002)
public ReconstructionNotifyService(
ILoggerService loggerService,
string host = "127.0.0.1",
int port = 11000,
ReconstructionMode mode = ReconstructionMode.Planar)
{
_logger = loggerService.ForModule();
_host = host;
_port = port;
_mode = mode;
}
///
/// 发送重建通知消息
/// 消息格式: configPath + 模式码(2001=平面CT, 2002=立式CT)
///
/// 扫描配置文件路径
public void SendReconstructionNotify(string configPath)
{
try
{
using var sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var ipAddress = IPAddress.Parse(_host);
var remoteEP = new IPEndPoint(ipAddress, _port);
sender.Connect(remoteEP);
// 拼接消息: 配置路径 + 重建模式码
string message = configPath + ((int)_mode).ToString();
byte[] msg = Encoding.UTF8.GetBytes(message);
sender.Send(msg);
_logger.Info($"重建通知已发送 | Reconstruction notify sent: {message}");
}
catch (SocketException ex)
{
_logger.Error(ex, "重建通知发送失败(Socket) | Reconstruction notify failed: {0}", ex.Message);
}
catch (Exception ex)
{
_logger.Error(ex, "重建通知发送异常 | Reconstruction notify error: {0}", ex.Message);
}
}
}
}