예제 #1
0
        static void AdvancedCircuitBreaker()
        {
            Action <Exception, TimeSpan, Context> onBreak = (exception, timespan, context) =>
            {
                Console.WriteLine("onBreak Running,the exception.Message : " + exception.Message);
            };

            Action <Context> onReset = context =>
            {
                Console.WriteLine("Job Back to normal");
            };

            var breaker = Policy.Handle <ArgumentException>()
                          .AdvancedCircuitBreaker(
                failureThreshold: 0.5,                     // Break on >=50% actions result in handled exceptions...
                samplingDuration: TimeSpan.FromSeconds(3), // ... over any 10 second period
                minimumThroughput: 3,                      // ... provided at least 8 actions in the 10 second period.
                durationOfBreak: TimeSpan.FromSeconds(5),  // Break for 30 seconds.
                onBreak: onBreak, onReset: onReset);

            ISyncPolicy policy = Policy.Handle <ArgumentException>()
                                 .WaitAndRetry(new[] { TimeSpan.FromMilliseconds(200),
                                                       TimeSpan.FromMilliseconds(400) }, (ex, timesapn, context) =>
            {
                // Monitor the circuit state, for example for health reporting.
                CircuitState state = breaker.CircuitState;
                Console.WriteLine(state.ToString());

                Console.WriteLine($"Runing Retry,Exception :{ex.Message},timesapn:{timesapn}");
            });

            while (true)
            {
                try
                {
                    var policyWrap = Policy.Wrap(policy, breaker);

                    // (wraps the policies around any executed delegate: fallback outermost ... bulkhead innermost)
                    policyWrap.Execute(() =>
                    {
                        Console.WriteLine("Job Start");

                        if (DateTime.Now.Second % 2 == 0)
                        {
                            throw new ArgumentException("Hello Polly!");
                        }
                    });
                }
                catch (Exception ex)
                {
                    // 手动打开熔断器,阻止执行
                    //breaker.Isolate();
                }
                Thread.Sleep(500);

                // 恢复操作,启动执行
                //breaker.Reset();
            }
        }
