You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.3 KiB

using Newtonsoft.Json;
namespace JiLinApp.Docking.Alarm;
public class AlarmCodeHelper
{
#region Fields
private static readonly Dictionary<string, AlarmCode> AlarmCodeDict;
private static readonly List<int> LevelList;
private static readonly List<string> TypeList;
#endregion Fields
static AlarmCodeHelper()
{
string filePath = Path.Combine("config", "alarmcodes.json");
if (!File.Exists(filePath)) throw new FileNotFoundException(filePath);
using StreamReader r = new(filePath);
string jsonStr = r.ReadToEnd();
List<AlarmCode> list = JsonConvert.DeserializeObject<List<AlarmCode>>(jsonStr) ?? new();
AlarmCodeDict = list.ToDictionary(item => item.Id, item => item);
LevelList = list.GroupBy(item => item.Level).Select(it => it.First().Level).ToList();
TypeList = list.GroupBy(item => item.Type).Select(it => it.First().Type).ToList();
}
public static void VirtualInit()
{ }
public static AlarmCode Get(string id)
{
return AlarmCodeDict[id] ?? AlarmCodeDict["1000"];
}
public static bool TryGet(string id, out AlarmCode alarmCode)
{
bool flag = AlarmCodeDict.TryGetValue(id, out AlarmCode? temp);
alarmCode = temp ?? new();
return flag;
}
}