using Admin.Core.Common;
using Aucma.Core.RunPlc;
using log4net;
using Microsoft.AspNetCore.Builder;
using System;
using System.Text;
using System.Threading.Tasks;

namespace Admin.Core.Extensions
{
    /// <summary>
    ///PLC
    /// </summary>
    public static class PlcMildd
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(PlcMildd));
        public static async void UsePlcMildd(this IApplicationBuilder app, IRunPlcService plc)
        {
            if (app == null) throw new ArgumentNullException(nameof(app));
            try
            {
                if (Appsettings.app("Middleware", "Plc", "Enabled").ObjToBool())
                {
                    //计算程序运行时间
                    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                    StringBuilder sb = new StringBuilder();
                    stopwatch.Start(); //  开始监视代码
                                       ///////////////////////////////////////操作代码/////////////////////////////////////////////////////

                    await plc.StartMelsecMcSeverAsync();
                    await plc.StartSiemensSever();
                    await plc.StartMelsecPlcAsync();
                    await plc.StartSiemensPlcAsync();
                    ////////////////////////////////////////////////////////////////////////////////////////////////////
                    stopwatch.Stop(); //  停止监视
                    TimeSpan timeSpan = stopwatch.Elapsed; //  获取总时间
                    double hours = timeSpan.TotalHours; // 小时
                    double minutes = timeSpan.TotalMinutes;  // 分钟
                    double seconds = timeSpan.TotalSeconds;  //  秒数
                    double milliseconds = timeSpan.TotalMilliseconds;  //  毫秒数

                    Console.WriteLine($"秒数:{seconds},毫秒数:{milliseconds}");
                  
                }
            }
            catch (Exception e)
            {
                log.Error($"An error was reported when starting the job service.\n{e.Message}");
                throw;
            }
        }
    }
}