- 在HttpServer中实现了新的API端点:/api/project/open和/api/model/shrinkwrap - 添加了ShrinkwrapModel和OpenProject命令的处理逻辑 - 在PdmsManager中实现了ShrinkwrapModel和OpenProject方法,支持相应请求的处理 - 更新了项目文件以包含新的命令和模型请求类 此更新增强了插件的功能,允许用户通过API进行模型缩减和项目打开操作。
124 lines
3.6 KiB
C#
124 lines
3.6 KiB
C#
using Aveva.ApplicationFramework;
|
||
using System;
|
||
using System.Windows.Forms;
|
||
using Aveva.ApplicationFramework;
|
||
using TellmePdmsPluging.Network;
|
||
using TellmePdmsPluging.Core;
|
||
|
||
namespace TellmePdmsPluging
|
||
{
|
||
public class TellmePdmsAddin : IAddin
|
||
{
|
||
private HttpServer _httpServer;
|
||
|
||
public string Name
|
||
{
|
||
get
|
||
{
|
||
return "TellmePdmsAddin";
|
||
}
|
||
}
|
||
|
||
public string Description
|
||
{
|
||
get
|
||
{
|
||
return "PDMS远程控制插件";
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 程序打开时,会调用这个方法
|
||
/// </summary>
|
||
/// <param name="serviceManager"></param>
|
||
public void Start(ServiceManager serviceManager)
|
||
{
|
||
try
|
||
{
|
||
// 记录启动日志
|
||
LogMessage("开始启动TellmePdms插件...");
|
||
|
||
SafeQueue.Init();
|
||
|
||
// 启动HTTP服务器
|
||
_httpServer = new HttpServer(9001);
|
||
_httpServer.Start();
|
||
|
||
Application.Idle += OnIdle;
|
||
|
||
// 弹出成功提示
|
||
MessageBox.Show(
|
||
"TellmePdms插件已成功加载!\n" +
|
||
"HTTP服务器已启动,监听端口: 9001\n\n" +
|
||
"测试地址:\n" +
|
||
"- 健康检查: http://localhost:9001/health\n" +
|
||
"- 测试接口: http://localhost:9001/test",
|
||
"插件加载成功",
|
||
MessageBoxButtons.OK,
|
||
MessageBoxIcon.Information);
|
||
|
||
LogMessage("TellmePdms插件启动成功");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
string errorMsg = $"插件启动失败: {ex.Message}";
|
||
LogMessage($"ERROR: {errorMsg}\n{ex.StackTrace}");
|
||
MessageBox.Show(errorMsg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 程序关闭时,会调用这个方法
|
||
/// </summary>
|
||
public void Stop()
|
||
{
|
||
try
|
||
{
|
||
LogMessage("开始停止TellmePdms插件...");
|
||
|
||
Application.Idle -= OnIdle;
|
||
|
||
// 停止HTTP服务器
|
||
if (_httpServer != null)
|
||
{
|
||
_httpServer.Stop();
|
||
_httpServer.Dispose();
|
||
_httpServer = null;
|
||
}
|
||
|
||
LogMessage("TellmePdms插件已停止");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogMessage($"插件停止时出错: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
private void OnIdle(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
while (SafeQueue.TryDequeue(out ICommand cmd))
|
||
{
|
||
cmd.Execute();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogMessage($"Idle执行命令时出错: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
private void LogMessage(string message)
|
||
{
|
||
try
|
||
{
|
||
string logPath = @"C:\temp\pdms_plugin_log.txt";
|
||
string logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}\r\n";
|
||
System.IO.File.AppendAllText(logPath, logEntry);
|
||
}
|
||
catch { }
|
||
}
|
||
}
|
||
}
|