157 lines
6.8 KiB
C#
157 lines
6.8 KiB
C#
using Aurora;
|
|
using Aurora.Api;
|
|
using MilCommon;
|
|
using VisionCommon;
|
|
using System;
|
|
using System.Reflection;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace MilCamera {
|
|
/// <summary>
|
|
/// 카메라 생성 및 등록
|
|
/// </summary>
|
|
public static class CameraFactory {
|
|
private static Logger _Logger = new Logger("CameraFactory");
|
|
private static IDisposable _EventDisposable;
|
|
|
|
/// <summary>
|
|
/// 카메라 생성 및 등록
|
|
/// </summary>
|
|
/// <param name="registry">Aurora 레지스트리</param>
|
|
public static void Register(IDeviceRegistry registry) {
|
|
VersionChecker.Print(Assembly.GetExecutingAssembly().GetName());
|
|
var callingAssembly = Assembly.GetCallingAssembly(); // 위치 바꾸면 안됨.
|
|
_Logger.I($"CallingAssembly : {callingAssembly}");
|
|
|
|
SetObservable();
|
|
|
|
try {
|
|
VersionChecker.Print(Assembly.GetExecutingAssembly().GetName());
|
|
|
|
var visionSetting = VisionSettingManager.VisionSetting;
|
|
if (visionSetting.RecipeModelType == eRecipeModelType.AuroraModel) {
|
|
foreach (string modelName in VisionEnv.AuroraEnv.ModelManager.Models()) {
|
|
var vsModelDef = VisionEnv.AuroraEnv.ModelManager.ModelItem(modelName, VisionKeys.VisionRecipeKey) as VisionModelDef;
|
|
RegisterInternal(registry, vsModelDef, callingAssembly);
|
|
}
|
|
}
|
|
else if (visionSetting.RecipeModelType == eRecipeModelType.AuroraIngredient) {
|
|
var vsIngredient = VisionEnv.AuroraEnv.IngredientsManager.Ingredient(VisionKeys.VisionRecipeKey);
|
|
foreach (var vsIngredientName in vsIngredient.Items()) {
|
|
var vsIngredientDef = vsIngredient.IngredientItem(vsIngredientName.name) as VisionIngredientDef;
|
|
RegisterInternal(registry, vsIngredientDef, callingAssembly);
|
|
}
|
|
}
|
|
else {
|
|
foreach (var recipeItem in VisionEnv.AuroraEnv.RecipeManager.Recipes()) {
|
|
var vsRecipeDef = VisionEnv.AuroraEnv.RecipeManager.RecipeItem(recipeItem.Id, VisionKeys.VisionRecipeKey) as VisionRecipeDef;
|
|
RegisterInternal(registry, vsRecipeDef, callingAssembly);
|
|
}
|
|
}
|
|
|
|
SetCameraAgentManager();
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
private static void SetObservable() {
|
|
if (_EventDisposable == null) {
|
|
var observable = VisionEventId.GetRecipeUpdateObservable();
|
|
_EventDisposable = observable.Subscribe(_ => SetCameraAgentManager());
|
|
}
|
|
}
|
|
|
|
private static void SetCameraAgentManager() {
|
|
CameraAgentManager.Clear();
|
|
|
|
foreach (var fovInfo in VisionRecipeManager.GetFovInfos()) {
|
|
if (!CameraAgentManager.ContainsKey(fovInfo)) {
|
|
var milCameraAgent = new MilCameraAgent(fovInfo);
|
|
CameraAgentManager.SetCameraAgent(fovInfo, milCameraAgent);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void RegisterInternal(IDeviceRegistry registry, VisionDef vsDef, Assembly callingAssembly) {
|
|
try {
|
|
foreach (var fovInfo in vsDef.GetFovInfos()) {
|
|
var camIdList = vsDef.GetCamIds(vsDef.GetFovRecipes(vsDef.GetFovInfos())).OrderBy(x => x);
|
|
foreach (var camId in camIdList) {
|
|
var boardType = (EquipmentInfoManager.EquipmentInfo.SimulationMode) ? eBoardType.Mock : GetBoardType(camId);
|
|
if (!VisionEnv.AuroraEnv.DeviceManager.IsRegisteredDevice(camId.ToString())) {
|
|
registry.Register(
|
|
camId.ToString(),
|
|
() => Create(boardType, camId.ToString(), callingAssembly),
|
|
CamConfig.GetDefaultConfig());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
private static CameraBase Create(eBoardType boardType, string deviceId, Assembly callingAssembly) {
|
|
eCamModel camModel = GetCamModel((eCamId)Enum.Parse(typeof(eCamId), deviceId));
|
|
|
|
if (boardType == eBoardType.Mock) {
|
|
return new MilCameraMock(deviceId);
|
|
}
|
|
else {
|
|
if (camModel == eCamModel.DEFAULT) {
|
|
if (EuresysEnvironment.IsEuresysBoardType(boardType)) {
|
|
return new EuresysCamera(deviceId);
|
|
}
|
|
else {
|
|
return new MilCamera(deviceId);
|
|
}
|
|
}
|
|
else {
|
|
CameraBase machineCamera = GetCameraFromMachine(camModel, deviceId, callingAssembly);
|
|
if (machineCamera != null) {
|
|
_Logger.FuncI($"{deviceId} is created from {camModel}.");
|
|
return machineCamera;
|
|
}
|
|
}
|
|
}
|
|
|
|
_Logger.FuncW($"{deviceId} is created from DEFAULT(MilCamera).");
|
|
return new MilCamera(deviceId); // Default
|
|
}
|
|
|
|
/// <summary>
|
|
/// 카메라 ID로 보드 타입 가져오기
|
|
/// </summary>
|
|
/// <param name="camId">카메라 ID</param>
|
|
/// <returns>보드 타입</returns>
|
|
public static eBoardType GetBoardType(eCamId camId) {
|
|
string configPath = Path.Combine(ApplicationExt.DataPath, "_config_device", camId + @".json");
|
|
return JsonManager.GetValue<eBoardType>(configPath, string.Format($"{typeof(CamConnConfig).Name}.BoardType"));
|
|
}
|
|
|
|
private static eCamModel GetCamModel(eCamId camId) {
|
|
string configPath = Path.Combine(ApplicationExt.DataPath, "_config_device", camId + @".json");
|
|
return JsonManager.GetValue<eCamModel>(configPath, "CamModel");
|
|
}
|
|
|
|
private static CameraBase GetCameraFromMachine(eCamModel camModel, string deviceId, Assembly callingAssembly) {
|
|
Type targetType = typeof(CameraBase);
|
|
string className = camModel.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 (CameraBase)ctor.Invoke(new object[] { deviceId });
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
} |