Fixes UpdateDeviceConfig()

This commit is contained in:
Jason T Alborough
2021-05-11 17:52:26 -04:00
parent 0ded3e30f9
commit da179c01f5

View File

@@ -1,162 +1,162 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Crestron.SimplSharp; using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronIO; using Crestron.SimplSharp.CrestronIO;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using PepperDash.Core; using PepperDash.Core;
namespace PepperDash.Essentials.Core.Config namespace PepperDash.Essentials.Core.Config
{ {
/// <summary> /// <summary>
/// Responsible for updating config at runtime, and writing the updates out to a local file /// Responsible for updating config at runtime, and writing the updates out to a local file
/// </summary> /// </summary>
public class ConfigWriter public class ConfigWriter
{ {
public const string LocalConfigFolder = "LocalConfig"; public const string LocalConfigFolder = "LocalConfig";
public const long WriteTimeout = 30000; public const long WriteTimeout = 30000;
public static CTimer WriteTimer; public static CTimer WriteTimer;
static CCriticalSection fileLock = new CCriticalSection(); static CCriticalSection fileLock = new CCriticalSection();
/// <summary> /// <summary>
/// Updates the config properties of a device /// Updates the config properties of a device
/// </summary> /// </summary>
/// <param name="deviceKey"></param> /// <param name="deviceKey"></param>
/// <param name="properties"></param> /// <param name="properties"></param>
/// <returns></returns> /// <returns></returns>
public static bool UpdateDeviceProperties(string deviceKey, JToken properties) public static bool UpdateDeviceProperties(string deviceKey, JToken properties)
{ {
bool success = false; bool success = false;
// Get the current device config // Get the current device config
var deviceConfig = ConfigReader.ConfigObject.Devices.FirstOrDefault(d => d.Key.Equals(deviceKey)); var deviceConfig = ConfigReader.ConfigObject.Devices.FirstOrDefault(d => d.Key.Equals(deviceKey));
if (deviceConfig != null) if (deviceConfig != null)
{ {
// Replace the current properties JToken with the new one passed into this method // Replace the current properties JToken with the new one passed into this method
deviceConfig.Properties = properties; deviceConfig.Properties = properties;
Debug.Console(1, "Updated properties of device: '{0}'", deviceKey); Debug.Console(1, "Updated properties of device: '{0}'", deviceKey);
success = true; success = true;
} }
ResetTimer(); ResetTimer();
return success; return success;
} }
public static bool UpdateDeviceConfig(DeviceConfig config) public static bool UpdateDeviceConfig(DeviceConfig config)
{ {
bool success = false; bool success = false;
var deviceConfig = ConfigReader.ConfigObject.Devices.FirstOrDefault(d => d.Key.Equals(config.Key)); var deviceConfig = ConfigReader.ConfigObject.Devices.FindIndex(d => d.Key.Equals(config.Key));
if (deviceConfig != null) if (deviceConfig >= 0)
{ {
deviceConfig = config; ConfigReader.ConfigObject.Devices[deviceConfig] = config;
Debug.Console(1, "Updated config of device: '{0}'", config.Key); Debug.Console(1, "Updated config of device: '{0}'", config.Key);
success = true; success = true;
} }
ResetTimer(); ResetTimer();
return success; return success;
} }
public static bool UpdateRoomConfig(DeviceConfig config) public static bool UpdateRoomConfig(DeviceConfig config)
{ {
bool success = false; bool success = false;
var deviceConfig = ConfigReader.ConfigObject.Rooms.FirstOrDefault(d => d.Key.Equals(config.Key)); var deviceConfig = ConfigReader.ConfigObject.Rooms.FirstOrDefault(d => d.Key.Equals(config.Key));
if (deviceConfig != null) if (deviceConfig != null)
{ {
deviceConfig = config; deviceConfig = config;
Debug.Console(1, "Updated config of device: '{0}'", config.Key); Debug.Console(1, "Updated config of device: '{0}'", config.Key);
success = true; success = true;
} }
ResetTimer(); ResetTimer();
return success; return success;
} }
/// <summary> /// <summary>
/// Resets (or starts) the write timer /// Resets (or starts) the write timer
/// </summary> /// </summary>
static void ResetTimer() static void ResetTimer()
{ {
if (WriteTimer == null) if (WriteTimer == null)
WriteTimer = new CTimer(WriteConfigFile, WriteTimeout); WriteTimer = new CTimer(WriteConfigFile, WriteTimeout);
WriteTimer.Reset(WriteTimeout); WriteTimer.Reset(WriteTimeout);
Debug.Console(1, "Config File write timer has been reset."); Debug.Console(1, "Config File write timer has been reset.");
} }
/// <summary> /// <summary>
/// Writes the current config to a file in the LocalConfig subfolder /// Writes the current config to a file in the LocalConfig subfolder
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private static void WriteConfigFile(object o) private static void WriteConfigFile(object o)
{ {
var filePath = Global.FilePathPrefix + LocalConfigFolder + Global.DirectorySeparator + "configurationFile.json"; var filePath = Global.FilePathPrefix + LocalConfigFolder + Global.DirectorySeparator + "configurationFile.json";
var configData = JsonConvert.SerializeObject(ConfigReader.ConfigObject); var configData = JsonConvert.SerializeObject(ConfigReader.ConfigObject);
WriteFile(filePath, configData); WriteFile(filePath, configData);
} }
/// <summary> /// <summary>
/// Writes /// Writes
/// </summary> /// </summary>
/// <param name="filepath"></param> /// <param name="filepath"></param>
/// <param name="o"></param> /// <param name="o"></param>
public static void WriteFile(string filePath, string configData) public static void WriteFile(string filePath, string configData)
{ {
if (WriteTimer != null) if (WriteTimer != null)
WriteTimer.Stop(); WriteTimer.Stop();
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Writing Configuration to file"); Debug.Console(0, Debug.ErrorLogLevel.Notice, "Writing Configuration to file");
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to write config file: '{0}'", filePath); Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to write config file: '{0}'", filePath);
try try
{ {
if (fileLock.TryEnter()) if (fileLock.TryEnter())
{ {
using (StreamWriter sw = new StreamWriter(filePath)) using (StreamWriter sw = new StreamWriter(filePath))
{ {
sw.Write(configData); sw.Write(configData);
sw.Flush(); sw.Flush();
} }
} }
else else
{ {
Debug.Console(0, Debug.ErrorLogLevel.Error, "Unable to enter FileLock"); Debug.Console(0, Debug.ErrorLogLevel.Error, "Unable to enter FileLock");
} }
} }
catch (Exception e) catch (Exception e)
{ {
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: Config write failed: \r{0}", e); Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: Config write failed: \r{0}", e);
} }
finally finally
{ {
if (fileLock != null && !fileLock.Disposed) if (fileLock != null && !fileLock.Disposed)
fileLock.Leave(); fileLock.Leave();
} }
} }
} }
} }