public Form1() { InitializeComponent(); Log.Logger = new LoggerConfiguration() .WriteTo.Console() .WriteTo.File($"{AppDomain.CurrentDomain.BaseDirectory}\\log\\log-.txt", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") .CreateLogger(); //宣告Serilog初始化 UserbarButtonItem.ImageOptions.Image = imageCollection16x16.Images["UserDisconnected"]; SettingbarButtonItem.ImageOptions.Image = imageCollection16x16.Images["Setting"]; SettingbarButtonItem.Visibility = BarItemVisibility.Never; #region Json載入 ButtonSetting = InitialMethod.ButtonLoad(); LogoSetting = InitialMethod.LogoLoad(); SqlDBSetting = InitialMethod.SqlDBLoad(); BankAccountSetting = InitialMethod.BankAccountLoad(); DemoSetting = InitialMethod.DemoLoad(); #endregion ConnectionFlag = DemoSetting.ConnectionFlag; #region 入Logo圖片 if (LogoSetting != null) { if (File.Exists(LogoSetting.LogoPath)) { LogopictureEdit.Image = Image.FromFile(LogoSetting.LogoPath); } else { LogopictureEdit.Image = LogoimageCollection.Images["ewatch_logo"]; } } else { LogopictureEdit.Image = LogoimageCollection.Images["ewatch_logo"]; } #endregion #region 鈕載入 NavigationFrame = new NavigationFrame() { Dock = DockStyle.Fill }; NavigationFrame.Parent = DisplaypanelControl; ButtonControl = new ButtonControl() { Form1 = this, navigationFrame = NavigationFrame }; ButtonControl.AccordionLoad(accordionControl1, ButtonSetting); #endregion #region 資料庫方法載入 if (SqlDBSetting != null && ConnectionFlag) { SqlMethod = new SqlMethod() { setting = SqlDBSetting, BankAccountSetting = BankAccountSetting }; SqlMethod.SQLConnect(); if (SqlMethod.Search_DataBase()) { SQLDataBaseFlag = true; GatewayConfigs = SqlMethod.Search_GatewayConfig(); //通道資訊 ElectricConfigs = SqlMethod.Search_Electricconfig(); //電表設備資訊 SqlComponent = new SqlComponent() { SqlMethod = SqlMethod, BankAccountSetting = BankAccountSetting }; SqlComponent.MyWorkState = ConnectionFlag; } else { SQLDataBaseFlag = false; } } else if (!ConnectionFlag) { SQLDataBaseFlag = true; GatewayConfig gateway = new GatewayConfig() { GatewayIndex = 0, GatewayName = "通道1" }; GatewayConfigs.Add(gateway); for (int i = 0; i < 20; i++) { if (i == 0) { ElectricConfig electric = new ElectricConfig() { TotalMeterFlag = true, DeviceName = $"電表{i + 1}", GatewayIndex = 0, DeviceIndex = i, DeviceID = i + 1 }; ElectricConfigs.Add(electric); } else { ElectricConfig electric = new ElectricConfig() { TotalMeterFlag = false, DeviceName = $"電表{i + 1}", GatewayIndex = 0, DeviceIndex = i, DeviceID = i + 1 }; ElectricConfigs.Add(electric); } } } #endregion #region 通訊 if (ConnectionFlag && GatewayConfigs != null && ElectricConfigs != null) { foreach (var item in GatewayConfigs) { GatewayEnumType = (GatewayEnumType)item.GatewayEnumType; var electricconfigs = ElectricConfigs.Where(g => g.GatewayIndex == item.GatewayIndex).ToList(); switch (GatewayEnumType) { case GatewayEnumType.ModbusRTU: { SerialportComponent component = new SerialportComponent() { BankAccountSetting = BankAccountSetting, Gatewayconfig = item, ElectricConfigs = electricconfigs }; component.MyWorkState = ConnectionFlag; ModbusComponents.Add(component); } break; case GatewayEnumType.ModbusTCP: { TcpComponent component = new TcpComponent() { BankAccountSetting = BankAccountSetting, Gatewayconfig = item, ElectricConfigs = electricconfigs }; component.MyWorkState = ConnectionFlag; ModbusComponents.Add(component); } break; } } } #endregion #region 畫面 foreach (var item in ButtonSetting.ButtonGroupSettings) { foreach (var Buttonitem in item.ButtonItemSettings) { ButtonItemEnumType = (ButtonItemEnumType)Buttonitem.ButtonItemEnumType; switch (ButtonItemEnumType) { case ButtonItemEnumType.Home: { ChargingPileView chargingPileView = new ChargingPileView(ElectricConfigs, SqlMethod, this, GatewayConfigs) { SqlMethod = SqlMethod, Dock = DockStyle.Fill }; field4UserControls.Add(chargingPileView); NavigationFrame.AddPage(chargingPileView); } break; case ButtonItemEnumType.Report: { dataReportUserControl = new DataReportUserControl(ElectricConfigs) { SqlMethod = SqlMethod, Dock = DockStyle.Fill, Form1 = this }; NavigationFrame.AddPage(dataReportUserControl); } break; case ButtonItemEnumType.BillingSheet: { billingSheetUserControl = new BillingSheetUserControl(ElectricConfigs) { BankaccountSetting = BankAccountSetting, SqlMethod = SqlMethod, Dock = DockStyle.Fill, Form1 = this }; NavigationFrame.AddPage(billingSheetUserControl); } break; } } } User_ButtonItem_Visible(); #endregion timer1.Interval = 1000; timer1.Enabled = true; }