using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraEditors; using LFP_Manager.DataStructure; using LFP_Manager.Function; using LFP_Manager.Controls; using LFP_Manager.Utils; namespace LFP_Manager.Forms { public delegate void InvDataUpdateEvent(object sendor); public delegate void InvDateCmdEvent(int sId, int mode, int index, int flag, ref DeviceInforation wData); public delegate void InvDateCmdEventBySnmp(int mode, string wData); public partial class fmxInventoryConfig : DevExpress.XtraEditors.XtraForm { #region VARIABLES int SystemId = 0; int CmdResult = 0; CommConfig Config; DeviceSystemData SystemData; DeviceSystemTotalData SystemTotalData; DeviceInforation wInvData; public event InvDataUpdateEvent OnUpdate = null; public event InvDateCmdEvent OnCommand = null; public event InvDateCmdEventBySnmp OnCommandBySnmp = null; #endregion public fmxInventoryConfig() { InitializeComponent(); } public fmxInventoryConfig(int sId, ref CommConfig aConfig, ref DeviceSystemData aSystemData) { InitializeComponent(); SystemId = sId; SystemData = aSystemData; Config = aConfig; tmrDisplay.Enabled = true; tmrCmd.Enabled = true; } void InitData() { wInvData = new DeviceInforation { BMS_SN = new byte[16], Module_SN = new byte[24] }; //22.06.30 Byul 32->16, 32->24 } #region DISPLAY DATA private void DisplayValue() { } private void DisplayInvDataByCAN() { // Manufacture Date lbMDate.Text = String.Format("Manufacture Date: {0} ({0:X8})", SystemData.Information.ManufactureDate) + "\r\n" + DisplayManufactureDate(SystemData.Information.ManufactureDate) ; // Device Address% string strSerial = Encoding.UTF8.GetString(SystemData.Information.BMS_SN); lbSerialNo.Text = String.Format("Serial No: {0}", strSerial); } private void DisplayInvDataBySNMP() { // Manufacture Date lbMDate.Text = String.Format("Manufacture Date: {0} ({0:X8})", SystemTotalData.IdentData.ManufactureDate) + "\r\n" + DisplayManufactureDate(SystemTotalData.IdentData.ManufactureDate) ; // Device Address% string strSerial = SystemTotalData.IdentData.SerialNumber; lbSerialNo.Text = String.Format("Serial No: {0}", strSerial); } private string DisplayManufactureDate(UInt32 mDate) { DateTime dtDate = csUtils.ConvertTimeStampToDateTime(mDate); return String.Format("{0:yyyy-MM-dd HH:mm:ss}", dtDate); } #endregion #region TIMER ENVENT private void tmrDisplay_Tick(object sender, EventArgs e) { DisplayValue(); if (OnUpdate != null) { OnUpdate(this); } } #endregion #region PUBLIC FUCTIONS public void UpdateData(ref DeviceSystemTotalData aSystemTotalData, ref DeviceSystemData aSystemData) { SystemTotalData = aSystemTotalData; SystemData = aSystemData; } public void RecvData(string result, bool error) { Cmd_Result(result, error); } public void Cmd_Result(string result, bool error) { if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate () { if (error) MessageBox.Show(result, "Result", MessageBoxButtons.OK, MessageBoxIcon.Error); else MessageBox.Show(result, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); })); } else { if (error) MessageBox.Show(result, "Result", MessageBoxButtons.OK, MessageBoxIcon.Error); else MessageBox.Show(result, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); } } #endregion #region BUTTON EVENT private void btnMDateRead_Click(object sender, EventArgs e) { OnCommand?.Invoke(SystemId, 900, 1, 0, ref wInvData); OnCommand?.Invoke(SystemId, 901, 1, 0, ref wInvData); OnCommand?.Invoke(SystemId, 902, 1, 0, ref wInvData); } private void btnWrite_Click(object sender, EventArgs e) { } private void btnInvWrite_Click(object sender, EventArgs e) { } private void InvWriteCmdByCAN() { if (nSerialNo.Text.Length != 15) { MessageBox.Show(String.Format("BMS serial number length error - [{0}]", nSerialNo.Text.Length) , "Warning" , MessageBoxButtons.OK , MessageBoxIcon.Warning ); } csDbUtils.DbCreate(nSerialNo.Text, "mBMS_16S"); if (CheckBmsSerialNo(nSerialNo.Text, "mBMS_16S") == false) { byte[] bSN = Encoding.UTF8.GetBytes(nSerialNo.Text); if (wInvData.BMS_SN == null) wInvData.BMS_SN = new byte[16]; //22.06.30 Byul 32->16 for (int i = 0; i < 16; i++) wInvData.BMS_SN[i] = 0; for (int i = 0; i < bSN.Length; i++) { wInvData.BMS_SN[i] = bSN[i]; } OnCommand?.Invoke(SystemId, 901, 1, 1, ref wInvData); OnCommand?.Invoke(SystemId, 902, 1, 1, ref wInvData); CmdMDateWrite(); CmdResult = 1; } else { MessageBox.Show(String.Format("Already used BMS serial number - [{0}]", nSerialNo.Text) , "Warning" , MessageBoxButtons.OK , MessageBoxIcon.Warning ); } } private void InvWriteCmdBySNMP() { if ((nSerialNo.Text.Length < 14) && (nSerialNo.Text.Length > 16)) { MessageBox.Show(String.Format("BMS serial number length error - [{0}]", nSerialNo.Text.Length) , "Warning" , MessageBoxButtons.OK , MessageBoxIcon.Warning ); } string wSN = nSerialNo.Text; csDbUtils.DbCreate(wSN, "BMCB_J"); if (CheckBmsSerialNo(wSN, "BMCB_J") == false) { OnCommandBySnmp?.Invoke(5, wSN); CmdMDateWrite(); CmdResult = 1; } else { MessageBox.Show(String.Format("Already used BMS serial number - [{0}]", nSerialNo.Text) , "Warning" , MessageBoxButtons.OK , MessageBoxIcon.Warning ); } } #endregion #region PROCESSING DATA private bool CheckBmsSerialNo(string bSN, string bName) { bool result = false; DataTable dtBmsData = new DataTable(); DataTable dtBmsMatch = new DataTable(); DataTable dtErrorLog = new DataTable(); //string sql = String.Format("SELECT * FROM TInventoryData WHERE pcb_sn = {0}", bSN); string sql = String.Format("SELECT * FROM TInventoryData"); dtBmsData = csDbUtils.GetDataTableBySelect(bSN, sql, "TInventoryData", bName); if (dtBmsData != null) { if (dtBmsData.Rows.Count > 0) { DataRow[] arrRows = null; arrRows = dtBmsData.Select(String.Format("pcb_sn = '{0}'", bSN)); if (arrRows.Length > 0) result = true; } } return result; } private void CmdMDateWrite() { UInt32 mTimeStamp = csUtils.CalcKKTimeStamp(DateTime.Now); wInvData.ManufactureDate = mTimeStamp; } private void CheckResult() { } private void CheckResultByCAN() { if (CmdResult == 1) { bool result = false; byte[] a = Encoding.UTF8.GetBytes(nSerialNo.Text); byte[] b = SystemData.Information.BMS_SN; for (int i = 0; i < a.Length; i++) { if (a[i] == b[i]) continue; result = true; } if ((result == false) && (wInvData.ManufactureDate == SystemData.Information.ManufactureDate)) { CmdResult = 0; try { csDbUtils.BmsDataInsert(SystemData, nSerialNo.Text, "mBMS_16S"); MessageBox.Show(String.Format("BMS data insert complete - [{0}]", nSerialNo.Text) , "Information" , MessageBoxButtons.OK , MessageBoxIcon.Information ); } catch (Exception ex) { MessageBox.Show(String.Format("BMS data insert fail - [{0}] \r\n{1}", nSerialNo.Text, ex.Message) , "Warning" , MessageBoxButtons.OK , MessageBoxIcon.Warning ); } } } } private void CheckResultBySNMP() { if (CmdResult == 1) { bool result = false; byte[] a = Encoding.UTF8.GetBytes(nSerialNo.Text); byte[] b = Encoding.UTF8.GetBytes(SystemTotalData.IdentData.SerialNumber); if (a.Length != b.Length) return; for (int i = 0; i < a.Length; i++) { if (a[i] == b[i]) continue; result = true; } if ((result == false) && (wInvData.ManufactureDate == SystemTotalData.IdentData.ManufactureDate)) { CmdResult = 0; try { csDbUtils.BmsDataInsert(SystemTotalData, nSerialNo.Text, "BMCB_J"); MessageBox.Show(String.Format("BMS data insert complete - [{0}]", nSerialNo.Text) , "Information" , MessageBoxButtons.OK , MessageBoxIcon.Information ); } catch (Exception ex) { MessageBox.Show(String.Format("BMS data insert fail - [{0}] \r\n{1}", nSerialNo.Text, ex.Message) , "Warning" , MessageBoxButtons.OK , MessageBoxIcon.Warning ); } } } } #endregion private void tmrCmd_Tick(object sender, EventArgs e) { CheckResult(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }