public Wrapper(Module m, Calculation a) { this.m = m; this.a = a; }
// This will be called from a different Thread, therefore post it to the main thread! public void Notify_AlarmOrEvent(AdapterAlarmOrEvent eventInfo, Calculation adapter) { moduleThread?.Post(Do_Notify_AlarmOrEvent, eventInfo, adapter); }
// This will be called from a different Thread, therefore post it to the main thread! public void Notify_NeedRestart(string reason, Calculation adapter) { moduleThread?.Post(Do_Notify_NeedRestart, reason, adapter); }
private void Do_Notify_NeedRestart(string reason, Calculation adapter) { CalcInstance ast = adapters.FirstOrDefault(a => a.CalcConfig.ID == adapter.ID); Task ignored = RestartAdapter(ast, reason); }
private async Task InitAdapter(CalcInstance adapter) { if (adapter.Instance == null) { throw new Exception("InitAdapter: instance is null"); } Calculation info = adapter.CalcConfig.ToCalculation(); try { var initParams = new InitParameter() { Calculation = info, LastOutput = adapter.LastOutputValues, LastState = adapter.LastStateValues, ConfigFolder = Path.GetDirectoryName(base.modelFileName) ?? "", DataFolder = initInfo.DataFolder, ModuleConfig = moduleConfig.ToNamedValues() }; adapter.State = State.InitStarted; InitResult res = await adapter.Instance.Initialize(initParams, new Wrapper(this, info)); if (adapter.State == State.InitStarted) { Config.Input[] newInputs = res.Inputs.Select(ip => MakeInput(ip, adapter.CalcConfig)).ToArray(); Config.Output[] newOutputs = res.Outputs.Select(ip => MakeOutput(ip, adapter.CalcConfig)).ToArray(); Config.State[] newStates = res.States.Select(MakeState).ToArray(); bool inputsChanged = !StdJson.ObjectsDeepEqual(adapter.CalcConfig.Inputs, newInputs); bool outputsChanged = !StdJson.ObjectsDeepEqual(adapter.CalcConfig.Outputs, newOutputs); bool statesChanged = !StdJson.ObjectsDeepEqual(adapter.CalcConfig.States, newStates); var changedMembers = new List <MemberValue>(2); if (inputsChanged) { changedMembers.Add(MemberValue.Make(moduleID, adapter.CalcConfig.ID, "Inputs", DataValue.FromObject(newInputs))); } if (outputsChanged) { changedMembers.Add(MemberValue.Make(moduleID, adapter.CalcConfig.ID, "Outputs", DataValue.FromObject(newOutputs))); } if (statesChanged) { changedMembers.Add(MemberValue.Make(moduleID, adapter.CalcConfig.ID, "States", DataValue.FromObject(newStates))); } if (changedMembers.Count > 0) { await UpdateConfig(GetModuleOrigin(), updateOrDeleteObjects : new ObjectValue[0], updateOrDeleteMembers : changedMembers.ToArray(), addArrayElements : new AddArrayElement[0]); } adapter.State = State.InitComplete; } } catch (Exception e) { Exception exp = e.GetBaseException() ?? e; adapter.State = State.InitError; adapter.LastError = exp.Message; throw new Exception($"Initialize of calculation {info.Name} failed: " + exp.Message, exp); } }