-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDeviceData.cs
59 lines (50 loc) · 1.9 KB
/
DeviceData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
namespace EWeLink.Api
{
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
internal static class DeviceData
{
private static Dictionary<string, int>? deviceChannelCount;
private static Dictionary<int, string>? deviceTypeUuid;
public static Dictionary<string, int> DeviceChannelCount
{
get
{
if (deviceChannelCount == null)
{
LoadData();
}
return deviceChannelCount ?? new Dictionary<string, int>();
}
}
public static Dictionary<int, string> DeviceTypeUuid
{
get
{
if (deviceTypeUuid == null)
{
LoadData();
}
return deviceTypeUuid ?? new Dictionary<int, string>();
}
}
private static void LoadData()
{
deviceChannelCount = LoadAndDeserializeResource<Dictionary<string, int>>("devices-channel-length.json");
deviceTypeUuid = LoadAndDeserializeResource<Dictionary<int, string>>("devices-type-uuid.json");
}
private static T? LoadAndDeserializeResource<T>(string resourceName)
{
var assembly = typeof(DeviceData).GetTypeInfo().Assembly;
var serializer = new JsonSerializer();
var assemblyResourceName = assembly.GetManifestResourceNames().Single(x => x.EndsWith($".Resources.{resourceName}"));
using var stream = assembly.GetManifestResourceStream(assemblyResourceName);
using var reader = new StreamReader(stream);
using var jsonReader = new JsonTextReader(reader);
return serializer.Deserialize<T>(jsonReader);
}
}
}