public ThreadTreeNode(ModuleThread aThread) { theThread = aThread; theThread.ScheduleChanged += NotifyModel; theThread.StateChanged += NotifyModel; theThread.StatusMessageChanged += NotifyModel; }
public override async Task Init(ModuleInitInfo info, VariableValue[] restoreVariableValues, Notifier notifier, ModuleThread moduleThread) { this.info = info; await base.Init(info, restoreVariableValues, notifier, moduleThread); }
public override async Task Init(ModuleInitInfo info, VariableValue[] restoreVariableValues, Notifier notifier, ModuleThread moduleThread) { await base.Init(info, restoreVariableValues, notifier, moduleThread); connection = await HttpConnection.ConnectWithModuleLogin(info, this); await connection.EnableAlarmsAndEvents(Severity.Info); }
private ThreadTreeNode CreateThreadTreeNode(ModuleThread thread) { ThreadTreeNode node = new ThreadTreeNode(thread) {Text = thread.Name, Tag = thread}; thread.ThreadAdded += thread_ThreadAdded; thread.ThreadRemoved += thread_ThreadRemoved; thread.StatusMessageChanged += thread_StatusMessageChanged; theThreadTreeNodes.Add(thread.Id, node); return node; }
public override async Task Init(ModuleInitInfo info, VariableValue[] restoreVariableValues, Notifier notifier, ModuleThread moduleThread) { theSyncContext = SynchronizationContext.Current; await base.Init(info, restoreVariableValues, notifier, moduleThread); var config = info.GetConfigReader(); clientPort = info.LoginPort; string baseDir = config.GetString("base-dir"); string host = config.GetString("listen-host"); int port = config.GetInt("listen-port"); string strViewAssemblies = config.GetString("view-assemblies"); const string releaseDebugPlaceHolder = "{RELEASE_OR_DEBUG}"; if (strViewAssemblies.Contains(releaseDebugPlaceHolder)) { #if DEBUG strViewAssemblies = strViewAssemblies.Replace(releaseDebugPlaceHolder, "Debug"); #else strViewAssemblies = strViewAssemblies.Replace(releaseDebugPlaceHolder, "Release"); #endif } string[] viewAssemblies = strViewAssemblies .Split(new char[] { ';', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()) .ToArray(); absolutBaseDir = Path.GetFullPath(baseDir); if (!Directory.Exists(absolutBaseDir)) { throw new Exception($"base-dir does not exist: {absolutBaseDir}"); } string[] absoluteViewAssemblies = viewAssemblies.Select(d => Path.GetFullPath(d)).ToArray(); foreach (string dir in absoluteViewAssemblies) { if (!File.Exists(dir)) { throw new Exception($"view-assembly does not exist: {dir}"); } } viewTypes = ReadAvailableViewTypes(absolutBaseDir, BundlesPrefix, absoluteViewAssemblies); uiModel = MakeUiModel(model, viewTypes); var builder = new WebHostBuilder(); builder.UseKestrel((KestrelServerOptions options) => { if (host.Equals("localhost", StringComparison.InvariantCultureIgnoreCase)) { options.ListenLocalhost(port); } else { options.Listen(IPAddress.Parse(host), port); } }); builder.UseWebRoot(absolutBaseDir); builder.UseStartup <Module>(); //builder.ConfigureLogging(logging => { // logging.AddConsole(); // Microsoft.Extensions.Logging.Console // logging.SetMinimumLevel(LogLevel.Information); //}); webHost = builder.Build(); webHost.Start(); }
public override async Task Init(ModuleInitInfo info, VariableValue[] restoreVariableValues, Notifier notifier, ModuleThread moduleThread) { this.moduleName = info.ModuleName; this.notifier = notifier; logger = LogManager.GetLogger(info.ModuleName); var config = info.GetConfigReader(); string cmd = config.GetString("ExternalCommand"); string args = config.GetString("ExternalArgs"); const string portPlaceHolder = "{PORT}"; if (!args.Contains(portPlaceHolder)) { throw new Exception("Missing port placeholder in args parameter: {PORT}"); } var server = TcpConnectorServer.ListenOnFreePort(); int port = server.Port; args = args.Replace(portPlaceHolder, port.ToString()); const string releaseDebugPlaceHolder = "{RELEASE_OR_DEBUG}"; if (args.Contains(releaseDebugPlaceHolder)) { #if DEBUG args = args.Replace(releaseDebugPlaceHolder, "Debug"); #else args = args.Replace(releaseDebugPlaceHolder, "Release"); #endif } try { var taskConnect = server.WaitForConnect(TimeSpan.FromSeconds(60)); process = StartProcess(cmd, args); while (!process.HasExited && !taskConnect.IsCompleted) { await Task.Delay(TimeSpan.FromMilliseconds(50)); } if (process.HasExited) { throw new Exception($"Failed to start command \"{cmd}\" with arguments \"{args}\""); } connection = await taskConnect; var parentInfo = new ParentInfoMsg() { PID = Process.GetCurrentProcess().Id }; Task ignored = SendVoidRequest(parentInfo); var initMsg = new InitOrThrowMsg() { InitInfo = info, RestoreVariableValues = restoreVariableValues }; Task tInit = SendVoidRequest(initMsg); taskReceive = connection.ReceiveAndDistribute(onEvent); Task t = await Task.WhenAny(tInit, taskReceive); if (t != tInit) { if (process.HasExited) { throw new Exception("Module process terminated during Init call."); } else { throw new Exception("TCP connection broken to Module process during Init call."); } } await tInit; } catch (Exception) { if (connection != null) { connection.Close("Init failed."); } StopProcess(process); process = null; throw; } finally { server.StopListening(); } }
public override async Task Init(ModuleInitInfo info, VariableValue[] restoreVariableValues, Notifier notifier, ModuleThread moduleThread) { this.notifier = notifier; con = await HttpConnection.ConnectWithModuleLogin(info, listener : null, timeoutSeconds : 10 * 60); moduleID = info.ModuleID; log = LogManager.GetLogger(info.ModuleID); }
private void AddThreadNode(ModuleThread aNewThread) { if (aNewThread.ParentThread != null) { ThreadTreeNode node = theThreadTreeNodes[aNewThread.ParentThread.Id]; if (node != null) node.Nodes.Add(CreateThreadTreeNode(aNewThread)); } }
private void DeleteThreadNode(ModuleThread aDeletedThread) { if (aDeletedThread.ParentThread != null) { ThreadTreeNode node = theThreadTreeNodes[aDeletedThread.Id]; if (node != null && node.Parent != null) node.Parent.Nodes.Remove(node); } }
private void ThreadStatusMessageChanged(ModuleThread aThread) { ThreadTreeNode node = theThreadTreeNodes[aThread.Id]; }
public override Task Init(ModuleInitInfo info, VariableValue[] restoreVariableValues, Notifier notifier, ModuleThread moduleThread) { // moduleThread is null here will be set later! CheckStarted(); var promise = new TaskCompletionSource <bool>(); initPromise = promise; queue.Post(new WorkItem(MethodID.Init, promise, info, restoreVariableValues, notifier)); return(promise.Task); }
/// <summary> /// Called as the first method after module start for initialization. /// </summary> /// <param name="info">Provides information like module id, login information and configuration</param> /// <param name="restoreVariableValues">Contains the last value for all module variables with <see cref="Variable.Remember"/> == true</param> /// <param name="notifier">Used to notify the Mediator core about events, e.g. variable value changes and alarms</param> /// <param name="moduleThread">Can be used to post methods to the module's main thread</param> public abstract Task Init(ModuleInitInfo info, VariableValue[] restoreVariableValues, Notifier notifier, ModuleThread moduleThread);
public override async Task Init(ModuleInitInfo info, VariableValue[] restoreVariableValues, Notifier notifier, ModuleThread moduleThread) { await base.Init(info, restoreVariableValues, notifier, moduleThread); foreach (VariableValue v in restoreVariableValues) { if (varVals.ContainsKey(v.Variable.Object)) { VariableValue[] values = varVals[v.Variable.Object]; for (int i = 0; i < values.Length; ++i) { if (values[i].Variable == v.Variable) { values[i].Value = v.Value; break; } } } } }
public override async Task Init(ModuleInitInfo info, VariableValue[] restoreVariableValues, Notifier notifier, ModuleThread moduleThread) { this.moduleID = info.ModuleID; this.notifier = notifier; var config = info.GetConfigReader(); modelFileName = Path.GetFullPath(config.GetString("model-file")); modelAsString = ReadConfigFileToString(modelFileName); model = DeserializeModelFromString(modelAsString); ModifyModelAfterInit(); await OnConfigModelChanged(init : true); }
public override async Task Init(ModuleInitInfo info, VariableValue[] restoreVariableValues, Notifier notifier, ModuleThread moduleThread) { this.moduleThread = moduleThread; this.initInfo = info; this.moduleConfig = info.GetConfigReader(); await base.Init(info, restoreVariableValues, notifier, moduleThread); string strAssemblies = moduleConfig.GetOptionalString("adapter-assemblies", ""); const string releaseDebugPlaceHolder = "{RELEASE_OR_DEBUG}"; if (strAssemblies.Contains(releaseDebugPlaceHolder)) { #if DEBUG strAssemblies = strAssemblies.Replace(releaseDebugPlaceHolder, "Debug"); #else strAssemblies = strAssemblies.Replace(releaseDebugPlaceHolder, "Release"); #endif } string[] assemblies = strAssemblies .Split(new char[] { ';', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()) .ToArray(); string[] absoluteAssemblies = assemblies.Select(d => Path.GetFullPath(d)).ToArray(); foreach (string assembly in absoluteAssemblies) { if (!File.Exists(assembly)) { throw new Exception($"calculation-assembly does not exist: {assembly}"); } } var adapterTypes = Reflect.GetAllNonAbstractSubclasses(typeof(CalculationBase)).ToList(); adapterTypes.AddRange(absoluteAssemblies.SelectMany(fLoadCalcTypesFromAssembly)); adapterTypesAttribute.Clear(); mapAdapterTypes.Clear(); foreach (Type type in adapterTypes) { Identify?id = type.GetCustomAttribute <Identify>(); if (id != null) { mapAdapterTypes[id.ID] = type; adapterTypesAttribute.Add(id); } } foreach (CalcInstance adapter in adapters) { adapter.CreateInstance(mapAdapterTypes); } Dictionary <VariableRef, VTQ> varMap = restoreVariableValues.ToDictionary(v => v.Variable, v => v.Value); foreach (CalcInstance adapter in adapters) { adapter.SetInitialOutputValues(varMap); adapter.SetInitialStateValues(varMap); } try { Task[] initTasks = adapters.Select(InitAdapter).ToArray(); await Task.WhenAll(initTasks); } catch (Exception exp) { string[] failedAdapters = adapters .Where(a => a.State == State.InitError) .Select(a => "Init of calculation '" + a.CalcConfig.Name + "' failed: " + a.LastError) .ToArray(); string errMessage = failedAdapters.Length > 0 ? string.Join("; ", failedAdapters) : exp.Message; Console.Error.WriteLine(errMessage); await Shutdown(); throw new Exception(errMessage); } }
public override async Task Init(ModuleInitInfo info, VariableValue[] restoreVariableValues, Notifier notifier, ModuleThread moduleThread) { this.moduleThread = moduleThread; await base.Init(info, restoreVariableValues, notifier, moduleThread); var config = info.GetConfigReader(); string strAssemblies = config.GetOptionalString("adapter-assemblies", ""); const string releaseDebugPlaceHolder = "{RELEASE_OR_DEBUG}"; if (strAssemblies.Contains(releaseDebugPlaceHolder)) { #if DEBUG strAssemblies = strAssemblies.Replace(releaseDebugPlaceHolder, "Debug"); #else strAssemblies = strAssemblies.Replace(releaseDebugPlaceHolder, "Release"); #endif } string[] assemblies = strAssemblies.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); string[] absoluteAssemblies = assemblies.Select(d => Path.GetFullPath(d)).ToArray(); foreach (string assembly in absoluteAssemblies) { if (!File.Exists(assembly)) { throw new Exception($"adapter-assembly does not exist: {assembly}"); } } foreach (VariableValue v in restoreVariableValues) { string dataItemID = v.Variable.Object.LocalObjectID; if (dataItemsState.ContainsKey(dataItemID)) { dataItemsState[dataItemID].LastReadValue = v.Value; } } var adapterTypes = Reflect.GetAllNonAbstractSubclasses(typeof(AdapterBase)).ToList(); adapterTypes.AddRange(absoluteAssemblies.SelectMany(fLoadAdaptersFromAssembly)); mapAdapterTypes.Clear(); foreach (var type in adapterTypes) { Identify id = type.GetCustomAttribute <Identify>(); if (id != null) { mapAdapterTypes[id.ID] = type; } } foreach (AdapterState adapter in adapters) { adapter.CreateInstance(mapAdapterTypes); } try { Task[] initTasks = adapters.Select(InitAdapter).ToArray(); await Task.WhenAll(initTasks); } catch (Exception exp) { string[] failedAdapters = adapters .Where(a => a.State == State.InitError) .Select(a => "Init of IO adapter '" + a.Config.Name + "' failed: " + a.LastError) .ToArray(); string errMessage = failedAdapters.Length > 0 ? string.Join("; ", failedAdapters) : exp.Message; Console.Error.WriteLine(errMessage); await Shutdown(); throw new Exception(errMessage); } }