wanshenmean
2026-03-13 d216edd0e9931d71664f33e625cff6d8131a0fad
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;
using WIDESEAWCS_S7Simulator.Core.Entities;
using WIDESEAWCS_S7Simulator.Core.Enums;
using WIDESEAWCS_S7Simulator.Web.Services;
 
namespace WIDESEAWCS_S7Simulator.Web.Pages;
 
/// <summary>
/// 创建实例页
/// </summary>
public class CreateModel : BasePageModel
{
    private readonly ILogger<CreateModel> _logger;
    private readonly ApiHttpClient _apiClient;
 
    public CreateModel(ILogger<CreateModel> logger, IConfiguration configuration, ApiHttpClient apiClient)
        : base(configuration)
    {
        _logger = logger;
        _apiClient = apiClient;
    }
 
    [BindProperty]
    public CreateInstanceInputModel Input { get; set; } = new();
 
    public void OnGet()
    {
        _logger.LogInformation("Loading create instance page");
    }
 
    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }
 
        try
        {
            var config = new InstanceConfig
            {
                Id = Input.Id,
                Name = Input.Name,
                PLCType = Input.PLCType,
                Port = Input.Port,
                ActivationKey = Input.ActivationKey ?? string.Empty,
                AutoStart = Input.AutoStart,
                MemoryConfig = new MemoryRegionConfig
                {
                    MRegionSize = Input.MRegionSize > 0 ? Input.MRegionSize : 1024,
                    DBBlockCount = Input.DBBlockCount > 0 ? Input.DBBlockCount : 100,
                    DBBlockSize = Input.DBBlockSize > 0 ? Input.DBBlockSize : 1024,
                    IRegionSize = Input.IRegionSize > 0 ? Input.IRegionSize : 256,
                    QRegionSize = Input.QRegionSize > 0 ? Input.QRegionSize : 256,
                    TRegionCount = Input.TRegionCount > 0 ? Input.TRegionCount : 64,
                    CRegionCount = Input.CRegionCount > 0 ? Input.CRegionCount : 64
                }
            };
 
            var result = await _apiClient.CreateInstanceAsync(config);
 
            if (result != null)
            {
                _logger.LogInformation("Instance {InstanceId} created successfully", Input.Id);
                TempData["SuccessMessage"] = $"实例 \"{Input.Id}\" 创建成功!";
                return RedirectToPage("/Index");
            }
            else
            {
                ModelState.AddModelError(string.Empty, "创建实例失败");
            }
        }
        catch (HttpRequestException ex)
        {
            _logger.LogError(ex, "Failed to create instance");
            ModelState.AddModelError(string.Empty, "网络错误,请稍后重试");
        }
 
        return Page();
    }
 
    /// <summary>
    /// 创建实例输入模型
    /// </summary>
    public class CreateInstanceInputModel
    {
        /// <summary>
        /// 实例ID
        /// </summary>
        [Required(ErrorMessage = "实例ID不能为空")]
        [StringLength(50, MinimumLength = 1, ErrorMessage = "实例ID长度必须在1-50个字符之间")]
        [RegularExpression("^[a-zA-Z0-9_-]+$", ErrorMessage = "实例ID只能包含字母、数字、下划线和连字符")]
        public string Id { get; set; } = string.Empty;
 
        /// <summary>
        /// 实例名称
        /// </summary>
        [Required(ErrorMessage = "实例名称不能为空")]
        [StringLength(100, ErrorMessage = "实例名称不能超过100个字符")]
        public string Name { get; set; } = string.Empty;
 
        /// <summary>
        /// PLC型号
        /// </summary>
        [Required(ErrorMessage = "PLC型号不能为空")]
        public SiemensPLCType PLCType { get; set; } = SiemensPLCType.S71200;
 
        /// <summary>
        /// 监听端口
        /// </summary>
        [Required(ErrorMessage = "端口不能为空")]
        [Range(1, 65535, ErrorMessage = "端口必须在1-65535之间")]
        public int Port { get; set; } = 102;
 
        /// <summary>
        /// HSL激活码
        /// </summary>
        [StringLength(200, ErrorMessage = "激活码不能超过200个字符")]
        public string? ActivationKey { get; set; }
 
        /// <summary>
        /// 自动启动
        /// </summary>
        public bool AutoStart { get; set; } = false;
 
        // 内存配置
        public int MRegionSize { get; set; }
        public int DBBlockCount { get; set; }
        public int DBBlockSize { get; set; }
        public int IRegionSize { get; set; }
        public int QRegionSize { get; set; }
        public int TRegionCount { get; set; }
        public int CRegionCount { get; set; }
    }
}