using IL.Common;
using MediII.Adapter.Scan.Interface;
using MediII.Adapter.Scanner;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO.Pipes;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace MediII.Adapter.MsgToScanMServer
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MsgToScanMServer : ServiceBase
    {

        private bool _addPool = false;//是否启动  
        private Scaner scanner = new Scaner();
        /// <summary>
        /// 
        /// </summary>
        public MsgToScanMServer()
        {
            InitializeComponent();

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            sp_MessageLog(null,
                new LogEventArgs()
                {
                    LogTime = DateTime.Now,
                    LogModel = new LogModel() { Message = "服务启动" }
                });

            LogHelper.LogDebug("服务启动", LogCatagories.General);
            LogHelper.LogInfo("服务启动", LogCatagories.General);
            LogHelper.LogError("服务启动", LogCatagories.General);


            LogHelper.LogDebug("服务启动", LogCatagories.Sock);
            LogHelper.LogInfo("服务启动", LogCatagories.Sock);
            LogHelper.LogError("服务启动", LogCatagories.Sock);

            LogHelper.LogDebug("服务启动", LogCatagories.AdapterScan);
            LogHelper.LogInfo("服务启动", LogCatagories.AdapterScan);
            LogHelper.LogError("服务启动", LogCatagories.AdapterScan);

            LogHelper.LogDebug("服务启动", LogCatagories.SQL);
            LogHelper.LogInfo("服务启动", LogCatagories.SQL);
            LogHelper.LogError("服务启动", LogCatagories.SQL);


            System.Diagnostics.Debug.WriteLine("OnStart", ApplictStatic.ServerName);
            //_running = true;
            if (!_addPool)
            {
                ThreadPool.QueueUserWorkItem(DoTask);
                _addPool = true;
            }
        }

        private void DoTask(object state)
        {
            var args = new string[] { "UE" };
            if (args.Length > 0)
            {

                string dllPath = System.IO.Path.Combine(Application.StartupPath, args[0]);
                string path = Environment.GetEnvironmentVariable("path") + ";" + dllPath;
                Environment.SetEnvironmentVariable("path", path);
                scanner.Start(new string[] { dllPath }, sp_MessageLog);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void OnStop()
        {
            sp_MessageLog(null, new LogEventArgs() { LogTime = DateTime.Now, LogModel = new LogModel() { Message = "服务停止" } });

            scanner.Stop();
            System.Threading.Thread.Sleep(10 * 1000);//暂时10秒再退出
        }


        void sp_MessageLog(object sender, LogEventArgs e)
        {
            ServiceStatusPublishManager.Instance.AppendMessage(e);
        }
    }


}
