using com.hnshituo.core.webapp.vo;
using Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace StorageMeterSystem
{
    public delegate void StorageEventDbCollection(object o, List<StorageCollectModel> e);
    public class StorageDataDbCollection
    {
        public event StorageEventDbCollection EventDataCollectionArgs;//定义事件
        private Thread CollectionThread;//采集线程

        /// <summary>
        /// 开启数据采集线程 
        /// </summary>
        public void Start()
        {
            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;
            MeterWorkStockOnlineService stockOnlineService = new MeterWorkStockOnlineService();

            while (true)
            {
                try
                {
                    icount++;
                    Thread.Sleep(500);
                    StorageCollectModel arg = new StorageCollectModel();
                    RESTfulResult<List<StorageCollectModel>> rES = stockOnlineService.doQueryStaticDbWf();
                    if (rES.Succeed)
                    {
#if DEBUG
                        if (rES.Data.Count == 0) 
                        {
                            rES.Data.Add(new StorageCollectModel { pointid = "CAR20", mainWeightStatus = 0, mainWgt = 1000, viceWgt = 1000, voiceWeightStatus = 1 });
                        }
#endif
                        if (rES.Data.Count > 0) 
                        {
                            foreach (StorageCollectModel scm in rES.Data) 
                            {
                                scm.pointid = PbStorageCache.ltMonitor.Where(s => s.pointNo == scm.pointid).FirstOrDefault().baseSpotNo;
                            }
                        }

                        EventDataCollectionArgs(this, rES.Data);
                    }
                    else 
                    {
                        WriteLog("数据采集线程异常！" + rES.Message);
                    }
                }
                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)
            {

            }
        }
    }
}
