using Common;
using Common.vo.pb;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace MeterPlugInLibrary
{
    public delegate void EventHotDeliveryDataCollection(object o, List<CollectModel> e);

    public class HotDeliveryDataCollectionControl
    {
        public event EventHotDeliveryDataCollection EventHotDeliveryDataCollectionArgs;//定义事件

        private Thread CollectionThread;//采集线程
        private string[] strPntNo = null;//计量点编号CAR40,CAR41,CAR42,CAR43,CAR44,CAR45

        /// <summary>
        /// 开启数据采集线程
        /// </summary>
        public void Start(params string[] strPntID)
        {
            this.strPntNo = strPntID;
            CollectionThread = new Thread(new ThreadStart(WgtThread));
            CollectionThread.Start();
        }

        public void Stop()
        {
            if (CollectionThread != null)
            {
                CollectionThread.Abort();
                CollectionThread = null;
            }
        }

        /// <summary>
        /// 重量采集线程
        /// </summary>
        private void WgtThread()
        {
            MemoryTableDataSocket MemoClass = new MemoryTableDataSocket("", AppConfigCache.icoredbTcp);//内存表
            //MemoryTableDataSocket MemoClass = new MemoryTableDataSocket("", "tarantool://guest@172.22.42.3:2101");//内存表
            while (true)
            {
                try
                {
                    Thread.Sleep(500);
                    List<CollectModel> larg = new List<CollectModel>();
                    List<JArray> lj = MemoClass.TrackTableList(strPntNo);//热送磅采集

                    //WriteThreadLog("数据采集线执行跟踪！");//2021年3月16日 杨秀东添加

                    if (lj != null) //正常采集，若为null则重量采集线程中断了
                    {
                        foreach (JArray jArray in lj)
                        {
                            CollectModel arg = new CollectModel();
                            arg.pointid = jArray[0].ToString(); //计量点的编号：CAR41
                            arg.weight = Convert.ToInt32(jArray[3].ToString());
                            arg.weightStatus = Convert.ToInt32(jArray[4].ToString());
                            arg.parkStatus = Convert.ToInt32(jArray[5].ToString());
                            arg.datetime = Convert.ToDateTime(jArray[6].ToString().Replace("T", " ").Replace("Z", ""));
                            arg.licType = Convert.ToInt32(jArray[7].ToString());
                            if (arg.licType == 0) //抓拍摄像头的数据
                            {
                                arg.carno = jArray[2].ToString();
                            }
                            else
                            {
                                arg.carno = jArray[8].ToString();
                            }
                            larg.Add(arg);
                        }
                    }
                    EventHotDeliveryDataCollectionArgs(this, larg); //赋值给主页面调用
                }
                catch (Exception exp)
                {
                    WriteThreadLog("数据采集线程异常！" + exp.Message);
                }
            }
        }

        public void WriteLog(string str)
        {
            try
            {
                string m_szRunPath;
                m_szRunPath = System.Environment.CurrentDirectory;
                if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
                {
                    System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
                }
                string strDate = System.DateTime.Now.ToString("yyyyMMdd");
                string strPathFile = m_szRunPath + "\\log\\" + strDate;
                if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(strPathFile);
                }
                System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\数据采集_" + strDate + ".log", true);

                tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                tw.WriteLine(str);
                tw.WriteLine("\r\n");

                tw.Close();
            }
            catch (Exception exp)
            {
            }
        }

        /// <summary>
        /// 写线程日志
        /// </summary>
        /// <param name="str"></param>
        public void WriteThreadLog(string str)
        {
            try
            {
                string m_szRunPath;
                m_szRunPath = System.Environment.CurrentDirectory;
                if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
                {
                    System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
                }
                string strDate = System.DateTime.Now.ToString("yyyyMMdd");
                string strPathFile = m_szRunPath + "\\log\\" + strDate;
                if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(strPathFile);
                }
                System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\数据采集线程_" + strDate + ".log", true);

                tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                tw.WriteLine(str);
                tw.WriteLine("\r\n");

                tw.Close();
            }
            catch (Exception exp)
            {
            }
        }

        public void WriteLogUpCardNo(string str)
        {
            try
            {
                string m_szRunPath;
                m_szRunPath = System.Environment.CurrentDirectory;
                if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
                {
                    System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
                }
                string strDate = System.DateTime.Now.ToString("yyyyMMdd");
                string strPathFile = m_szRunPath + "\\log\\" + strDate;
                if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(strPathFile);
                }
                System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\修正车号_" + strDate + ".log", true);

                tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                tw.WriteLine(str);
                tw.WriteLine("\r\n");

                tw.Close();
            }
            catch (Exception exp)
            {
            }
        }

        public void WriteLogStatus(string str)
        {
            try
            {
                string m_szRunPath;
                m_szRunPath = System.Environment.CurrentDirectory;
                if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
                {
                    System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
                }
                string strDate = System.DateTime.Now.ToString("yyyyMMdd");
                string strPathFile = m_szRunPath + "\\log\\" + strDate;
                if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(strPathFile);
                }
                System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\重量稳定判断_" + strDate + ".log", true);

                tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                tw.WriteLine(str);
                tw.WriteLine("\r\n");

                tw.Close();
            }
            catch (Exception exp)
            {
            }
        }
    }
}