refactor: 插件窗体由 static 单例状态改为实例字段(对齐 TextViewer)
将 10 个 CycleGUI 窗体/管理器从进程级 static 单例状态改为实例字段 + 实例方法, 消除本地端/Web 端共享同一可变状态的隐患(对齐 TextViewer 正例)。 每个类保留一个 private static _instance 单实例持有者;静态入口 Open()/OpenViewer() 与实例 Show() 均转发到该实例,调用方零改动、单实例“置前”行为不变。 涉及:ChargeStationManagementForm、ChargeStrategyConfigForm、CommunicationMonitorForm、 AlarmConfigManagementForm、ButtonBoxManager、DoorManager、DoorMonitor、 LoopViewer、DeliveryViewer、TrafficInterlockViewer。 构建:dotnet build StandardScene.sln --no-incremental → 0 错误,30 警告(与基线一致,无新增)。
This commit is contained in:
@@ -17,32 +17,36 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
public class ButtonBoxManager
|
||||
{
|
||||
private const string DataFileName = "ButtonBoxConfig.json";
|
||||
private static string DataFilePath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DataFileName);
|
||||
private static readonly object SaveLock = new object();
|
||||
private string DataFilePath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DataFileName);
|
||||
private readonly object SaveLock = new object();
|
||||
|
||||
private static Panel _panel;
|
||||
private static List<ButtonBoxModel> _boxes = new List<ButtonBoxModel>();
|
||||
private static int _selectedBoxIdx = -1;
|
||||
private static int _selectedBtnIdx = -1;
|
||||
private static string _status = "";
|
||||
private Panel _panel;
|
||||
private List<ButtonBoxModel> _boxes = new List<ButtonBoxModel>();
|
||||
private int _selectedBoxIdx = -1;
|
||||
private int _selectedBtnIdx = -1;
|
||||
private string _status = "";
|
||||
|
||||
private static string _boxIp = "";
|
||||
private static string _boxPort = "502";
|
||||
private static string _boxIndex = "";
|
||||
private static int _typeIdx;
|
||||
private static string[] _typeNames = Array.Empty<string>();
|
||||
private string _boxIp = "";
|
||||
private string _boxPort = "502";
|
||||
private string _boxIndex = "";
|
||||
private int _typeIdx;
|
||||
private string[] _typeNames = Array.Empty<string>();
|
||||
|
||||
private static string _btnIndex = "";
|
||||
private static string _triggerMission = "";
|
||||
private static string _triggerMethod = "";
|
||||
private static string _triggerParams = "";
|
||||
private static string _triggerDelay = "0";
|
||||
private static int _triggerStateIdx;
|
||||
private static readonly string[] _triggerStateNames = Enum.GetNames(typeof(ButtonState));
|
||||
private string _btnIndex = "";
|
||||
private string _triggerMission = "";
|
||||
private string _triggerMethod = "";
|
||||
private string _triggerParams = "";
|
||||
private string _triggerDelay = "0";
|
||||
private int _triggerStateIdx;
|
||||
private readonly string[] _triggerStateNames = Enum.GetNames(typeof(ButtonState));
|
||||
|
||||
private static ButtonBoxManager _instance;
|
||||
|
||||
public static void OpenViewer() => Open();
|
||||
|
||||
public static void Open()
|
||||
public static void Open() => (_instance ??= new ButtonBoxManager()).OpenCore();
|
||||
|
||||
private void OpenCore()
|
||||
{
|
||||
if (_panel != null)
|
||||
{
|
||||
@@ -155,17 +159,17 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
});
|
||||
}
|
||||
|
||||
private static ButtonBoxModel GetSelectedBox() =>
|
||||
private ButtonBoxModel GetSelectedBox() =>
|
||||
_selectedBoxIdx >= 0 && _selectedBoxIdx < _boxes.Count ? _boxes[_selectedBoxIdx] : null;
|
||||
|
||||
private static ButtonModel GetSelectedButton()
|
||||
private ButtonModel GetSelectedButton()
|
||||
{
|
||||
var box = GetSelectedBox();
|
||||
return box != null && _selectedBtnIdx >= 0 && _selectedBtnIdx < box.Buttons.Count
|
||||
? box.Buttons[_selectedBtnIdx] : null;
|
||||
}
|
||||
|
||||
private static string[] DiscoverTypes()
|
||||
private string[] DiscoverTypes()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -184,7 +188,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
}
|
||||
}
|
||||
|
||||
private static void LoadData()
|
||||
private void LoadData()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -201,7 +205,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
}
|
||||
}
|
||||
|
||||
private static void SaveData()
|
||||
private void SaveData()
|
||||
{
|
||||
var json = _boxes.ToJson();
|
||||
var path = DataFilePath;
|
||||
@@ -220,7 +224,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
});
|
||||
}
|
||||
|
||||
private static void LoadBoxFields(ButtonBoxModel b)
|
||||
private void LoadBoxFields(ButtonBoxModel b)
|
||||
{
|
||||
_boxIp = b.Ip;
|
||||
_boxPort = b.Port.ToString();
|
||||
@@ -229,7 +233,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
if (_typeIdx < 0) _typeIdx = 0;
|
||||
}
|
||||
|
||||
private static void ClearBoxFields()
|
||||
private void ClearBoxFields()
|
||||
{
|
||||
_boxIp = "";
|
||||
_boxPort = "502";
|
||||
@@ -237,7 +241,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
_typeIdx = 0;
|
||||
}
|
||||
|
||||
private static void LoadButtonFields(ButtonModel b)
|
||||
private void LoadButtonFields(ButtonModel b)
|
||||
{
|
||||
_btnIndex = b.Index.ToString();
|
||||
_triggerMission = b.TriggerMission ?? "";
|
||||
@@ -247,7 +251,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
_triggerStateIdx = Math.Max(0, Array.IndexOf(_triggerStateNames, b.TriggerState));
|
||||
}
|
||||
|
||||
private static void ClearButtonFields()
|
||||
private void ClearButtonFields()
|
||||
{
|
||||
_btnIndex = "";
|
||||
_triggerMission = "";
|
||||
@@ -257,7 +261,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
_triggerStateIdx = 0;
|
||||
}
|
||||
|
||||
private static void AddBox()
|
||||
private void AddBox()
|
||||
{
|
||||
if (!TryParseBoxInput(out var index, out var ip, out var port, out var type, false, out var err))
|
||||
{ CycleUiHelper.Alert("错误", err); return; }
|
||||
@@ -272,7 +276,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
_panel?.Repaint();
|
||||
}
|
||||
|
||||
private static void SaveBox()
|
||||
private void SaveBox()
|
||||
{
|
||||
var cur = GetSelectedBox();
|
||||
if (cur == null) { CycleUiHelper.Alert("提示", "请先选择按钮盒"); return; }
|
||||
@@ -287,7 +291,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
_panel?.Repaint();
|
||||
}
|
||||
|
||||
private static void ConfirmDeleteBox()
|
||||
private void ConfirmDeleteBox()
|
||||
{
|
||||
var cur = GetSelectedBox();
|
||||
if (cur == null) { CycleUiHelper.Alert("提示", "请选择要删除的按钮盒"); return; }
|
||||
@@ -304,7 +308,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
});
|
||||
}
|
||||
|
||||
private static void AddButton()
|
||||
private void AddButton()
|
||||
{
|
||||
var box = GetSelectedBox();
|
||||
if (box == null) { CycleUiHelper.Alert("提示", "请先选择按钮盒"); return; }
|
||||
@@ -327,7 +331,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
_panel?.Repaint();
|
||||
}
|
||||
|
||||
private static void SaveButton()
|
||||
private void SaveButton()
|
||||
{
|
||||
var box = GetSelectedBox();
|
||||
var btn = GetSelectedButton();
|
||||
@@ -347,7 +351,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
_panel?.Repaint();
|
||||
}
|
||||
|
||||
private static void ConfirmDeleteButton()
|
||||
private void ConfirmDeleteButton()
|
||||
{
|
||||
var box = GetSelectedBox();
|
||||
var btn = GetSelectedButton();
|
||||
@@ -363,7 +367,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
});
|
||||
}
|
||||
|
||||
private static bool TryParseBoxInput(out int index, out string ip, out int port, out string type, bool requireSelection, out string err)
|
||||
private bool TryParseBoxInput(out int index, out string ip, out int port, out string type, bool requireSelection, out string err)
|
||||
{
|
||||
index = 0; ip = ""; port = 502; type = ""; err = "";
|
||||
ip = string.IsNullOrWhiteSpace(_boxIp) ? "192.168.1.100" : _boxIp.Trim();
|
||||
@@ -379,7 +383,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool TryParseButtonInput(out int index, out ushort delay, out string err)
|
||||
private bool TryParseButtonInput(out int index, out ushort delay, out string err)
|
||||
{
|
||||
index = 0; delay = 0; err = "";
|
||||
var box = GetSelectedBox();
|
||||
@@ -393,7 +397,7 @@ namespace StandardScene.ExtendDevice.ButtonBox
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsValidIp(string ip)
|
||||
private bool IsValidIp(string ip)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ip)) return false;
|
||||
var pattern = @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
|
||||
|
||||
Reference in New Issue
Block a user