借助网络配置,控制Modbus工具的日志显示,避免卡死界面
智能大石头 authored at 2021-12-11 22:09:19
9.02 KiB
XCoder
using System;
using System.Collections.Generic;
using NewLife;
using NewLife.XCoder.Tools.Can;
using NewLife.XCoder.Tools.Coord;
using NewLife.XCoder.Tools.Crypto;
using NewLife.XCoder.Tools.Dbc;
using NewLife.XCoder.Tools.FileEncoding;
using NewLife.XCoder.Tools.Folder;
using NewLife.XCoder.Tools.Md5;
using NewLife.XCoder.Tools.Modbus;
using NewLife.XCoder.Tools.Platform;
using NewLife.XCoder.Tools.Regex;
using NewLife.XCoder.Tools.Settings;
using NewLife.XCoder.Tools.Sql;
using NewLife.XCoder.Tools.Symmetric;
using NewLife.XCoder.Tools.Voice;
using NewLife.XCoder.Views;

namespace NewLife.XCoder.Shell;

/// <summary>工具注册表:显式登记全部工具,不用反射扫描</summary>
public static class ToolRegistry
{
    /// <summary>分类</summary>
    public static IReadOnlyList<ToolCategory> Categories { get; } =
    [
        new("dev", "Development & Data", "🗄"),
        new("net", "Network & Communication", "🌐"),
        new("ind", "Industrial & Devices", "⚙"),
        new("eff", "Productivity", "✦"),
        new("sys", "System", "🛠"),
    ];

    /// <summary>全部工具(35 个);ViewType 为空的表示尚未迁移</summary>
    public static IReadOnlyList<ToolDescriptor> Tools { get; } =
    [
        // 开发与数据
        new("modeling", "Data Modeling", "Connect to a database or import a model to generate entity code and DDL.", "dev", "🗄", typeof(DataModelingWindow), ["数据库", "实体", "代码生成", "DDL"]),
        new("sync", "Data Sync", "Sync schema and data across databases with diff preview and execution.", "dev", "🔄", typeof(DataSyncWindow), ["同步", "迁移", "备份"]),
        new("sql", "SQL Workbench", "Browse schema, edit and run SQL, view results and export.", "dev", "🖥", typeof(SqlWorkbenchToolView), ["SQL", "查询", "执行"]),
        new("api", "API Debugger", "Connect to NewLife.Remoting RPC, invoke actions and inspect interaction logs.", "dev", "🔗", typeof(ApiDebugWindow), ["RPC", "接口", "远程"]),
        new("discover", "API Discovery", "Scan LAN ApiServer instances and inspect service information.", "dev", "🔎", typeof(ApiDiscoverWindow), ["扫描", "服务"]),
        new("redis", "Redis Manager", "Connect, browse keys, edit values and run commands.", "dev", "🧱", typeof(RedisManagerWindow), ["缓存", "键值"]),
        new("regex", "Regex", "Match, replace, analyze capture groups and batch-replace files.", "dev", "🔍", typeof(RegexToolView), ["正则", "替换"]),
        new("crypto", "Encrypt / Decrypt", "34 security commands including digest, HMAC, Hex/Base64, URL and RSA.", "dev", "🔒", typeof(CryptoToolView), ["AES", "RSA", "Base64", "摘要"]),
        new("symmetric", "Symmetric Encryption", "AES/DES and other symmetric algorithms with parameter configuration.", "dev", "🔑", typeof(SymmetricToolView), ["AES", "DES"]),
        new("md5", "MD5 Lookup", "MD5 lookup and online cracking, local first.", "dev", "#️⃣", typeof(Md5ToolView), ["MD5", "哈希"]),
        new("filecode", "File Encoding", "Detect, preview and batch-convert file charsets.", "dev", "📄", typeof(FileEncodingToolView), ["编码", "字符集", "GBK"]),

        // 网络与通信
        new("network", "Network Debugger", "TCP/UDP client and server debugging with send/receive logs.", "net", "🌐", typeof(NetworkWindow), ["TCP", "UDP", "端口"]),
        new("message", "Message Debugger", "Assemble, send, receive and parse message protocols.", "net", "💬", typeof(MessageDebugWindow), ["报文", "协议"]),
        new("mqtt", "MQTT Client", "Broker connection, publish/subscribe, topic tree and message logs.", "net", "📡", typeof(MqttWindow), ["MQTT", "Broker", "订阅"]),
        new("ssh", "SSH Tool", "Remote terminal and command execution with key or password auth.", "net", "🖥", typeof(SshWindow), ["SSH", "终端"]),
        new("modbustcp", "Modbus TCP", "Master read/write, register table and periodic polling.", "net", "🔀", typeof(ModbusTcpWindow), ["Modbus", "寄存器"]),
        new("modbusrtu", "Modbus RTU", "Serial master read/write, CRC validation and frame logs.", "net", "🔀", typeof(ModbusRtuWindow), ["Modbus", "串口"]),
        new("serial", "Serial Port", "Enumerate ports, configure parameters, send/receive and HEX view.", "net", "🔌", typeof(SerialPortWindow), ["串口", "COM", "波特率"]),
        new("voicecast", "Network Voice Broadcast", "Listen for UDP broadcast messages; supports plain text and devnotify JSON.", "net", "🔊", typeof(VoiceNotifyToolView), ["语音", "播报"]),

        // 工业与设备
        new("can", "CAN Debugger", "CAN frame send/receive and filtering; virtual loopback for offline testing; auto-detects SocketCAN on Linux.", "ind", "🚌", typeof(CanToolView), ["CAN", "报文"]),
        new("settings", "Settings", "Theme, language, config/log directories and shell behavior.", "sys", "🛠", typeof(SettingsToolView), ["主题", "语言", "目录"]),
        new("platform", "Platform Diagnostics", "Platform capabilities, USB/audio/NIC enumeration and NIC command generation.", "sys", "🧩", typeof(PlatformToolView), ["平台", "USB", "网卡", "诊断"]),
        new("dbc", "CAN DBC Viewer", "DBC parsing, signal decoding and J1939 support.", "ind", "📄", typeof(DbcToolView), ["DBC", "J1939"]),
        new("mbmaster", "Modbus Master", "Batch register read/write and device templates.", "ind", "🔧", typeof(ModbusMasterToolView), ["Modbus", "主站"]),
        new("mbslave", "Modbus Slave", "Slave simulation for host integration testing.", "ind", "🔧", typeof(ModbusSlaveToolView), ["Modbus", "从站"]),
        new("io", "I/O Control", "Modbus I/O module read/write and status monitoring.", "ind", "⚡", typeof(IoControlWindow), ["IO", "继电器"]),
        new("usb", "USB Devices", "USB device and HUB topology enumeration with VID/PID/speed.", "ind", "🔌", typeof(UsbDeviceWindow), ["USB", "HUB"]),
        new("audio", "Audio Test", "Audio device enumeration, recording and playback tests.", "ind", "🎵", typeof(AudioDeviceWindow), ["声卡", "录音"]),
        new("ip", "IP Settings", "View NIC status and configure addresses with rollback.", "ind", "🌍", typeof(IpConfigWindow), ["网卡", "地址", "网关"]),

        // 效率工具
        new("icon", "Icon Watermark", "Batch icon processing, watermarking, sizing and format conversion.", "eff", "🎨", typeof(IconToolWindow), ["图标", "水印", "ICO"]),
        new("folder", "Disk Atlas", "Disk space map: drill down, find large items and clean up safely.", "eff", "📁", typeof(FolderStatToolView), ["磁盘", "目录", "统计", "占用", "大小", "清理", "星图"]),
        new("coord", "Coordinate Converter", "WGS84/GCJ02/BD09 conversion and batch conversion.", "eff", "🧭", typeof(CoordToolView), ["坐标", "转换"]),
        new("gps", "GPS Assistant", "Parse positioning data, convert HEX to float and view tracks.", "eff", "📍", typeof(GpsWindow), ["GPS", "定位"]),
        new("map", "Map APIs", "Geocoding, reverse geocoding and route planning debugging.", "eff", "🗺", typeof(MapWindow), ["地图", "地理编码"]),
        new("backup", "Phone Backup", "Back up and organize device files via adb.", "eff", "📱", typeof(BackupWindow), ["手机", "adb", "备份"]),
        new("speech", "Voice Assistant", "Text-to-speech broadcast and audio export.", "eff", "🗣", typeof(SpeechWindow), ["语音", "合成", "TTS"]),
        new("mel", "Mel Spectrum", "Audio capture, Mel spectrum computation and visualization.", "eff", "🎶", typeof(MelSpectrumWindow), ["频谱", "音频"]),
    ];

