예제 #1
0
        public override Task <StepResult> Step(Timestamp t, InputValue[] inputValues)
        {
            VTQ a = inputValues[0].Value;
            VTQ b = inputValues[1].Value;

            float res = (float)(a.V.AsDouble() !+b.V.AsDouble() !);

            Thread.Sleep(100);

            VTQ r      = VTQ.Make(res, t, GetWorstOf(a.Q, b.Q));
            var result = new StepResult()
            {
                Output = new OutputValue[] {
                    new OutputValue()
                    {
                        OutputID = "Y",
                        Value    = r
                    }
                }
            };

            return(Task.FromResult(result));
        }
예제 #2
0
        private async Task AdapterRunLoopTask(CalcInstance adapter)
        {
            adapter.State = State.Running;

            Duration  cycle    = adapter.ScaledCycle();
            Duration  offset   = adapter.ScaledOffset();
            Timestamp t        = Time.GetNextNormalizedTimestamp(cycle, offset);
            string    moduleID = base.moduleID;

            await adapter.WaitUntil(t);

            var inputs    = new List <Config.Input>();
            var inputVars = new List <VariableRef>();

            var notifier = this.notifier !;

            //var listVarValueTimer = new List<VariableValue>(1);

            //var sw = System.Diagnostics.Stopwatch.StartNew();
            while (adapter.State == State.Running)
            {
                //sw.Restart();

                inputs.Clear();
                inputVars.Clear();
                foreach (Config.Input inp in adapter.CalcConfig.Inputs)
                {
                    if (inp.Variable.HasValue)
                    {
                        inputs.Add(inp);
                        inputVars.Add(inp.Variable.Value);
                    }
                }

                // Config.Input[] inputs = adapter.CalcConfig.Inputs.Where(inp => inp.Variable.HasValue).ToArray();
                // VariableRef[] inputVars = inputs.Select(inp => inp.Variable.Value).ToArray();

                VTQs values = await ReadInputVars(adapter, inputs, inputVars, t);

                // sw.Stop();
                // double dd = sw.ElapsedTicks;
                //if (dd > 100) {
                //    Console.Error.WriteLine(dd / TimeSpan.TicksPerMillisecond + " ms");
                //}

                adapter.UpdateInputValues(inputVars, values);

                InputValue[] inputValues = adapter.CurrentInputValues(t);

                List <VariableValue> inValues = inputValues.Select(v => VariableValue.Make(adapter.GetInputVarRef(v.InputID), v.Value.WithTime(t))).ToList();
                notifier.Notify_VariableValuesChanged(inValues);

                var instance = adapter.Instance;
                if (instance == null || adapter.State != State.Running)
                {
                    break;
                }
                StepResult result = await instance.Step(t, inputValues);

                OutputValue[] outValues   = result.Output ?? new OutputValue[0];
                StateValue[]  stateValues = result.State ?? new StateValue[0];

                // Console.WriteLine($"{Timestamp.Now}: out: " + StdJson.ObjectToString(outValues));
                var listVarValues = new List <VariableValue>(outValues.Length + stateValues.Length);
                foreach (OutputValue v in outValues)
                {
                    var vv = VariableValue.Make(adapter.GetOutputVarRef(v.OutputID), v.Value);
                    listVarValues.Add(vv);
                }
                foreach (StateValue v in stateValues)
                {
                    var vv = VariableValue.Make(adapter.GetStateVarRef(v.StateID), VTQ.Make(v.Value, t, Quality.Good));
                    listVarValues.Add(vv);
                }

                List <VariableValue> outputDest = new List <VariableValue>();
                foreach (Config.Output ot in adapter.CalcConfig.Outputs)
                {
                    if (ot.Variable.HasValue)
                    {
                        int idx = outValues.FindIndex(o => o.OutputID == ot.ID);
                        if (idx > -1)
                        {
                            outputDest.Add(VariableValue.Make(ot.Variable.Value, outValues[idx].Value));
                        }
                    }
                }
                notifier.Notify_VariableValuesChanged(listVarValues);

                await WriteOutputVars(outputDest);

                adapter.SetLastOutputValues(outValues);
                adapter.SetLastStateValues(stateValues);

                //sw.Stop();
                //var vvv1 = VariableValue.Make(adapter.GetLastRunDurationVarRef(), VTQ.Make(sw.ElapsedMilliseconds, t, Quality.Good));
                //listVarValueTimer.Clear();
                //listVarValueTimer.Add(vvv1);
                //notifier.Notify_VariableValuesChanged(listVarValueTimer);

                t = GetNextNormalizedTimestamp(t, cycle, offset, adapter);

                await adapter.WaitUntil(t);
            }
        }