mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 03:01:24 +00:00
Moved MewExplorer to a new repo
This commit is contained in:
@@ -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;
|
||||
|
||||
33
MewTerminal/Commands/ListSupportCommand.cs
Normal file
33
MewTerminal/Commands/ListSupportCommand.cs
Normal 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());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
53
MewTerminal/Commands/OnlineCommands/GetRegisterCommand.cs
Normal file
53
MewTerminal/Commands/OnlineCommands/GetRegisterCommand.cs
Normal 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());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
46
MewTerminal/Commands/OnlineCommands/OnlineCommand.cs
Normal file
46
MewTerminal/Commands/OnlineCommands/OnlineCommand.cs
Normal 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();
|
||||
|
||||
}
|
||||
64
MewTerminal/Commands/OnlineCommands/SetRegisterCommand.cs
Normal file
64
MewTerminal/Commands/OnlineCommands/SetRegisterCommand.cs
Normal 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");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user