    /// <summary>按分类取工具</summary>
    /// <param name="category">分类标识</param>
    public static IReadOnlyList<ToolDescriptor> GetByCategory(String category)
    {
        var list = new List<ToolDescriptor>();
        foreach (var item in Tools)
        {
            if (item.Category == category) list.Add(item);
        }

        return list;
    }

    /// <summary>关键字匹配(名称 / 说明 / 关键字)</summary>
    /// <param name="tool">工具</param>
    /// <param name="keyword">关键字</param>
    public static Boolean Match(ToolDescriptor tool, String keyword)
    {
        if (keyword.IsNullOrEmpty()) return true;
        if (tool.Title.Contains(keyword, StringComparison.OrdinalIgnoreCase)) return true;
        if (tool.Description.Contains(keyword, StringComparison.OrdinalIgnoreCase)) return true;
        if (tool.DisplayTitle.Contains(keyword, StringComparison.OrdinalIgnoreCase)) return true;
        if (tool.DisplayDescription.Contains(keyword, StringComparison.OrdinalIgnoreCase)) return true;
        if (tool.Keywords != null)
        {
            foreach (var item in tool.Keywords)
            {
                if (item.Contains(keyword, StringComparison.OrdinalIgnoreCase)) return true;
            }
        }

        return false;
    }

    /// <summary>按标识取工具</summary>
    /// <param name="key">标识</param>
    public static ToolDescriptor? Find(String key)
    {
        foreach (var item in Tools)
        {
            if (item.Key == key) return item;
        }

        return null;
    }
}