/// <summary> /// 加载所有配置实现 /// </summary> /// <param name="services"></param> private static void AddImplementTypes(this IServiceCollection services) { var configs = new ConfigCollection(); var assemblies = AssemblyHelper.Load(); foreach (var assembly in assemblies) { var types = assembly.GetTypes().Where(m => typeof(IConfig).IsImplementType(m) && typeof(IConfig) != m); foreach (var implType in types) { if (implType.FullName.NotNull()) { var descriptor = new ConfigDescriptor { Type = implType.FullName.Contains(".Lib.") ? ConfigType.Library : ConfigType.Module, Code = implType.Name.Replace("Config", ""), ImplementType = implType }; configs.Add(descriptor); } } } services.AddChangedEvent(assemblies, configs); services.AddSingleton <IConfigCollection>(configs); }
private IConfig Get(ConfigDescriptor descriptor) { var key = _redisHelper.GetKey($"{CACHE_KEY}:{descriptor.Type.ToString().ToUpper()}:{descriptor.Code.ToUpper()}"); var cache = _redisHelper.Db.StringGetAsync(key).Result; if (cache.HasValue) { return((IConfig)_redisSerializer.Deserialize(cache, descriptor.ImplementType)); } IConfig config; var json = _storageProvider.GetJson(descriptor.Type, descriptor.Code).Result; if (json.IsNull()) { config = (IConfig)Activator.CreateInstance(descriptor.ImplementType); if (descriptor.Type == ConfigType.Library) { var section = _cfg.GetSection(descriptor.Code); if (section != null) { section.Bind(config); } } } else { config = (IConfig)JsonConvert.DeserializeObject(json, descriptor.ImplementType); } _redisHelper.Db.StringSetAsync(key, _redisSerializer.Serialize(config)).GetAwaiter().GetResult(); return(config); }
public async Task <T> Resolve <T>() where T : IConfig, new() { var type = typeof(T); var cacheKey = CacheKeyPrefix + type.FullName; if (!_cacheHandler.TryGetValue(cacheKey, out T config)) { config = new T(); var list = await _storage.GetAll(); if (list != null && list.Any()) { var properties = type.GetProperties(); foreach (var property in properties) { var key = type.FullName + "." + property.Name; var configDescriptor = list.FirstOrDefault(m => m.Key.EqualsIgnoreCase(key)); if (configDescriptor != null) { property.SetValue(config, configDescriptor.Value); } else { configDescriptor = new ConfigDescriptor { Key = key, Value = string.Empty, Remarks = property.Name }; var value = property.GetValue(config); if (value != null) { configDescriptor.Value = value.ToString(); } var desc = property.GetCustomAttributes(false) .FirstOrDefault(m => m.GetType() == typeof(DescriptionAttribute)); if (desc != null) { configDescriptor.Remarks = ((DescriptionAttribute)desc).Description; } await _storage.Add(configDescriptor); } CacheDic.TryAdd(key, cacheKey); } } await _cacheHandler.SetAsync(cacheKey, config); } return(config); }
/// <summary> /// 解析所有配置信息 /// </summary> /// <returns></returns> public async Task RefreshAll() { if (_moduleCollection != null) { _logger.LogInformation("刷新所有配置项"); //查询所有 var list = await _storage.GetAll(); foreach (var module in _moduleCollection) { _logger.LogInformation("刷新配置项:{@name}", module.Name); var configTypes = module.AssemblyDescriptor.Infrastructure.GetTypes().Where(m => typeof(IConfig).IsAssignableFrom(m)).ToList(); if (configTypes.Any()) { foreach (var configType in configTypes) { var config = Activator.CreateInstance(configType); var properties = configType.GetProperties(); foreach (var property in properties) { var key = configType.FullName + "." + property.Name; if (!list.Any(m => m.Key.EqualsIgnoreCase(key))) { var configDescriptor = new ConfigDescriptor { Key = key, Value = string.Empty, Remarks = property.Name }; var value = property.GetValue(config); if (value != null) { configDescriptor.Value = value.ToString(); } var desc = property.GetCustomAttributes(false).FirstOrDefault(m => m.GetType() == typeof(DescriptionAttribute)); if (desc != null) { configDescriptor.Remarks = ((DescriptionAttribute)desc).Description; } await _storage.Add(configDescriptor); _logger.LogInformation("新增配置:{@key}", key); } } } } } } }
public async Task Add(ConfigDescriptor descriptor) { if (descriptor == null) { return; } await _repository.AddAsync(new ConfigEntity { Key = descriptor.Key, Value = descriptor.Value, Remarks = descriptor.Remarks }); }
public void ReadConfig() { #if (!TEST_WITHOUT_PIN2DMD) try { byte[] frame = new byte[2052]; frame[0] = 0x81; frame[1] = 0xc3; frame[2] = 0xe7; frame[3] = 0xff; // cmd frame[4] = 0x10; var writer = _pin2DmdDevice.OpenEndpointWriter(WriteEndpointID.Ep01); int bytesWritten; var error = writer.Write(frame, 2000, out bytesWritten); if (error != ErrorCode.None) { Logger.Error("Error sending data to device: {0}", UsbDevice.LastErrorString); } } catch (Exception e) { Logger.Error(e, $"Error sending data to {ProductString}: {e.Message}"); } try { byte[] config = new byte[64]; var reader = _pin2DmdDevice.OpenEndpointReader(ReadEndpointID.Ep01); int bytesRead; var error = reader.Read(config, 2000, out bytesRead); if (error != ErrorCode.None) { Logger.Error("Error reading config from device: {0}", UsbDevice.LastErrorString); } else { pin2dmdConfig = ReadConfigUsingPointer(config); } } catch (Exception e) { Logger.Error(e, $"Error reading config from {ProductString}: {e.Message}"); } #endif }
internal Configuration(DeviceHandle handle, int configuration) { descriptor = handle.Device.GetConfigDescriptor(configuration); }
internal static unsafe UsbConfigInfo FromUsbConfigDescriptor(global::LibUsbDotNet.LibUsb.UsbDevice device, ConfigDescriptor descriptor) { Debug.Assert(descriptor.DescriptorType == (int)DescriptorType.Config, "A config descriptor was expected"); UsbConfigInfo value = new UsbConfigInfo(); value.Attributes = descriptor.Attributes; value.Configuration = device.GetStringDescriptor(descriptor.Configuration, failSilently: true); value.ConfigurationValue = descriptor.ConfigurationValue; value.RawDescriptors = new byte[descriptor.ExtraLength]; if (descriptor.ExtraLength > 0) { Span <byte> extra = new Span <byte>(descriptor.Extra, descriptor.ExtraLength); extra.CopyTo(value.RawDescriptors); } var interfaces = (Interface *)descriptor.Interface; for (int i = 0; i < descriptor.NumInterfaces; i++) { var values = UsbInterfaceInfo.FromUsbInterface(device, interfaces[i]); value.interfaces.AddRange(values); } value.MaxPower = descriptor.MaxPower; return(value); }