DBLineControl.cs 9.7 KB
using log4net;
using OnlineStore.Common;
using OnlineStore.DeviceLibrary.doubleStore;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using System.Windows.Forms;

namespace OnlineStore.DeviceLibrary
{
    /// <summary>
    /// 流水线读写文件操作
    /// </summary>
    public class DBLineControl
    {
        protected static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// 配置需要运行的流水线,同时只有一个流水线运行
        /// </summary>
        public static List<int> TheLineIdList = new List<int>();
        /// <summary>
        /// 0=需要等下位机信号,1=不等下位机信号,板子直接通过
        /// </summary>
        public static int NoWaitSingle = ConfigAppSettings.GetIntValue(Setting_Init.LineNoWaitSingle);

        public static int ChangeNum = 0;
        /// <summary>
        /// 当前使用的产品ID
        /// </summary>
        public static int CurrProjectID=0;
        /// <summary>
        /// 当前使用的产品名称
        /// </summary>
        public static string CurrProjectName="";
        public static string CurrM_Time = "";
        public static int File_UpdateTime = 300;
        /// <summary>
        /// 当前的宽度
        /// </summary>
        public static int CurrWidth=200;
        /// <summary>
        /// 与iLIB通信的MachineId
        /// </summary>
        public static string MachineId = ConfigAppSettings.GetValue(Setting_Init.ManchineID);
        /// <summary>
        /// 远程共享文件夹地址
        /// </summary>
        public static string RemoteURL = "";
        /// <summary>
        /// 共享文件夹用户名
        /// </summary>
        public static string RemoteUsername= "";
        /// <summary>
        /// 共享文件夹密码
        /// </summary>
        public static string RemotePassword = "";
        /// <summary>
        /// 监控文件的过滤条件
        /// </summary>
        public static string FileListerFilter = "";

        public static string CurrAA = "";
        public static string currM_Time = "";

        public static int PCB_CHECK = 0;
        /// <summary>
        /// 开始连接远程共享文件夹
        /// </summary>
        public static void StartConnect()
        {
            File_UpdateTime = ConfigAppSettings.GetIntValue(Setting_Init.File_UpdateTime);
           string  line = ConfigAppSettings.GetValue(Setting_Init.TheLineId);
           TheLineIdList = new List<int>();
           try
           {
               string[] objs = line.Split(',');
               foreach (string str in objs)
               {
                   TheLineIdList.Add(Int32.Parse(str));
               }
           }
           catch (Exception ex)
           {
               LogUtil.error(LOGGER, "解析启动的流水线ID时出错:" + ex.ToString());
           }

            MachineId = ConfigAppSettings.GetValue(Setting_Init.ManchineID);
            RemoteURL = ConfigAppSettings.GetValue(Setting_Init.RemoteURL);
            RemoteUsername = ConfigAppSettings.GetValue(Setting_Init.RemoteUsername);
            RemotePassword = ConfigAppSettings.GetValue(Setting_Init.RemotePassword);
            FileListerFilter = ConfigAppSettings.GetValue(Setting_Init.FileListerFilter);
            FileGenerated.StartListerDirectory(RemoteURL, "*.*");

            //读取所有产品
            string appPath = Application.StartupPath;
            string programConfig = ConfigAppSettings.GetValue(Setting_Init.ProgramConfig);
            Dictionary<string, DBLineProgram> map = CSVProgramReader<DBLineProgram>.AddCSVFile(appPath + programConfig);
        }
        /// <summary>
        /// 更改产品
        /// </summary>
        /// <param name="AA">产品ID</param>
        /// <param name="projectName">产品名称</param>
        public static void UpdateProject(string AA, string programName, string m_Time)
        {
            CurrAA = AA;
            currM_Time = m_Time;
            string AANumber = AA.Substring(7);
            int pID = Convert.ToInt32(AANumber);
            if (MachineId.Equals(""))
            {
                MachineId = ConfigAppSettings.GetValue(Setting_Init.ManchineID);
            }
            if (AA.Equals(CurrProjectID))
            {
                return;
            }

            //获取宽度
            int width = GetWidthById(programName);
            CurrWidth = width;
            LogUtil.info(LOGGER, DBStoreManager.StoreName + "收到产品更改命令:ID【" + AA + "】Name【" + programName + "】width【" + width + "】time【" + m_Time + "】");
            //更改失败
            if (width <= 0)
            {
                DBStoreManager.WarnMsg = DBStoreManager.StoreName + "收到产品AA【" + AA + "】PP【" + programName + "】更改命令:更改失败,未找到产品的宽度";
                LogUtil.info(LOGGER, DBStoreManager.WarnMsg);
                FileGenerated.writeRequestFile(MachineId, AA, programName, FileGenerated.REQUEST_NG, m_Time);
            }
            ChangeNum++;
            CurrProjectID = pID;
            CurrProjectName = programName;
            CurrM_Time = m_Time;

            //如果所有的宽度和当前一样,直接返回ok
            bool isOk = true;
            foreach (DB_LA_LineBean line in DBStoreManager.StoreLineMap.Values)
            {
                if (line.Config.IsInDebug == 0 && line.currWidth != CurrWidth && line.storeRunStatus >= StoreRunStatus.Runing)
                {
                    isOk = false;
                    break;
                }
            }
            if (isOk)
            {
                FileGenerated.writeRequestFile(MachineId, AA, programName, FileGenerated.REQUEST_OK, m_Time);
                //更新文件
                FileGenerated.writeLSTFile(MachineId);
                LogUtil.info(LOGGER, "收到的宽度和所有的流水线宽度一致,不需要更改宽度,直接返回ok");
            }
            else
            {
                LogUtil.info(LOGGER, "需要等待流水线更改宽度后才能OK");
            }
        }

