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

namespace CarMonitor
{
    /// <summary>
    /// 基础数据获取
    /// </summary>
    public class BaseDbCls
    {
        Log lg = Log.GetInstance();
        /// <summary>
        /// 将基础数据全部载入缓存中
        /// </summary>
        /// <returns></returns>
        public bool setBaseDb()
        {
            #region 初始化一次缓存类
            PbCacheMonitor.monitor = new MeterWorkMonitor();
            PbCacheMonitor.Load();
            #endregion

            #region 读取音频文件名称
            List<string> ls = new List<string>();
            DirectoryInfo folder = new DirectoryInfo(PbCacheMonitor.path + "\\Sound");

            foreach (FileInfo file in folder.GetFiles("*.wav"))
            {
                ls.Add(file.Name.Replace(".wav", ""));
            }
            PbCacheMonitor.voiceInfo = ls;
            #endregion

            #region 所有摄像头配置信息
            MeterBaseVideoinfoService mbvs = new MeterBaseVideoinfoService();
            RESTfulResult<List<MeterBaseVideoinfo>> rm = mbvs.doQueryWf(new MeterBaseVideoinfo { });
            if (rm.Succeed && rm.Data != null && rm.Data.Count > 0)
            {
                PbCacheMonitor.ltVideoInfo = rm.Data;
            }
            else
            {
                lg.WriteLog(1, "未找到摄像头配置信息MeterBaseVideoinfo");
                return false;
            }
            #endregion

            #region 所有计量点数据
            MeterBaseSpotInfoService service = new MeterBaseSpotInfoService();
            RESTfulResult<List<MeterBaseSpotInfo>> rms = service.doQueryWf(new MeterBaseSpotInfo { validFlag = "1" });
            if (rms.Succeed && rms.Data != null && rms.Data.Count > 0)
            {
                PbCacheMonitor.sportInfos = rms.Data;
            }
            else
            {
                lg.WriteLog(1, "MeterBaseSpotInfo");
                return false;
            }
            #endregion

            #region 秤体量程配置 
            MeterBaseScaleRangeService rangeService = new MeterBaseScaleRangeService();
            RESTfulResult<List<MeterBaseScaleRange>> rmR = rangeService.doQueryWf(new MeterBaseScaleRange { });
            if (rmR.Succeed && rmR.Data != null && rmR.Data.Count > 0)
            {
                PbCacheMonitor.ltRange = rmR.Data;
            }
            else
            {
                lg.WriteLog(1, "未找到秤体量程配置信息MeterBaseScaleRange");
                return false;
            }
            #endregion

            return true;
        }

        public bool getDb() 
        {
            #region 实时获取监控信息
            MeterWorkMonitorService monitorService = new MeterWorkMonitorService();
            RESTfulResult<List<MeterWorkMonitor>> rmMonitor = monitorService.doQueryAllWf(new MeterWorkMonitor { validFlag = "1" });
            if (rmMonitor.Succeed)
            {
                PbCacheMonitor.ltWorkMonitor = rmMonitor.Data;
                return true;
            }
            else 
            {
                lg.WriteLog(4, "未找到秤体量程配置信息MeterBaseScaleRange");
                return false;
            }
            #endregion
        }

        public bool getSpot()
        {
            #region 获取摄像头数据用于截图
            if (PbCacheMonitor.ltVideoInfo != null && PbCacheMonitor.ltVideoInfo.Count > 0)
            {
                PbCacheMonitor.videoInfo = PbCacheMonitor.ltVideoInfo.Where(s => s.pointid == PbCacheMonitor.sportInfo.baseSpotNo).FirstOrDefault();
                List<MeterBaseVideoChild> videoChild = new List<MeterBaseVideoChild>();

                Type postType = PbCacheMonitor.videoInfo.GetType();
                PropertyInfo[] postTypeInfos = postType.GetProperties(); //返回为当前 Type 的所有公共属性,PropertyInfo[] PropertyInfo 的所有公共属性的 Type 对象数组
                foreach (PropertyInfo p in postTypeInfos)
                {
                    if (p.Name.Contains("position"))
                    {
                        string str = (string)p.GetValue(PbCacheMonitor.videoInfo, null);
                        if (str != "" && str != null)
                        {
                            videoChild.Add(
                                new MeterBaseVideoChild
                                {
                                    pointid = PbCacheMonitor.videoInfo.pointid,
                                    p_ip = str,
                                    p_rate = "0",//************解码率直接给定了，这里没取配置值*************
                                    vdo_port = PbCacheMonitor.videoInfo.vdoPort,
                                    vdo_pwd = PbCacheMonitor.videoInfo.vdoPwd,
                                    vdo_user = PbCacheMonitor.videoInfo.vdoUser
                                }
                            );
                        }
                    }
                }

                PbCacheMonitor.videoChild = videoChild;
            }
            #endregion

            return true;
        }
    }
}
