73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// 重建通知服务 | Reconstruction notification service
|
||
/// 通过 TCP Socket 向本地重建软件发送配置路径 + 重建模式码
|
||
/// </summary>
|
||
public class ReconstructionNotifyService : IReconstructionNotifyService
|
||
{
|
||
private readonly ILoggerService _logger;
|
||
private readonly string _host;
|
||
private readonly int _port;
|
||
private readonly ReconstructionMode _mode;
|
||
|
||
/// <summary>
|
||
/// 构造函数
|
||
/// </summary>
|
||
/// <param name="loggerService">日志服务</param>
|
||
/// <param name="host">重建软件 IP 地址(本机默认 127.0.0.1)</param>
|
||
/// <param name="port">重建软件监听端口</param>
|
||
/// <param name="mode">CT 重建模式(Planar=2001, Vertical=2002)</param>
|
||
public ReconstructionNotifyService(
|
||
ILoggerService loggerService,
|
||
string host = "127.0.0.1",
|
||
int port = 11000,
|
||
ReconstructionMode mode = ReconstructionMode.Planar)
|
||
{
|
||
_logger = loggerService.ForModule<ReconstructionNotifyService>();
|
||
_host = host;
|
||
_port = port;
|
||
_mode = mode;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发送重建通知消息
|
||
/// 消息格式: configPath + 模式码(2001=平面CT, 2002=立式CT)
|
||
/// </summary>
|
||
/// <param name="configPath">扫描配置文件路径</param>
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
}
|