ConfigItemBase.cs 11.6 KB
using log4net;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 

namespace OnlineStore.LoadCSVLibrary
{
    /// <summary>
    /// 基础的属性配置
    /// </summary>
    public class ConfigBase
    {
        public string DataStr = "";
    //    protected static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// DI=输入IO,DO=输出IO,PRO=属性,AXIS=轴
        /// </summary>
        [CSVAttribute("类型")]
        public string ProType { get; set; }
        /// <summary>
        /// 说明
        /// </summary>
        [CSVAttribute("说明")]
        public string Explain { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        [CSVAttribute("名称")]
        public string ProName { get; set; }
        /// <summary>
        /// 属性值
        /// </summary>
        [CSVAttribute("属性值")]
        public string ProVale { get; set; }

        public ushort GetIOAddr()
        {
            ushort value = 0;
            try
            {
                value = (ushort)Convert.ToInt32(ProVale);
            }
            catch (Exception ex)
            {
                LogUtil.error("出错:", ex);
            }
            return value;
        }
        public virtual int GetValue()
        {
            if (ProVale.Equals(""))
            {
                return -1;
            }
            return Convert.ToInt32(ProVale);
        }
        public override string ToString()
        {
            return   "ConfigBase。" + "类型:" + ProType + ",说明:" + Explain + ",名称:" + ProName + ",属性值:" + ProVale;
        }

        public virtual void CheckField()
        {
            if (String.IsNullOrEmpty(ProType) || String.IsNullOrEmpty(ProName) ||   String.IsNullOrEmpty(ProVale))
            {
                throw new CVSFieldNotMatchingExection("["+ToString() + "],【类型:ProType】【名称:ProName】【属性值:ProVale】必须配置值!");
            }
        }
    }
    /// <summary>
    /// 运动轴配置
    /// </summary>
    public class ConfigMoveAxis : ConfigBase
    {
        /// <summary>
        /// 使用AC伺服时表示串口号,使用康泰克运动版表示板卡名称
        /// </summary>
        [CSVAttribute("设备名称")]
        public string DeviceName { get; set; }

   //     目标速度 加速时间 减速时间 原点低速度   原点高速 原点加速度   脉冲最小误差 脉冲最大误差  脉冲最小限位 脉冲最大限位

        /// <summary>
        /// 待机位置
        /// </summary>
        //[CSVAttribute("默认值")]
        public int DefaultPosition { get; set; }
        /// <summary>
        /// 目标速度
        /// </summary>
        [CSVAttribute("目标速度")]
        public int TargetSpeed { get; set; }
         
        /// <summary>
        /// 加速度
        /// </summary>
        [CSVAttribute("加速时间")]
        public short AddSpeed { get; set; }
        /// <summary>
        /// 减速度
        /// </summary>
        [CSVAttribute("减速时间")]
        public short DelSpeed { get; set; }

        [CSVAttribute("原点低速度")]
        public int HomeLowSpeed { get; set; }
        [CSVAttribute("原点高速")]
        public int HomeHighSpeed { get; set; }
        [CSVAttribute("原点加速度")]
        public int HomeAddSpeed { get; set; }
        /// <summary>
        /// 可以误差的脉冲范围的最小值
        /// </summary>
        [CSVAttribute("脉冲最小误差")]
        public int CanErrorCountMin { get; set; }
        /// <summary>
        /// 可以误差的脉冲范围的最大值
        /// </summary>
        [CSVAttribute("脉冲最大误差")]
        public int CanErrorCountMax { get; set; }

        /// <summary>
        /// 出入库目标值(只有出入库过程中才会有效)
        /// </summary>
        public int TargetPosition { get; set; }

        /// <summary>
        /// 最小位置
        /// </summary>
        [CSVAttribute("脉冲最小限位")]
        public int PositionMin { get; set; }
        /// <summary>
        /// 最大位置
        /// </summary>
        [CSVAttribute("脉冲最大限位")]
        public int PositionMax { get; set; }


        public string Axis_Brake_DO = "";
        public string Axis_Run_DO = ""; 

