diff --git a/Pepperdash Core/Pepperdash Core/PasswordManager/Config.cs b/Pepperdash Core/Pepperdash Core/PasswordManager/Config.cs new file mode 100644 index 0000000..c783113 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/PasswordManager/Config.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.PasswordManager +{ + // Example JSON password configuration object + //{ + // "global":{ + // "passwords":[ + // { + // "key": "Password01", + // "name": "Technician Password", + // "enable": true, + // "password": "1988" + // } + // ] + // } + //} + + /// + /// Passwrod manager JSON configuration + /// + public class PasswordConfig + { + /// + /// Key used to search for object in JSON array + /// + public string key { get; set; } + /// + /// Friendly name of password object + /// + public string name { get; set; } + /// + /// Password object enabled + /// + public bool enable { get; set; } + /// + /// Password object configured password + /// + public string password { get; set; } + + /// + /// Constructor + /// + public PasswordConfig() + { + + } + } + + /// + /// Global JSON object + /// + public class GlobalConfig + { + public List passwords { get; set; } + + /// + /// Constructor + /// + public GlobalConfig() + { + + } + } + + /// + /// Root JSON object + /// + public class RootObject + { + public GlobalConfig global { get; set; } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/PasswordManager/Constants.cs b/Pepperdash Core/Pepperdash Core/PasswordManager/Constants.cs new file mode 100644 index 0000000..a85349e --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/PasswordManager/Constants.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash.Core.PasswordManager +{ + /// + /// Constants + /// + public class PasswordManagerConstants + { + /// + /// Generic boolean value change constant + /// + public const ushort BoolValueChange = 1; + /// + /// Evaluated boolean change constant + /// + public const ushort BoolEvaluatedChange = 2; + + /// + /// Generic ushort value change constant + /// + public const ushort UshrtValueChange = 101; + + /// + /// Generic string value change constant + /// + public const ushort StringValueChange = 201; + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/PasswordManager/PasswordManager.cs b/Pepperdash Core/Pepperdash Core/PasswordManager/PasswordManager.cs new file mode 100644 index 0000000..e31ba00 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/PasswordManager/PasswordManager.cs @@ -0,0 +1,167 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Crestron.SimplSharp; +using PepperDash.Core.JsonToSimpl; + +namespace PepperDash.Core.PasswordManager +{ + public class PasswordManager : IKeyed + { + public string Key { get; set; } + + public List Passwords { get; set; } + + /// + /// Boolean event + /// + public event EventHandler BoolChange; + /// + /// Ushort event + /// + public event EventHandler UshrtChange; + /// + /// String event + /// + public event EventHandler StringChange; + + /// + /// Constructor + /// + public PasswordManager() + { + + } + + /// + /// Initialize method + /// + /// + /// + public void Initialize(string uniqueId, string key) + { + OnBoolChange(false, 0, PasswordManagerConstants.BoolEvaluatedChange); + + try + { + if(string.IsNullOrEmpty(uniqueId) || string.IsNullOrEmpty(key)) + { + Debug.Console(1, "PasswordManager.Initialize({0}, {1}) null or empty parameters", uniqueId, key); + return; + } + + Key = key; + + JsonToSimplMaster master = J2SGlobal.GetMasterByFile(uniqueId); + if(master == null) + { + Debug.Console(1, "PassowrdManager.Initialize failed:\rCould not find JSON file with uniqueID {0}", uniqueId); + return; + } + + var passwords = master.JsonObject.ToObject().global.passwords; + if(passwords == null) + { + Debug.Console(1, "PasswordManager.Initialize failed:\rCould not find password object"); + return; + } + + foreach(var password in passwords) + { + AddPassword(password); + } + } + catch(Exception e) + { + var msg = string.Format("PasswordManager.Initialize({0}, {1}) failed:\r{2}", uniqueId, Key, e.Message); + CrestronConsole.PrintLine(msg); + ErrorLog.Error(msg); + } + finally + { + OnBoolChange(true, 0, PasswordManagerConstants.BoolEvaluatedChange); + } + } + + /// + /// Adds password to the list + /// + /// + private void AddPassword(PasswordConfig password) + { + if (password == null) + return; + + RemovePassword(password); + Passwords.Add(password); + } + + /// + /// Removes password from the list + /// + /// + private void RemovePassword(PasswordConfig password) + { + if (password == null) + return; + + var item = Passwords.FirstOrDefault(p => p.key.Equals(password.key)); + if (item != null) + Passwords.Remove(item); + } + + /// + /// Protected boolean change event handler + /// + /// + /// + /// + protected void OnBoolChange(bool state, ushort index, ushort type) + { + var handler = BoolChange; + if (handler != null) + { + var args = new BoolChangeEventArgs(state, type); + args.Index = index; + BoolChange(this, args); + } + } + + /// + /// Protected ushort change event handler + /// + /// + /// + /// + protected void OnUshrtChange(ushort value, ushort index, ushort type) + { + var handler = UshrtChange; + if (handler != null) + { + var args = new UshrtChangeEventArgs(value, type); + args.Index = index; + UshrtChange(this, args); + } + } + + /// + /// Protected string change event handler + /// + /// + /// + /// + protected void OnStringChange(string value, ushort index, ushort type) + { + var handler = StringChange; + if (handler != null) + { + var args = new StringChangeEventArgs(value, type); + args.Index = index; + StringChange(this, args); + } + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj index e0fef86..4e6bc43 100644 --- a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj +++ b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj @@ -110,6 +110,9 @@ + + +