
using System.Text;
using System.Threading.Tasks;
using BLWLogProduce.Models;
using NLog;

namespace BLWLogProduce.Services
{
    public class MyTimer : BackgroundService
    {
        public System.Timers.Timer jiankong_timer = null;
        public MyTimer()
        {

        }
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            jiankong_timer = new System.Timers.Timer();
            jiankong_timer.Interval = 60000;
            jiankong_timer.Elapsed += Jiankong_timer_Elapsed;
            jiankong_timer.Start();
            return Task.CompletedTask;
        }
        public static Logger logger = LogManager.GetCurrentClassLogger();
        private void Jiankong_timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                var df = WaiBuJianKong.data;

                string NewStringData = WaiBuJianKong.Data;
                NewStringData = NewStringData.Replace("{1}", df.UDP_Upload)
                                            .Replace("{2}", df.UDP_Sent)
                                            .Replace("{3}", df.RCU_Online)
                                            .Replace("{4}", df.RCU_TakeCard)
                                            .Replace("{5}", df.RCU_Heart)
                                            .Replace("{6}", df.RCU_SearchHost)
                                            .Replace("{7}", df.Intercept)
                                            .Replace("{8}", df.WebUser)
                                            .Replace("{9}", df.Energy)
                                            .Replace("{10}", df.RCU_OE)
                                            .Replace("{15}", df.WireShark_UDPCapCount)
                                            .Replace("{16}", df.Process_TH1.ToString())
                                            .Replace("{17}", df.Process_TH2.ToString())
                                            .Replace("{18}", df.Process_TH3.ToString())
                                            .Replace("{19}", df.Process_TH4.ToString())
                                            .Replace("{20}", df.Process_TH5.ToString())
                                            .Replace("{21}", df.TotalErrorPackageReceiveCount)
                                            .Replace("{22}", df.GetRoomAirList.ToString())
                                            .Replace("{23}", df.SetRCUAir.ToString())
                                            .Replace("{24}", df.GetRoomSceneList.ToString())
                                            .Replace("{25}", df.SetRCUScene.ToString())
                                            .Replace("{26}", df.GetRoomLightList.ToString())
                                            .Replace("{27}", df.SetRCULight.ToString())
                                            .Replace("{28}", df.GetRoomCurtainList.ToString())
                                            .Replace("{29}", df.SetRCUCurtain.ToString())
                                            .Replace("{30}", df.GetRoomServiceList.ToString())
                                            .Replace("{31}", df.SetRCUService.ToString())
                                            .Replace("{32}", df.GetOperationLog.ToString())
                                            .Replace("{33}", df.GetRoomMusicList.ToString())
                                            .Replace("{34}", df.SetRCUMusic.ToString())
                                            .Replace("{35}", df.GetRCUStatus.ToString())
                                            .Replace("{36}", df.GetAirDetectList.ToString())
                                            .Replace("{37}", df.GetHostFaultList.ToString())
                                            .Replace("{38}", df.GetRoomTypeAndModalsList.ToString())
                                            .Replace("{39}", df.GetRCUInfoForPDU.ToString())
                                            .Replace("{40}", df.GetSessionKey.ToString())
                                            .Replace("{41}", df.GetPhoneNumber.ToString())
                                            .Replace("{42}", df.GetHotelInfoForWX.ToString());

                int a = 0;
                int b = 0;
                int.TryParse(df.TCL_Ctr_D, out a);
                int.TryParse(df.TCL_Ctr_C, out b);
                NewStringData = NewStringData.Replace("{11}", (a + b).ToString());


                int c = 0;
                int d = 0;
                int.TryParse(df.TM_Ctr_D, out c);
                int.TryParse(df.TM_Ctr_C, out d);
                NewStringData = NewStringData.Replace("{12}", (c + d).ToString());


                int ee = 0;
                int ff = 0;
                int.TryParse(df.XD_Ctr_D, out ee);
                int.TryParse(df.XD_Ctr_C, out ff);
                NewStringData = NewStringData.Replace("{13}", (ee + ff).ToString()).Replace("{14}", "0");


                WanBuJianKong(NewStringData, "1.txt");
                File.WriteAllText("2.txt", NewStringData, Encoding.UTF8);
                Console.WriteLine("here");
                WaiBuJianKong.data.Process_Error = 0;
                WaiBuJianKong.data.Process_TH1 = 0;
                WaiBuJianKong.data.Process_TH2 = 0;
                WaiBuJianKong.data.Process_TH3 = 0;
                WaiBuJianKong.data.Process_TH4 = 0;
                WaiBuJianKong.data.Process_TH5 = 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
            }
        }


        public static void WanBuJianKong(string Data, string SourceTxt)
        {
            System.IO.File.WriteAllText(SourceTxt, Data, Encoding.UTF8);
            // 如果目标文件存在，先删除
            if (System.IO.File.Exists(WaiBuJianKong.WaiBuJianKongTargetFileName))
            {
                System.IO.File.Delete(WaiBuJianKong.WaiBuJianKongTargetFileName);
            }

            // 重命名/移动文件
            System.IO.File.Move(SourceTxt, WaiBuJianKong.WaiBuJianKongTargetFileName);
        }
    }
}
