using Admin.Core.Common;
using Admin.Core.RunPlc;
using log4net;
using Microsoft.AspNetCore.Builder;
using System;

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())
                {
                    await plc.StartPlcAsync();
                }
            }
            catch (Exception e)
            {
                log.Error($"An error was reported when starting the job service.\n{e.Message}");
                throw;
            }
        }
    }
}