2026-01-26 18:56:53 +09:00

100 lines
3.9 KiB
C#

/*
* Copyright (c) 2025 LG Electronics Inc.
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
*/
using Aurora;
using Aurora.Api;
using VisionCommon;
using System;
using System.Reflection;
using System.IO;
namespace VisionLight {
/// <summary>
/// 조명 생성 및 등록
/// </summary>
public static class LightFactory {
private static Logger _Logger = new Logger("LightFactory");
/// <summary>
/// 조명 생성 및 등록
/// </summary>
/// <param name="registry">Aurora 레지스트리</param>
public static void Register(IDeviceRegistry registry) {
try {
VersionChecker.Print(Assembly.GetExecutingAssembly().GetName());
var callingAssembly = Assembly.GetCallingAssembly(); // 위치 바꾸면 안됨.
_Logger.I($"CallingAssembly : {callingAssembly}");
var LightCnt = VisionSettingManager.VisionSetting.LightCount;
for (int i = 0; i < LightCnt; i++) {
var LightId = ((eLightId)i).ToString();
if (!VisionEnv.AuroraEnv.DeviceManager.IsRegisteredDevice(LightId)) {
var lightBase = Create(LightId, callingAssembly);
registry.Register(
LightId,
() => lightBase,
LightConfig.GetDefaultConfig(lightBase.LightConnType), (device) => new UCLightMainView(device));
}
}
LightHub.Instance.GetLightIds();
}
catch (Exception e) {
_Logger.FuncE(e.Message);
}
}
private static LightBase Create(string deviceId, Assembly callingAssembly) {
eLightModel lightModel = GetLightModel((eLightId)Enum.Parse(typeof(eLightId), deviceId));
LightBase light = GetLightFromVisionLight(lightModel, deviceId);
if (light != null) {
_Logger.FuncI($"{deviceId} is created from {lightModel}.");
return light;
}
light = GetLightFromMachine(lightModel, deviceId, callingAssembly);
if (light != null) {
_Logger.FuncI($"{deviceId} is created from {lightModel}.");
return light;
}
_Logger.FuncW($"{deviceId} is created from DEFAULT(Light_TSM_I16R).");
return new Light_TSM_I16R(deviceId); // Default
}
private static eLightModel GetLightModel(eLightId lightId) {
string configPath = Path.Combine(ApplicationExt.DataPath, "_config_device", lightId + @".json");
return JsonManager.GetValue<eLightModel>(configPath, "LightModel");
}
private static LightBase GetLightFromVisionLight(eLightModel lightModel, string deviceId) {
if (LightModelDef.LightModels.ContainsKey(lightModel)) {
Type type = LightModelDef.LightModels[lightModel];
if (type != null) {
ConstructorInfo ctor = type.GetConstructor(new[] { typeof(string) });
return (LightBase)ctor.Invoke(new object[] { deviceId });
}
}
return null;
}
private static LightBase GetLightFromMachine(eLightModel lightModel, string deviceId, Assembly callingAssembly) {
Type targetType = typeof(LightBase);
string className = lightModel.ToString();
var types = callingAssembly.GetExportedTypes();
foreach (var type in types) {
if (className == type.Name &&
targetType.IsAssignableFrom(type)) {
ConstructorInfo ctor = type.GetConstructor(new[] { typeof(string) });
return (LightBase)ctor.Invoke(new object[] { deviceId });
}
}
return null;
}
}
}