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

106 lines
2.5 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 State
{
public byte Index { get; set; }
public string Name { get; set; }
public string Color { get; set; }
}
public class StateStruct
{
public string Name { get; set; }
public State[] States { get; set; }
}
}
namespace ApiServer
{
public static partial class Configuration
{
public static StateStruct[] GetStateStructArray()
{
var result = new List<StateStruct>();
var json_conf =
JObject.Parse(
File.ReadAllText(
Path.Combine(
Directory.GetCurrentDirectory(),
"Config",
"StateStruct.json"
)
)
);
var list_objects = json_conf.GetEnumerator();
while (list_objects.MoveNext())
{
var sub_res = GetStateStruct(
list_objects.Current.Key,
(JArray)list_objects.Current.Value
);
if (sub_res != null)
result.Add(sub_res);
}
return result.ToArray();
}
public static StateStruct GetStateStruct(string name)
{
var json_conf =
JObject.Parse(
File.ReadAllText(
Path.Combine(
Directory.GetCurrentDirectory(),
"Config",
"StateStruct.json"
)
)
);
if (!json_conf.ContainsKey(name))
return null;
return GetStateStruct(name, json_conf.Value<JArray>(name));
}
private static StateStruct GetStateStruct(string name, JArray data)
{
if (data == null)
return null;
var result = new StateStruct() { Name = name };
var sub_res = new List<State>();
foreach (JObject j_state_struct in data)
{
var state = GetState(j_state_struct);
if (state == null)
continue;
sub_res.Add(state);
}
result.States = sub_res.ToArray();
return result;
}
private static State GetState(JObject data)
{
if (
data == null ||
!data.ContainsKey("index") ||
!data.ContainsKey("name") ||
!data.ContainsKey("color")
)
return null;
return new State()
{
Index = data.Value<byte>("index"),
Name = data.Value<string>("name"),
Color = data.Value<string>("color")
};
}
}
}