Skip to content

Commit

Permalink
Merge pull request #97 from meshtastic/wip
Browse files Browse the repository at this point in the history
Protos
  • Loading branch information
thebentern authored Oct 30, 2024
2 parents 31a5c9c + 0f99116 commit cba77e3
Show file tree
Hide file tree
Showing 7 changed files with 531 additions and 176 deletions.
52 changes: 52 additions & 0 deletions Meshtastic.Cli/CommandHandlers/RegisterCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Security.Cryptography;
using Meshtastic.Data;
using Meshtastic.Data.MessageFactories;
using Meshtastic.Protobufs;
using Microsoft.Extensions.Logging;
using Spectre.Console.Json;
using Newtonsoft.Json;

namespace Meshtastic.Cli.CommandHandlers;

public class RegisterCommandHandlerCommandHandler(DeviceConnectionContext context, CommandContext commandContext) : DeviceCommandHandler(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)
{
Logger.LogInformation("Getting registration info...");
var key = container.MyNodeInfo.DeviceId;
var user = container.Nodes.Find(n => n.Num == container.MyNodeInfo.MyNodeNum)?.User;
#pragma warning disable CS0612 // Type or member is obsolete
var macAddress = user?.Macaddr;
#pragma warning restore CS0612 // Type or member is obsolete
if (key == null || key.All(b => b == 0) || user == null || macAddress == null)
{
Logger.LogError("Device does not have a valid key or mac address, and cannot be registered.");
return;
}
var jsonForm = JsonConvert.SerializeObject(new
{
MeshtasticDeviceId = Convert.ToHexString(key.ToByteArray()),
MACAddress = Convert.ToHexString(macAddress.ToByteArray()),
DeviceHardwareId = container.Metadata.HwModel,
container.Metadata.FirmwareVersion,
});

var json = new JsonText(jsonForm);

AnsiConsole.Write( new Panel(json)
.Header("Registration Information")
.Collapse()
.RoundedBorder()
.BorderColor(Color.Blue));

await Task.CompletedTask;
}
}
21 changes: 21 additions & 0 deletions Meshtastic.Cli/Commands/RegisterCommand.cs
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 RegisterCommand : Command
{
public RegisterCommand(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 RegisterCommandHandlerCommandHandler(context, commandContext);
await handler.Handle();
},
new DeviceConnectionBinder(port, host),
new CommandContextBinder(log, output, new Option<uint?>("dest"), new Option<bool>("select-dest")));
}
}
4 changes: 3 additions & 1 deletion Meshtastic.Cli/Meshtastic.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
<PackageReference Include="MQTTnet" Version="4.3.3.952" />
<PackageReference Include="QRCoder" Version="1.4.3" />
<PackageReference Include="SimpleExec" Version="12.0.0" />
<PackageReference Include="Spectre.Console" Version="0.48.0" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="Spectre.Console.Json" Version="0.49.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
<PackageReference Include="YamlDotNet" Version="15.1.2" />
Expand Down
1 change: 1 addition & 0 deletions Meshtastic.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
root.AddCommand(new ChannelCommand("channel", "Enable, Disable, Add, Save channels on the device", port, host, output, log, dest, selectDest));
root.AddCommand(new UrlCommand("url", "Get or set shared channel url", port, host, output, log));
root.AddCommand(new RebootCommand("reboot", "Reboot the device", port, host, output, log, dest, selectDest));
root.AddCommand(new RegisterCommand("register", "Print registration info for the device", port, host, output, log));
root.AddCommand(new MetadataCommand("metadata", "Get device metadata from the device", port, host, output, log, dest, selectDest));
root.AddCommand(new FactoryResetCommand("factory-reset", "Factory reset configuration of the device", port, host, output, log, dest, selectDest));
root.AddCommand(new FixedPositionCommand("fixed-position", "Set the device to a fixed position", port, host, output, log, dest, selectDest));
Expand Down
Loading

0 comments on commit cba77e3

Please sign in to comment.