        public string GetNameStr()
        {
            return DeviceName + "_" + GetAxisValue();
        }
        public bool IsSameAxis(string portName, int slv)
        {
            if (DeviceName.Equals(portName) && slv.Equals(GetAxisValue()))
            {
                return true;
            }
            return false;
        }
        public bool PositionIsHasLimit()
        {
            if (PositionMin.Equals(PositionMax))
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 下拉列表显示
        /// </summary>
        public string DisplayStr
        {
            get {
                return  Explain + "(" + ProName + ")"; 
            }
            set
            {
            }
        }
        public override int GetValue()
        {
            return GetAxisValue();
        }
        /// <summary>
        /// 使用康泰克板卡表示轴编号,使用Ac伺服电机表示伺服地址
        /// </summary>
        /// <returns></returns>
        public short GetAxisValue()
        { 
            if (ProVale.Equals("") || ProVale.Equals("-1"))
            { 
                return -1;
            }
            return (short)Convert.ToInt32(ProVale);
        }

        public override string ToString()
        {
            return "ConfigMoveAxis。" + "类型:" + ProType + ",说明:" + Explain + ",名称:" + ProName + ",属性值:" + ProVale + ",设备名称:" + DeviceName;
        }
        public override void CheckField()
        {
            if (String.IsNullOrEmpty( ProType) || String.IsNullOrEmpty( ProName ) || String.IsNullOrEmpty( DeviceName ) || String.IsNullOrEmpty( ProVale ))
            {
                throw new CVSFieldNotMatchingExection(ToString() + ",【类型:ProType】【名称:ProName】【属性值:ProVale】【设备名称:DeviceName】必须配置值!");
            }
        }

        public bool IsMyAxis(string DeviceName, int AxisNo)
        {
            int AxisValue = GetAxisValue();
            if (DeviceName.Equals(DeviceName) && AxisValue.Equals(AxisNo))
            {
                return true;
            }
            return false;
        }

        public void SetParam(int targetSpeed,short addSpeed, short delSpeed,int homeAddSpeed,int homeHighSpeed,int homeLowSpeed,int defP1)
        {
            TargetSpeed = addSpeed;
            AddSpeed = addSpeed;
            DelSpeed = delSpeed;
            HomeAddSpeed = homeAddSpeed;
            HomeHighSpeed = homeHighSpeed;
            HomeLowSpeed = homeLowSpeed;
            DefaultPosition = defP1;
        }
    }

     
    /// <summary>
    /// io配置
    /// </summary>
    public class ConfigIO : ConfigBase
    {
        public ConfigIO()
        {
            SlaveID = 0;
        }
        /// <summary>
        /// 设备名称
        /// </summary>
        [CSVAttribute("设备名称")]
        public string DeviceName { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        [CSVAttribute("描述")]
        public string Describe { get; set; }
        /// <summary>
        /// 电器定义
        /// </summary>
        [CSVAttribute("电器定义")]
        public string ElectricalDefinition { get; set; }
        /// <summary>
        /// 代码定义
        /// </summary>
        [CSVAttribute("代码定义")]
        public string CodeDefinition { get; set; }
   

        public byte SlaveID = 0;
        private int IoValue = -10;
        public string DisplayStr
        {
            get
            {
                return ElectricalDefinition + "_" + Explain + "(" + ProName+")";
            }
            set
            {
            }
        }
        public override int GetValue()
        {
            return GetIOValue();
        }
        public string IO_IP
        {
            get
            {
                return DeviceName;
            }
        }
        public int GetIOValue()
        {
            if (IoValue >= -1)
            {
                return IoValue;
            } 
            try
            {
                if (ProVale.Equals("") || ProVale.Equals("-1"))
                {
                    IoValue = -1;
                }
                else if (ProType.Equals(ConfigItemType.DI))
                {
                    IoValue = Convert.ToInt32(ProVale, 8);
                }
                else if (ProType.Equals(ConfigItemType.DO))
                {
                    if (ProVale.Length == 2)
                    {
                        int a = Convert.ToInt32(ProVale.Substring(0, 1), 16) - 8;
                        string str =a+ProVale.Substring(1,1);
                        IoValue = Convert.ToInt32( str, 8);
                        //IoValue = a + b;
                    }
                    else
                    {
                        IoValue = Convert.ToInt32(ProVale, 16) - Convert.ToInt32("80", 16);
                    }
                }
                //LOGGER.Info("IO转换:(" + ElectricalDefinition + ")" + ProVale + "=" + IoValue);
            }
            catch (Exception ex)
            {
                LogUtil.error ("出错了:", ex);
                return -1;
            }
            return IoValue;
        }

        public override string ToString()
        {
            return "ConfigIO。" + ProType + "," + Explain + "," + ProName + ",属性值:" + ProVale + ",设备名称:" + DeviceName + ",描述:" + Describe + ",电器定义:" + ElectricalDefinition + ",代码定义:" + CodeDefinition + "";
        }
        public override void CheckField()
        {
            if (String.IsNullOrEmpty(ProType) || String.IsNullOrEmpty(ProName) || String.IsNullOrEmpty(DeviceName) || String.IsNullOrEmpty(ProVale))
            {
                throw new CVSFieldNotMatchingExection(ToString() + ",【类型:ProType】【名称:ProName】【属性值:ProVale】【设备名称:DeviceName】必须配置值!");
            }
        }
    }

    public static class ConfigItemType
    {
        public static string DI = "DI";
        public static string DO = "DO";
        public static string AXIS = "AXIS";
        public static string PRO = "PRO";
    }

    public static class ConfigItemName
    {
        /// <summary>
        /// 上下转动轴
        /// </summary>
        public static string UpDown_Axis = "UpDown_Axis";
        /// <summary>
        /// 中转伝动轴	Middle_Axis
        /// </summary>
        public static string Middle_Axis = "Middle_Axis";
        /// <summary>
        /// 中轴移动速度	MiddleAxis_Move_Speed
        /// </summary>
        public static string MiddleAxis_Move_Speed = "MiddleAxis_Move_Speed";
        /// <summary>
        /// 上下轴普通速度	Updown_Move_Speed
        /// </summary>
        public static string Updown_Move_Speed = "Updown_Move_Speed";
        /// <summary>
        /// 上下轴慢速度	Updown_Move_Speed_Low
        /// </summary>
        public static string Updown_Move_Speed_Low = "Updown_Move_Speed_Low";
        /// <summary>
        /// 温湿度传感器地址	Temperate_ServerAddress
        /// </summary>
        public static string Temperate_ServerAddress = "Temperate_ServerAddress";

    }
}