using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using ApiServer.Parametrs; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace ApiServer.Parametrs { public class DiscretParams { public int Index { get; set; } public bool Skip { get; set; } public string Name { get; set; } public ByteBit[] Bits {get;set;} public string StateStruct { get; set; } } public class ByteBit { public int ByteIndex { get; set; } public byte BitIndex { get; set; } } } namespace ApiServer { public static partial class Configuration { public static DiscretParams[] GetDiscretParams(string name_discret) { var result = new List(); var list_files = Directory.GetFiles( Path.Combine( Directory.GetCurrentDirectory(), "Config", "Discret" ) ); foreach (var file in list_files) { var json_conf = JObject.Parse( File.ReadAllText( file ) ); if (!json_conf.ContainsKey(name_discret)) continue; var discret = json_conf.Value(name_discret); if (discret.ContainsKey("struct")) { var struct_arr = discret.Value("struct"); foreach (JObject struct_el in struct_arr) { var discret_param = GetDiscretParam(struct_el); if (discret_param != null) if (result.Find(x => x.Index == discret_param.Index) == null) result.Add(discret_param); } } if (discret.ContainsKey("parent")) { var parent_list = discret.Value("parent"); for(var i = parent_list.Count - 1; i >= 0; i--) { var parent_states = GetDiscretParams(parent_list[i].Value()); foreach (var state in parent_states) { if (state != null) if (result.Find(x => x.Index == state.Index) == null) result.Add(state); } } } break; } return result.ToArray(); } private static DiscretParams GetDiscretParam(JObject data) { if ( data == null || !data.ContainsKey("index")) return null; if (data.ContainsKey("skip")) { var skip = data.Value("skip"); if (skip) { return new DiscretParams() { Index = data.Value("index"), Skip = true }; } } if ( data == null || !data.ContainsKey("index") || !data.ContainsKey("name") || !data.ContainsKey("state_struct") || !data.ContainsKey("bits") ) return null; var bits = data.Value("bits"); var arr = new List(); try { for (var i = 0; i < bits.Count; i++) { if ( bits[i] == null || !((JObject)bits[i]).ContainsKey("byte_index") || !((JObject)bits[i]).ContainsKey("bit_index") ) continue; arr.Add(new ByteBit() { ByteIndex = ((JObject)bits[i]).Value("byte_index"), BitIndex = ((JObject)bits[i]).Value("bit_index") }); } } catch { return null; } return new DiscretParams() { Index = data.Value("index"), Skip = false, Name = data.Value("name"), Bits = arr.ToArray(), StateStruct = data.Value("state_struct") }; } } }