        private static int GetWidthById(string programName)
        {
            DBLineProgram program = CSVProgramReader<DBLineProgram>.GetProgram(programName);
            if (program != null)
            {
                return program.ProgramWidth;
            }
            return -1;
        }


        public static string[] GetProgramList()
        {
            List<string> list = new List<string>(CSVProgramReader<DBLineProgram>.allProgramMap.Keys);
            return list.ToArray();
        }
        #region 松下流水线轨道计算备份(康奈德,凌华C154,电钢组合版)
        ////松下流水线轨道计算备份(康奈德,凌华C154,电钢组合版)
        //public static int GetNeedMS(int width)
        //{
        //     double newWidht = width;
        //    //if (width <= 70)
        //    //{ 
        //    //    newWidht = width + 2;
        //    //} 
        //    //else if (width < 120)
        //    //{
        //    //    newWidht = width - 1;
        //    //}
        //    //else if (width < 300)
        //    //{
        //    //    newWidht = width - 4;
        //    //}
        //    //int ms = (int)((double)((newWidht - 64) * 6000 / 130));
        //    //ms = ms + 1150;
        //    //return ms;

           
        //    int ms = (int)((double)((newWidht - 45) * 44.5)); 
        //    if (width <= 70)
        //    {                //70
        //        ms = (int)((double)((newWidht - 47.5) * 44.5));
        //    }
        //    else if (width <= 80)
        //    {                //75
        //        ms = (int)((double)((newWidht - 47) * 44.5));
        //    }
        //    else if (width <= 120)
        //    {                //100
        //        ms = 0 + (int)((double)((newWidht - 46) * 44.5));
        //    }
        //    else if (width <= 200)
        //    {                //200
        //        ms = 0 + (int)((double)((newWidht - 45) * 44.5));
        //    }
        //  ms = ms + 1243;
        //    return ms;
        //} 
        #endregion

       
        #region (康泰克,康奈德,电钢组合流水线1的轨道计算)
        ///// <summary>
        ///// 流水线轨道调宽,(康泰克,康奈德,电钢组合)
        ///// </summary> 
        //public static int GetNeedMS(int width)
        //{
        //    double newWidht = width;
        //    int ms = (int)((double)((newWidht - 52) * 108));
        //    if (width <= 70)
        //    {                //70
        //        ms = (int)((double)((newWidht - 51.2) * 108));
        //    }
        //    else if (width <= 80)
        //    {                //75
        //        ms = (int)((double)((newWidht - 51.2) * 108));
        //    }
        //    else if (width <= 120)
        //    {                //100
        //        ms = 0 + (int)((double)((newWidht - 51.6) * 108));
        //    }
        //    else if (width <= 200)
        //    {                //200
        //        ms = 0 + (int)((double)((newWidht - 52) * 108));
        //    }
        //    ms = ms + 1243;
        //    return ms;
        //} 
        #endregion
        /// <summary>
        /// 轨道调宽需要的脉冲数(硕科步进控制驱动器)
        /// </summary> 
        public static int GetNeedPosition(int width,int homeWidth)
        {
            //homeWidth是原点的宽度,1068是1毫米的脉冲数
            double newWidht = width * 10 + 10 - homeWidth;
            int position = (int)newWidht * 1068 / 10;             
            return position;
        } 
    }
}