using MECF.Framework.Common.Device.Galil;
using MECF.Framework.Common.TwinCat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace PunkHPX8_RT.Devices.AXIS
{
    public class GalilCommonAxis
    {
        #region 常量
        private const string IS_SWITCH_ON = "IsSwitchOn";
        private const string STOP_CODE = "StopCode";
        private const string MOTOR_POSITION = "MotorPosition";
        private const string POSITION_ERROR = "PositionError";
        private const string ACTUAL_TORQUE = "ActualTorque";
        private const string ACTUAL_VELOCITY = "Velocity";
        private const string FORWARD_LIMIT = "ForwardLimit";
        private const string TARGET_POSITION = "ReferencePosition";
        #endregion
        #region 内部变量
        private string _module;
        private string _name;
        private JetAxisBase _axis;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        /// 
        public GalilCommonAxis(string module,string name,JetAxisBase jetAxisBase)
        {
            _module = module;
            _name = name;
            _axis = jetAxisBase;
        }
        /// 
        /// 订阅变量
        /// 
        public void SubscriptionVariable()
        {
            _axis.AxisSubscribeUpdateVariable(IS_SWITCH_ON);
            _axis.AxisSubscribeUpdateVariable(STOP_CODE);
            _axis.AxisSubscribeUpdateVariable(MOTOR_POSITION);
            _axis.AxisSubscribeUpdateVariable(POSITION_ERROR);
            _axis.AxisSubscribeUpdateVariable(ACTUAL_TORQUE);
            _axis.AxisSubscribeUpdateVariable(ACTUAL_VELOCITY);
            _axis.AxisSubscribeUpdateVariable(FORWARD_LIMIT);
            _axis.AxisSubscribeUpdateVariable(TARGET_POSITION);
        }
        /// 
        /// 上电
        /// 
        /// 
        public bool WriteSwitchOn()
        {
            return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "SH", null);
        }
        /// 
        /// 下电
        /// 
        /// 
        public bool WriteSwitchOff()
        {
            return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "MO", null);
        }
        /// 
        /// 写入相对位置
        /// 
        /// 
        /// 
        public bool WriteReferencePosition(int referencePosition)
        {
            return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "PR", referencePosition);
        }
        /// 
        /// 写入绝对位置
        /// 
        /// 
        /// 
        public bool WriteAbsolutePosition(int absolutePosition)
        {
            return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "PA", absolutePosition);
        }
        /// 
        /// 写入速度
        /// 
        /// 
        /// 
        public bool WriteSpeed(int speed)
        {
            return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "SP", speed);
        }
        /// 
        /// 写入加速度
        /// 
        /// 
        /// 
        public bool WriteAcceleration(int acceleration)
        {
            return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "AC", acceleration);
        }
        /// 
        /// 写入减速度
        /// 
        /// 
        /// 
        public bool WriteDeceleration(int deceleration)
        {
            return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "DC", deceleration);
        }
        /// 
        /// 停止
        /// 
        /// 
        public bool WriteStop()
        {
            return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "ST", null);
        }
        /// 
        /// 开始运动
        /// 
        /// 
        public bool WriteStartMotion()
        {
            return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "BG", null);
        }
        /// 
        /// Home 电机
        /// 
        /// 
        public bool WriteHomeAxisCommand()
        {
            return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "HM", null);
        }
        /// 
        /// Home 电机
        /// 
        /// 
        public bool WriteFIAxisCommand()
        {
            return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "FI", null);
        }
        /// 
        /// Home 电机
        /// 
        /// 
        public bool WriteCNCommand(string cn)
        {
            return GalilControllerCfgManager.Instance.SetSystemCommand(_module, _name, "CN", cn);
        }
        /// 
        /// 手动置零
        /// 
        /// 
        public bool WriteDP(int dp)
        {
            bool result = GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "DP", dp);
            if (result)
            {
                return GalilControllerCfgManager.Instance.SetAxisCommand(_module, _name, "DE", dp);
            }
            return false;
        }
    }
}