예제 #1
0
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            ClearAllErrorMessage();
            ControllerModel controller = new ControllerModel();

            bool verifyFlag = true;

            if (ControllerTypeComboBox.SelectedItem != null)
            {
                controller.Type = (ControllerType)ControllerTypeComboBox.SelectedItem;
            }
            else
            {
                this.ErrorMessageControllerType.Text = "请选择控制器类型";
                verifyFlag = false;
            }
            if (DeviceCodeLengthComboBox.SelectedItem != null)
            {
                controller.DeviceAddressLength = Convert.ToInt32(DeviceCodeLengthComboBox.SelectedItem);
            }
            else
            {
                this.ErrorMessageControllerDeviceAddressLength.Text = "请选择器件长度";
                verifyFlag = false;
            }
            if (SerialPortNumberComboBox.SelectedItem != null)
            {
                controller.PortName = SerialPortNumberComboBox.SelectedItem.ToString();
            }
            else
            {
                this.ErrorMessageControllerPortName.Text = "请选择端口";
                verifyFlag = false;
            }
            if (this.ControllerMachineNumInputTextBox.Text == "")
            {
                this.ErrorMessageControllerDeviceAddressLength.Text = "请填写器件长度";
                verifyFlag = false;
            }
            if (verifyFlag)
            {
                controller.Name              = ControllerNameInputTextBox.Text;
                controller.MachineNumber     = ControllerMachineNumInputTextBox.Text;
                controller.LoopAddressLength = 2;//回路地址长度默认为2
                IControllerConfig config = ControllerConfigManager.GetConfigObject(controller.Type);
                int maxMachineNumber     = config.GetMaxMachineAmountValue(controller.DeviceAddressLength);
                Dictionary <string, RuleAndErrorMessage> dictRule = config.GetControllerInfoRegularExpression(controller.DeviceAddressLength);

                RuleAndErrorMessage rule = dictRule["Name"];

                Regex exminator = new Regex(rule.Rule);
                if (!string.IsNullOrEmpty(controller.Name))
                {
                    if (!exminator.IsMatch(controller.Name))
                    {
                        this.ErrorMessageControllerName.Text = rule.ErrorMessage;
                        verifyFlag = false;
                    }
                }
                else
                {
                    this.ErrorMessageControllerName.Text = "请填写控制器名称";
                    verifyFlag = false;
                }
                rule      = dictRule["MachineNumber"];
                exminator = new Regex(rule.Rule);
                if (!exminator.IsMatch(ControllerMachineNumInputTextBox.Text))
                {
                    this.ErrorMessageControllerMachineNumber.Text = rule.ErrorMessage;
                    verifyFlag = false;
                }
                else
                {
                    controller.MachineNumber = this.ControllerMachineNumInputTextBox.Text;
                }
                if (verifyFlag)
                {
                    if (Convert.ToInt16(controller.MachineNumber) > maxMachineNumber)
                    {
                        this.ErrorMessageControllerMachineNumber.Text = "机号超出范围,最大机号为" + maxMachineNumber.ToString();

                        verifyFlag = false;
                    }
                }
            }
            if (verifyFlag)
            {
                ControllerManager controllerManager = new ControllerManager();
                controllerManager.InitializeAllControllerOperation(null);
                IControllerOperation controllerOperation = controllerManager.GetController(controller.Type);
                controllerOperation.AddControllerToProject(controller);
                RaiseEvent(new RoutedEventArgs(AddButtonClickEvent));
            }
        }
예제 #2
0
        //public static readonly RoutedEvent AllControllerInfoUploadedEvent = EventManager.RegisterRoutedEvent(
        //    "AllControllerInfoUploaded", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SummaryInfoView)
        //);


        //public event RoutedEventHandler AllControllerInfoUploaded
        //{
        //    add { AddHandler(AllControllerInfoUploadedEvent, value); }
        //    remove { RemoveHandler(AllControllerInfoUploadedEvent, value); }
        //}


        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            this.ErrorMessage.Text = "";
            // if (TheController != null)
            // {
            // ControllerModel controller=new ControllerModel();
            // //controller.ID = TheController.ID;

            // ControllerModel controller = ((SCA.WPF.ViewModelsRoot.ViewModels.Query.SummaryInfoViewModel)this.DataContext).TheController;
            // TheController.Name = ControllerNameInputTextBox.Text;
            // TheController.MachineNumber = MachineNumberInputTextBox.Text;

            // TheController.BaudRate = Convert.ToInt32(BaudsRateComboBox.SelectedItem);

            // TheController.PortName = ComPortComboBox.SelectedItem.ToString();
            SCA.WPF.ViewModelsRoot.ViewModels.Query.SummaryInfoViewModel vm = (SCA.WPF.ViewModelsRoot.ViewModels.Query.SummaryInfoViewModel) this.DataContext;
            ControllerModel controller = vm.TheController;

            IControllerConfig config = ControllerConfigManager.GetConfigObject(controller.Type);
            int maxMachineNumber     = config.GetMaxMachineAmountValue(controller.DeviceAddressLength);
            Dictionary <string, RuleAndErrorMessage> dictRule = config.GetControllerInfoRegularExpression(controller.DeviceAddressLength);

            RuleAndErrorMessage rule = dictRule["Name"];

            Regex  exminator    = new Regex(rule.Rule);
            string errorMessage = "";
            bool   verifyFlag   = true;

            if (!exminator.IsMatch(ControllerNameInputTextBox.Text))
            {
                errorMessage += "控制器名称:" + rule.ErrorMessage + "; ";
                //errorMessage += rule.ErrorMessage+";";
                verifyFlag = false;
            }
            rule      = dictRule["MachineNumber"];
            exminator = new Regex(rule.Rule);
            if (!exminator.IsMatch(MachineNumberInputTextBox.Text))
            {
                //errorMessage += rule.ErrorMessage+";";
                errorMessage += "控制器机号:" + rule.ErrorMessage + "; ";
                verifyFlag    = false;
            }

            if (verifyFlag)
            {
                if (Convert.ToInt16(MachineNumberInputTextBox.Text) > maxMachineNumber)
                {
                    errorMessage += "控制器机号:机号超出范围,最大机号为" + maxMachineNumber.ToString() + "; ";
                    //errorMessage += "机号超出范围,最大机号为" + maxMachineNumber.ToString();
                    verifyFlag = false;
                }
            }
            if (verifyFlag)
            {
                controller.Name          = ControllerNameInputTextBox.Text;
                controller.MachineNumber = MachineNumberInputTextBox.Text;

                controller.PortName = ComPortComboBox.SelectedItem.ToString();
                controller.BaudRate = Convert.ToInt32(BaudsRateComboBox.SelectedItem);
                vm.SaveExecute(controller);
                controller.IsDirty = true;

                EventMediator.NotifyColleagues("RefreshNavigator", controller);
                RaiseEvent(new RoutedEventArgs(AddButtonClickEvent, controller));
            }
            else
            {
                this.ErrorMessage.Text       = errorMessage;
                this.ErrorMessage.Visibility = Visibility.Visible;
            }
            // }
        }