예제 #2
0
        static void FallbackWrap()
        {
            Action <Exception, TimeSpan, Context> onBreak = (exception, timespan, context) =>
            {
                Console.WriteLine("onBreak Running,the exception.Message : " + exception.Message);
            };

            Action <Context> onReset = context =>
            {
                Console.WriteLine("Job Back to normal");
            };

            ISyncPolicy fallback = Policy.Handle <Polly.CircuitBreaker.BrokenCircuitException>()
                                   .Or <System.ArgumentException>()
                                   .Fallback(() =>
            {
                Console.WriteLine("Error occured,runing fallback");
            },
                                             (ex) => { Console.WriteLine($"Fallback exception:{ex.GetType().ToString()},Message:{ex.Message}"); });

            ISyncPolicy retry = Policy.Handle <ArgumentException>()
                                .WaitAndRetry(new[] { TimeSpan.FromMilliseconds(200),
                                                      TimeSpan.FromMilliseconds(300) }, (ex, timesapn, context) =>
            {
                Console.WriteLine($"Runing Retry,Exception :{ex.Message},timesapn:{timesapn}");
            });

            var breaker = Policy.Handle <ArgumentException>()
                          .AdvancedCircuitBreaker(
                failureThreshold: 0.5,                     // Break on >=50% actions result in handled exceptions...
                samplingDuration: TimeSpan.FromSeconds(3), // ... over any 10 second period
                minimumThroughput: 3,                      // ... provided at least 8 actions in the 10 second period.
                durationOfBreak: TimeSpan.FromSeconds(8),  // Break for 30 seconds.
                onBreak: onBreak, onReset: onReset);

            // Monitor the circuit state, for example for health reporting.
            CircuitState state = breaker.CircuitState;

            Console.WriteLine(state.ToString());

            while (true)
            {
                var policyWrap = Policy.Wrap(fallback, retry, breaker);

                // (wraps the policies around any executed delegate: fallback outermost ... bulkhead innermost)
                policyWrap.Execute(() =>
                {
                    Console.WriteLine("Job Start");

                    if (DateTime.Now.Second % 2 == 0)
                    {
                        throw new ArgumentException("Hello Polly!");
                    }
                });

                Thread.Sleep(300);
            }
        }
        /// <summary>
        /// 等待油口B的压力上升到设定压力的50%
        /// </summary>
        /// <param name="pressure"></param>
        //public void WaitPressureBUntil(float pressure)
        //{
        //    while (Group["B口压力"] <= 0.5 * pressure)
        //    {
        //        System.Threading.Thread.Sleep(1000);
        //    }
        //    System.Threading.Thread.Sleep(2000);
        //}
        ///// <summary>
        ///// 等待油口A的压力上升到设定压力的50%
        ///// </summary>
        ///// <param name="pressure"></param>
        //public void WaitPressureSUntil(float pressure)
        //{
        //    while (Group["A口压力"] <= 0.5 * pressure)
        //    {
        //        System.Threading.Thread.Sleep(1000);
        //    }
        //    System.Threading.Thread.Sleep(2000);
        //}

        /// <summary>
        /// 稳态压差实验-单一状态
        /// </summary>
        /// <param name="dictCurve">由曲线名字和曲线构成的字典</param>
        /// <param name="state">球阀状态,也即液控单向阀液流方向</param>
        /// <param name="pa_pressure">控制油压力</param>
        private void SteadystatePreeesureSingleSide(Dictionary <string, Curve> dictCurve, CircuitState state, float pa_pressure)
        {
            if (state != CircuitState.ABOut && state != CircuitState.BAOut)
            {
                return;
            }
            string key   = state.ToString() + "-控制油" + pa_pressure.ToString() + "MPa";
            Curve  curve = new Curve();

            curve.Name = key;
            dictCurve.Add(key, curve);

            this.SetCircuitState(state);
            this.SetThrottleValve(100f);
            this.SetTestValveState(TestValveState.右位);
            this.SetValve4(10f);
            this.SetPaPressure(pa_pressure);//设置先导油的压力
            this.SetSourcePre(this.额定压力);
            float max_flow = (float)(this.额定流量);

            this.SetSourceFlow(0);
            Pause(3000);
            const int div_cnt     = 20;
            const int measure_cnt = 10;
            float     pressure_A  = 0;
            float     pressure_B  = 0;
            float     delta_q     = (float)(max_flow / div_cnt);
            float     flow        = 0;

            for (int i = 0; i <= div_cnt; i++)
            {
                this.SetSourceFlow(i * delta_q);

                //读取压力流量数据
                flow       = 0;
                pressure_A = 0;
                pressure_B = 0;
                for (int j = 0; j < measure_cnt; j++)
                {
                    Pause(1000);
                    flow       += this.当前流量绝对值 / measure_cnt;
                    pressure_A += this.当前A口压力 / measure_cnt;
                    pressure_B += this.当前B口压力 / measure_cnt;
                }
                //将数据添加到曲线
                curve.AddPoint(flow, Math.Abs(pressure_A - pressure_B));
            }

            this.SetSourcePre(0);
            this.SetSourceFlow(0);
            this.SetThrottleValve(50f);
        }
예제 #4
0
        /// <summary>
        /// 稳态流量压力特性试验-指定阀口开度
        /// </summary>
        /// <param name="dictCurve"></param>
        /// <param name="state"></param>
        /// <param name="n">第N次试验</param>
        private void StaticFlowPressSingleState(CurvePanel panel, CircuitState state, int n)
        {
            string key   = state.ToString() + "曲线" + n.ToString();
            Curve  curve = new Curve();

            curve.Name = key;
            panel.AddCurve(curve);

            this.SetCircuitState(state);  //设置球阀状态
            this.SetThrottleValve(100f);
            this.SetSourcePre(this.额定压力);
            float max_flow = (float)(this.额定流量);

            this.SetSourceFlow(0);
            //Pause(3000);

            const int div_cnt     = 20;
            const int measure_cnt = 5;
            float     pressure_A  = 0;
            float     pressure_T  = 0;
            float     delta_q     = (float)(max_flow / div_cnt);
            float     flow        = 0;

            for (int i = 0; i <= div_cnt; i++)
            {
                this.SetSourceFlow(i * delta_q);
                //读取压力流量数据
                flow       = 0;
                pressure_A = 0;
                pressure_T = 0;
                for (int j = 0; j < measure_cnt; j++)
                {
                    Pause(1000);
                    flow       += this.当前流量绝对值 / measure_cnt;
                    pressure_A += this.当前A口压力 / measure_cnt;
                    pressure_T += this.当前T口压力 / measure_cnt;
                }
                //将数据添加到曲线
                curve.AddPoint(flow, pressure_A - pressure_T);
            }
            this.SetSourcePre(0);
            this.SetSourceFlow(0);
            this.SetThrottleValve(50f);
        }
