using Autofac; using DotNetty.Handlers.Logging; using HybirdFrameworkCore.Autofac; using Newtonsoft.Json; using Service.Charger.Client; namespace WinFormStarter; public partial class Form2 : Form { private ChargerClient? _chargerClient; public Form2() { InitializeComponent(); Init(); } private void Init() { this.txtIp.Text = @"127.0.0.1"; this.txtPort.Text = @"9998"; this.txtDestAddr.Text = @"03,01,01,02"; this.grpCmd.Enabled = false; this.grpData.Enabled = false; this.rTxtSend.Enabled = false; this.rTxtReceive.Enabled = false; } #region ui invoke private void EnableUi(Control control, bool enabled) { if (control.InvokeRequired) { void Enable() { control.Enabled = enabled; } control.Invoke((MethodInvoker)Enable); } else { control.Enabled = enabled; } } private void AppendText(RichTextBox rtxt, string t) { if (rtxt.InvokeRequired) { void Mi() { rtxt.AppendText(t); } rtxt.Invoke((MethodInvoker)Mi); } else { rtxt.AppendText(t); } } private void SetText(Control textBox, string t) { if (textBox.InvokeRequired) { void Mi() { textBox.Text = t; } textBox.Invoke((MethodInvoker)Mi); } else { textBox.Text = t; } } #endregion private void DisplayData() { this.SetText(this.rTxtData, JsonConvert.SerializeObject(_chargerClient, Formatting.Indented)); } private void btnChangeInOrOut_Click(object sender, EventArgs e) { } private void btnConn_Click(object sender, EventArgs e) { string ip = txtIp.Text; var port = int.Parse(txtPort.Text); string destAddr = txtDestAddr.Text; Task.Run(() => { if (_chargerClient is not { Connected: true }) { _chargerClient = AppInfo.Container.Resolve(); _chargerClient.AutoReconnect = false; _chargerClient.LogLevel = LogLevel.TRACE; _chargerClient.InitBootstrap(ip, port); _chargerClient.BaseConnect(); if (_chargerClient.Connected) { _chargerClient.SessionAttr("1", destAddr); SetText(lblConnStatus, @"连接成功"); SetText(btnConn, @"断开连接"); EnableUi(this.grpCmd, true); EnableUi(this.grpData, true); EnableUi(this.rTxtSend, true); EnableUi(this.rTxtReceive, true); ClientMgr.AddBySn("1", _chargerClient); } } else { _chargerClient.Close(); _chargerClient = null; SetText(lblConnStatus, @"未连接"); SetText(btnConn, @"连接"); EnableUi(this.grpCmd, false); EnableUi(this.grpData, false); EnableUi(this.rTxtSend, false); EnableUi(this.rTxtReceive, false); } }); } private void btnAuth_Click(object sender, EventArgs e) { _chargerClient?.SendAuth(); AppendText(this.rTxtSend, _chargerClient.CurrentCmd); DisplayData(); } private void btnSendBinStatus_Click(object sender, EventArgs e) { } private void btnSendAuxiliaryPower_Click(object sender, EventArgs e) { } private void btnStartCharge_Click(object sender, EventArgs e) { } private void btnStopCharge_Click(object sender, EventArgs e) { } private void btnChangePower_Click(object sender, EventArgs e) { } private void btnSetChargeRate_Click(object sender, EventArgs e) { } private void btnOfflineStopCharge_Click(object sender, EventArgs e) { } private void btnSendOutEnableCharge_Click(object sender, EventArgs e) { } private void btnSetPrice_Click(object sender, EventArgs e) { } private void btnReadBatteryInfo_Click(object sender, EventArgs e) { _chargerClient.SendQueryBattery(); DisplayData(); } }