protected override string GetCommand(Mediator.Config config) { string baseDir = Path.GetDirectoryName(GetType().Assembly.Location); string local = Path.Combine(baseDir, @"OPC\OPC_Adapter.exe"); if (File.Exists(local)) { return(local); } else { return(Path.GetFullPath(Path.Combine(baseDir, @"..\..\..\..\..\OPC\Adapter\bin\Release\OPC_Adapter.exe"))); } }
protected override string GetCommand(Mediator.Config config) { const string SIMBA_LOCATION = "simba-location"; string simbaLoc = config.GetString(SIMBA_LOCATION).Trim(); if (simbaLoc == "") { throw new Exception($"No SIMBA executable specified (setting '{SIMBA_LOCATION}' in AppConfig.xml)"); } string fullLoc = Path.GetFullPath(simbaLoc); if (!File.Exists(fullLoc)) { throw new Exception($"SIMBA executable not found at {fullLoc} (setting '{SIMBA_LOCATION}' in AppConfig.xml)"); } return(fullLoc); }
protected override string GetCommand(Mediator.Config config) { string baseDir = Path.GetDirectoryName(GetType().Assembly.Location) ?? ""; string local = Path.Combine(baseDir, @"OPC\OPC_Adapter.exe"); if (File.Exists(local)) { return(local); } else { string path = Path.GetFullPath(Path.Combine(baseDir, @"..\..\..\..\..\..\OPC\Adapter\bin\Release\OPC_Adapter.exe")); if (!File.Exists(path)) { Console.Error.WriteLine("File not found: " + path); } return(path); } }
protected override string GetArgs(Mediator.Config config) { return("StartInProcSimbaController MediatorSim.dll MediatorSim.ControlAdapter.InProcSimbaControllerImpl {PORT}"); }
private async Task <InitResult> DoInit(InitParameter parameter, string code) { var config = new Mediator.Config(parameter.ModuleConfig); string libs = config.GetOptionalString("csharp-libraries", ""); bool cache = config.GetOptionalBool("csharp-cache-scripts", true); string[] assemblies = libs .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($"csharp-library does not exist: {assembly}"); } } absoluteAssemblies = absoluteAssemblies.Select(assembly => { if (assembly.ToLowerInvariant().EndsWith(".cs")) { CompileResult compileRes = CompileLib.CSharpFile2Assembly(assembly); Print(compileRes, assembly); return(compileRes.AssemblyFileName); } return(assembly); }).ToArray(); var referencedAssemblies = new List <Assembly>(); foreach (string assembly in absoluteAssemblies) { try { Assembly ass = Assembly.LoadFrom(assembly); referencedAssemblies.Add(ass); } catch (Exception exp) { throw new Exception($"Failed to load csharp-library {assembly}: {exp.Message}"); } } CodeToObjectBase objMaker; if (cache) { objMaker = new CodeToObjectCompile(); } else { objMaker = new CodeToObjectScripting(); } object obj = await objMaker.MakeObjectFromCode(parameter.Calculation.Name, code, referencedAssemblies); inputs = GetIdentifiableMembers <InputBase>(obj, "", recursive: false).ToArray(); outputs = GetIdentifiableMembers <OutputBase>(obj, "", recursive: false).ToArray(); states = GetIdentifiableMembers <AbstractState>(obj, "", recursive: true).ToArray(); var eventProviders = GetMembers <EventProvider>(obj, recursive: true); foreach (EventProvider provider in eventProviders) { provider.EventSinkRef = this; } Type type = obj.GetType(); MethodInfo[] methods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) .Where(m => m.Name == "Step" && IsStepSignature(m)) .ToArray(); if (methods.Length == 0) { throw new Exception("No Step(Timestamp t, TimeSpan dt) method found."); } MethodInfo step = methods[0]; stepAction = (Action <Timestamp, Duration>)step.CreateDelegate(typeof(Action <Timestamp, Duration>), obj); foreach (StateValue v in parameter.LastState) { AbstractState state = states.FirstOrDefault(s => s.ID == v.StateID); if (state != null) { state.SetValueFromDataValue(v.Value); } } return(new InitResult() { Inputs = inputs.Select(MakeInputDef).ToArray(), Outputs = outputs.Select(MakeOutputDef).ToArray(), States = states.Select(MakeStateDef).ToArray(), ExternalStatePersistence = true }); }
protected override string GetArgs(Mediator.Config config) { return("{PORT}"); }
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); } }