Files
GenericTeProgramLibrary/Source/Program/DataDef/PowerSupplySharedData.cs

95 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProgramLib
{
/// <summary>
/// Stores voltage and current reading for each power module
/// </summary>
internal class PowerSupplySharedData
{
private object syncObj = new object();
private Dictionary<string,object> syncObjDict = new Dictionary<string, object>();
private Dictionary<string, PowerSupplyData> _powerSupplyDataDict = new Dictionary<string, PowerSupplyData>();
/// <summary>
/// Set data for a power supply module
/// </summary>
public void SetData(string moduleName, double voltage, double current, Raytheon.Instruments.PowerSupplies.PowerSupplyModuleInfo powerSupplyModuleInfo)
{
lock (syncObj)
{
if (!syncObjDict.ContainsKey(moduleName))
{
// create a mutex for each module
syncObjDict[moduleName] = new object();
}
}
lock (syncObjDict[moduleName])
{
if (!_powerSupplyDataDict.ContainsKey(moduleName))
{
_powerSupplyDataDict[moduleName] = new PowerSupplyData();
}
_powerSupplyDataDict[moduleName]._voltage = voltage;
_powerSupplyDataDict[moduleName]._current = current;
_powerSupplyDataDict[moduleName]._initialized = true;
_powerSupplyDataDict[moduleName]._powerSupplyModuleInfo = powerSupplyModuleInfo;
}
}
/// <summary>
/// Get data for a power supply module
/// </summary>
public PowerSupplyData GetData(string moduleName)
{
lock (syncObj)
{
if (!syncObjDict.ContainsKey(moduleName))
{
// create a mutex for each module
syncObjDict[moduleName] = new object();
}
}
lock (syncObjDict[moduleName])
{
if (!_powerSupplyDataDict.ContainsKey(moduleName))
{
throw new Exception($"{moduleName} is invalid");
}
return _powerSupplyDataDict[moduleName];
}
}
/// <summary>
/// Set each power supply data to uninialized
/// </summary>
public void ResetAll()
{
foreach (KeyValuePair<string, PowerSupplyData> entry in _powerSupplyDataDict)
{
lock (syncObj)
{
if (!syncObjDict.ContainsKey(entry.Key))
{
// create a mutex for each module
syncObjDict[entry.Key] = new object();
}
}
lock (syncObjDict[entry.Key])
{
entry.Value.Reset();
}
}
}
}
}