public async Task <MotionConfig> ReadConfiguration(string filePath) { if (File.Exists(filePath)) { var reader = new StreamReader(filePath); var content = await reader.ReadToEndAsync(); reader.Close(); var config = new MotionConfig(); var motionConfigProperties = config.GetType().GetProperties(); var fields = motionConfigProperties .Select(p => new { p, attr = p.GetCustomAttribute <MotionConfigProperty>() }) .Where(p => p.attr != null); foreach (var field in fields) { var propertyName = field.attr.PropertyName; var fieldValue = BetweenStrings(content, $"\n{propertyName} ", "\n\n#"); Console.WriteLine($"{propertyName}={fieldValue}"); var motionConfigProperty = motionConfigProperties.FirstOrDefault(rp => rp.CustomAttributes.FirstOrDefault(ca => ca.ConstructorArguments.Select(coa => coa.Value).ToList().Contains(propertyName)) != null); if (motionConfigProperty != null) { motionConfigProperty.SetValue(config, fieldValue); } } config.RecordMotion = await IsRecording(); return(config); } return(null); }
public async Task SaveConfiguration(MotionConfig config) { var currentConfig = await ReadConfiguration(_motionConfigParh); if (config.RecordMotion != currentConfig.RecordMotion) { await SendRecordStateChange(config.RecordMotion); } var configParams = new List <string>(); var motionConfigProperties = config.GetType().GetProperties(); foreach (var property in motionConfigProperties) { foreach (var customAttribute in property.CustomAttributes) { var attributeName = customAttribute.ConstructorArguments.Select(ca => ca.Value).FirstOrDefault(); var value = property.GetValue(config); configParams.Add($"{attributeName}={value}"); } } await SendConfigParams(configParams); }