alielie
2026-04-23 ef52fd6fd07508883dce4eb002acda59f0169e59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using HMI.Core.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Plugin.TestResult.Lixun
{
    public class SNValidator
    {
        public static bool ValidateSN(string model, string sn, out string msg)
        {
            msg = string.Empty;
            string validate = string.Empty;
            try
            {
                validate = ProductManager.GetParameterByProductAndName(model, "SNRValidate")?.Value;
                if (string.IsNullOrEmpty(validate))
                {
                    msg = $"当前型号[{model}]未配置SNRValidate参数,无法校验SN合法性";
                    return true;
                }
                string[] arr = validate.Split(';');
                if (arr.Length == 1)
                {
                    if (!sn.Contains(arr[0]))
                    {
                        msg = $"条码不包含固定码[{arr[0]}]";
                        return false;
                    }
                    return true;
                }
 
                if (arr.Length == 2)
                {
                    int length = int.Parse(arr[1]);
                    if (!sn.Contains(arr[0]))
                    {
                        msg = $"条码不包含固定码[{arr[0]}]";
                        return false;
                    }
                    if (sn.Length != length)
                    {
                        msg = $"条码长度应为[{length}]";
                        return false;
                    }
                    return true;
                }
 
                if (arr.Length >= 3)
                {
                    int length = int.Parse(arr[1]);
                    int startIndex = int.Parse(arr[2]);
 
                    if (sn.Length != length)
                    {
                        msg = $"条码长度应为[{length}]";
                        return false;
                    }
 
                    if (startIndex >= sn.Length)
                    {
                        msg = $"条码长读小于起始下标[{validate}]";
                        return false;
                    }
 
                    if (arr[0].Length > sn.Length)
                    {
                        msg = $"条码长读小于固定码长度[{validate}]";
                        return false;
                    }
 
                    if (sn.Substring(startIndex, arr[0].Length) != arr[0])
                    {
                        msg = $"固定码检查失败: [{validate}]";
                        return false;
                    }
                    return true;
                }
                msg = $"当前型号[{model}]配置参数[{validate}]解析失败";
                return false;
            }
            catch (Exception ex)
            {
                msg = $"当前型号[{model}]配置参数[{validate}]解析失败";
                return false;
            }
        }
    }
}