using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.TwinCat;
using CyberX8_Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MECF.Framework.Common.IOCore;
namespace CyberX8_RT.Devices.Safety
{
    public class SafetyArmResetRoutine : RoutineBase, IRoutine
    {
        private enum SafetyToggleStep
        {
            WriteFalse,
            WriteTrue,
            End
        }
        #region 内部变量
        /// 
        /// do名称
        /// 
        private string _doName = "";
        /// 
        /// 写入数值
        /// 
        private double _doWriteValue = 0;
        /// 
        /// 设备
        /// 
        private SafetyDevice _device;
        /// 
        /// 超晨
        /// 
        private int _timeout = 1000;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public SafetyArmResetRoutine(string module) : base(module)
        {
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop($"Manual Safty {_doName} Toggle");
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(SafetyToggleStep.WriteFalse, () => { return WriteDoValue(0); }, CheckDoValue, _timeout)
                .Run(SafetyToggleStep.WriteTrue, () => { return WriteDoValue(1); }, CheckDoValue, _timeout)
                .End(SafetyToggleStep.End, NullFun, _delay_1ms);
            return Runner.Status;
        }
        /// 
        /// 写入do数值
        /// 
        /// 
        /// 
        private bool WriteDoValue(double value)
        {
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{_doName}");
            _doWriteValue = value;
            return IOModuleManager.Instance.WriteIoValue(ioName, value);
        }
        /// 
        /// 检验do的数值
        /// 
        /// 
        private bool CheckDoValue()
        {
            PropertyInfo propertyInfo = _device.SafetyData.GetType().GetProperty(_doName);
            if (propertyInfo != null)
            {
                double doValue = (double)propertyInfo.GetValue(_device.SafetyData);
                return doValue==_doWriteValue;
            }
            else
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, $"{_doName} in not in property");
                return false;
            }
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _device = DEVICE.GetDevice(Module);
            _doName= objs[0].ToString();
            return Runner.Start(Module, $"{_doName} toggle");
        }
    }
}