using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

using FObjBase;
using Misc;
using FLY.FilmCasting.AutoDie.Server.Model;
using System.Threading.Tasks;
using FLY.OBJComponents.Server;

namespace FLY.FilmCasting.AutoDie.Server
{
    public class TDGage
    {
        #region 参数--------------------------------------------------------------------
        #region 初始化------------------------------------------------------------------
        public string mParamDirectory;//参数目录 m_path;//数据文件的 根目录默认是 Gage1
        public string mName;//名字 如:gage0
        #endregion
        #region 本地数据----------------------------------------------------------------
        public SysParam mSysParam;
        public FeedbackHeat mfeedback;
        #endregion
        #region 服务器数据--------------------------------------------------------------

        FLY.Thick.FilmCasting.IService.IBoltMapFilmCastingService boltMapFilmCasting;
        public FLY.Thick.FilmCasting.IService.IBulkDbService bulkDb;
        FLY.Thick.Base.IService.ITDGageService gageService;
        /// <summary>
        /// 报警系统
        /// </summary>
        public FLY.OBJComponents.Server.WarningSystem2 mWarning;

        public HistoryDb mHistoryDb;

        DbModel mDbModel;
        
        #endregion
        #endregion


        public TDGage(string nam)
        {
            mName = nam;
            mParamDirectory = mName;// @"D:\" + m_name;

            if (!System.IO.Directory.Exists(mParamDirectory))
            {
                System.IO.Directory.CreateDirectory(mParamDirectory);
            }
            System.Environment.CurrentDirectory = mParamDirectory;

            mSysParam = new SysParam();
            mfeedback = new FeedbackHeat();

            mDbModel = new DbModel();
            mHistoryDb = new HistoryDb();
            mWarning = new WarningSystem2();
        }


        #region Init登陆服务器初始化----------------------------------------------------

        public async Task Init()
        {
            //---------------------------------------------------------------------------------------------------------------
            //数据库
            await Task.Factory.StartNew(() =>
            {
                //初始数据库(加载 MaxId)
                mDbModel.Init();

                //删除不需要的备份数据
                mDbModel.KeepBackupSize(12);

                //备份上一个月之前的数据到 yyyy-MM 文件夹
                mDbModel.BackupBbInSize(12);

                //当前的数据库只保存上个月与这个月的数据
                mDbModel.KeepDBSize(1);
            });

            mHistoryDb.Init(mDbModel);





            #region 提取保存的数据


            //---------------------------------------------------------------------------------------------------------------
            //Warning_Create
            
            mWarning.Init(mHistoryDb.ErrorBuffer);


            #region 与服务器同步的数据
            string thick_connName = "thick";
            FObjServiceClientManager.Instance.ConnAddrs.Add(new ConnAddr() { ConnName = thick_connName, Addr = mSysParam.ThickAddr });
            boltMapFilmCasting = new FLY.Thick.FilmCasting.Client.BoltMapFilmCastingServiceClient(FLY.Thick.FilmCasting.OBJ_INTERFACE.OBJ_INTERFACE_ID.BOLTMAP_ID, thick_connName);
            bulkDb = new FLY.Thick.FilmCasting.Client.BulkDbServiceClient(FLY.Thick.FilmCasting.OBJ_INTERFACE.OBJ_INTERFACE_ID.BULKDB_ID, thick_connName);
            gageService = new FLY.Thick.Base.Client.TDGageServiceClient(FLY.Thick.FilmCasting.OBJ_INTERFACE.OBJ_INTERFACE_ID.TDGAGE_ID, thick_connName);

            #endregion

            mfeedback.Init(
                boltMapFilmCasting,
                bulkDb,
                gageService,
                mWarning,
                mHistoryDb
                );
            

            #endregion

        }



        #endregion

    }
}
