using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Linq; using System.Windows.Forms; using LFP_Manager.Forms; using LFP_Manager.DataStructure; using LFP_Manager.Utils; using DevExpress.XtraPrinting.Preview; namespace LFP_Manager.Controls { public partial class ucCalibration : DevExpress.XtraEditors.XtraUserControl { #region DELEGATE public delegate void setCalibUpdate(object sender); public delegate void setCalibCommand(int mode, int index, int flag, ref DeviceParamData aParm, ref DeviceCalibration aCalib); public delegate Int32 getBattData(int item, int cno); #endregion #region VARIABLES const int CALC_INDEX = 6; CommConfig Config; DeviceSystemData SystemData; DeviceCalibration rCalib; DeviceCalibration wCalib; DeviceParamData rParam; public event setCalibCommand OnCommand = null; int wFlag = 0; int dFlag; int PasswordResult; #endregion #region CONSTRUCTORS // Battery No: // 0: No 1,3,5,7,9,11,13,15 ODD // 1: No 2,4,6,8,10,12,14 Even public ucCalibration() { InitializeComponent(); ComponentLoad(); Config = new CommConfig(); dFlag = 0; } private void ComponentLoad() { CbNewChaMode.Clear(); for (int i = 0; i < csConstData.SystemInfo.CHG_MODE.Length; i++) { CbNewChaMode.Properties.Items.Add(csConstData.SystemInfo.CHG_MODE[i]); } } #endregion #region EXT EVENT FUNCTION private void OnCommnadEvent(int mode, int index, int flag, ref DeviceParamData aParam, ref DeviceCalibration aCalib) { OnCommand?.Invoke(mode, index, flag, ref aParam, ref aCalib); } #endregion #region PUBLIC FUCTIONS public void UpdateConfig(CommConfig aConfig) { Config = aConfig; } public void UpdateData(DeviceSystemData aSystemData, DeviceCalibration aCalib) { SystemData = aSystemData; rCalib = aCalib; DisplayCalib(); UpdateNewCalib(); } private void UpdateNewCalib() { if (SystemData != null) { if (wFlag == 0) { teNewCapacity.Text = teCurrCapacity.Text; teNewSoc.Text = teCurrSoc.Text; wFlag = 1; } } } #endregion #region DISPLAY DATA private void DisplayCalib() { if (SystemData != null) { teCurrCapacity.Text = string.Format("{0}", SystemData.ValueData.designedCapacity / 10); teCurrSoc.Text = string.Format("{0}", SystemData.ValueData.rSOC / 10); if (SystemData.CalibriationData.ChaMode.Mode >= csConstData.SystemInfo.CHG_MODE.Length) { TeChaMode.Text = string.Format("Unknow ({0})", SystemData.CalibriationData.ChaMode.Mode); } else { TeChaMode.Text = csConstData.SystemInfo.CHG_MODE[SystemData.CalibriationData.ChaMode.Mode]; } TeChgModeValueCurr.Text = string.Format("{0:0.00}", Convert.ToDouble(SystemData.CalibriationData.ChaMode.Value) / 100); TeCurrDateTime.Text = string.Format("{0:yyyy-MM-dd HH:mm:ss}", SystemData.BmsDateTime); // Anti-Theft Data Display if (SystemData.CalibriationData.AntiTheft.GyroScope == 0) { TeAntiTheftGyroCurr.Text = "DISABLE"; } else { TeAntiTheftGyroCurr.Text = "ENABLE"; } TeSensitive.Text = string.Format("{0}", SystemData.CalibriationData.AntiTheft.GyroScope); if (dFlag == 0) { CbNewChaMode.SelectedIndex = 1; CbNewChaMode.SelectedIndex = SystemData.CalibriationData.ChaMode.Mode; TeChgModeValueNew.Text = TeChgModeValueCurr.Text; dFlag = 1; } } } #endregion #region BUTTON EVENT private void TextBox_KeyPress(object sender, KeyPressEventArgs e) { csUtils.TypingOnlyNumber(sender, e, true, true); } private void btnCapacitySet_Click(object sender, EventArgs e) { wCalib = rCalib.DeepCopy(); if ((teNewCapacity.Text == "") || (teNewSoc.Text == "") ) { MessageBox.Show("Please check new data"); } wCalib.CapCalib.DesignCapacity = Convert.ToInt32(teNewCapacity.Text) * 10; wCalib.CapCalib.SocValue = Convert.ToInt32(teNewSoc.Text); OnCommnadEvent(15, CALC_INDEX, 1, ref rParam, ref wCalib); } private void BtnChaModeSet_Click(object sender, EventArgs e) { wCalib = rCalib.DeepCopy(); if (CbNewChaMode.Text == "") { MessageBox.Show("Please check new data"); } wCalib.ChaMode.Mode = CbNewChaMode.SelectedIndex; wCalib.ChaMode.Value = (int)(Convert.ToDouble(TeChgModeValueNew.Text) * 100); OnCommnadEvent(16, CALC_INDEX, 1, ref rParam, ref wCalib); } private void BtnBmsDateTimeSet_Click(object sender, EventArgs e) { wCalib = rCalib.DeepCopy(); if (TeNewDateTime.Text == "") { MessageBox.Show("Please check new data"); } DateTime dateTime = Convert.ToDateTime(TeNewDateTime.Text); int iDateTime = ((dateTime.Year - 2000) << 26) | (dateTime.Month << 22) | (dateTime.Day << 17) | (dateTime.Hour << 12) | (dateTime.Minute << 6) | (dateTime.Second << 0) ; wCalib.BmsDateTime.sValue[0] = (short)(iDateTime >> 16); wCalib.BmsDateTime.sValue[1] = (short)(iDateTime >> 0); OnCommnadEvent(17, CALC_INDEX, 1, ref rParam, ref wCalib); } private void BtnGetCurrTime_Click(object sender, EventArgs e) { TeNewDateTime.Text = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now); } private void BtnClearAntiTheftProtect_Click(object sender, EventArgs e) { // Anti Theft Data Clear PasswordResult = 0; foreach (Form frm in Application.OpenForms) { if (frm.Name == "FmxPassword") { frm.Activate(); return; } } FmxPassword PasswordForm = new FmxPassword(Config, 0); PasswordForm.OnResult += PassWordResultProc; PasswordForm.ShowDialog(); if (PasswordResult == 1) { wCalib = rCalib.DeepCopy(); wCalib.AntiTheft.GyroScope = Config.GyroSensitive; OnCommnadEvent(19, CALC_INDEX, 1, ref rParam, ref wCalib); } } private void PassWordResultProc(int result) { PasswordResult = result; } private void BtnAntiTheftEnable_Click(object sender, EventArgs e) { // Anti Theft Data Get PasswordResult = 0; foreach (Form frm in Application.OpenForms) { if (frm.Name == "FmxPassword") { frm.Activate(); return; } } FmxPassword PasswordForm = new FmxPassword(Config, 1); PasswordForm.OnResult += PassWordResultProc; PasswordForm.ShowDialog(); if (PasswordResult == 1) { wCalib = rCalib.DeepCopy(); wCalib.AntiTheft.GyroScope = Config.GyroSensitive; OnCommnadEvent(18, CALC_INDEX, 1, ref rParam, ref wCalib); } } private void BtnAntiTheftDisable_Click(object sender, EventArgs e) { // Anti Theft Data Get PasswordResult = 0; foreach (Form frm in Application.OpenForms) { if (frm.Name == "FmxPassword") { frm.Activate(); return; } } FmxPassword PasswordForm = new FmxPassword(Config, 2); PasswordForm.OnResult += PassWordResultProc; PasswordForm.ShowDialog(); if (PasswordResult == 1) { wCalib = rCalib.DeepCopy(); wCalib.AntiTheft.GyroScope = 0; OnCommnadEvent(18, CALC_INDEX, 1, ref rParam, ref wCalib); } } #endregion #region COMPONENT EVENT private void CbNewChaMode_SelectedIndexChanged(object sender, EventArgs e) { switch (CbNewChaMode.SelectedIndex) { case 0: TeChgModeValueNew.Enabled = false; break; case 1: TeChgModeValueNew.Enabled = false; break; case 2: TeChgModeValueNew.Enabled = true; break; case 3: TeChgModeValueNew.Enabled = true; break; case 4: TeChgModeValueNew.Enabled = true; break; default: TeChgModeValueNew.Enabled = false; break; } } #endregion } }