예제 #5
0
        /// <summary>
        /// 设置球阀状态
        /// list变量的第0个阀为阀16,后续的1~12分别对应阀2.1~2.12
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool SetCircuitState(CircuitState state)
        {
            if (球阀需要手动切换)
            {
                string info = "球阀操作装置被设置为无法正常切换,请手动将球阀设置为" + state.ToString();
                LOG.Debug(info);
                MessageBox.Show(info);
            }
            //如果需要调整球阀操作时的设定压力,请更改valve类的变量:球阀操作最低设定压力
            this.允许设置球阀状态 = true;
            bool[] list = new bool[阀试验台球阀总数];
            int    ballvalve_cnt_need_operate = 11;

            switch (state)
            {
            case CircuitState.ATOut:
                list[0]  = false;
                list[1]  = true;
                list[2]  = false;
                list[3]  = false;
                list[4]  = true;
                list[5]  = true;
                list[6]  = false;
                list[7]  = true;
                list[8]  = false;
                list[9]  = false;
                list[10] = true;
                break;

            case CircuitState.ATIn:
                list[0]  = false;
                list[1]  = true;
                list[2]  = true;
                list[3]  = false;
                list[4]  = true;
                list[5]  = false;
                list[6]  = true;
                list[7]  = false;
                list[8]  = true;
                list[9]  = true;
                list[10] = false;
                break;

            case CircuitState.BTOut:
                list[0]  = false;
                list[1]  = true;
                list[2]  = false;
                list[3]  = true;
                list[4]  = false;
                list[5]  = false;
                list[6]  = true;
                list[7]  = true;
                list[8]  = false;
                list[9]  = false;
                list[10] = true;
                break;

            case CircuitState.BTIn:
                list[0]  = false;
                list[1]  = true;
                list[2]  = true;
                list[3]  = true;
                list[4]  = false;
                list[5]  = true;   //0822
                list[6]  = false;  //0822
                list[7]  = false;
                list[8]  = true;
                list[9]  = true;
                list[10] = false;
                break;

            case CircuitState.PTOut:
                list[0]  = true;     //原来为false
                list[1]  = true;
                list[2]  = false;
                list[3]  = false;
                list[4]  = false;
                list[5]  = false;
                list[6]  = false;
                list[7]  = true;
                list[8]  = false;
                list[9]  = false;
                list[10] = true;
                break;

            case CircuitState.ABOut:
                list[0]  = false;
                list[1]  = true;
                list[2]  = false;
                list[3]  = true;
                list[4]  = true;
                list[5]  = true;
                list[6]  = false;
                list[7]  = false;
                list[8]  = true;
                list[9]  = false;
                list[10] = true;
                break;

            case CircuitState.BAOut:
                list[0]  = false;
                list[1]  = true;
                list[2]  = true;
                list[3]  = true;
                list[4]  = true;
                list[5]  = false;
                list[6]  = true;
                list[7]  = true;
                list[8]  = false;
                list[9]  = true;
                list[10] = false;
                break;

            case CircuitState.PAOut:
                list[0]  = true;   //0821改原为false
                list[1]  = true;
                list[2]  = true;
                list[3]  = false;
                list[4]  = true;
                list[5]  = false;
                list[6]  = false;
                list[7]  = true;
                list[8]  = false;
                list[9]  = true;
                list[10] = false;
                break;

            case CircuitState.PBOut:
                list[0]  = true;   //原来为false
                list[1]  = true;
                list[2]  = true;   //原来为false
                list[3]  = true;
                list[4]  = false;
                list[5]  = false;
                list[6]  = false;
                list[7]  = false;
                list[8]  = true;
                list[9]  = false;
                list[10] = true;        //原来为false
                break;

            case CircuitState.P:
                list[0]  = true;
                list[1]  = true;
                list[2]  = false;
                list[3]  = false;
                list[4]  = false;
                list[5]  = false;
                list[6]  = false;
                list[7]  = false;
                list[8]  = false;
                list[9]  = false;
                list[10] = false;
                break;

            case CircuitState.T:
                list[0]  = false;
                list[1]  = false;
                list[2]  = true;
                list[3]  = false;
                list[4]  = false;
                list[5]  = true;
                list[6]  = false;
                list[7]  = false;
                list[8]  = false;
                list[9]  = true;
                list[10] = true;
                break;

            case CircuitState.A:
                list[0]  = false;
                list[1]  = false;
                list[2]  = false;
                list[3]  = false;
                list[4]  = true;
                list[5]  = true;
                list[6]  = false;
                list[7]  = false;
                list[8]  = false;
                list[9]  = false;
                list[10] = false;
                break;

            case CircuitState.B:
                list[0]  = false;
                list[1]  = false;
                list[2]  = false;
                list[3]  = true;
                list[4]  = false;
                list[5]  = false;
                list[6]  = true;
                list[7]  = false;
                list[8]  = false;
                list[9]  = false;
                list[10] = false;
                break;

            case CircuitState.PABTOut:
                list[0]  = true;
                list[1]  = true;
                list[2]  = true;
                list[3]  = true;
                list[4]  = true;
                list[5]  = false;
                list[6]  = false;
                list[7]  = false;
                list[8]  = true;
                list[9]  = true;
                list[10] = false;
                break;

            case CircuitState.PT2Out:
                list[0]  = true;   //0828修改,做内泄露实验时需要球阀16打开
                list[1]  = false;
                list[2]  = true;
                list[3]  = false;
                list[4]  = false;
                list[5]  = false;
                list[6]  = false;
                list[7]  = false;
                list[8]  = false;
                list[9]  = false;
                list[10] = false;
                break;

            case CircuitState.ALLOn:
                list[0]  = true;
                list[1]  = true;
                list[2]  = true;
                list[3]  = true;
                list[4]  = true;
                list[5]  = true;
                list[6]  = true;
                list[7]  = true;
                list[8]  = true;
                list[9]  = true;
                list[10] = true;

                list[11] = true;
                list[12] = true;

                ballvalve_cnt_need_operate = 13;
                break;
            }

            //设置球阀状态
            for (int i = 0; i < ballvalve_cnt_need_operate; i++)
            {
                //Group[conVarNameL[i]] = list[i];
                //Group[conVarNameR[i]] = !list[i];
            }

            //等待球阀接近开关反馈状态
            int timeout;

            for (timeout = 球阀操作最长允许超时; timeout >= 0; timeout--)
            {
                int i;
                for (i = 0; i < ballvalve_cnt_need_operate; i++)
                {
                    //if (Group[retVarNameL[i]] != list[i] || Group[retVarNameR[i]] == list[i])
                    {
                        LOG.Debug(retVarNameL[i] + " 或者 " + retVarNameR[i] + " 尚未到位,继续等待球阀操作到位");
                        break;
                    }
                }
                if (i == ballvalve_cnt_need_operate)
                {
                    break;
                }

                Pause(1000);
            }
            if (timeout <= 0)
            {
                throw new BallValveOperateTimeOutException();
            }

            for (int i = 0; i < ballvalve_cnt_need_operate; i++)
            {
                //Group[conVarNameL[i]] = false;
                //Group[conVarNameR[i]] = false;
            }
            this.允许设置球阀状态 = false;
            LOG.Debug("已经将阀试验台球阀状态设置为" + state.ToString());
            return(true);
        }