ECGPrint/1200Gxml心电图绘制/服务/ExampleServer.cs

341 lines
14 KiB
C#
Raw Normal View History

2024-12-25 17:24:22 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HTTPServerLib;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using System.Reflection.Emit;
using ;
using Newtonsoft.Json;
using .Properties;
using System.Drawing;
2025-01-08 16:15:00 +08:00
using Newtonsoft.Json.Linq;
2024-12-25 17:24:22 +08:00
namespace _1200Gxml心电图绘制
{
public class ExampleServer : HTTPServerLib.HttpServer
{
private Control _logControl;
ConsoleLogger logger;
print print = new print();
/// <summary>
/// 报告样式类型
/// </summary>
public int reportMode;
/// <summary>
/// 报告样式类型名称
/// </summary>
public string strreportMode = string.Empty;
/// <summary>
/// 走速
/// </summary>
public int cBoxSpeed = 0;
/// <summary>
/// 振幅
/// </summary>
public int cBoxAmplitude = 0;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="ipAddress">IP地址</param>
/// <param name="port">端口号</param>
public ExampleServer(string ipAddress, int port, Control _logControl)
: base(ipAddress, port)
{
logger = new ConsoleLogger(_logControl);
}
public override void OnPost(HttpRequest request, HttpResponse response)
{
//获取客户端传递的参数
// string data = request.Params == null ? "" : string.Join(";", request.Params.Select(x => x.Key + "=" + x.Value).ToArray());
string strMQName = string.Empty;
2025-01-08 16:15:00 +08:00
//LogHelper.Info("接收到请求", "");
2024-12-25 17:24:22 +08:00
string data = "";
try
{
if (request != null && request.Params != null)
{
2025-01-08 16:15:00 +08:00
//注意调整URL顺序
if (request.URL.StartsWith(@"/printecg_image"))
{
JObject paramsObj = null;
string[] array = request.Params.Select(x => x.Value).ToArray();
if (array.Length > 0 && !string.IsNullOrWhiteSpace(array[0]))
try
{
//LogHelper.Info("请求参数", array[0].Trim());
paramsObj = JsonConvert.DeserializeObject<JObject>(array[0].Trim());
}
catch (Exception ex) { paramsObj = null; }
if (paramsObj != null)
{
if (paramsObj.ContainsKey("filepath") &&
!string.IsNullOrWhiteSpace(paramsObj["filepath"] == null ? null : paramsObj["filepath"].ToString().Trim()))
{
try
{
Log("开始处理请求:" + paramsObj["regid"].ToString().Trim());
print.GetEcgViewDataImage_img(paramsObj);
if (paramsObj["type"].ToString().Trim().Equals(@"1"))
{
data = print.imagebase;
}
else
{
data = print.filename;
}
Log("请求处理完成:" + paramsObj["regid"].ToString().Trim());
try
{
if (false)
print.FlySubmit3(new PatientInfo()
{
regid = paramsObj["regid"].ToString().Trim(),
examid = paramsObj["examid"].ToString().Trim(),
diagContent = paramsObj["diagContent"] == null ? "" : paramsObj["diagContent"].ToString().Trim(),
orgid = paramsObj["orgid"] == null ? "" : paramsObj["orgid"].ToString().Trim(),
orgname = paramsObj["orgname"] == null ? "" : paramsObj["orgname"].ToString().Trim(),
doctorName = paramsObj["doctorName"] == null ? "" : paramsObj["doctorName"].ToString().Trim(),
isAbnormal = paramsObj["isAbnormal"] == null ? "" : paramsObj["isAbnormal"].ToString().Trim()
});
}
catch (Exception ex) { }
}
catch (Exception ex) { data = "生成数据文件异常"; }
}
else
data = "心电数据为空";
}
else
data = "解析数据不正确";
}
else if (request.URL.StartsWith(@"/printecg"))
2024-12-25 17:24:22 +08:00
{
string[] array = request.Params.Select(x => x.Value).ToArray();
if (array.Length > 0)
{
2025-01-08 16:15:00 +08:00
//LogHelper.Info("请求参数", array[0]);
2024-12-25 17:24:22 +08:00
PatientInfo patientInfo = JsonConvert.DeserializeObject<PatientInfo>(array[0]);
print.cBoxSpeed = cBoxSpeed;
print.cBoxAmplitude = cBoxAmplitude;
print.strreportMode = strreportMode;
print.reportMode = reportMode;
if (patientInfo != null)
{
2025-01-08 16:15:00 +08:00
if (!string.IsNullOrWhiteSpace(patientInfo.filepath))
2024-12-25 17:24:22 +08:00
{
2025-01-08 16:15:00 +08:00
try
2024-12-25 17:24:22 +08:00
{
2025-01-08 16:15:00 +08:00
Log("开始处理请求:" + patientInfo.regid);
Image image = null;
if (!string.IsNullOrWhiteSpace(patientInfo.image))
{
image = UniversalFun.ConvertFromBase64String(patientInfo.image);
}
string ecgDataJsonStr = UniversalFun.GetTextFileContent(patientInfo.filepath);
print.GetEcgViewDataImage(image, "", patientInfo, patientInfo.type, ecgDataJsonStr);
if (patientInfo.type == 1)
{
data = print.imagebase;
}
else
{
data = print.filename;
}
Log("请求处理完成:" + patientInfo.regid);
try
{
if (false)
{
patientInfo.diagContent = patientInfo.diagContent == null ? "" : patientInfo.diagContent.Trim();
patientInfo.orgid = patientInfo.orgid == null ? "" : patientInfo.orgid.Trim();
patientInfo.orgname = patientInfo.orgname == null ? "" : patientInfo.orgname.Trim();
patientInfo.doctorName = patientInfo.doctorName == null ? "" : patientInfo.doctorName.Trim();
patientInfo.isAbnormal = patientInfo.isAbnormal == null ? "" : patientInfo.isAbnormal.Trim();
print.FlySubmit3(patientInfo);
}
}
catch (Exception ex) { }
2024-12-25 17:24:22 +08:00
}
2025-01-08 16:15:00 +08:00
catch (Exception ex) { data = "生成数据文件异常"; }
2024-12-25 17:24:22 +08:00
}
else
{
data = "心电数据为空";
}
}
else
{
data = "解析数据不正确";
}
}
}
else
{
2025-01-08 16:15:00 +08:00
data = "非指定请求方法";
2024-12-25 17:24:22 +08:00
}
}
else
{
data = "请求参数为空";
}
}
catch (Exception e)
{
2025-01-08 16:15:00 +08:00
//LogHelper.Info("OnPost Error:", e.Message);
2024-12-25 17:24:22 +08:00
}
//设置返回信息
string content = data; //string.Format("<vtma><result>0000</result><desc>Successful!</desc><data><outputXml>{0}</outputXml></data></vtma>", data);
//构造响应报文
response.SetContent(content);
response.Content_Encoding = "utf-8";
response.StatusCode = "200";
response.Content_Type = "application/json; charset=UTF-8";
response.Server = "ExampleServer";
// LogHelper.Info("返回值:" ,content);
//发送响应
response.Send();
}
public override void OnGet(HttpRequest request, HttpResponse response)
{
2025-01-08 16:15:00 +08:00
//LogHelper.Info("请求到接口", "");
2024-12-25 17:24:22 +08:00
//当文件不存在时应返回404状态码
string requestURL = request.URL;
requestURL = requestURL.Replace("/", @"\").Replace("\\..", "").TrimStart('\\');
/*
string requestFile = Path.Combine(ServerRoot, requestURL);
//根据不同业务进行修改
//判断地址中是否存在扩展名
string extension = Path.GetExtension(requestFile);
//根据有无扩展名按照两种不同链接进行处
if (extension != "")
{
//从文件中返回HTTP响应
response = LoadFromFile(response, requestFile);
}
else
{
//目录存在且不存在index页面时时列举目录
if (Directory.Exists(requestFile) && !File.Exists(requestFile + "\\index.html"))
{
requestFile = Path.Combine(ServerRoot, requestFile);
var content = ListDirectory(requestFile, requestURL);
response = response.SetContent(content, Encoding.UTF8);
response.Content_Type = "text/html; charset=UTF-8";
}
else
{
//加载静态HTML页面
requestFile = Path.Combine(requestFile, "index.html");
response = LoadFromFile(response, requestFile);
response.Content_Type = "text/html; charset=UTF-8";
}
}
*/
//发送HTTP响应
StringBuilder sb = new StringBuilder();
sb.Append("接收到接口数据");
response.SetContent(sb.ToString());
response.StatusCode = "200";
response.Content_Type = "text/html";
response.Server = "ExampleServer";
response.Send();
}
public override void OnDefault(HttpRequest request, HttpResponse response)
{
}
private async void GetFilePathUrl(string filepath)
{
}
/// <summary>
/// 从文件返回一个HTTP响应
/// </summary>
/// <param name="fileName">文件名</param>
private HttpResponse LoadFromFile(HttpResponse response, string fileName)
{
//获取文件扩展名以判断内容类型
string extension = Path.GetExtension(fileName);
//获取当前内容类型
string contentType = GetContentType(extension);
//如果文件不存在则返回404否则读取文件内容
if (!File.Exists(fileName))
{
response.SetContent("<html><body><h1>404 - Not Found</h1></body></html>");
response.StatusCode = "404";
response.Content_Type = "text/html";
response.Server = "ExampleServer";
}
else
{
response.SetContent(File.ReadAllBytes(fileName));
response.StatusCode = "200";
response.Content_Type = contentType;
response.Server = "ExampleServer";
}
//返回数据
return response;
}
private string ConvertPath(string[] urls)
{
string html = string.Empty;
int length = ServerRoot.Length;
foreach (var url in urls)
{
var s = url.StartsWith("..") ? url : url.Substring(length).TrimEnd('\\');
html += String.Format("<li><a href=\"{0}\">{0}</a></li>", s);
}
return html;
}
private string ListDirectory(string requestDirectory, string requestURL)
{
//列举子目录
var folders = requestURL.Length > 1 ? new string[] { "../" } : new string[] { };
folders = folders.Concat(Directory.GetDirectories(requestDirectory)).ToArray();
var foldersList = ConvertPath(folders);
//列举文件
var files = Directory.GetFiles(requestDirectory);
var filesList = ConvertPath(files);
//构造HTML
StringBuilder builder = new StringBuilder();
builder.Append(string.Format("<html><head><title>{0}</title></head>", requestDirectory));
builder.Append(string.Format("<body><h1>{0}</h1><br/><ul>{1}{2}</ul></body></html>",
requestURL, filesList, foldersList));
return builder.ToString();
}
}
}