You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
8.8 KiB
214 lines
8.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Timers;
|
|
using Dicom.Imaging;
|
|
using DicomTool.Model;
|
|
using DicomTool.Utils;
|
|
|
|
namespace DicomTool
|
|
{
|
|
internal class Program
|
|
{
|
|
// 设置循环间隔时间为10分钟
|
|
private const int intervalInMinutes = 60;
|
|
// PACS 共享文件访问
|
|
private const string UserName = "XBDLISUser";
|
|
private const string Password = "BlueFlag.Lis!@#";
|
|
// DCM文件下载存放路径
|
|
private static readonly string DcmPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DicomFiles/");
|
|
private static readonly string EcgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EcgFiles/");
|
|
|
|
private const string ConnectionString = @"Data Source=200.200.200.71;Initial Catalog=peisdb;User ID=sa;Password=wVJeC28@eY*&F#5NGL^eYC3m;";
|
|
|
|
private static void Main(string[] args)
|
|
{
|
|
Console.WriteLine($"【启动】{DateTime.Now:yyyy-MM-dd HH:mm}");
|
|
// 创建一个 Timer 实例
|
|
var timer = new Timer();
|
|
try
|
|
{
|
|
DownEcgFtpImage();
|
|
|
|
Console.ReadKey();
|
|
timer.Interval = intervalInMinutes * 60 * 1000;
|
|
// 设置为 true,使得 Timer 每隔设定的间隔时间自动触发一次 Elapsed 事件
|
|
timer.AutoReset = true;
|
|
// 绑定 Elapsed 事件处理程序
|
|
timer.Elapsed += Timer_Elapsed;
|
|
// 启动 Timer
|
|
timer.Start();
|
|
// 阻止控制台程序退出
|
|
Console.ReadKey();
|
|
// 停止 Timer
|
|
timer.Stop();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
timer.Stop();
|
|
}
|
|
|
|
Console.WriteLine($"【停止】{DateTime.Now:yyyy-MM-dd HH:mm}");
|
|
Console.ReadKey();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 定时执行任务
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
|
|
{
|
|
Console.WriteLine($@"执行-{DateTime.Now:yyyy-MM-dd HH:mm}");
|
|
DownEcgFtpImage();
|
|
}
|
|
|
|
public static void DownEcgFtpImage()
|
|
{
|
|
Console.WriteLine("-----------------------------------");
|
|
Console.WriteLine($"【ECG-START】{DateTime.Now:yyyy-MM-dd HH:mm}");
|
|
var files = DAOHelp.GetDataBySQL<ReportEcg>
|
|
($@"SELECT id,ReportUrl FROM Report_GSEXD WHERE ReportUrl is not null and ReportData is null");
|
|
Console.WriteLine("[ECG-ReportCount]" + files.Count);
|
|
if (files.Count == 0)
|
|
return;
|
|
var name = "TJ_HXECG";
|
|
var password = "123456";
|
|
if (!Directory.Exists(EcgPath))
|
|
{
|
|
Directory.CreateDirectory(EcgPath);
|
|
}
|
|
DownloadFtpFile(files, name, password, EcgPath);
|
|
UploadEcgImg(EcgPath, files.Select(s => s.ID.ToString()).ToList());
|
|
Console.WriteLine("ECG 下载完毕");
|
|
Console.WriteLine($"【ECG-END】{DateTime.Now:yyyy-MM-dd HH:mm}");
|
|
Console.WriteLine("-----------------------------------");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 1、将ecg图片发送到服务器
|
|
/// </summary>
|
|
public static void UploadEcgImg(string folderPath, List<string> idList)
|
|
{
|
|
var i = 0;
|
|
// 建立数据库连接
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
{
|
|
// 遍历文件路径并输出
|
|
foreach (var id in idList)
|
|
{
|
|
var jpgFilePath = Path.Combine(folderPath, id + ".jpg");
|
|
try
|
|
{
|
|
if (!File.Exists(jpgFilePath))
|
|
{
|
|
Console.WriteLine(id + "NotFound" + jpgFilePath);
|
|
continue;
|
|
}
|
|
|
|
// 加载JPG图片
|
|
using (var image = Image.FromFile(jpgFilePath))
|
|
{
|
|
// 创建内存流
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
{
|
|
// 将图像保存到内存流中,指定图像格式为JPEG
|
|
image.Save(memoryStream, ImageFormat.Jpeg);
|
|
// 将内存流中的数据复制到字节数组
|
|
var imgBytes = memoryStream.ToArray();
|
|
//上传
|
|
{
|
|
connection.Open();
|
|
// 创建插入记录的 SQL 查询
|
|
string updateEcgRpt =
|
|
@"update Report_GSEXD set ReportData=@ReportData where id=@ID";
|
|
// 创建命令对象
|
|
using (var command = new SqlCommand(updateEcgRpt, connection))
|
|
{
|
|
// 设置参数值
|
|
command.Parameters.AddWithValue("@ReportData", imgBytes);
|
|
command.Parameters.AddWithValue("@ID", id);
|
|
Console.WriteLine($@"【上传】{id}.jpg");
|
|
// 执行插入操作
|
|
command.ExecuteNonQuery();
|
|
i++;
|
|
}
|
|
|
|
connection.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
File.Delete(jpgFilePath);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(id + "-" + e.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine($@"【上传成功】{i}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 下载ECG图片
|
|
/// </summary>
|
|
/// <param name="ftpUriList"></param>
|
|
/// <param name="username"></param>
|
|
/// <param name="password"></param>
|
|
/// <param name="localFilePath"></param>
|
|
public static void DownloadFtpFile(List<ReportEcg> ftpUriList, string username, string password, string localFilePath)
|
|
{
|
|
// 确保本地文件路径是目录
|
|
if (!Directory.Exists(localFilePath))
|
|
{
|
|
Directory.CreateDirectory(localFilePath);
|
|
}
|
|
|
|
foreach (var reportEcg in ftpUriList)
|
|
{
|
|
// 创建 FtpWebRequest 对象
|
|
try
|
|
{
|
|
var request = (FtpWebRequest)WebRequest.Create(reportEcg.ReportUrl);
|
|
request.Method = WebRequestMethods.Ftp.DownloadFile;
|
|
request.Credentials = new NetworkCredential(username, password);
|
|
// 获取响应并读取数据
|
|
var response = (FtpWebResponse)request.GetResponse();
|
|
var responseStream = response.GetResponseStream();
|
|
|
|
// 获取文件名
|
|
// string fileName = Path.GetFileName(new Uri(reportEcg.ReportUrl).LocalPath);
|
|
var fileName = reportEcg.ID + ".jpg";
|
|
Console.WriteLine("【下载】" + reportEcg.ID + ".jpg");
|
|
var localFileFullPath = Path.Combine(localFilePath, fileName);
|
|
// 将 FTP 文件内容写入本地文件
|
|
using (FileStream fileStream = new FileStream(localFileFullPath, FileMode.Create, FileAccess.Write))
|
|
{
|
|
byte[] buffer = new byte[4096];
|
|
int bytesRead;
|
|
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) != 0)
|
|
{
|
|
fileStream.Write(buffer, 0, bytesRead);
|
|
}
|
|
}
|
|
responseStream.Close();
|
|
response.Close();
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
// 处理异常,例如打印错误信息
|
|
Console.WriteLine("Download failed for " + reportEcg + ": " + ex.Message);
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |