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 ProductDataCollection(object o, List<StorageCollectModel> e);

    public class ProductDataControl
    {
        public event ProductDataCollection EventDataCollectionArgs;//定义事件
        private Thread CollectionThread;//采集线程
        private string[] strPntNo = null;//计量点编号CAR11  CAR12
        
        /// <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("", "tarantool://guest@172.22.42.3:2101");//内存表

            while (true)
            {
                try
                {
                    Thread.Sleep(500);
                    List<StorageCollectModel> larg = new List<StorageCollectModel>();

                    List<JArray> lj = MemoClass.TrackTable(strPntNo);
                    //*
                    if (lj != null) //正常采集，若为null则重量采集线程中断了
                    {
                        foreach (JArray jr in lj)
                        {
                            StorageCollectModel arg = new StorageCollectModel();
                            arg.pointid = jr[0].ToString();
                            arg.mainWeightStatus = Convert.ToInt32(jr[2].ToString()); //0:重量稳定; 1:重量不稳定;  2:空磅
                            arg.datetime = Convert.ToDateTime(jr[3].ToString().Replace("T", " ").Replace("Z", "")); //上称时间    uint 当前时间的距离1970.1.1.08:00时间的秒数; 秒
                            arg.mainWgt = Convert.ToDouble(jr[4].ToString());
                            arg.viceWgt = jr.Count > 5 ? Convert.ToDouble(jr[5].ToString()) : 0;
                            //arg.voiceWeightStatus = Convert.ToInt32(jArray[2].ToString());  
                            larg.Add(arg);
                        }
                    }

                    //每隔0.5秒调用一次写入到界面数据
                    EventDataCollectionArgs(this, larg);
                }
                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)
            {

            }
        }
    }
}
