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

namespace MeterPlugInLibrary
{
    public delegate void StaticEventDataCollection(object o, StaticCollectModel e);

    public class StaticDataCollectionControl
    {
        public event StaticEventDataCollection EventDataCollectionArgs;//定义事件
        private Thread CollectionThread;//采集线程

        private string strPntNo = "";//计量点编号CAR11  CAR12

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

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

        /// <summary>
        /// 重量采集线程
        /// </summary>
        private void WgtThread()
        {

            int icount = 0;
            MemoryTableDataSocket MemoClass = new MemoryTableDataSocket(strPntNo);//内存表

            while (true)
            {
                try
                {
                    icount++;
                    Thread.Sleep(500);
                    StaticCollectModel arg = new StaticCollectModel();

                    JArray jArray = MemoClass.TrackTable(strPntNo);
                    if (jArray != null) //正常采集，若为null则重量采集线程中断了
                    {
                        arg.carno = jArray[2].ToString().Length > 14 ? jArray[2].ToString().Substring(7, 7) : jArray[2].ToString();
                        arg.mainWgt = 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());
                        //arg.vdioCarNos = jArray[8].ToString();
                        //arg.RfidNos = jArray[9].ToString();

                        arg.mainWgt = jArray.Count > 14 ? Convert.ToInt32(jArray[14]) : 0;
                        arg.viceWgt= jArray.Count > 15 ? Convert.ToInt32(jArray[15]) : 0;

                        //2021年7月16日 杨秀东对丢失精度进行处理
                        if (arg.mainWgt.ToString().EndsWith("1"))
                            arg.mainWgt = arg.mainWgt - 1;
                        if (arg.mainWgt.ToString().EndsWith("9"))
                            arg.mainWgt = arg.mainWgt + 1;

                        if (arg.viceWgt.ToString().EndsWith("1"))
                            arg.viceWgt = arg.viceWgt - 1;
                        if (arg.viceWgt.ToString().EndsWith("9"))
                            arg.viceWgt = arg.viceWgt + 1;

                        arg.carType = "";//车型，在rfid中取
                        arg.firstRed = "";//第一个红外
                        arg.secondRed = "";//第二个红外
                        arg.thirdRed = "";//第三个红外
                    }

                    if (strPntNo == "") //两个红外的情况
                    {
                        JArray jArray1 = MemoClass.LiveTable(strPntNo, "tagName");
                        if (jArray1 != null) 
                        {
                            arg.firstRed = jArray1[1].ToString();
                        }
                        JArray jArray2 = MemoClass.LiveTable(strPntNo, "tagName");
                        if (jArray2 != null)
                        {
                            arg.secondRed = jArray2[1].ToString();
                        }
                    }
                    else if (strPntNo == "") //三个红外的情况
                    {
                        JArray jArray1 = MemoClass.LiveTable(strPntNo, "tagName");
                        if (jArray1 != null)
                        {
                            arg.firstRed = jArray1[1].ToString();
                        }
                        JArray jArray2 = MemoClass.LiveTable(strPntNo, "tagName");
                        if (jArray2 != null)
                        {
                            arg.secondRed = jArray2[1].ToString();
                        }
                        JArray jArray3 = MemoClass.LiveTable(strPntNo, "tagName");
                        if (jArray3 != null)
                        {
                            arg.thirdRed = jArray3[1].ToString();
                        }
                    }


                    //每隔0.5秒调用一次写入到界面数据
                    //if (icount > 4)
                    {
                        icount = 0;
                        EventDataCollectionArgs(this, arg);
                    }
                }
                catch (Exception exp)
                {
                    WriteLog("数据采集线程异常！" + 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)
            {

            }
        }
    }
}
