2024-06-12 07:29:44 +08:00
|
|
|
|
using HslCommunication;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
2024-05-14 16:30:56 +08:00
|
|
|
|
|
|
|
|
|
|
namespace WcsMain.ExtendMethod;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 扩展方法,检查基础数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static partial class StringExtendMethod
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 检测一个字符串是不是 NoRead
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static bool IsNoRead(this string? value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(value)) return false;
|
|
|
|
|
|
return value.Replace(" ", "").Equals("noread", StringComparison.CurrentCultureIgnoreCase);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 检查一个字符串是否是数字
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static bool IsNumber(this string? value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(value)) return false;
|
|
|
|
|
|
return IsNumberRegex().IsMatch(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 将字符串转换为 bool,只有等于null和0的情况下是false
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static bool ToBool(this string? value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(value == null) return false;
|
|
|
|
|
|
return value != "0";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 检测一个字符串是不是时间格式,如果是返回时间,如果不是返回 default
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static DateTime? ToDateTime(this string? value)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
DateTime time = Convert.ToDateTime(value);
|
|
|
|
|
|
return time;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
return default;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-12 07:29:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 格式化条码,返回条码和方位码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-14 16:30:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 将载具号转换为 plc 可识别的载具号
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|