ASCU_ALL/ApiServer/Parametrs/Discret.cs
2024-09-22 04:27:05 +05:00

143 lines
3.6 KiB
C#

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<DiscretParams>();
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<JObject>(name_discret);
if (discret.ContainsKey("struct"))
{
var struct_arr = discret.Value<JArray>("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<JArray>("parent");
for(var i = parent_list.Count - 1; i >= 0; i--)
{
var parent_states = GetDiscretParams(parent_list[i].Value<string>());
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<bool>("skip");
if (skip)
{
return new DiscretParams()
{
Index = data.Value<int>("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<JArray>("bits");
var arr = new List<ByteBit>();
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<int>("byte_index"),
BitIndex = ((JObject)bits[i]).Value<byte>("bit_index")
});
}
}
catch
{
return null;
}
return new DiscretParams()
{
Index = data.Value<int>("index"),
Skip = false,
Name = data.Value<string>("name"),
Bits = arr.ToArray(),
StateStruct = data.Value<string>("state_struct")
};
}
}
}