Exemple #1
0
        public void DoWork(ICalculateContext context)
        {
            List <ICalculateOutput> outputs = new List <ICalculateOutput>();

            foreach (ICalculate calc in context.CalculateOperators)
            {
                ICalculateOutput output = calc.Calc(context.CalculateInpute);
                if (output != null)
                {
                    outputs.Add(output);
                }
            }
#if DEBUG
            List <string> vals = new List <string>();
#endif
            if (outputs != null && outputs.Count > 0)
            {
                foreach (ICalculateOutput output in outputs)
                {
                    foreach (SinkFunction sf in context.Sinks)
                    {
                        try
                        {
                            sf.Open();

                            sf.Invoke(output.DataSource, null);

                            sf.Close();
#if DEBUG
                            foreach (IMetaData md in output.DataSource)
                            {
                                vals.Add(md.TagValue);
                            }
#endif
                        }
                        catch (Exception ex)
                        {
                            Logger.Log.Error(true, sf.GetType().ToString(), ex);
                        }
                    }
                }

                if (PublishCalculateCompleted != null)
                {
                    context.CalculateOutputs = outputs;

                    PublishCalculateCompleted(context);
                }
#if DEBUG
                string resultVals = String.Join(",", vals);
                Logger.Log.Info(false, $"{context.Name}_{this.Name}_{context.Desc}-线程({Thread.CurrentThread.ManagedThreadId.ToString("0000")}):【{context.LeftTime.ToString()}-{context.RightTime.ToString()}】,【Result】:{resultVals}");
#else
                Logger.Log.Info(false, $"{context.Name}_{this.Name}_{context.Desc}-线程({Thread.CurrentThread.ManagedThreadId.ToString("0000")}):【{context.LeftTime.ToString()}-{context.RightTime.ToString()}】,【Result】:{outputs.Count.ToString()}");
#endif
            }
            else
            {
                Logger.Log.Info(false, $"{context.Name}_{this.Name}_{context.Desc}-线程({Thread.CurrentThread.ManagedThreadId.ToString("0000")}):【{context.LeftTime.ToString()}-{context.RightTime.ToString()}】,【Result】:计算结果为空");
            }
        }
        public override void Calculate(ICalculateContext context)
        {
            int inValue = In.Connected ? In.GetValue <int>() : default(int);

            Out.SetValue(inValue + 1);
            context.Success();
        }
Exemple #3
0
        public void DoWork(ICalculateContext context)
        {
            ICalculateOutput output = context.CalculateOperator.Calc(context.CalculateInpute);

            if (output != null)
            {
                foreach (SinkFunction sf in context.Sinks)
                {
                    try
                    {
                        sf.Open();

                        sf.Invoke(output.DataSource, null);

                        sf.Close();
                    }
                    catch (Exception ex)
                    {
                        Logger.Log.Error(true, sf.GetType().ToString(), ex);
                    }
                }

                if (PublishCalculateCompleted != null)
                {
                    context.CalculateOutput = output;

                    PublishCalculateCompleted(context);
                }
            }

            if (output != null)
            {
                Logger.Log.Info(false, $"{context.Name}_{this.Name}_{context.Desc}-线程({Thread.CurrentThread.ManagedThreadId.ToString("0000")}):【{context.LeftTime.ToString()}-{context.RightTime.ToString()}】,【Result】:{output.DataSource[0].TagValue}");
            }
            else
            {
                Logger.Log.Info(false, $"{context.Name}_{this.Name}_{context.Desc}-线程({Thread.CurrentThread.ManagedThreadId.ToString("0000")}):【{context.LeftTime.ToString()}-{context.RightTime.ToString()}】,【Result】:计算结果为空");
            }
        }
        private void ParallelCalculate(ICalculateContext context)
        {
            if (context == null)
            {
                Logger.Log.Info(true, "ParallelCalculate参数(context)为空");
                return;
            }

            try
            {
                bool isCalc = true;
                if (context.CalculateType == CalculateType.Expression)
                {
                    foreach (IMetaData md in context.CalculateInpute.DataSource)
                    {
                        if (_winList.ContainsKey(md.TagId))
                        {
                            md.TagName  = _winList[md.TagId].Current.TagName;
                            md.TagTime  = _winList[md.TagId].Current.TagTime;
                            md.TagValue = _winList[md.TagId].Current.TagValue;
                        }
                        else
                        {
                            isCalc = false;
                            break;
                        }
                    }
                }

                if (isCalc)
                {
                    if (_channelMessageHandler.ClientCount > 0)
                    {
                        #region
                        CalculateContext calcContext = (CalculateContext)context;

                        DownTransmission downTrans = new DownTransmission(DownTransmission.GetNewTransmissionId(calcContext.Name, calcContext.LeftTime, context.RightTime), calcContext);

                        byte[] data = TransmissionUtil.SerializeAndCompress <DownTransmission>(downTrans);

                        if (data != null && data.Length > 0)
                        {
                            _masterCache.WriteCache(downTrans.Key, data);

                            _masterCacheList.TryAdd(downTrans.Key, data);

                            Logger.Log.Info(false, $"{downTrans.Key},增加任务,内存计算任务数量:" + _masterCacheList.Count.ToString());
                        }

                        calcContext = null;
                        downTrans   = null;
                        data        = null;
                        #endregion
                    }
                    else
                    {
                        #region
                        if (_workList.Count <= 0)
                        {
                            if (_defaultWorker == null)
                            {
                                _defaultWorker = Worker.Worker.GetDefaultWorker();
                            }

                            _defaultWorker.DoWork(context);
                        }
                        else
                        {
                            IWorker worker = GetPollWorker();
                            if (worker != null)
                            {
                                worker.DoWork(context);
                            }
                        }
                        #endregion
                    }
                }
                context.CalculateInpute.DataSource = null;
                context = null;
            }
            catch (Exception ex)
            {
                Logger.Log.Info(true, $"{context.Name}-{context.Desc}-线程({Thread.CurrentThread.ManagedThreadId.ToString("0000")}):【{context.LeftTime.ToString()}-{context.RightTime.ToString()}】,异常:", ex);
            }
        }
Exemple #5
0
 public abstract void Calculate(ICalculateContext context);
 public override void Calculate(ICalculateContext context)
 {
     Result = In.Connected ? In.GetValue <int>() : Result;
     context.Success();
 }
Exemple #7
0
 public override void Calculate(ICalculateContext context)
 {
     Out.SetValue(Value);
     context.Success();
 }