private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //初始化限位塞
            SetLimitSwitch();

            userLine1.BarFactHeght = LumbarRobotController.RobotController.PushRodAngle;

            if (LimitSwitchChanged == null)
            {
                LimitSwitchChanged = new EventHandler(RobotController_LimitSwitchChanged);
                LumbarRobotController.RobotController.LimitSwitchChanged += LimitSwitchChanged;
            }

            if (PushRodChanged == null)
            {
                PushRodChanged = new EventHandler(RobotController_PushRodChanged);
                LumbarRobotController.RobotController.PushRodChanged += PushRodChanged;
            }

            if (AlarmEvent == null)
            {
                AlarmEvent = new LumbarRobotController.AlarmHandler(RobotController_Alarm);
                LumbarRobotController.RobotController.Alarm += AlarmEvent;
            }

            //上次训练推杆高度
            var record = (from r in MySession.Query<Exerciserecord>()
                          where r.PatientId == ModuleConstant.PatientId
                          select r).OrderByDescending(x => x.StartTime).Take(1).ToList();

            if (record.Count > 0)
            {
                userLine1.BarHeight = record.First().PushRodValue;
            }                         

            //报警信息
            if (LumbarRobotController.RobotController.AlarmCode != 0)
            {
                var args = new LumbarRobotController.AlarmArgs();
                args.AlarmCode = LumbarRobotController.RobotController.AlarmCode;
                RobotController_Alarm(args);
            }
            
            lock (_lock)
            {
                FitResultList = null;
                if (LumbarRobotController.RobotController.IsPause)
                {
                    LumbarRobotController.RobotController.IsPause = false;
                }
                else
                {

                    if (Mode == EvaluateModeEnum.Strength)
                    {
                        sinWpf1.Uint = "牛";
                    }
                    else
                    {
                        sinWpf1.Uint = "度";
                    }

                    sinWpf1.ReSet();
                    eilelc21.Rest();

                    if (Move == null)
                    {
                        Move = new LumbarRobotController.MoveHandler(RobotController_Move);
                        LumbarRobotController.RobotController.Move += Move;
                    }

                    if (NextTime == null)
                    {
                        NextTime = new EventHandler(RobotController_NextTime);
                        LumbarRobotController.RobotController.NextTime += NextTime;
                    }
                    if (StartAction == null)
                    {
                        StartAction = new EventHandler(RobotController_StartAction);
                        LumbarRobotController.RobotController.StartAction += StartAction;
                    }

                    if (Mode == EvaluateModeEnum.Strength)
                    {                            

                        if (SetForce == null)
                        {
                            SetForce = new SetForceHandler(RobotController_SetForce);
                            LumbarRobotController.RobotController.SetForce += SetForce;
                        }
                    }


                    SetParam();

                  
                }

                txtPrompt.Text = "系统正在复位请放松";
              
                //设置当前次数
                this.txtTimes.Text = "0/0";
            }
            
        }
        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            LumbarRobotController.RobotController.IsStop = true;
            LumbarRobotController.RobotController.ControlCommand.PauseCmd();

            if (LimitSwitchChanged != null)
            {
                LumbarRobotController.RobotController.LimitSwitchChanged -= LimitSwitchChanged;
                LimitSwitchChanged = null;
            }

            if (PushRodChanged != null)
            {
                LumbarRobotController.RobotController.PushRodChanged -= PushRodChanged;
                PushRodChanged = null;
            }

            if (AlarmEvent != null)
            {
                LumbarRobotController.RobotController.Alarm -= AlarmEvent;
                AlarmEvent = null;
            }

            if (Move != null)
            {
                LumbarRobotController.RobotController.Move -= Move;
                Move = null;
            }

            if (NextTime != null)
            {
                LumbarRobotController.RobotController.NextTime -= NextTime;
                NextTime = null;
            }

            if (StopAction != null)
            {
                LumbarRobotController.RobotController.StopAction -= StopAction;
                StopAction = null;
            }

            if (SetForce != null)
            {
                LumbarRobotController.RobotController.SetForce -= SetForce;
                SetForce = null;
            }

            if (StartAction != null)
            {
                LumbarRobotController.RobotController.StartAction -= StartAction;
                StartAction = null;
            }

            if (msgBox != null)
            {
                msgBox.Close();
            }
        }
        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void playBtn_PlayClick(object sender, EventArgs e)
        {
            if (LumbarRobotController.RobotController.IsPause)
            {
                LumbarRobotController.RobotController.Continue();
            }
            else
            {
                SetParam();

                if (Action == EvaluateActionEnum.RangeBend || Action == EvaluateActionEnum.RangeProtrusive)
                {

                    //判断限位塞是否有效
                    var limitSwitchs = LumbarRobotController.RobotController.LimitSwitch;
                    bool minFlag = false;
                    bool maxFlag = false;
                    int minAngle = int.MinValue;
                    int maxAngle = int.MaxValue;
                    foreach (var ls in limitSwitchs)
                    {
                        var limitAngle = GetLimitSwitchAngle(ls);
                        if (LumbarRobotController.RobotController.CurrentBendStretchAngle - (LumbarRobotController.RobotController.HomingAngle - LumbarRobotController.RobotController.PushRodAngle) < limitAngle)
                        {
                            if (limitAngle < maxAngle)
                            {
                                maxAngle = limitAngle;
                            }
                            maxFlag = true;
                        }
                        else
                        {
                            if (limitAngle > minAngle)
                            {
                                minAngle = limitAngle;
                            }
                            minFlag = true;
                        }
                    }

                    if (!(minFlag && maxFlag))
                    {
                        playBtn.Init(1, 1);
                        AlarmDialog dialog = new AlarmDialog();
                        dialog.lblTitle.Text = "提示信息";
                        dialog.lblMsg.Text = "限位塞还未设置或设置错误!";
                        dialog.ShowDialog();
                        return;
                    }

                    //判断设置角度是否大于限位栓角度
                    if (LumbarRobotController.RobotController.IsConnected)
                    {
                        if (InitialPosition < minAngle)
                        {
                            playBtn.Init(1, 1);
                            AlarmDialog dialog = new AlarmDialog();
                            dialog.lblTitle.Text = "提示信息";
                            dialog.lblMsg.Text = "初始角度不能小于限位塞角度!";
                            dialog.ShowDialog();
                            return;
                        }

                        if (InitialPosition > maxAngle)
                        {
                            playBtn.Init(1, 1);
                            AlarmDialog dialog = new AlarmDialog();
                            dialog.lblTitle.Text = "提示信息";
                            dialog.lblMsg.Text = "初始角度不能大于限位塞角度!";
                            dialog.ShowDialog();
                            return;
                        }
                    }

                }

                this.playBtn.imgStop.IsEnabled = true;
                this.playBtn.imgNext.IsEnabled = false;
                this.playBtn.imgPrev.IsEnabled = false;

                ControlIsEnabled(false);

                if (Mode == EvaluateModeEnum.Strength)
                {
                    sinWpf1.Uint = "牛";
                }
                else
                {
                    sinWpf1.Uint = "度";
                }

                sinWpf1.ReSet();
                eilelc21.Rest();

                if (Move == null)
                {
                    Move = new LumbarRobotController.MoveHandler(RobotController_Move);
                    LumbarRobotController.RobotController.Move += Move;
                }

                if (StopAction == null)
                {
                    StopAction = new EventHandler(RobotController_StopAction);
                    LumbarRobotController.RobotController.StopAction += StopAction;
                }

                if (SetForce == null)
                {
                    SetForce = new SetForceHandler(RobotController_SetForce);
                    LumbarRobotController.RobotController.SetForce += SetForce;
                }

                if (StartAction == null)
                {
                    StartAction = new EventHandler(RobotController_StartAction);
                    LumbarRobotController.RobotController.StartAction += StartAction;
                }


                if (Mode == EvaluateModeEnum.Range)
                {
                    SetTargetLine(Time, InitialPosition, Angle);
                }
                else
                {
                    SetTargetLine(Time, 0, Power);
                }

                LumbarRobotController.RobotController.DoEvaluateAction(Action, Mode);

                txtPrompt.Text = "系统正在复位请放松";

                //禁用fit按钮
                btnActionFree.IsEnabled = false;
            }
        }
        private void Stop(bool isShowDialog = true)
        {
            var record = LumbarRobotController.RobotController.GetEvaluteResult();
            ControlIsEnabled(true);

            if (Move != null)
            {
                LumbarRobotController.RobotController.Move -= Move;
                Move = null;
            }

            if (NextTime != null)
            {
                LumbarRobotController.RobotController.NextTime -= NextTime;
                NextTime = null;
            }

            if (StopAction != null)
            {
                LumbarRobotController.RobotController.StopAction -= StopAction;
                StopAction = null;
            }

            if (SetForce != null)
            {
                LumbarRobotController.RobotController.SetForce -= SetForce;
                SetForce = null;
            }

            if (StartAction != null)
            {
                LumbarRobotController.RobotController.StartAction -= StartAction;
                StartAction = null;
            }

            SaveTrainRecord(record);

            //sinWpf1.ReSet();
            //eilelc21.Rest();
            LumbarRobotController.RobotController.ControlCommand.PauseCmd();
            txtPrompt.Text = "当前评测结束!";
            if (isShowDialog)
            {
                AlarmDialog dialog = new AlarmDialog();
                dialog.lblTitle.Text = "提示信息";
                dialog.lblMsg.Text = "当前评测结束!";
                dialog.Topmost = true;
                dialog.ShowDialog();
            }
        }
        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void playBtn_PlayClick(object sender, EventArgs e)
        {
            if (LumbarRobotController.RobotController.IsPause)
            {
                LumbarRobotController.RobotController.Continue();
            }
            else
            {
                ItemDemo item = ActionList[MyActionListBox.SelectedIndex] as ItemDemo;
                item.MaxAngle = this.setControlMaxAngle.Value;
                ComboBoxItem cbi = (ComboBoxItem)this.CboAction.SelectedItem;
                item.ActionName = cbi.Content.ToString();
                ComboBoxItem cbMode = (ComboBoxItem)this.CboMode.SelectedItem;
                item.ModeName = cbMode.Content.ToString();
                item.MinAngle = this.setControlMinAngle.Value;
                item.Mode = this.CboMode.SelectedIndex;
                item.ActionId = this.CboAction.SelectedIndex;
                item.Speed = this.setControlSpeed.Value;
                item.Times = this.setControlTimes.Value;
                item.Force = this.setControlForce.Value;
                item.Position = this.setControlPosition.Value;
                item.PicturePath = "/images/prescribe.png";
                item.GroupTimes = this.setControlGroup.Value;
                MyActionListBox_SelectionChanged(null, null);

                //设置当前次数
                this.txtTimes.Text = (LumbarRobotController.RobotController.CurrentTimes + 1).ToString();
                if (ActionList != null && ActionList.Count > CurrentIndex)
                {
                    this.txtTimes.Text += "/" + ActionList[CurrentIndex].Times * ActionList[CurrentIndex].GroupTimes;
                }

                if ((ActionEnum)ActionList[CurrentIndex].ActionId == ActionEnum.Bend ||
                    (ActionEnum)ActionList[CurrentIndex].ActionId == ActionEnum.Protrusive ||
                    (ActionEnum)ActionList[CurrentIndex].ActionId == ActionEnum.ProtrusiveOrBend)
                {
                    UPLable.Content = "前屈";
                    Downlabel.Content = "后伸";

                    //判断限位塞是否有效
                    var limitSwitchs = LumbarRobotController.RobotController.LimitSwitch;
                    bool minFlag = false;
                    bool maxFlag = false;
                    int minAngle = int.MinValue;
                    int maxAngle = int.MaxValue;
                    foreach (var ls in limitSwitchs)
                    {
                        var limitAngle = GetLimitSwitchAngle(ls);
                        if (LumbarRobotController.RobotController.CurrentBendStretchAngle - (LumbarRobotController.RobotController.HomingAngle - LumbarRobotController.RobotController.PushRodAngle) < limitAngle)
                        {
                            if (limitAngle < maxAngle)
                            {
                                maxAngle = limitAngle;
                            }
                            maxFlag = true;
                        }
                        else
                        {
                            if (limitAngle > minAngle)
                            {
                                minAngle = limitAngle;
                            }
                            minFlag = true;
                        }
                    }

                    if (!(minFlag && maxFlag))
                    {
                        playBtn.Init(MyActionListBox.SelectedIndex, ActionList.Count);
                        AlarmDialog dialog = new AlarmDialog();
                        dialog.lblTitle.Text = "提示信息";
                        dialog.lblMsg.Text = "限位塞还未设置或设置错误!";
                        dialog.ShowDialog();
                        return;
                    }

                    //判断设置角度是否大于限位栓角度
                    if (LumbarRobotController.RobotController.IsConnected)
                    {
                        if (ActionList[CurrentIndex].MinAngle < minAngle)
                        {
                            playBtn.Init(MyActionListBox.SelectedIndex, ActionList.Count);
                            AlarmDialog dialog = new AlarmDialog();
                            dialog.lblTitle.Text = "提示信息";
                            dialog.lblMsg.Text = "最小角度不能小于限位塞角度!";
                            dialog.ShowDialog();
                            return;
                        }

                        if (ActionList[CurrentIndex].MaxAngle > maxAngle)
                        {
                            playBtn.Init(MyActionListBox.SelectedIndex, ActionList.Count);
                            AlarmDialog dialog = new AlarmDialog();
                            dialog.lblTitle.Text = "提示信息";
                            dialog.lblMsg.Text = "最大角度不能大于限位塞角度!";
                            dialog.ShowDialog();
                            return;
                        }
                    }

                    //判断初始位置是否大于或小于最大值和最小值
                    if ((ModeEnum)ActionList[CurrentIndex].Mode != ModeEnum.IsotonicA
                        && (ModeEnum)ActionList[CurrentIndex].Mode != ModeEnum.IsotonicB)
                    {
                        if (ActionList[CurrentIndex].Position > ActionList[CurrentIndex].MaxAngle)
                        {
                            playBtn.Init(MyActionListBox.SelectedIndex, ActionList.Count);
                            AlarmDialog dialog = new AlarmDialog();
                            dialog.lblTitle.Text = "提示信息";
                            dialog.lblMsg.Text = "起始位置不能大于最大角度!";
                            dialog.ShowDialog();
                            return;
                        }

                        if (ActionList[CurrentIndex].Position < ActionList[CurrentIndex].MinAngle)
                        {
                            playBtn.Init(MyActionListBox.SelectedIndex, ActionList.Count);
                            AlarmDialog dialog = new AlarmDialog();
                            dialog.lblTitle.Text = "提示信息";
                            dialog.lblMsg.Text = "起始位置不能小于最小角度!";
                            dialog.ShowDialog();
                            return;
                        }
                    }

                    //加载Fit结果
                    var fit = (from f in MySession.Query<FitRecord>()
                               where f.PatientID == ModuleConstant.PatientId && f.ModeID == (int)Common.Enums.FitModeEnum.ProtrusiveOrBendFit
                               select f).OrderByDescending(x => x.CreateTime).Take(1).ToList();

                    if (fit != null && fit.Count > 0)
                    {
                        eilelc21.FltMinusValue = fit[0].MinAngle;
                        eilelc21.FltValu = fit[0].MaxAngle;
                    }
                    else
                    {
                        playBtn.Init(MyActionListBox.SelectedIndex, ActionList.Count);
                        AlarmDialog dialog = new AlarmDialog();
                        dialog.lblTitle.Text = "提示信息";
                        dialog.lblMsg.Text = "还未测试!请先测试后再进行训练!";
                        dialog.ShowDialog();
                        return;
                    }

                    //判断设置角度是否大于fit角度
                    if (ActionList[CurrentIndex].MinAngle < fit[0].MinAngle)
                    {
                        playBtn.Init(MyActionListBox.SelectedIndex, ActionList.Count);
                        AlarmDialog dialog = new AlarmDialog();
                        dialog.lblTitle.Text = "提示信息";
                        dialog.lblMsg.Text = "最小角度不能小于FIT角度!";
                        dialog.ShowDialog();
                        return;
                    }

                    if (ActionList[CurrentIndex].MaxAngle > fit[0].MaxAngle)
                    {
                        playBtn.Init(MyActionListBox.SelectedIndex, ActionList.Count);
                        AlarmDialog dialog = new AlarmDialog();
                        dialog.lblTitle.Text = "提示信息";
                        dialog.lblMsg.Text = "最大角度不能大于FIT角度!";
                        dialog.ShowDialog();
                        return;
                    }

                }
                else
                {
                    UPLable.Content = "左";
                    Downlabel.Content = "右";

                    //加载Fit结果
                    var fit = (from f in MySession.Query<FitRecord>()
                               where f.PatientID == ModuleConstant.PatientId && f.ModeID == (int)Common.Enums.FitModeEnum.RotationFit
                               select f).OrderByDescending(x => x.CreateTime).Take(1).ToList();

                    if (fit != null && fit.Count > 0)
                    {
                        eilelc21.FltMinusValue = fit[0].MinAngle;
                        eilelc21.FltValu = fit[0].MaxAngle;
                    }
                    else
                    {
                        playBtn.Init(MyActionListBox.SelectedIndex, ActionList.Count);
                        AlarmDialog dialog = new AlarmDialog();
                        dialog.lblTitle.Text = "提示信息";
                        dialog.lblMsg.Text = "还未测试!请先测试后再进行训练!";
                        dialog.ShowDialog();
                        return;
                    }

                    //判断设置角度是否大于fit角度
                    if (ActionList[CurrentIndex].MinAngle < fit[0].MinAngle)
                    {
                        playBtn.Init(MyActionListBox.SelectedIndex, ActionList.Count);
                        AlarmDialog dialog = new AlarmDialog();
                        dialog.lblTitle.Text = "提示信息";
                        dialog.lblMsg.Text = "最小角度不能小于FIT角度!";
                        dialog.ShowDialog();
                        return;
                    }

                    if (ActionList[CurrentIndex].MaxAngle > fit[0].MaxAngle)
                    {
                        playBtn.Init(MyActionListBox.SelectedIndex, ActionList.Count);
                        AlarmDialog dialog = new AlarmDialog();
                        dialog.lblTitle.Text = "提示信息";
                        dialog.lblMsg.Text = "最大角度不能大于FIT角度!";
                        dialog.ShowDialog();
                        return;
                    }
                }

                this.playBtn.imgStop.IsEnabled = true;
                this.playBtn.imgNext.IsEnabled = false;
                this.playBtn.imgPrev.IsEnabled = false;
                
                MyActionListBox.IsEnabled = false;
                SetIsEnabled();
                ControlIsEnabled(false);


                if (LumbarRobotController.RobotController.IsPause)
                {
                    LumbarRobotController.RobotController.IsPause = false;
                }
                else
                {
                    if ((ModeEnum)ActionList[CurrentIndex].Mode == ModeEnum.IsotonicA
                    || (ModeEnum)ActionList[CurrentIndex].Mode == ModeEnum.IsotonicB
                    || (ModeEnum)ActionList[CurrentIndex].Mode == ModeEnum.StrengthEvaluation)
                    {
                        sinWpf1.Uint = "牛";
                    }
                    else
                    {
                        sinWpf1.Uint = "度";
                    }
                    if (LumbarRobotController.RobotController.IsPause)
                    {
                        sinWpf1.ReSet(false);
                    }
                    else
                    {
                        sinWpf1.ReSet(); 
                    }
                    eilelc21.Rest();



                    //设置当前次数
                    this.txtTimes.Text = (LumbarRobotController.RobotController.CurrentTimes + 1).ToString();
                    if (ActionList != null && ActionList.Count > CurrentIndex)
                    {
                        this.txtTimes.Text += "/" + ActionList[CurrentIndex].Times * ActionList[CurrentIndex].GroupTimes;
                    }

                    if (ActionList != null && ActionList.Count > CurrentIndex)
                    {
                        if (Move == null)
                        {
                            Move = new LumbarRobotController.MoveHandler(RobotController_Move);
                            LumbarRobotController.RobotController.Move += Move;
                        }

                        if (NextTime == null)
                        {
                            NextTime = new EventHandler(RobotController_NextTime);
                            LumbarRobotController.RobotController.NextTime += NextTime;
                        }

                        if (StopAction == null)
                        {
                            StopAction = new EventHandler(RobotController_StopAction);
                            LumbarRobotController.RobotController.StopAction += StopAction;
                        }

                        if (SetForce == null)
                        {
                            SetForce = new SetForceHandler(RobotController_SetForce);
                            LumbarRobotController.RobotController.SetForce += SetForce;
                        }

                        if (StartAction == null)
                        {
                            StartAction = new EventHandler(RobotController_StartAction);
                            LumbarRobotController.RobotController.StartAction += StartAction;
                        }

                        SetParam();

                        if ((ModeEnum)ActionList[CurrentIndex].Mode == ModeEnum.IsotonicA)
                        {
                            //设置预设目标
                            if ((ActionEnum)ActionList[CurrentIndex].ActionId == ActionEnum.Protrusive)
                            {
                                SetTargetLineByForce(ActionList[CurrentIndex].Speed, 0, ActionList[CurrentIndex].Force, ActionList[CurrentIndex].Times, ActionList[CurrentIndex].GroupTimes, 0);
                            }
                            else if ((ActionEnum)ActionList[CurrentIndex].ActionId == ActionEnum.Bend)
                            {
                                SetTargetLineByForce(ActionList[CurrentIndex].Speed, -1 * ActionList[CurrentIndex].Force,0, ActionList[CurrentIndex].Times, ActionList[CurrentIndex].GroupTimes, 0);
                            }
                            else if ((ActionEnum)ActionList[CurrentIndex].ActionId == ActionEnum.ProtrusiveOrBend
                                || (ActionEnum)ActionList[CurrentIndex].ActionId == ActionEnum.Rotation)
                            {
                                SetTargetLineByForce(ActionList[CurrentIndex].Speed, -1 * ActionList[CurrentIndex].Force, ActionList[CurrentIndex].Force, ActionList[CurrentIndex].Times, ActionList[CurrentIndex].GroupTimes, 0);
                            }
                        }
                        else if ((ModeEnum)ActionList[CurrentIndex].Mode == ModeEnum.IsotonicB)
                        {
                            SetTargetLineByForce2((ActionEnum)ActionList[CurrentIndex].ActionId, ActionList[CurrentIndex].Speed, ActionList[CurrentIndex].Force, ActionList[CurrentIndex].Times, ActionList[CurrentIndex].GroupTimes, 0);
                        }
                        else
                        {
                            if ((ModeEnum)ActionList[CurrentIndex].Mode != ModeEnum.Guided
                                && (ModeEnum)ActionList[CurrentIndex].Mode != ModeEnum.Fit)
                                
                            {
                                //设置预设目标
                                SetTargetLine(ActionList[CurrentIndex].Speed, ActionList[CurrentIndex].MaxAngle, ActionList[CurrentIndex].MinAngle, ActionList[CurrentIndex].Times, ActionList[CurrentIndex].GroupTimes, ActionList[CurrentIndex].Position);
                            }
                        }

                        LumbarRobotController.RobotController.DoAction((ModeEnum)ActionList[CurrentIndex].Mode, (ActionEnum)ActionList[CurrentIndex].ActionId);

                    }
                }
                txtPrompt.Text = "系统正在复位请放松";

                //禁用fit按钮
                btnRotationFit.IsEnabled = false;
                btnPBFit.IsEnabled = false;
                btnActionFree.IsEnabled = false;
            }
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (ActionList.Count == 1)
            {
                this.playBtn.IsPlayNextValid = false;
                this.playBtn.IsPlayPrevValid = false;
            }
            else
            {
                this.playBtn.IsPlayPrevValid = false;
                this.playBtn.IsPlayNextValid = true;
            }

            //初始化限位塞
            SetLimitSwitch();

            userLine1.BarFactHeght = LumbarRobotController.RobotController.PushRodAngle;

            if (LimitSwitchChanged == null)
            {
                LimitSwitchChanged = new EventHandler(RobotController_LimitSwitchChanged);
                LumbarRobotController.RobotController.LimitSwitchChanged += LimitSwitchChanged;
            }

            if (PushRodChanged == null)
            {
                PushRodChanged = new EventHandler(RobotController_PushRodChanged);
                LumbarRobotController.RobotController.PushRodChanged += PushRodChanged;
            }

            if (AlarmEvent == null)
            {
                AlarmEvent = new LumbarRobotController.AlarmHandler(RobotController_Alarm);
                LumbarRobotController.RobotController.Alarm += AlarmEvent;
            }

            //上次训练推杆高度
            var record = (from r in MySession.Query<Exerciserecord>()
                          where r.PatientId == ModuleConstant.PatientId
                          select r).OrderByDescending(x => x.StartTime).Take(1).ToList();

            if (record.Count > 0)
            {
                userLine1.BarHeight = record.First().PushRodValue;
            }                         

            //报警信息
            if (LumbarRobotController.RobotController.AlarmCode != 0)
            {
                var args = new LumbarRobotController.AlarmArgs();
                args.AlarmCode = LumbarRobotController.RobotController.AlarmCode;
                RobotController_Alarm(args);
            }

            if (fitType != LumbarRobot.FitType.None)
            {
                lock (_lock)
                {
                    FitResultList = null;
                    if (LumbarRobotController.RobotController.IsPause)
                    {
                        LumbarRobotController.RobotController.IsPause = false;
                    }
                    else
                    {

                        if (fitType == LumbarRobot.FitType.BendStretchStrength
                            || fitType == LumbarRobot.FitType.RotateStrength)
                        {
                            sinWpf1.Uint = "牛";
                        }
                        else
                        {
                            sinWpf1.Uint = "度";
                        }

                        sinWpf1.ReSet();
                        eilelc21.Rest();

                        if (Move == null)
                        {
                            Move = new LumbarRobotController.MoveHandler(RobotController_Move);
                            LumbarRobotController.RobotController.Move += Move;
                        }

                        if (NextTime == null)
                        {
                            NextTime = new EventHandler(RobotController_NextTime);
                            LumbarRobotController.RobotController.NextTime += NextTime;
                        }
                        //StopAction = new EventHandler(RobotController_StopAction);
                        //LumbarRobotController.RobotController.StopAction += StopAction;

                        if (StartAction == null)
                        {
                            StartAction = new EventHandler(RobotController_StartAction);
                            LumbarRobotController.RobotController.StartAction += StartAction;
                        }

                        if (fitType == LumbarRobot.FitType.BendStretchStrength
                            || fitType == LumbarRobot.FitType.RotateStrength)
                        {                            

                            if (SetForce == null)
                            {
                                SetForce = new SetForceHandler(RobotController_SetForce);
                                LumbarRobotController.RobotController.SetForce += SetForce;
                            }
                        }


                        SetParam();

                        if (fitType == LumbarRobot.FitType.BendStretchRange)
                        {
                            LumbarRobotController.RobotController.DoAction(ModeEnum.Fit, ActionEnum.ProtrusiveOrBend);
                        }
                        else if (fitType == LumbarRobot.FitType.BendStretchStrength)
                        {
                            LumbarRobotController.RobotController.DoAction(ModeEnum.StrengthEvaluation, ActionEnum.ProtrusiveOrBend);
                        }
                        else if (fitType == LumbarRobot.FitType.RotateRange)
                        {
                            LumbarRobotController.RobotController.DoAction(ModeEnum.Fit, ActionEnum.Rotation);
                        }
                        else
                        {
                            LumbarRobotController.RobotController.DoAction(ModeEnum.StrengthEvaluation, ActionEnum.Rotation);
                        }

                        //LimitSwitchChanged = new EventHandler(RobotController_LimitSwitchChanged);
                        //LumbarRobotController.RobotController.LimitSwitchChanged += LimitSwitchChanged;
                    }

                    txtPrompt.Text = "系统正在复位请放松";
                    if (fitType == LumbarRobot.FitType.BendStretchStrength)
                    {
                        txtMsg.Text = "正在进行屈伸力量测试,完成后点击确定!";
                    }
                    else if (fitType == LumbarRobot.FitType.RotateStrength)
                    {
                        txtMsg.Text = "正在进行旋转力量测试,完成后点击确定!";
                    }
                    else if (fitType == LumbarRobot.FitType.BendStretchRange)
                    {
                        txtMsg.Text = "正在进行屈伸Fit,完成后点击确定!";
                    }
                    else
                    {
                        txtMsg.Text = "正在进行旋转Fit,完成后点击确定!";
                    }

                    //设置当前次数
                    this.txtTimes.Text = "0/0";
                }
            }
        }