using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Linq; using System.Windows.Forms; using DevExpress.XtraEditors; using LFP_Manager.Forms; using LFP_Manager.Controls; using LFP_Manager.DataStructure; using LFP_Manager.Function; using LFP_Manager.Threads; using LFP_Manager.Utils; namespace LFP_Manager.Forms { public partial class fmxMain : DevExpress.XtraEditors.XtraForm { #region VARIABLES // Child form private fmxInventoryConfig InventoryConfig = null; private fmxModuleDetail ModuleDetailForm = null; private fmxFwUpdate FwUpdateForm = null; private fmxHistory HistoryForm = null; private fmxPacketLog PacketLogForm = null; // Static Variables CommConfig Config = new CommConfig(); DeviceSystemTotalData SystemTotalData; DeviceSystemData SystemData; // Thread Variables csUartThread uartThread = null; csUartThreadEltek uartThreadEltek = null; csDbThread dbThread = null; //DevExpress.XtraTab.XtraTabPage[] pgModule; //ucShelfStatus[] ModuleStatus; ucMainStatus MainStatus = null; ucShelfStatus ShelfStatus = null; private int SystemID = 1; bool Active = false; #endregion #region CONSTRUCTORS public fmxMain() { InitializeComponent(); StructDataInit(); //DataInit(); IniLoad(ref Config); uTarget.OnConfig += ConfigButtonClick; uTarget.OnStart += StartButtonClick; uDataLog.OnUpdate += LogStatusUpdate; uEventLog.OnPacketAnalyzerClick += packetAnalyzerToolStripMenuItem_Click; this.Text = String.Format("LFP SYSTEM [{0}] -- TW (for DBG)", Application.ProductVersion); } private void IniLoad(ref CommConfig aConfig) { csIniControlFunction.IniLoad(Application.ExecutablePath, ref aConfig); uTarget.UpdateConfig(ref aConfig); TypeChangedUpdate(ref aConfig); } private void StructDataInit() { SystemTotalData = new DeviceSystemTotalData { ValueData = new DeviceValueTotalData(), StatusData = new DeviceStatusTotalData() }; SystemData = new DeviceSystemData(); } #endregion #region BUTTON EVENT private void ConfigButtonClick(object sender, EventArgs e) { foreach (Form frm in Application.OpenForms) { if (frm.Name == "fmxCommConfig") { frm.Activate(); return; } } fmxCommConfig CommConfigForm = new fmxCommConfig { Owner = this }; CommConfigForm.OnUpdate += UpdateConfig; CommConfigForm.Show(); } private void StartButtonClick(object sender, EventArgs e) { if (Active) { StructDataInit(); switch (Config.CommType) { case csConstData.CommType.COMM_UART: switch (Config.UartProtocol) { case 0: // AMG Protocol if (uartThread != null) { uartThread.disposeThread(); uartThread = null; UpdateData(this, ref SystemData); edSystemId.Enabled = true; chbPolling.Enabled = true; } break; case 1: // ELTEK Protocol if (uartThreadEltek != null) { uartThreadEltek.disposeThread(); uartThreadEltek = null; UpdateData(this, ref SystemData); edSystemId.Enabled = true; chbPolling.Enabled = true; } break; } break; } if (dbThread != null) { dbThread.disposeThread(); dbThread = null; } Active = false; } else { switch (Config.CommType) { case csConstData.CommType.COMM_UART: // UART MODE - RS-485 switch (Config.UartProtocol) { case 0: // AMG Protocol CommUARTStart(); break; case 1: // ELTEK Protocol CommUARTStartEltek(); break; } break; } } uTarget.UpdateStatus(Active); uDataLog.UpdateActiveStatus(Active, Config, ref SystemTotalData, ref SystemData); DisplayCommStatus(); } private void UartRecvData(byte[] data) { if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate () { FwUpdateForm?.RecvData(data); HistoryForm?.RecvUardData(data); })); } else { FwUpdateForm?.RecvData(data); HistoryForm?.RecvUardData(data); } } private void CommUARTStart() { try { int sId = 0; if (edSystemId.Text != string.Empty) sId = Convert.ToInt16(edSystemId.Text); uartThread = new csUartThread(sId, Config, ref SystemData); uartThread.OnUpdate += UpdateData; uartThread.OnDataRecv += UartRecvData; uartThread.OnPrint += CommDataPrint; uartThread.OnPacketLog += PacketLogUpdate; uartThread.SetPolling(chbPolling.Checked, sId, ref SystemData); if (uartThread.Start(ref Config, sId, chbPolling.Checked)) { Active = true; edSystemId.Enabled = false; chbPolling.Enabled = true; if (chbPolling.Checked) edSystemId.Enabled = false; else edSystemId.Enabled = true; uartThread.SetPolling(chbPolling.Checked, sId, ref SystemData); MainStatus?.Start(ref Config, ref SystemTotalData, ref SystemData); ShelfStatus?.UpdateSystemId(sId); dbThread = new csDbThread(ref Config, ref SystemData); dbThread.Start(Config); } else { if (uartThread != null) { uartThread.disposeThread(); uartThread = null; } if (dbThread != null) { dbThread.disposeThread(); dbThread = null; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); if (uartThread != null) { uartThread.disposeThread(); uartThread = null; } if (dbThread != null) { dbThread.disposeThread(); dbThread = null; } } } private void CommUARTStartEltek() { try { int sId = 0; if (edSystemId.Text != string.Empty) sId = Convert.ToInt16(edSystemId.Text); uartThreadEltek = new csUartThreadEltek(sId, Config, ref SystemData); uartThreadEltek.OnUpdate += UpdateData; //uartThreadEltek.OnDataRecv += UartRecvData; uartThreadEltek.OnPrint += CommDataPrint; uartThreadEltek.SetPolling(chbPolling.Checked, sId, ref SystemData); if (uartThreadEltek.Start(Config, sId, chbPolling.Checked)) { Active = true; edSystemId.Enabled = false; chbPolling.Enabled = true; if (chbPolling.Checked) edSystemId.Enabled = false; else edSystemId.Enabled = true; uartThreadEltek.SetPolling(chbPolling.Checked, sId, ref SystemData); ShelfStatus.UpdateSystemId(sId); dbThread = new csDbThread(ref Config, ref SystemData); dbThread.Start(Config); } else { if (uartThreadEltek != null) { uartThreadEltek.disposeThread(); uartThreadEltek = null; } if (dbThread != null) { dbThread.disposeThread(); dbThread = null; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); if (uartThreadEltek != null) { uartThreadEltek.disposeThread(); uartThreadEltek = null; } if (dbThread != null) { dbThread.disposeThread(); dbThread = null; } } } #endregion #region UPDATE FUNCTIONS private void TypeChangeUpdate(ref CommConfig aConfig) { // Display Screen Delete if (MainStatus != null) { MainStatus.Dispose(); MainStatus = null; } if (ShelfStatus != null) { ShelfStatus.Dispose(); ShelfStatus = null; } if (aConfig.ModuleQty > 1) { // All disable first for (int i = 0; i < tabStatus.TabPages.Count; i++) { tabStatus.TabPages[i].PageVisible = false; } MainStatus = new ucMainStatus(); MainStatus.OnCommand += SetCmdEvent; tabStatus.TabPages[0].Text = String.Format("LFP System [{0}.{1}.{2}.{3}]" , 0 , 0 , 0 , 0 ); tabStatus.TabPages[0].PageVisible = true; tabStatus.TabPages[0].Controls.Add(MainStatus); MainStatus.Dock = DockStyle.Fill; MainStatus.UpdateMainConfig(ref Config, ref SystemData); //SystemTotalData.IdentData.ModuleQuantity = Config.ModuleQty; edSystemId.Enabled = false; chbPolling.Enabled = false; } else { TypeChangeUpdateUart(ref aConfig); } } private void TypeChangeUpdateUart(ref CommConfig aConfig) { tabStatus.TabPages[0].PageVisible = true; for (int i = 1; i < tabStatus.TabPages.Count; i++) { tabStatus.TabPages[i].PageVisible = false; } tabStatus.TabPages[0].Text = String.Format("LFP Module [{0}.{1}.{2}.{3}]" , 0 , 0 , 0 , 0 ); ShelfStatus = new ucShelfStatus(); ShelfStatus.OnCommand += SetCmdEvent; ShelfStatus.Dock = DockStyle.Fill; tabStatus.TabPages[0].PageVisible = true; tabStatus.TabPages[0].Controls.Add(ShelfStatus); ShelfStatus.Start(Convert.ToInt32(edSystemId.Text), ref aConfig, ref SystemData); edSystemId.Enabled = true; chbPolling.Enabled = true; } private void TypeChangedUpdate(ref CommConfig aConfig) { // Display Screen Deletes if (MainStatus != null) { MainStatus.Dispose(); MainStatus = null; } if (ShelfStatus != null) { ShelfStatus.Dispose(); ShelfStatus = null; } switch (aConfig.CommType) { case csConstData.CommType.COMM_UART: TypeChangeUpdate(ref aConfig); break; } //DisplayCommStatus(); } private void UpdateData(object sender, ref DeviceSystemData rData) { SystemData = rData; MakeTotalData(); MakeTotalStatus(); MakeTotalAlarm(); ShelfStatus?.UpdateData(ref SystemData); uDataLog.UpdateData(ref SystemTotalData, ref SystemData); //DisplaySystemFwVerModule(); //ucCalibrationView1.DisplayValue(ref SystemData); } private void DisplaySystemFwVerModule() { if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate () { if (SystemData.ValueData.fw_ver[0] != 0x00) { byte[] sn = new byte[33]; for (int i = 0; i < 32; i++) sn[i] = SystemData.Information.BMS_SN[i]; sn[32] = 0; tabStatus.TabPages[0].Text = String.Format("{0} [{1}] - {2}", "LFP MODULE" , Encoding.UTF8.GetString(SystemData.ValueData.fw_ver) , Encoding.Default.GetString(sn).Trim('\0') ); } else { tabStatus.TabPages[0].Text = String.Format("LFP MODULE"); } })); } else { if (SystemData.ValueData.fw_ver[0] != 0x00) { byte[] sn = new byte[17]; for (int i = 0; i < 16; i++) sn[i] = SystemData.Information.BMS_SN[i]; //22.06.30 Byul 32->16 sn[16] = 0; tabStatus.TabPages[0].Text = String.Format("{0} [{1}] - {2}", "LFP MODULE" , Encoding.UTF8.GetString(SystemData.ValueData.fw_ver).Trim('\0') , Encoding.Default.GetString(sn).Trim('\0') ); } else { tabStatus.TabPages[0].Text = String.Format("LFP MODULE"); } } } private void UpdateSnmpData(object sender, ref DeviceSystemTotalData tData, ref DeviceSystemData rData) { SystemTotalData = tData; SystemData = rData; MakeTotalData(); MakeTotalStatus(); MakeTotalAlarm(); if (MainStatus != null) { MainStatus.UpdateData(ref SystemTotalData, ref SystemData); if (ModuleDetailForm != null) { int mID = ModuleDetailForm.GetModuleID(); ModuleDetailForm.UpdateData(ref SystemData); } } uDataLog.UpdateData(ref SystemTotalData, ref SystemData); DisplaySystemFwVer(); } private void DisplaySystemFwVer() { if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate () { if (SystemTotalData.IdentData.fw_ver[0] != 0x00) { tabStatus.TabPages[0].Text = String.Format("LFP System [{0}.{1}.{2}.{3}]" , SystemTotalData.IdentData.fw_ver[0] , SystemTotalData.IdentData.fw_ver[1] , SystemTotalData.IdentData.fw_ver[2] , SystemTotalData.IdentData.fw_ver[3] ); } else { tabStatus.TabPages[0].Text = String.Format("LFP System [------]"); } })); } else { if (SystemTotalData.IdentData.fw_ver[0] != 0x00) { tabStatus.TabPages[0].Text = String.Format("LFP System [{0}.{1}.{2}.{3}]" , SystemTotalData.IdentData.fw_ver[0] , SystemTotalData.IdentData.fw_ver[1] , SystemTotalData.IdentData.fw_ver[2] , SystemTotalData.IdentData.fw_ver[3] ); } else { tabStatus.TabPages[0].Text = String.Format("LFP System [------]"); } } } private void MakeTotalData() { if (Config.CommType == csConstData.CommType.COMM_UART) { MakeTotalDataByUartMulti(); //MakeTotalDataByUART(); } } private void MakeTotalDataByUART() //Byul 21.08.09 Make Function { SystemTotalData.ValueData.TotalVoltage = SystemData.ValueData.voltageOfPack; SystemTotalData.ValueData.TotalCurrent = SystemData.ValueData.current; SystemTotalData.ValueData.TotalSOC = SystemData.ValueData.rSOC; SystemTotalData.ValueData.TotalTemp = SystemData.AvgData.avgTemp; } private void MakeTotalDataByCAN() { SystemTotalData.ValueData.TotalVoltage = SystemData.ValueData.voltageOfPack; SystemTotalData.ValueData.TotalCurrent = SystemData.ValueData.current; SystemTotalData.ValueData.TotalSOC = SystemData.ValueData.rSOC; SystemTotalData.ValueData.TotalTemp = SystemData.AvgData.maxTemp; } private void MakeTotalDataByUartMulti() { int j = 0; int vsum, csum, ssum, tsum; int mQty = Config.ModuleQty; vsum = csum = ssum = tsum = 0; for (int i = 0; i < mQty; i++) { if (SystemData.ShelfCommFail == false) { vsum += SystemData.ValueData.voltageOfPack; csum += SystemData.ValueData.current; ssum += SystemData.ValueData.rSOC; tsum += SystemData.AvgData.avgTemp; j++; } } if (j > 0) { SystemTotalData.ValueData.TotalVoltage = (short)(vsum / j); SystemTotalData.ValueData.TotalCurrent = (short)(csum); SystemTotalData.ValueData.TotalSOC = (short)(ssum / j); SystemTotalData.ValueData.TotalTemp = (short)(tsum / j); } else { SystemTotalData.ValueData.TotalVoltage = 0; SystemTotalData.ValueData.TotalCurrent = 0; SystemTotalData.ValueData.TotalSOC = 0; SystemTotalData.ValueData.TotalTemp = 0; } } private void MakeTotalStatus() { if (Config.CommType == csConstData.CommType.COMM_UART) { MakeTotalStatusByCAN(); } } private void MakeTotalStatusByCAN() { SystemTotalData.StatusData.status = SystemData.StatusData.status; SystemTotalData.CommFail = SystemData.ShelfCommFail; } private void MakeTotalStatusBySNMP() { int mQty = csUtils.GetModuleQty(Config); switch (SystemTotalData.StatusData.status) { case 0: // Standby if (SystemTotalData.ValueData.TotalCurrent > 50) // Charging 10.0A SystemTotalData.StatusData.status = 1; // Charging else if (SystemTotalData.ValueData.TotalCurrent < -50) // Discharge -10.0A SystemTotalData.StatusData.status = 2; // Discharging else { bool flag = true; for (int i = 0; i < mQty; i++) { if (SystemData.StatusData.status != 3) { flag = false; break; } } if (flag == true) SystemTotalData.StatusData.status = 3; // Floating } break; case 1: // Charging if (SystemTotalData.ValueData.TotalCurrent < -50) // Discharge -10.0A SystemTotalData.StatusData.status = 2; // Discharging else if (SystemTotalData.ValueData.TotalCurrent < 30) // Charging 5.0A SystemTotalData.StatusData.status = 0; // Standby else { bool flag = true; for (int i = 0; i < mQty; i++) { if (SystemData.StatusData.status != 3) { flag = false; break; } } if (flag == true) SystemTotalData.StatusData.status = 3; // Floating } break; case 2: // Discharging if (SystemTotalData.ValueData.TotalCurrent > 50) // Charging 10.0A SystemTotalData.StatusData.status = 1; // Charging else if (SystemTotalData.ValueData.TotalCurrent > -30) // Discharge -5.0A SystemTotalData.StatusData.status = 0; // Standby else { bool flag = true; for (int i = 0; i < mQty; i++) { if (SystemData.StatusData.status != 3) { flag = false; break; } } if (flag == true) SystemTotalData.StatusData.status = 3; // Floating } break; case 3: // Floating if (SystemTotalData.ValueData.TotalCurrent > 50) // Charging 10.0A SystemTotalData.StatusData.status = 1; // Charging else if (SystemTotalData.ValueData.TotalCurrent < -50) // Discharge -10.0A SystemTotalData.StatusData.status = 2; // Discharge else { bool flag = true; for (int i = 0; i < mQty; i++) { if (SystemData.StatusData.status != 3) { flag = false; break; } } if (flag == false) SystemTotalData.StatusData.status = 0; // Standby } break; } } private void MakeTotalAlarm() { int mQty = csUtils.GetModuleQty(Config); bool aTrip, aWarning; aTrip = aWarning = false; for (int i = 0; i < mQty; i++) { if (SystemData.StatusData.batteryStatus == 2) aTrip = true; else if ((SystemData.StatusData.batteryStatus == 1) || (SystemData.ShelfCommFail == true)) aWarning = true; } if (aTrip) SystemTotalData.StatusData.batteryStatus = 2; else if (aWarning) SystemTotalData.StatusData.batteryStatus = 1; else SystemTotalData.StatusData.batteryStatus = 0; } private void UpdateConfig(object aConfig) { Config = (CommConfig)aConfig; IniLoad(ref Config); } #endregion #region MENU EVENT private void commConfigToolStripMenuItem_Click(object sender, EventArgs e) { ConfigButtonClick(sender, e); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Close(); } private void testSendToolStripMenuItem_Click(object sender, EventArgs e) { csHistoryFunction.DbCreate(System.IO.Path.GetDirectoryName(Application.ExecutablePath)); } private void firmwareUpdateToolStripMenuItem_Click(object sender, EventArgs e) { foreach (Form frm in Application.OpenForms) { if (frm.Name == "fmxFwUpdate") { frm.Activate(); return; } } if (uartThread != null) { FwUpdateForm = new fmxFwUpdate(uartThread.SystemId); } else { return; } FwUpdateForm.OnAutoTxSet += SetAutoTx; FwUpdateForm.OnSendUartData += SetWriteData; FwUpdateForm.Show(); } private void historyViewToolStripMenuItem_Click(object sender, EventArgs e) { foreach (Form frm in Application.OpenForms) { if (frm.Name == "fmxHistory") { frm.Activate(); return; } } HistoryForm = new fmxHistory(Config, SystemID); HistoryForm.OnAutoTxSet += SetAutoTx; HistoryForm.OnSendUartData += SetWriteData; HistoryForm.Show(); HistoryForm.Show(); } private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e) { foreach (Form frm in Application.OpenForms) { if (frm.Name == "FmxChangePassword") { frm.Activate(); return; } } using (FmxChangePassword ChangePwForm = new FmxChangePassword(Config)) { ChangePwForm.ShowDialog(this); } } private void packetAnalyzerToolStripMenuItem_Click(object sender, EventArgs e) { if (PacketLogForm == null || PacketLogForm.IsDisposed) { PacketLogForm = new fmxPacketLog(); } PacketLogForm.Show(); PacketLogForm.BringToFront(); } private void PacketLogUpdate(DateTime timestamp, bool isTx, byte[] packet, string description) { PacketLogForm?.AddPacketLog(timestamp, isTx, packet, description); } #endregion #region FORM EVENT private void fmxMain_FormClosing(object sender, FormClosingEventArgs e) { if (uartThread != null) { uartThread.disposeThread(); uartThread = null; } if (uartThreadEltek != null) { uartThreadEltek.disposeThread(); uartThreadEltek = null; } if (dbThread != null) { dbThread.disposeThread(); dbThread = null; } } #endregion #region TIMER EVENT #endregion #region STATUS BAR EVENT void DisplayCommStatus() { if (Active == true) CommStatusBar.Text = "Comm: On"; else CommStatusBar.Text = "Comm: Off"; } void DisplayCommStatus(string msg) { if (msg != "") CommStatusBar.Text = String.Format("Comm: {0}", msg); } void LogStatusUpdate(object sender, string msg, bool active, int aLogTime) { LogStatusBar.Text = msg; } #endregion #region CAN COMMAND AND SET private void CommDataPrint(object sender, string msg) { if (msg != "") { uEventLog.EventUpdate(msg + "\r\n"); } } private void SetWriteData(ushort WriteAddr, byte[] WriteData, bool RelyFlag) { uartThread?.SetWriteReg(WriteAddr, WriteData, RelyFlag); } private void SetCmdEvent(int sId, int cmd, int index, int flag, ref DeviceParamData aParam, ref DeviceCalibration aCalib) { if (MainStatus != null) { switch (cmd) { case 1: // Module quatity set break; case 2: CreateModuleDetailForm(sId); break; } } uartThread?.SendProcessFromApp(sId, cmd, index, flag, ref aParam, ref aCalib); uartThreadEltek?.SendProcessFromApp(sId, cmd, index, flag, ref aParam, ref aCalib); } private void CreateModuleDetailForm(int sID) { foreach (Form frm in Application.OpenForms) { if (frm.Name == "fmxModuleDetail") { ((fmxModuleDetail)frm).SetModuleID(sID); frm.Activate(); return; } } ModuleDetailForm = new fmxModuleDetail(sID, ref Config, ref SystemData); //ModuleDetailForm.OnAutoTxCanSet += SetAutoTxCan; //ModuleDetailForm.OnSendDataCan += SendDataToCan; ModuleDetailForm.Show(); } private void chbPolling_CheckedChanged(object sender, EventArgs e) { if (edSystemId.Text != "") { SystemID = Convert.ToInt32(edSystemId.Text); if (uartThread != null) { uartThread.SetPolling(chbPolling.Checked, SystemID, ref SystemData); ShelfStatus?.UpdateSystemId(SystemID); } if (uartThreadEltek != null) { uartThreadEltek.SetPolling(chbPolling.Checked, SystemID, ref SystemData); ShelfStatus?.UpdateSystemId(SystemID); } if (chbPolling.Checked) edSystemId.Enabled = false; else edSystemId.Enabled = true; } } private void edSystemId_KeyPress(object sender, KeyPressEventArgs e) { csUtils.TypingOnlyNumber(sender, e, false, false); } #endregion private void SetAutoTx(bool autoTx) { uartThread?.SetAutoTx(autoTx); uartThreadEltek?.SetAutoTx(autoTx); } private void inventoryControlToolStripMenuItem_Click(object sender, EventArgs e) { foreach (Form frm in Application.OpenForms) { if (frm.Name == "fmxInventoryConfig") { frm.Activate(); return; } } InventoryConfig = new fmxInventoryConfig(0, ref Config, ref SystemData); InventoryConfig.OnUpdate += UpdateInformation; InventoryConfig.OnCommand += InvDataSetCmd; //InventoryConfig.OnCommandBySnmp += SetInvEventBySnmp; InventoryConfig.Show(); } private void UpdateInformation(object sendor) { ((fmxInventoryConfig)sendor).UpdateData(ref SystemTotalData, ref SystemData); } private void InvDataSetCmd(int sId, int mode, int index, int flag, ref DeviceInforation aInvData) { SystemData.CalibriationData.InvData = aInvData; //OnCommand?.Invoke(sId, mode, index, flag, ref SystemData[0].ParamData, ref SystemData[0].CalibriationData); //SetCmdToCan(sId, mode, index, flag, ref SystemData[0].ParamData, ref SystemData[0].CalibriationData); } private void checkDBToolStripMenuItem_Click(object sender, EventArgs e) { foreach (Form frm in Application.OpenForms) { if (frm.Name == "fmxExcelFile") { frm.Activate(); return; } } fmxExcelFile DbControl = new fmxExcelFile(Config); DbControl.Show(); } #region TIMER EVENT private void tmrDisplay_Tick(object sender, EventArgs e) { DisplaySystemFwVerModule(); } #endregion } }