91 lines
3.6 KiB
C#
91 lines
3.6 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Windows;
|
||
using YuncangTrace.LocalService.Services.Scan;
|
||
|
||
namespace YuncangTrace.LocalService;
|
||
|
||
/// <summary>
|
||
/// 「添加通道」弹窗:设置**通道号**与**设备名**(与 AndroidScanCenter 的添加流程一致)。
|
||
///
|
||
/// - 通道号是本地概念(现场认工位用),被占用的号照样列出来、只标注谁占着
|
||
/// —— 直接藏掉的话现场看到「怎么就没有 3 号」还得来问一遍;
|
||
/// - 设备名会写进设备本身(SYS.HostName),默认填设备自报的名字。
|
||
/// </summary>
|
||
public partial class AddChannelWindow : Window
|
||
{
|
||
/// <summary>下拉框选项(带占用标注)</summary>
|
||
public sealed class NumberOption
|
||
{
|
||
public int Number { get; init; }
|
||
public string Label { get; init; } = string.Empty;
|
||
public bool Taken { get; init; }
|
||
}
|
||
|
||
private readonly IReadOnlyList<ScanChannel> _channels;
|
||
|
||
public AddChannelWindow(DiscoveredDevice device, IReadOnlyList<ScanChannel> channels)
|
||
{
|
||
InitializeComponent();
|
||
|
||
_channels = channels;
|
||
|
||
var reported = (device.HostName ?? string.Empty).Trim();
|
||
DeviceText.Text = reported.Length == 0
|
||
? $"{device.Mac} · {device.Ip} · {device.Model}"
|
||
: $"{reported}({device.Mac} · {device.Ip} · {device.Model})";
|
||
|
||
// 默认名:设备自报的 HostName(通常就是工位名),没有就用 MAC
|
||
var defaultName = reported.Length == 0 ? device.Mac : reported;
|
||
NameBox.Text = defaultName;
|
||
|
||
var suggested = ChannelStore.NextNumber(channels);
|
||
var options = new List<NumberOption>();
|
||
for (var number = 1; number <= ChannelStore.MaxNumber; number++)
|
||
{
|
||
var owner = ChannelStore.FindByNumber(channels, number);
|
||
options.Add(new NumberOption
|
||
{
|
||
Number = number,
|
||
Taken = owner is not null,
|
||
Label = owner is null ? number.ToString() : $"{number}(已被「{owner.DisplayName}」占用)",
|
||
});
|
||
}
|
||
NumberBox.ItemsSource = options;
|
||
NumberBox.SelectedItem = options.FirstOrDefault(item => item.Number == suggested) ?? options[0];
|
||
|
||
HintText.Text = $"添加时会先检查并确保设备上有给中控用的 TCP 服务(条目 {ScanProtocol.SockName}、端口 {ScanProtocol.TcpPort},"
|
||
+ "与 Android 中控的 8086 分开),设备名有变化会写入设备并落盘";
|
||
}
|
||
|
||
/// <summary>用户选的通道号(确定后有值)</summary>
|
||
public int ChannelNumber { get; private set; }
|
||
|
||
/// <summary>用户填的设备名(确定后有值,可能为空 = 用设备自报名)</summary>
|
||
public string ChannelName { get; private set; } = string.Empty;
|
||
|
||
private void Ok_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
if (NumberBox.SelectedItem is not NumberOption option)
|
||
{
|
||
MessageBox.Show(this, "请先选择通道号", "添加通道", MessageBoxButton.OK, MessageBoxImage.Information);
|
||
return;
|
||
}
|
||
|
||
// 已被占用:只提示、不关框,用户可以接着选(与 AndroidScanCenter 一致)
|
||
if (option.Taken)
|
||
{
|
||
MessageBox.Show(this, $"通道号 {option.Number} 已经被占用了,请换一个号", "添加通道",
|
||
MessageBoxButton.OK, MessageBoxImage.Warning);
|
||
return;
|
||
}
|
||
|
||
ChannelNumber = option.Number;
|
||
ChannelName = (NameBox.Text ?? string.Empty).Trim();
|
||
DialogResult = true;
|
||
}
|
||
|
||
private void Cancel_Click(object sender, RoutedEventArgs e) => DialogResult = false;
|
||
}
|