ASCU_ALL/ApiServer/Parametrs/Analog.cs

162 lines
4.6 KiB
C#
Raw Normal View History

2024-09-22 04:27:05 +05:00
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;
using NLog;
namespace ApiServer.Parametrs
{
public class AnalogParams
{
public int Index { get; set; }
public bool ShowDefault { get; set; }
public bool Skip { get; set; }
public string Name { get; set; }
public string Sname { get; set; }
public string Metric { get; set; }
public double? Min { get; set; }
public double? Max { get; set; }
public double Mul { get; set; }
public int[] Bytes {get;set;}
}
}
namespace ApiServer
{
public static partial class Configuration
{
private static Logger log = LogManager.GetCurrentClassLogger();
public static Dictionary<string,AnalogParams[]> r_analogParams = null;
public static AnalogParams[] GetAnalogParams(string name_analog)
{
log.Info("Ask analog parasm: " + name_analog);
if (r_analogParams != null && r_analogParams.ContainsKey(name_analog))
{
log.Info("Exist: " + name_analog);
return r_analogParams[name_analog];
}
var result = new List<AnalogParams>();
var list_files = Directory.GetFiles(
Path.Combine(
Directory.GetCurrentDirectory(),
"Config",
"Analog"
)
);
foreach (var file in list_files) {
log.Info("Try read " + file);
var json_conf =
JObject.Parse(
File.ReadAllText(
file
)
);
if (!json_conf.ContainsKey(name_analog))
continue;
var analog = json_conf.Value<JObject>(name_analog);
if (analog.ContainsKey("struct"))
{
var struct_arr = analog.Value<JArray>("struct");
foreach (JObject struct_el in struct_arr)
{
var analog_param = GetAnalogParam(struct_el);
if (analog_param != null)
if (result.Find(x => x.Index == analog_param.Index) == null)
result.Add(analog_param);
}
}
if (analog.ContainsKey("parent"))
{
var parent_list = analog.Value<JArray>("parent");
for(var i = parent_list.Count - 1; i >= 0; i--)
{
var parent_states = GetAnalogParams(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;
}
if (r_analogParams == null)
r_analogParams = new Dictionary<string, AnalogParams[]>();
if (r_analogParams.ContainsKey(name_analog))
r_analogParams[name_analog] = result.ToArray();
else
r_analogParams.Add(name_analog, result.ToArray());
return result.ToArray();
}
private static AnalogParams GetAnalogParam(JObject data)
{
if ( data == null || !data.ContainsKey("index"))
return null;
var index = data.Value<int>("index");
var result = new AnalogParams()
{
Index = index,
Name = "Аналог " + index,
Sname = "Аналог " + index,
Min = null,
Max = null,
Mul = 1,
Metric = "УЕ",
Skip = false,
ShowDefault = false,
Bytes = new int[0]
};
if (data.ContainsKey("skip"))
{
var skip = data.Value<bool>("skip");
if (skip)
{
result.Skip = true;
return result;
}
}
if (data.ContainsKey("name"))
result.Name = data.Value<string>("name");
if (data.ContainsKey("s_name"))
result.Sname = data.Value<string>("s_name");
if (data.ContainsKey("metric"))
result.Metric = data.Value<string>("metric");
if (data.ContainsKey("min"))
result.Min = data.Value<double>("min");
if (data.ContainsKey("max"))
result.Max = data.Value<double>("max");
if (data.ContainsKey("mul"))
result.Mul = data.Value<double>("mul");
if (data.ContainsKey("show_default"))
result.ShowDefault = data.Value<bool>("show_default");
if (data.ContainsKey("bytes"))
{
var bytes = data.Value<JArray>("bytes");
var arr = new List<int>();
try
{
for (var i = 0; i < bytes.Count; i++)
arr.Add(bytes[i].Value<int>());
result.Bytes = arr.ToArray();
}
catch
{
result.Bytes = new int[0];
}
}
return result;
}
}
}