using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ICommLayer;
using CommLayerFactory;
using DBFactory;
namespace ControlSystem
{
    public class CMonitorDeviceState
    {
        CParsePLCData cppd = new CParsePLCData();
        DBOperator dbo = new DBOperator();
        DBOperator dboM = new DBOperator("ManConnString", "ManDBFactory");
        private string _monitorDeviceStateError = "";
        /// <summary>
        /// 实时监控设备状态时发生的故障
        /// </summary>
        public string MonitorDeviceStateError
        {
            get { return _monitorDeviceStateError; }
            set { _monitorDeviceStateError = value; }
        }

        public CMonitorDeviceState()
        {

        }
        /// <summary>
        /// 实时监视每个设备（堆垛机、输送机、RGV）的运行、故障、入口探物、出口探物；
        /// 反馈实时状态到FrmControlMonitor；反馈故障信息到管理的路径表IO_ROUTEPATH和调度T_Base_Device表F_ErrorCode
        /// </summary>
        public void RealTimeMonitorDeviceState()
        {
            string sql = "SELECT F_DeviceIndex, F_DeviceKindIndex, F_BindingDevice, F_BindingDeviceOut,F_DeviceVisual, F_ControlName, F_Picture, F_ErrorCode, F_HaveGoods FROM T_Base_Device WHERE (F_DeviceKindIndex = 1) OR (F_DeviceKindIndex = 2) OR (F_DeviceKindIndex = 4)";
            DataView dv = dbo.ExceSQL(sql).Tables[0].DefaultView;
            for (int i = 0; i < dv.Count; i++)
            {
                int[] states= cppd.GetDeviceState(Convert.ToInt32(dv[i]["F_DeviceIndex"]));
                if (states == null) continue;
                if (states[1] == 1)//运行
                {

                }
                if (states[1] >= 30)//故障
                {

                }
                if (dv[i]["F_BindingDevice"] != DBNull.Value)//找到它对应的设备索引的F_ControlName
                {
                    string sqlb = "SELECT F_DeviceIndex, F_DeviceKindIndex, F_BindingDevice, F_BindingDeviceOut,F_DeviceVisual, F_ControlName, F_Picture, F_ErrorCode, F_HaveGoods FROM T_Base_Device WHERE (F_DeviceKindIndex = " + dv[i]["F_BindingDevice"] + ") ";
                    DataView dvb = dbo.ExceSQL(sqlb).Tables[0].DefaultView;
                    if (dvb.Count > 0)
                    {

                    }
                }
                if (dv[i]["F_BindingDeviceOut"] != DBNull.Value)//找到它对应的设备索引的F_ControlName
                {
                    string sqlb = "SELECT F_DeviceIndex, F_DeviceKindIndex, F_BindingDevice, F_BindingDeviceOut,F_DeviceVisual, F_ControlName, F_Picture, F_ErrorCode, F_HaveGoods FROM T_Base_Device WHERE (F_DeviceKindIndex = " + dv[i]["F_BindingDeviceOut"] + ") ";
                    DataView dvb = dbo.ExceSQL(sqlb).Tables[0].DefaultView;
                    if (dvb.Count > 0)
                    {

                    }
                }
            }

        }
    }
}
