using Common;
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 StorageEventDataCollection(object o, StorageCollectModel e);

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

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

        /// <summary>
        /// 开启数据采集线程 
        /// </summary>
        public void Start(string strPntID)
        {
            this.strPntNo = strPntID;
            PbStorageCache.collect = new StorageCollectModel();
            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);
                    StorageCollectModel arg = new StorageCollectModel();

                    JArray jArray = MemoClass.TrackTable(strPntNo);
                    if (jArray != null) //正常采集，若为null则重量采集线程中断了
                    {
                        arg.mainWgt = Convert.ToInt32(jArray[3].ToString());
                        arg.mainWeightStatus = Convert.ToInt32(jArray[4].ToString());
                        arg.voiceWeightStatus = Convert.ToInt32(jArray[4].ToString());

                        arg.datetime = Convert.ToDateTime(jArray[6].ToString().Replace("T", " ").Replace("Z", ""));
                        
                        arg.mainWgt = jArray.Count > 14 ? Convert.ToInt32(jArray[14]) : 0;
                        arg.viceWgt = jArray.Count > 15 ? Convert.ToInt32(jArray[15]) : 0;
                    }

                   

                    //每隔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)
            {

            }
        }
    }
}
