using HslCommunication;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace WcsMain.ExtendMethod;
///
/// 扩展方法,检查基础数据
///
public static partial class StringExtendMethod
{
///
/// 检测一个字符串是不是 NoRead
///
///
///
public static bool IsNoRead(this string? value)
{
if (string.IsNullOrEmpty(value)) return false;
return value.Replace(" ", "").Equals("noread", StringComparison.CurrentCultureIgnoreCase);
}
///
/// 检查一个字符串是否是数字
///
///
///
public static bool IsNumber(this string? value)
{
if (string.IsNullOrEmpty(value)) return false;
return IsNumberRegex().IsMatch(value);
}
///
/// 将字符串转换为 bool,只有等于null和0的情况下是false
///
///
///
public static bool ToBool(this string? value)
{
if(value == null) return false;
return value != "0";
}
///
/// 检测一个字符串是不是时间格式,如果是返回时间,如果不是返回 default
///
///
///
public static DateTime? ToDateTime(this string? value)
{
try
{
DateTime time = Convert.ToDateTime(value);
return time;
}
catch
{
return default;
}
}
///
/// 格式化条码,返回条码和方位码
///
///
///
[Obsolete("客户变更需求,不使用方向码", true)]
public static (string code, string direction) FormatDir(this string? value)
{
if (string.IsNullOrEmpty(value)) return (string.Empty, string.Empty);
string[] vals = value.Split('-');
if (vals.Length < 2) return (value, string.Empty);
string direction = vals[^1];
string code = value.RemoveLast(direction.Length + 1);
return (code, direction);
}
///
/// 将载具号转换为 plc 可识别的载具号
///
///
///
public static int ToPlcVehicleNo(this string? value)
{
if (string.IsNullOrEmpty(value)) return 0;
try
{
return Convert.ToInt32(Regex.Replace(value, "\\D", ""));
}
catch
{
return 0;
}
}
[GeneratedRegex("^\\d+$")]
private static partial Regex IsNumberRegex();
///
/// 32位加密
///
///
///
public static string MD5Encrypt32(this string password)
{
string cl = password;
string pwd = "";
//MD5 md5 = MD5.Create(); //实例化一个md5对像
byte[] s = MD5.HashData(Encoding.UTF8.GetBytes(cl));
for (int i = 0; i < s.Length; i++)
{
pwd = pwd + s[i].ToString("X2");
}
return pwd;
}
}