125 lines
3.8 KiB
C#
Raw Normal View History

2025-04-27 09:16:37 +08:00
using System.Text.RegularExpressions;
using JiShe.CollectBus.Common.Extensions;
2025-04-24 19:31:28 +08:00
2025-04-27 09:16:37 +08:00
namespace JiShe.CollectBus.Protocol.T37612012
2025-04-24 19:31:28 +08:00
{
public static class Protocol3761Extensions
{
/// <summary>
/// 3761协议数据字节校验
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool Check3761Byte(this string value)
{
if (!value.ToUpper().Equals("FF") && !value.ToUpper().Equals("EE"))
return true;
return false;
}
/// <summary>
/// 字符串中是否包含字母
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsLetterExists(this string value)
{
return Regex.Matches(value, "[a-zA-Z]").Count > 0;
}
/// <summary>
/// 判断错误代码
/// </summary>
public static Tuple<string,string>? CheckErrorCode(this List<string> data)
{
var value = string.Join("", data);
if (value.IsLetterExists())
{
var code = ErrorCodes().Where(f => value.Contains(f.Key)).FirstOrDefault();
if (code.Key != null)
return Tuple.Create(code.Key,code.Value);
else
return Tuple.Create("EE", "未知数据错误");
}
return null;
}
/// <summary>
/// 判断错误代码
/// </summary>
public static Tuple<string, string>? CheckErrorCode(this string value)
{
if (value.IsLetterExists())
{
var code = ErrorCodes().Where(f => value.Contains(f.Key)).FirstOrDefault();
if (code.Key != null)
return Tuple.Create(code.Key, code.Value);
else
return Tuple.Create("EE", "未知数据错误");
}
return null;
}
/// <summary>
/// 错误信息
/// </summary>
/// <returns></returns>
public static Dictionary<string, string> ErrorCodes()
{
return new Dictionary<string, string>()
{
{ "FF", "电表无此数据项" },
{ "EE", "未知数据错误" },
{ "E1", "数据点缺少(停电)" },
{ "E2", "通讯异常" },
{ "E3", "集中器未配置数据项" },
{ "E4", "电表档案无效" },
{ "E5", "电表无此数据项" },
{ "E6", "电表时间异常" },
{ "E7","暂停抄表" }
};
}
/// <summary>
/// 费率数
/// </summary>
/// <param name="index"></param>
/// <param name="len"></param>
/// <returns></returns>
public static int GetRatingCount(this List<string> hexMessageList ,int index, int len)
{
var list = hexMessageList.GetRange(index, len);
return list.Count > 0 ? list[0].HexToDec() : 0;
}
/// <summary>
/// 抄表时间
/// </summary>
/// <param name="index"></param>
/// <param name="len"></param>
/// <returns></returns>
public static string GetReadTime(this List<string> hexMessageList, int index, int len)
{
var list = hexMessageList.GetRange(index, len);
return list.GetReadTime();
}
/// <summary>
/// 抄表时间
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string GetReadTime(this List<string> data)
{
data.Reverse();
data.Insert(0, DateTime.Now.ToString("yyyy").Substring(0, 2));
return string.Join("", data);
}
}
}