using Hermes; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace VisionCommon { internal class RecipeUpdater_01_02 : Updater { private const string KEY_LOW_VERSION = nameof(UnitRecipe.UnitName); private const string KEY_HIGH_VERSION = "UnitName_02"; public RecipeUpdater_01_02() : base(nameof(RecipeUpdater_01_02), 0.1, 0.2) { } protected override int Upgrade(JToken machineToken) { _logger.FuncI(); string KEY_UPDATE_ITEM = KEY_LOW_VERSION; var removeTokenList = new List(); try { foreach (JToken recipe in RecipeUpdaterUtil.GetUnitRecipes(machineToken)) { foreach (JToken item in recipe) { if (RecipeUpdaterUtil.KeyEquals(item, KEY_UPDATE_ITEM)) { removeTokenList.Add(item); } } } foreach (var token in removeTokenList) { JToken oldItem = token.Parent.SelectToken(KEY_UPDATE_ITEM); if (oldItem != null) { string oldValue = oldItem.Value(); token.AddBeforeSelf(new JProperty(KEY_HIGH_VERSION, JToken.FromObject(KEY_HIGH_VERSION))); token.Remove(); } } } catch (Exception e) { _logger.E(e.Message); return E1.GENERIC; } return E1.OK; } protected override int Downgrade(JToken machineToken) { _logger.FuncI(); string KEY_UPDATE_ITEM = KEY_HIGH_VERSION; var removeTokenList = new List(); try { foreach (JToken recipe in RecipeUpdaterUtil.GetUnitRecipes(machineToken)) { foreach (JToken item in recipe) { if (RecipeUpdaterUtil.KeyEquals(item, KEY_UPDATE_ITEM)) { removeTokenList.Add(item); } } } foreach (var token in removeTokenList) { JToken oldItem = token.Parent.SelectToken(KEY_UPDATE_ITEM); if (oldItem != null) { string oldValue = oldItem.Value(); token.AddBeforeSelf(new JProperty(KEY_LOW_VERSION, JToken.FromObject(KEY_LOW_VERSION))); token.Remove(); } } } catch (Exception e) { _logger.E(e.Message); return E1.GENERIC; } return E1.OK; } } }