-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from meshtastic/wip
Update protos and add remove-node command
- Loading branch information
Showing
17 changed files
with
1,244 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Meshtastic.Data; | ||
using Meshtastic.Data.MessageFactories; | ||
using Meshtastic.Display; | ||
using Meshtastic.Protobufs; | ||
using Meshtastic.Extensions; | ||
|
||
namespace Meshtastic.Cli.CommandHandlers; | ||
|
||
public class CaptureCommandHandler : DeviceCommandHandler | ||
{ | ||
public CaptureCommandHandler(DeviceConnectionContext context, CommandContext commandContext) : base(context, commandContext) { } | ||
|
||
public async Task<DeviceStateContainer> Handle() | ||
{ | ||
var wantConfig = new ToRadioMessageFactory().CreateWantConfigMessage(); | ||
var container = await Connection.WriteToRadio(wantConfig, CompleteOnConfigReceived); | ||
Connection.Disconnect(); | ||
return container; | ||
} | ||
|
||
public override async Task OnCompleted(FromRadio packet, DeviceStateContainer container) | ||
{ | ||
await Connection.ReadFromRadio((fromRadio, container) => | ||
{ | ||
if (fromRadio!.PayloadVariantCase == FromRadio.PayloadVariantOneofCase.Packet) { | ||
var fromRadioDecoded = new FromRadioDecoded(fromRadio) | ||
{ | ||
PortNum = fromRadio.Packet?.Decoded?.Portnum, | ||
PayloadSize = fromRadio.Packet?.Decoded?.Payload.Length ?? 0, | ||
ReceivedAt = DateTime.Now, | ||
}; | ||
if (fromRadio.GetPayload<AdminMessage>() != null) | ||
fromRadioDecoded.DecodedPayload = fromRadio.GetPayload<AdminMessage>(); | ||
else if (fromRadio.GetPayload<XModem>() != null) | ||
fromRadioDecoded.DecodedPayload = fromRadio.GetPayload<XModem>(); | ||
else if (fromRadio.GetPayload<NodeInfo>() != null) | ||
fromRadioDecoded.DecodedPayload = fromRadio.GetPayload<NodeInfo>(); | ||
else if (fromRadio.GetPayload<Position>() != null) | ||
fromRadioDecoded.DecodedPayload = fromRadio.GetPayload<Position>(); | ||
else if (fromRadio.GetPayload<Waypoint>() != null) | ||
fromRadioDecoded.DecodedPayload = fromRadio.GetPayload<Waypoint>(); | ||
else if (fromRadio.GetPayload<Telemetry>() != null) | ||
fromRadioDecoded.DecodedPayload = fromRadio.GetPayload<Telemetry>(); | ||
else if (fromRadio.GetPayload<Routing>() != null) | ||
fromRadioDecoded.DecodedPayload = fromRadio.GetPayload<Routing>(); | ||
else if (fromRadio.GetPayload<RouteDiscovery>() != null) | ||
fromRadioDecoded.DecodedPayload = fromRadio.GetPayload<RouteDiscovery>(); | ||
else if (fromRadio.GetPayload<string>() != null) | ||
fromRadioDecoded.DecodedPayload = fromRadio.GetPayload<string>(); | ||
} | ||
|
||
return Task.FromResult(false); | ||
}); | ||
} | ||
} | ||
|
||
public class FromRadioDecoded | ||
{ | ||
public FromRadioDecoded(FromRadio fromRadio) | ||
{ | ||
Packet = fromRadio; | ||
} | ||
|
||
public FromRadio Packet { get; set; } | ||
public object? DecodedPayload { get; set; } | ||
public PortNum? PortNum { get; set; } | ||
public int PayloadSize { get; set; } | ||
public int TotalSize { get; set; } | ||
public DateTime ReceivedAt { get; set; } | ||
} |
39 changes: 39 additions & 0 deletions
39
Meshtastic.Cli/CommandHandlers/RemoveNodeCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Meshtastic.Data; | ||
using Meshtastic.Data.MessageFactories; | ||
using Meshtastic.Protobufs; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Meshtastic.Cli.CommandHandlers; | ||
|
||
public class RemoveNodeCommandHandler : DeviceCommandHandler | ||
{ | ||
private readonly uint nodeNum; | ||
|
||
public RemoveNodeCommandHandler(uint nodeNum, DeviceConnectionContext context, | ||
CommandContext commandContext) : base(context, commandContext) | ||
{ | ||
this.nodeNum = nodeNum; | ||
} | ||
|
||
public async Task<DeviceStateContainer> Handle() | ||
{ | ||
var wantConfig = new ToRadioMessageFactory().CreateWantConfigMessage(); | ||
var container = await Connection.WriteToRadio(wantConfig, CompleteOnConfigReceived); | ||
Connection.Disconnect(); | ||
return container; | ||
} | ||
|
||
public override async Task OnCompleted(FromRadio packet, DeviceStateContainer container) | ||
{ | ||
if (!container.Nodes.Any(n => n.Num == nodeNum)) | ||
{ | ||
Logger.LogError($"Node with nodenum {nodeNum} not found in device's NodeDB"); | ||
return; | ||
} | ||
|
||
Logger.LogInformation("Removing device from NodeDB.."); | ||
var adminMessageFactory = new AdminMessageFactory(container, Destination); | ||
var adminMessage = adminMessageFactory.CreateRemoveByNodenumMessage(nodeNum); | ||
await Connection.WriteToRadio(ToRadioMessageFactory.CreateMeshPacketMessage(adminMessage), AnyResponseReceived); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Meshtastic.Cli.Binders; | ||
using Meshtastic.Cli.CommandHandlers; | ||
using Meshtastic.Cli.Enums; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Meshtastic.Cli.Commands; | ||
|
||
public class CaptureCommand : Command | ||
{ | ||
public CaptureCommand(string name, string description, Option<string> port, Option<string> host, | ||
Option<OutputFormat> output, Option<LogLevel> log) : base(name, description) | ||
{ | ||
this.SetHandler(async (context, commandContext) => | ||
{ | ||
var handler = new CaptureCommandHandler(context, commandContext); | ||
await handler.Handle(); | ||
}, | ||
new DeviceConnectionBinder(port, host), | ||
new CommandContextBinder(log, output, new Option<uint?>("dest") { }, new Option<bool>("select-dest") { })); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Meshtastic.Cli.Binders; | ||
using Meshtastic.Cli.CommandHandlers; | ||
using Meshtastic.Cli.Enums; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Meshtastic.Cli.Commands; | ||
|
||
public class RemoveNodeCommand : Command | ||
{ | ||
public RemoveNodeCommand(string name, string description, Option<string> port, Option<string> host, | ||
Option<OutputFormat> output, Option<LogLevel> log, Option<uint?> dest, Option<bool> selectDest) : | ||
base(name, description) | ||
{ | ||
var nodeNumArgument = new Argument<uint>("nodenum", "Nodenum of the node to remove from the device NodeDB"); | ||
AddArgument(nodeNumArgument); | ||
|
||
this.SetHandler(async (nodeNum, context, commandContext) => | ||
{ | ||
var handler = new RemoveNodeCommandHandler(nodeNum, context, commandContext); | ||
await handler.Handle(); | ||
}, | ||
nodeNumArgument, | ||
new DeviceConnectionBinder(port, host), | ||
new CommandContextBinder(log, output, dest, selectDest)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
using Google.Protobuf; | ||
using Meshtastic.Protobufs; | ||
using System.Linq; | ||
|
||
namespace Meshtastic.Data; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.