138 lines
4.9 KiB
C#
138 lines
4.9 KiB
C#
using Aurora;
|
|
using Aurora.Api;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace VisionCommon {
|
|
internal class RecipeUpdaterUtil {
|
|
private const string KEY_UNIT_RECIPES = nameof(VisionDef.UnitRecipes);
|
|
private const string KEY_SUB_SYS_RECIPES = nameof(UnitRecipe.SubSysRecipes);
|
|
private const string KEY_FOV_RECIPES = nameof(SubSysRecipe.FovRecipes);
|
|
private const string KEY_ALIGN_RECIPES = nameof(SubSysRecipe.AlignRecipes);
|
|
|
|
public static List<JToken> GetAlignRecipes(JToken machineVisionRecipe) {
|
|
var tokens = new List<JToken>();
|
|
|
|
foreach (JToken subSysRecipe in GetSubSysRecipes(machineVisionRecipe)) {
|
|
JToken alignRecipes = subSysRecipe.SelectToken(KEY_ALIGN_RECIPES);
|
|
if (alignRecipes != null) {
|
|
foreach (JToken alignRecipe in alignRecipes) {
|
|
tokens.Add(alignRecipe);
|
|
}
|
|
}
|
|
}
|
|
|
|
return tokens;
|
|
}
|
|
|
|
public static List<JToken> GetFovRecipes(JToken machineVisionRecipe) {
|
|
var tokens = new List<JToken>();
|
|
|
|
foreach (JToken subSysRecipe in GetSubSysRecipes(machineVisionRecipe)) {
|
|
JToken fovRecipes = subSysRecipe.SelectToken(KEY_FOV_RECIPES);
|
|
if (fovRecipes != null) {
|
|
foreach (JToken fovRecipe in fovRecipes) {
|
|
tokens.Add(fovRecipe);
|
|
}
|
|
}
|
|
}
|
|
|
|
return tokens;
|
|
}
|
|
|
|
public static List<JToken> GetSubSysRecipes(JToken machineVisionRecipe) {
|
|
var tokens = new List<JToken>();
|
|
|
|
foreach (JToken unitRecipe in GetUnitRecipes(machineVisionRecipe)) {
|
|
JToken subSysRecipes = unitRecipe.SelectToken(KEY_SUB_SYS_RECIPES);
|
|
if (subSysRecipes != null) {
|
|
foreach (JToken subSysRecipe in subSysRecipes) {
|
|
tokens.Add(subSysRecipe);
|
|
}
|
|
}
|
|
}
|
|
|
|
return tokens;
|
|
}
|
|
|
|
public static List<JToken> GetUnitRecipes(JToken machineVisionRecipe) {
|
|
var tokens = new List<JToken>();
|
|
JToken unitRecipes = machineVisionRecipe.SelectToken(KEY_UNIT_RECIPES);
|
|
if (unitRecipes != null) {
|
|
foreach (JToken unitRecipe in unitRecipes) {
|
|
tokens.Add(unitRecipe);
|
|
}
|
|
}
|
|
|
|
return tokens;
|
|
}
|
|
|
|
public static bool KeyEquals(JToken jtoken, string key) {
|
|
if (jtoken.Path.EndsWith($".{key}")) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static List<string> GetDataDirectories(eRecipeModelType recipeModelType) {
|
|
switch(recipeModelType) {
|
|
case eRecipeModelType.AuroraRecipe:
|
|
return GetRecipeDirectories();
|
|
case eRecipeModelType.AuroraModel:
|
|
return GetModelDirectories();
|
|
default:
|
|
return new List<string>();
|
|
}
|
|
}
|
|
|
|
private static List<string> GetRecipeDirectories() {
|
|
var recipeDirectories = new List<string>();
|
|
var recipeDirPath = Path.Combine(
|
|
ApplicationExt.DataPath,
|
|
"_config_recipe");
|
|
var recipesFilePath = Path.Combine(
|
|
recipeDirPath,
|
|
"recipes.json");
|
|
|
|
var recipesRootToken = JsonManager.ParseFromFile(recipesFilePath);
|
|
if (recipesRootToken != null) {
|
|
var recipesToken = recipesRootToken.SelectToken("Recipes");
|
|
if (recipesToken != null) {
|
|
foreach (JToken recipeInfoToken in recipesToken) {
|
|
var recipeInfo = recipeInfoToken.ToObject<RecipeInfo>();
|
|
if (recipeInfo != null) {
|
|
recipeDirectories.Add(Path.Combine(
|
|
recipeDirPath,
|
|
recipeInfo.Name));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return recipeDirectories;
|
|
}
|
|
|
|
private static List<string> GetModelDirectories() {
|
|
var modelDirectories = new List<string>();
|
|
var modelDirPath = Path.Combine(
|
|
ApplicationExt.DataPath,
|
|
"_config_model");
|
|
|
|
var modelDirCandidates = Directory.GetDirectories(modelDirPath);
|
|
foreach (var modelDir in modelDirCandidates) {
|
|
var recipeFilePath = Path.Combine(modelDir, $"{VisionKeys.VisionRecipeKey}.json");
|
|
if (File.Exists(recipeFilePath)) {
|
|
modelDirectories.Add(modelDir);
|
|
}
|
|
}
|
|
|
|
return modelDirectories;
|
|
}
|
|
}
|
|
}
|