Moved MewExplorer to a new repo

This commit is contained in:
Felix Weiß
2023-07-10 11:00:34 +02:00
parent 88cdc1a36c
commit d8c18bbf34
58 changed files with 239 additions and 1608 deletions

View File

@@ -13,27 +13,6 @@ public abstract class CommandLineExcecuteable {
[Option('v', "verbosity", HelpText = "Sets the Loglevel verbosity", Default = LogLevel.None)]
public LogLevel LogLevel { get; set; } = LogLevel.None;
[Usage]
public static IEnumerable<Example> Examples {
get {
return new List<Example>() {
new Example(
helpText: "Sanning from adapter with ip 127.0.0.1 and logging all critical messages",
formatStyle: UnparserSet,
sample: new ScanCommand {
IPSource = "127.0.0.1",
LogLevel = LogLevel.Critical,
}),
new Example(
helpText: "Scanning from all adapters and logging only errors",
formatStyle: UnparserSet,
sample: new ScanCommand {
LogLevel = LogLevel.Error,
}),
};
}
}
public virtual void Run() { }
public virtual Task RunAsync () => Task.CompletedTask;

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using CommandLine;
using MewtocolNet;
using MewtocolNet.ComCassette;
using MewtocolNet.Logging;
using Spectre.Console;
namespace MewTerminal.Commands;
[Verb("support-list", HelpText = "Lists all supported PLC types")]
internal class ListSupportCommand : CommandLineExcecuteable {
public override void Run () {
var plcs = Enum.GetValues<PlcType>().Cast<PlcType>();
var lst = new List<ParsedPlcName>();
foreach (var plcT in plcs) {
var decomp = plcT.ToNameDecompose();
foreach (var name in decomp)
lst.Add(name);
}
AnsiConsole.Write(lst.ToTable());
}
}

View File

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using CommandLine;
using MewtocolNet;
using MewtocolNet.ComCassette;
using MewtocolNet.Logging;
using MewtocolNet.RegisterBuilding;
using Spectre.Console;
namespace MewTerminal.Commands.OnlineCommands;
[Verb("rget", HelpText = "Gets the values of the given PLC registers")]
internal class GetRegisterCommand : OnlineCommand {
[Value(0, MetaName = "registers", Default = "DT0", HelpText = "The registers to read formatted as <mewtocol_name:plc_type> (DT0:INT)")]
public IEnumerable<string> Registers { get; set; }
internal override async Task AfterSetup(IPlc plc) {
var builder = RegBuilder.ForInterface(plc);
foreach (var reg in Registers) {
var split = reg.Split(":");
if (split.Length <= 1) {
throw new FormatException($"Register name was not formatted correctly: {reg}, missing :PlcVarType");
}
var mewtocolName = split[0];
var mewtocolType = split[1];
if (Enum.TryParse<PlcVarType>(mewtocolType, out var parsedT)) {
builder.FromPlcRegName(mewtocolName).AsPlcType(parsedT).Build();
}
}
await plc.ConnectAsync();
foreach (var reg in plc.GetAllRegisters()) {
await reg.ReadAsync();
}
AnsiConsole.Write(plc.GetAllRegisters().ToTable());
}
}

View File

@@ -0,0 +1,46 @@
using CommandLine;
using MewtocolNet;
using Spectre.Console;
namespace MewTerminal.Commands.OnlineCommands;
internal class OnlineCommand : CommandLineExcecuteable {
[Option('e', "ethernet", Default = "127.0.0.1:9094", HelpText = "Ethernet config")]
public string? EthernetStr { get; set; }
[Option('s', "serial", Default = null, HelpText = "Serial port config")]
public string? SerialStr { get; set; }
public override async Task RunAsync() {
try {
if (!string.IsNullOrEmpty(SerialStr)) {
} else {
var split = EthernetStr.Split(":");
string ip = split[0];
int port = int.Parse(split[1]);
using (var plc = Mewtocol.Ethernet(ip, port)) {
await AfterSetup(plc);
}
}
} catch (Exception ex) {
AnsiConsole.WriteLine($"[red]{ex.Message.ToString()}[/]");
}
}
internal virtual async Task AfterSetup(IPlc plc) => throw new NotImplementedException();
}

View File

@@ -0,0 +1,64 @@
using CommandLine;
using MewtocolNet;
using MewtocolNet.RegisterBuilding;
using MewtocolNet.Registers;
using Spectre.Console;
namespace MewTerminal.Commands.OnlineCommands;
[Verb("rset", HelpText = "Sets the values of the given PLC registers")]
internal class SetRegisterCommand : OnlineCommand {
[Value(0, MetaName = "registers", Default = "DT0", HelpText = "The registers to write formatted as <mewtocol_name:plc_type> (DT0:INT:VALUE)")]
public IEnumerable<string> Registers { get; set; }
internal override async Task AfterSetup(IPlc plc) {
var builder = RegBuilder.ForInterface(plc);
var toWriteVals = new List<object>();
foreach (var reg in Registers) {
var split = reg.Split(":");
if (split.Length <= 2) {
throw new FormatException($"Register name was not formatted correctly: {reg}, missing :PlcVarType:Value");
}
var mewtocolName = split[0];
var mewtocolType = split[1];
var value = split[2];
if (Enum.TryParse<PlcVarType>(mewtocolType, out var parsedT)) {
var built = builder.FromPlcRegName(mewtocolName).AsPlcType(parsedT).Build();
if(built is BoolRegister) toWriteVals.Add(bool.Parse(value));
else if(built is NumberRegister<short>) toWriteVals.Add(short.Parse(value));
else if(built is NumberRegister<ushort>) toWriteVals.Add(ushort.Parse(value));
else if(built is NumberRegister<int>) toWriteVals.Add(int.Parse(value));
else if(built is NumberRegister<uint>) toWriteVals.Add(uint.Parse(value));
else if(built is NumberRegister<float>) toWriteVals.Add(float.Parse(value));
else if(built is NumberRegister<TimeSpan>) toWriteVals.Add(TimeSpan.Parse(value));
}
}
await plc.ConnectAsync();
int i = 0;
foreach (var reg in plc.GetAllRegisters()) {
await reg.WriteAsync(toWriteVals[i]);
i++;
}
AnsiConsole.WriteLine("All registers written");
}
}

View File

@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace MewTerminal;
@@ -24,7 +25,7 @@ internal static class Helpers {
foreach (var prop in props) {
if(isFirst) table.AddColumn(prop.Name);
if(isFirst) table.AddColumn(prop.Name.SplitCamelCase());
var propVal = prop.GetValue(item);
@@ -34,6 +35,10 @@ internal static class Helpers {
strVal = string.Join(" ", bArr.Select(x => x.ToString("X2")));
}
if (propVal is string[] sArr) {
strVal = string.Join(", ", sArr);
}
strVal = strVal.Replace("[", "");
strVal = strVal.Replace("]", "");
@@ -51,4 +56,10 @@ internal static class Helpers {
}
private static string SplitCamelCase (this string str) {
return Regex.Replace(Regex.Replace(str, @"(\P{Ll})(\P{Ll}\p{Ll})", "$1 $2"), @"(\p{Ll})(\P{Ll})", "$1 $2");
}
}

View File

@@ -6,6 +6,8 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<ApplicationManifest>app.manifest</ApplicationManifest>
<NoWarn>IL2026,IL2027,IL2090</NoWarn>
</PropertyGroup>
<ItemGroup>
@@ -18,9 +20,12 @@
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<AssemblyName>mewcmd</AssemblyName>
<PlatformTarget>x64</PlatformTarget>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<TrimmerRemoveSymbols>true</TrimmerRemoveSymbols>
<TrimMode>partial</TrimMode>
<SelfContained>True</SelfContained>
<DebugType>None</DebugType>
<DebugSymbols>False</DebugSymbols>

View File

@@ -3,6 +3,7 @@ using CommandLine.Text;
using MewTerminal.Commands;
using MewtocolNet.Logging;
using Spectre.Console;
using System.Globalization;
using System.Reflection;
namespace MewTerminal;
@@ -11,6 +12,11 @@ internal class Program {
static void Main(string[] args) {
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
Console.OutputEncoding = System.Text.Encoding.UTF8;
Logger.OnNewLogMessage((dt, lv, msg) => {
AnsiConsole.WriteLine($"{msg}");

18
MewTerminal/app.manifest Normal file
View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="mewterminal.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
</application>
</compatibility>
</assembly>