mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-01-11 19:44:52 +00:00
Merge pull request #1344 from PepperDash/feature/allow-config-tool-v2-structure
Feature/allow config tool v2 structure
This commit is contained in:
@@ -9,11 +9,30 @@ using Serilog.Events;
|
||||
|
||||
namespace PepperDash.Core.Config
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Reads a Portal formatted config file
|
||||
/// </summary>
|
||||
public class PortalConfigReader
|
||||
{
|
||||
const string template = "template";
|
||||
const string system = "system";
|
||||
const string systemUrl = "system_url";
|
||||
const string templateUrl = "template_url";
|
||||
const string info = "info";
|
||||
const string devices = "devices";
|
||||
const string rooms = "rooms";
|
||||
const string sourceLists = "sourceLists";
|
||||
const string destinationLists = "destinationLists";
|
||||
const string cameraLists = "cameraLists";
|
||||
const string audioControlPointLists = "audioControlPointLists";
|
||||
|
||||
const string tieLines = "tieLines";
|
||||
const string joinMaps = "joinMaps";
|
||||
const string global = "global";
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Reads the config file, checks if it needs a merge, merges and saves, then returns the merged Object.
|
||||
/// </summary>
|
||||
@@ -24,25 +43,25 @@ namespace PepperDash.Core.Config
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
Debug.Console(1, Debug.ErrorLogLevel.Error,
|
||||
Debug.LogError(
|
||||
"ERROR: Configuration file not present. Please load file to {0} and reset program", filePath);
|
||||
}
|
||||
|
||||
using (StreamReader fs = new StreamReader(filePath))
|
||||
{
|
||||
var jsonObj = JObject.Parse(fs.ReadToEnd());
|
||||
if(jsonObj["template"] != null && jsonObj["system"] != null)
|
||||
if(jsonObj[template] != null && jsonObj[system] != null)
|
||||
{
|
||||
// it's a double-config, merge it.
|
||||
var merged = MergeConfigs(jsonObj);
|
||||
if (jsonObj["system_url"] != null)
|
||||
if (jsonObj[systemUrl] != null)
|
||||
{
|
||||
merged["systemUrl"] = jsonObj["system_url"].Value<string>();
|
||||
merged[systemUrl] = jsonObj[systemUrl].Value<string>();
|
||||
}
|
||||
|
||||
if (jsonObj["template_url"] != null)
|
||||
if (jsonObj[templateUrl] != null)
|
||||
{
|
||||
merged["templateUrl"] = jsonObj["template_url"].Value<string>();
|
||||
merged[templateUrl] = jsonObj[templateUrl].Value<string>();
|
||||
}
|
||||
|
||||
jsonObj = merged;
|
||||
@@ -77,62 +96,62 @@ namespace PepperDash.Core.Config
|
||||
var merged = new JObject();
|
||||
|
||||
// Put together top-level objects
|
||||
if (system["info"] != null)
|
||||
merged.Add("info", Merge(template["info"], system["info"], "infO"));
|
||||
if (system[info] != null)
|
||||
merged.Add(info, Merge(template[info], system[info], info));
|
||||
else
|
||||
merged.Add("info", template["info"]);
|
||||
merged.Add(info, template[info]);
|
||||
|
||||
merged.Add("devices", MergeArraysOnTopLevelProperty(template["devices"] as JArray,
|
||||
system["devices"] as JArray, "key", "devices"));
|
||||
merged.Add(devices, MergeArraysOnTopLevelProperty(template[devices] as JArray,
|
||||
system[devices] as JArray, "key", devices));
|
||||
|
||||
if (system["rooms"] == null)
|
||||
merged.Add("rooms", template["rooms"]);
|
||||
if (system[rooms] == null)
|
||||
merged.Add(rooms, template[rooms]);
|
||||
else
|
||||
merged.Add("rooms", MergeArraysOnTopLevelProperty(template["rooms"] as JArray,
|
||||
system["rooms"] as JArray, "key", "rooms"));
|
||||
merged.Add(rooms, MergeArraysOnTopLevelProperty(template[rooms] as JArray,
|
||||
system[rooms] as JArray, "key", rooms));
|
||||
|
||||
if (system["sourceLists"] == null)
|
||||
merged.Add("sourceLists", template["sourceLists"]);
|
||||
if (system[sourceLists] == null)
|
||||
merged.Add(sourceLists, template[sourceLists]);
|
||||
else
|
||||
merged.Add("sourceLists", Merge(template["sourceLists"], system["sourceLists"], "sourceLists"));
|
||||
merged.Add(sourceLists, Merge(template[sourceLists], system[sourceLists], sourceLists));
|
||||
|
||||
if (system["destinationLists"] == null)
|
||||
merged.Add("destinationLists", template["destinationLists"]);
|
||||
if (system[destinationLists] == null)
|
||||
merged.Add(destinationLists, template[destinationLists]);
|
||||
else
|
||||
merged.Add("destinationLists",
|
||||
Merge(template["destinationLists"], system["destinationLists"], "destinationLists"));
|
||||
merged.Add(destinationLists,
|
||||
Merge(template[destinationLists], system[destinationLists], destinationLists));
|
||||
|
||||
|
||||
if (system["cameraLists"] == null)
|
||||
merged.Add("cameraLists", template["cameraLists"]);
|
||||
if (system[cameraLists] == null)
|
||||
merged.Add(cameraLists, template[cameraLists]);
|
||||
else
|
||||
merged.Add("cameraLists", Merge(template["cameraLists"], system["cameraLists"], "cameraLists"));
|
||||
merged.Add(cameraLists, Merge(template[cameraLists], system[cameraLists], cameraLists));
|
||||
|
||||
if (system["audioControlPointLists"] == null)
|
||||
merged.Add("audioControlPointLists", template["audioControlPointLists"]);
|
||||
if (system[audioControlPointLists] == null)
|
||||
merged.Add(audioControlPointLists, template[audioControlPointLists]);
|
||||
else
|
||||
merged.Add("audioControlPointLists",
|
||||
Merge(template["audioControlPointLists"], system["audioControlPointLists"], "audioControlPointLists"));
|
||||
merged.Add(audioControlPointLists,
|
||||
Merge(template[audioControlPointLists], system[audioControlPointLists], audioControlPointLists));
|
||||
|
||||
|
||||
// Template tie lines take precedence. Config tool doesn't do them at system
|
||||
// level anyway...
|
||||
if (template["tieLines"] != null)
|
||||
merged.Add("tieLines", template["tieLines"]);
|
||||
else if (system["tieLines"] != null)
|
||||
merged.Add("tieLines", system["tieLines"]);
|
||||
if (template[tieLines] != null)
|
||||
merged.Add(tieLines, template[tieLines]);
|
||||
else if (system[tieLines] != null)
|
||||
merged.Add(tieLines, system[tieLines]);
|
||||
else
|
||||
merged.Add("tieLines", new JArray());
|
||||
merged.Add(tieLines, new JArray());
|
||||
|
||||
if (template["joinMaps"] != null)
|
||||
merged.Add("joinMaps", template["joinMaps"]);
|
||||
if (template[joinMaps] != null)
|
||||
merged.Add(joinMaps, template[joinMaps]);
|
||||
else
|
||||
merged.Add("joinMaps", new JObject());
|
||||
merged.Add(joinMaps, new JObject());
|
||||
|
||||
if (system["global"] != null)
|
||||
merged.Add("global", Merge(template["global"], system["global"], "global"));
|
||||
if (system[global] != null)
|
||||
merged.Add(global, Merge(template[global], system[global], global));
|
||||
else
|
||||
merged.Add("global", template["global"]);
|
||||
merged.Add(global, template[global]);
|
||||
|
||||
//Debug.Console(2, "MERGED CONFIG RESULT: \x0d\x0a{0}", merged);
|
||||
return merged;
|
||||
@@ -228,7 +247,7 @@ namespace PepperDash.Core.Config
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.Console(1, Debug.ErrorLogLevel.Warning, "Cannot merge items at path {0}: \r{1}", propPath, e);
|
||||
Debug.LogError($"Cannot merge items at path {propPath}: \r{e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,19 +127,32 @@ namespace PepperDash.Essentials.Core.Config
|
||||
}
|
||||
else
|
||||
{
|
||||
var doubleObj = JObject.Parse(fs.ReadToEnd());
|
||||
ConfigObject = PortalConfigReader.MergeConfigs(doubleObj).ToObject<EssentialsConfig>();
|
||||
var parsedConfig = JObject.Parse(fs.ReadToEnd());
|
||||
|
||||
// Extract SystemUrl and TemplateUrl into final config output
|
||||
// Check if it's a v2 config (check for "version" node)
|
||||
// this means it's already merged by the Portal API
|
||||
// from the v2 config tool
|
||||
var isV2Config = parsedConfig["versions"] != null;
|
||||
|
||||
if (doubleObj["system_url"] != null)
|
||||
if (isV2Config)
|
||||
{
|
||||
ConfigObject.SystemUrl = doubleObj["system_url"].Value<string>();
|
||||
Debug.LogMessage(LogEventLevel.Information, "Config file is a v2 format, no merge necessary.");
|
||||
ConfigObject = parsedConfig.ToObject<EssentialsConfig>();
|
||||
Debug.LogMessage(LogEventLevel.Information, "Successfully Loaded v2 Config");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (doubleObj["template_url"] != null)
|
||||
// Extract SystemUrl and TemplateUrl into final config output
|
||||
ConfigObject = PortalConfigReader.MergeConfigs(parsedConfig).ToObject<EssentialsConfig>();
|
||||
|
||||
if (parsedConfig["system_url"] != null)
|
||||
{
|
||||
ConfigObject.TemplateUrl = doubleObj["template_url"].Value<string>();
|
||||
ConfigObject.SystemUrl = parsedConfig["system_url"].Value<string>();
|
||||
}
|
||||
|
||||
if (parsedConfig["template_url"] != null)
|
||||
{
|
||||
ConfigObject.TemplateUrl = parsedConfig["template_url"].Value<string>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,13 +16,21 @@ namespace PepperDash.Essentials.Core.Config
|
||||
/// </summary>
|
||||
public class EssentialsConfig : BasicConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the SystemUrl
|
||||
/// </summary>
|
||||
[JsonProperty("system_url")]
|
||||
public string SystemUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the TemplateUrl
|
||||
/// </summary>
|
||||
[JsonProperty("template_url")]
|
||||
public string TemplateUrl { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the SystemUuid extracted from the SystemUrl
|
||||
/// </summary>
|
||||
[JsonProperty("systemUuid")]
|
||||
public string SystemUuid
|
||||
{
|
||||
@@ -45,6 +53,9 @@ namespace PepperDash.Essentials.Core.Config
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the TemplateUuid extracted from the TemplateUrl
|
||||
/// </summary>
|
||||
[JsonProperty("templateUuid")]
|
||||
public string TemplateUuid
|
||||
{
|
||||
@@ -67,13 +78,20 @@ namespace PepperDash.Essentials.Core.Config
|
||||
}
|
||||
}
|
||||
|
||||
[JsonProperty("rooms")]
|
||||
/// <summary>
|
||||
/// Gets or sets the Rooms
|
||||
/// </summary>
|
||||
[JsonProperty("rooms")]
|
||||
public List<DeviceConfig> Rooms { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Versions
|
||||
/// </summary>
|
||||
public VersionData Versions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="EssentialsConfig"/> class.
|
||||
/// </summary>
|
||||
public EssentialsConfig()
|
||||
: base()
|
||||
{
|
||||
@@ -81,6 +99,50 @@ namespace PepperDash.Essentials.Core.Config
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents version data for Essentials and its packages
|
||||
/// </summary>
|
||||
public class VersionData
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the Essentials version
|
||||
/// </summary>
|
||||
[JsonProperty("essentials")]
|
||||
public NugetVersion Essentials { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the list of Packages
|
||||
/// </summary>
|
||||
[JsonProperty("packages")]
|
||||
public List<NugetVersion> Packages { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="VersionData"/> class.
|
||||
/// </summary>
|
||||
public VersionData()
|
||||
{
|
||||
Packages = new List<NugetVersion>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a NugetVersion
|
||||
/// </summary>
|
||||
public class NugetVersion
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the Version
|
||||
/// </summary>
|
||||
[JsonProperty("version")]
|
||||
public string Version { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the PackageId
|
||||
/// </summary>
|
||||
[JsonProperty("packageId")]
|
||||
public string PackageId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a SystemTemplateConfigs
|
||||
/// </summary>
|
||||
@@ -91,6 +153,9 @@ namespace PepperDash.Essentials.Core.Config
|
||||
/// </summary>
|
||||
public EssentialsConfig System { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Template
|
||||
/// </summary>
|
||||
public EssentialsConfig Template { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user