/// <summary> /// Open connection to plc /// </summary> private void OpenConnection() { using (var guard = new WriterGuard(_connectionLock)) { _client.Connect(_connectionString); //If papper was null or pdu size of the client is smaller then the last detected if (_client.IsConnected && (_papper == null || _papper.PduSize > _client.PduSize)) { var pduSize = _client.PduSize; using (var papperGuard = new WriterGuard(_papperLock)) { ClosePapperIfExits(); _papper = new PlcDataMapper(pduSize); _papper.OnRead += OnRead; _papper.OnWrite += OnWrite; foreach (var type in _rumtimeCompiler.GetTypes() .Where(type => type.GetTypeInfo() .GetCustomAttribute <MappingAttribute>() != null)) { _papper.AddMapping(type); } } } } }
/// <summary> /// If papper is existing, release the events and set it to null /// </summary> private void ClosePapperIfExits() { if (_papper != null) { using (var papperGuard = new WriterGuard(_papperLock)) { ClosePapperIfExits(); _papper.OnRead -= OnRead; _papper.OnWrite -= OnWrite; _papper = null; } } }
WriterGuard GetGuard(TextWriter writer) { if (guardCache == null) { guardCache = new Dictionary <TextWriter, WriterGuard> (); } if (guardCache.TryGetValue(writer, out WriterGuard? ret)) { return(ret); } ret = new WriterGuard(writer); guardCache.Add(writer, ret); return(ret); }
/// <summary> /// Close connection to plc /// </summary> private void CloseConnection() { using (var guard = new WriterGuard(_connectionLock)) _client.Disconnect(); }
public void Bootstrap(IEnumerable <Assembly> assemblies, IEnumerable <Type> types) { var generatedServices = new List <string>(); var clientProxiesToGenerate = new Dictionary <string, Type>(); foreach (var type in types) { var ti = type.GetTypeInfo(); var attr = ti.GetCustomAttribute <ScriptUnitAttribute>(); if (attr != null) { Scripts.Add(type); if (attr.AccessType == AccessType.Rest || attr.AccessType == AccessType.Remote) { generatedServices.Add(ControllerCreator.Create(type, attr)); _logger.LogInformation($"WebApi Controller for type {type} was generated."); } if (attr.AccessType == AccessType.SignalR || attr.AccessType == AccessType.Remote) { var result = HubCreator.Create(type, attr); generatedServices.Add(result.Item2); _logger.LogInformation($"SignalR Hub for type {type} was generated."); if (attr.ClientInterface != null) { //var ifType = typeof(IClientProxy<>).MakeGenericType(attr.ClientInterface); clientProxiesToGenerate.Add(result.Item1, attr.ClientInterface); } } switch (attr.LifecycleType) { case LifecycleType.Singleton: using (var guard = new WriterGuard(_serviceLock)) { _services.AddSingleton(type, type); _servicesChanged = true; _logger.LogInformation($"Singleton service {type} was registred."); } break; case LifecycleType.Transient: using (var guard = new WriterGuard(_serviceLock)) { _services.AddTransient(type, type); _servicesChanged = true; _logger.LogInformation($"Transient service {type} was registred."); } break; } } } var clientProxies = new List <string>(); if (generatedServices.Any()) { var compiledType = _compiler.CompileFiles(generatedServices, "Remote"); _apm.ApplicationParts.Add(new AssemblyPart(compiledType.Item1)); _locator.AddAssemblyReference(compiledType.Item1); foreach (var type in compiledType.Item2) { Type t; if (clientProxiesToGenerate.TryGetValue(type.Name.Split('.').Last(), out t)) { clientProxies.Add(ClientProxyCreator.Create(type, t)); } _services.AddTransient(type, type); _logger.LogInformation($"Transient service {type} was registred."); } } if (clientProxies.Any()) { var compiledType = _compiler.CompileFiles(clientProxies, "Proxies"); _apm.ApplicationParts.Add(new AssemblyPart(compiledType.Item1)); foreach (var type in compiledType.Item2) { _services.AddTransient(type.GetInterfaces().FirstOrDefault(), type); _logger.LogInformation($"Transient service {type} was registred."); } } }