Skip to content

Commit cba77e3

Browse files
authored
Merge pull request #97 from meshtastic/wip
Protos
2 parents 31a5c9c + 0f99116 commit cba77e3

File tree

7 files changed

+531
-176
lines changed

7 files changed

+531
-176
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Security.Cryptography;
2+
using Meshtastic.Data;
3+
using Meshtastic.Data.MessageFactories;
4+
using Meshtastic.Protobufs;
5+
using Microsoft.Extensions.Logging;
6+
using Spectre.Console.Json;
7+
using Newtonsoft.Json;
8+
9+
namespace Meshtastic.Cli.CommandHandlers;
10+
11+
public class RegisterCommandHandlerCommandHandler(DeviceConnectionContext context, CommandContext commandContext) : DeviceCommandHandler(context, commandContext)
12+
{
13+
public async Task<DeviceStateContainer> Handle()
14+
{
15+
var wantConfig = new ToRadioMessageFactory().CreateWantConfigMessage();
16+
var container = await Connection.WriteToRadio(wantConfig, CompleteOnConfigReceived);
17+
Connection.Disconnect();
18+
return container;
19+
}
20+
21+
public override async Task OnCompleted(FromRadio packet, DeviceStateContainer container)
22+
{
23+
Logger.LogInformation("Getting registration info...");
24+
var key = container.MyNodeInfo.DeviceId;
25+
var user = container.Nodes.Find(n => n.Num == container.MyNodeInfo.MyNodeNum)?.User;
26+
#pragma warning disable CS0612 // Type or member is obsolete
27+
var macAddress = user?.Macaddr;
28+
#pragma warning restore CS0612 // Type or member is obsolete
29+
if (key == null || key.All(b => b == 0) || user == null || macAddress == null)
30+
{
31+
Logger.LogError("Device does not have a valid key or mac address, and cannot be registered.");
32+
return;
33+
}
34+
var jsonForm = JsonConvert.SerializeObject(new
35+
{
36+
MeshtasticDeviceId = Convert.ToHexString(key.ToByteArray()),
37+
MACAddress = Convert.ToHexString(macAddress.ToByteArray()),
38+
DeviceHardwareId = container.Metadata.HwModel,
39+
container.Metadata.FirmwareVersion,
40+
});
41+
42+
var json = new JsonText(jsonForm);
43+
44+
AnsiConsole.Write( new Panel(json)
45+
.Header("Registration Information")
46+
.Collapse()
47+
.RoundedBorder()
48+
.BorderColor(Color.Blue));
49+
50+
await Task.CompletedTask;
51+
}
52+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Meshtastic.Cli.Binders;
2+
using Meshtastic.Cli.CommandHandlers;
3+
using Meshtastic.Cli.Enums;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace Meshtastic.Cli.Commands;
7+
8+
public class RegisterCommand : Command
9+
{
10+
public RegisterCommand(string name, string description, Option<string> port, Option<string> host,
11+
Option<OutputFormat> output, Option<LogLevel> log) : base(name, description)
12+
{
13+
this.SetHandler(async (context, commandContext) =>
14+
{
15+
var handler = new RegisterCommandHandlerCommandHandler(context, commandContext);
16+
await handler.Handle();
17+
},
18+
new DeviceConnectionBinder(port, host),
19+
new CommandContextBinder(log, output, new Option<uint?>("dest"), new Option<bool>("select-dest")));
20+
}
21+
}

Meshtastic.Cli/Meshtastic.Cli.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
<PackageReference Include="MQTTnet" Version="4.3.3.952" />
4444
<PackageReference Include="QRCoder" Version="1.4.3" />
4545
<PackageReference Include="SimpleExec" Version="12.0.0" />
46-
<PackageReference Include="Spectre.Console" Version="0.48.0" />
46+
<PackageReference Include="Spectre.Console" Version="0.49.1" />
47+
<PackageReference Include="Spectre.Console.Json" Version="0.49.1" />
48+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
4749
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
4850
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
4951
<PackageReference Include="YamlDotNet" Version="15.1.2" />

Meshtastic.Cli/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
root.AddCommand(new ChannelCommand("channel", "Enable, Disable, Add, Save channels on the device", port, host, output, log, dest, selectDest));
4949
root.AddCommand(new UrlCommand("url", "Get or set shared channel url", port, host, output, log));
5050
root.AddCommand(new RebootCommand("reboot", "Reboot the device", port, host, output, log, dest, selectDest));
51+
root.AddCommand(new RegisterCommand("register", "Print registration info for the device", port, host, output, log));
5152
root.AddCommand(new MetadataCommand("metadata", "Get device metadata from the device", port, host, output, log, dest, selectDest));
5253
root.AddCommand(new FactoryResetCommand("factory-reset", "Factory reset configuration of the device", port, host, output, log, dest, selectDest));
5354
root.AddCommand(new FixedPositionCommand("fixed-position", "Set the device to a fixed position", port, host, output, log, dest, selectDest));

0 commit comments

Comments
 (0)