DeviceManager/TsSFCDevice.Client.Launch/FormatProviderExtend/StatusFormatter.cs
2024-08-02 10:52:45 +08:00

107 lines
2.7 KiB
C#

using System;
namespace TsSFCDevice.Client.Launch.FormatProviderExtend
{
public class StatusFormatter : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
{
return this;
}
return null;
}
public string Format(string format, object arg, IFormatProvider formatProvider)
{
string value = string.Empty;
switch (format.ToLower())
{
case "tag":
value = tagFormat(arg);
break;
case "status":
value = StatusFormat(arg);
break;
case "Enable":
value = EnableFormat(arg);
break;
case "device":
value = DeviceStatusFormat(arg);
break;
default:
break;
}
return value;
}
/// <summary>
/// 设备状态
/// </summary>
/// <param name="arg"></param>
/// <returns></returns>
string DeviceStatusFormat(object arg)
{
string value = string.Empty;
int status = 0;
if (arg != null && int.TryParse(arg + "", out status))
{
value = status == 0 ? "禁用" : "启用";
}
return value;
}
/// <summary>
/// tag状态
/// </summary>
/// <param name="arg"></param>
/// <returns></returns>
string tagFormat(object arg)
{
string value = string.Empty;
switch (arg?.ToString() ?? "")
{
case "N":
value = "已提交";
break;
case "C":
value = "已完成";
break;
default:
break;
}
return value;
}
string EnableFormat(object arg)
{
bool Enable = false;
bool.TryParse(arg + "", out Enable);
return Enable ? "启用" : "禁用";
}
string StatusFormat(object arg)
{
string value = string.Empty;
switch (arg?.ToString() ?? "")
{
case "A":
value = "启用";
break;
case "D":
value = "停用";
break;
default:
break;
}
return value;
}
}
}