Repository navigation
Expand file tree
/
Copy pathappsettings.json
More file actions
50 lines (43 loc) · 2 KB
/
Copy pathappsettings.json
File metadata and controls
50 lines (43 loc) · 2 KB
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
namespace GenetecEdwardsBridge
{
public class JsonConfigLoader
{
// Global variables to hold parsed configurations
public GenetecSettings GenetecConfig { get; private set; }
public EdwardsSettings EdwardsConfig { get; private set; }
// Highly optimized lookup dictionary (Key: Camera GUID string in lowercase)
public Dictionary<string, CameraMapping> LookupMap { get; private set; }
public void LoadConfiguration()
{
try
{
// 1. Build the configuration reader targeting appsettings.json
var builder = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
// 2. Extract specific configuration sections straight into POCO classes
GenetecConfig = configuration.GetSection("GenetecConfig").Get<GenetecSettings>();
EdwardsConfig = configuration.GetSection("EdwardsConfig").Get<EdwardsSettings>();
// 3. Extract the array of mappings
var mappings = configuration.GetSection("HospitalMap").Get<List<CameraMapping>>() ?? new List<CameraMapping>();
// 4. Transform the list into an in-memory dictionary for immediate O(1) lookups
LookupMap = mappings
.Where(m => !string.IsNullOrEmpty(m.CameraGuid))
.ToDictionary(
m => m.CameraGuid.ToLower().Trim(),
m => m
);
}
catch (Exception ex)
{
throw new InvalidOperationException($"Critical failure parsing appsettings.json: {ex.Message}", ex);
}
}
}
}