using Newtonsoft.Json;
using OpcBasic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using SystemFramework;

namespace MesWork
{
    public partial class Temp { }
    public partial class MesWorkForm
    {
        protected override void OnShown(EventArgs e)
        {
            var text = System.IO.File.ReadAllText(@"config.json");
            scadaConfig = JsonConvert.DeserializeObject<ScadaConfig>(text);

            ThreadPool.SetMaxThreads(2000, 800);
            Never_Know.Hello_You_All_Never_Know = Hello_You_All_Never_Know;
            new ApplicationLog();

            ConnectService();

            LoadOPCData(BasicDataTableFile, IsDemoMesServer, IsWriteMonitorLog, true, true, out string err);
            if (err.Length > 0)
            {
                MessageBox.Show($"LoadOPCData错误：{err}");
                Process.GetCurrentProcess().Kill();
            }


            CommunicationCheckOnLine();

            LoadDevice(scadaConfig);

            Thread.Sleep(200);
            StartServer();
            //Thread.Sleep(200);
            //First_TagMonitor();

            //Thread.Sleep(1000);
            //Loop_Fast_Task();
            // Loop_Slow_Task();
            // 开启webApi
            initServer = new InitServer(MesWorkForm.WebSvrUrlPort);
 

            var task1 = new Thread(new ThreadStart(Loop));
            task1.IsBackground = true;
            task1.Start();

            //var task2 = new Thread(new ThreadStart(getCharDataToken));
            //task2.IsBackground = true;
            //task2.Start();

            //var task3 = new Thread(new ThreadStart(getCharData));
            //task3.IsBackground = true;
            //task3.Start();

            //var task4 = new Thread(new ThreadStart(StationNameStatus_Update));
            //task4.IsBackground = true;
            //task4.Start();

            //var task5 = new Thread(new ThreadStart(StationNameDeviceData_Insert));
            //task5.IsBackground = true;
            //task5.Start();

            //var task6 = new Thread(new ThreadStart(StationNameDeviceData_Update));
            //task6.IsBackground = true;
            //task6.Start(); 

            //var task7 = new Thread(new ThreadStart(SelectData_WritePLC_Task));
            //task7.IsBackground = true;
            //task7.Start();

        }

    }
}
