using JiShe.CollectBus.Common.Extensions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace JiShe.CollectBus.Protocol { public static class Protocol3761Extensions { /// /// 3761协议数据字节校验 /// /// /// public static bool Check3761Byte(this string value) { if (!value.ToUpper().Equals("FF") && !value.ToUpper().Equals("EE")) return true; return false; } /// /// 字符串中是否包含字母 /// /// /// public static bool IsLetterExists(this string value) { return Regex.Matches(value, "[a-zA-Z]").Count > 0; } /// /// 判断错误代码 /// public static Tuple? CheckErrorCode(this List 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; } /// /// 判断错误代码 /// public static Tuple? 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; } /// /// 错误信息 /// /// public static Dictionary ErrorCodes() { return new Dictionary() { { "FF", "电表无此数据项" }, { "EE", "未知数据错误" }, { "E1", "数据点缺少(停电)" }, { "E2", "通讯异常" }, { "E3", "集中器未配置数据项" }, { "E4", "电表档案无效" }, { "E5", "电表无此数据项" }, { "E6", "电表时间异常" }, { "E7","暂停抄表" } }; } /// /// 费率数 /// /// /// /// public static int GetRatingCount(this List hexMessageList ,int index, int len) { var list = hexMessageList.GetRange(index, len); return list.Count > 0 ? list[0].HexToDec() : 0; } /// /// 抄表时间 /// /// /// /// public static string GetReadTime(this List hexMessageList, int index, int len) { var list = hexMessageList.GetRange(index, len); return list.GetReadTime(); } /// /// 抄表时间 /// /// /// public static string GetReadTime(this List data) { data.Reverse(); data.Insert(0, DateTime.Now.ToString("yyyy").Substring(0, 2)); return string.Join("", data); } } }