|
| 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 | +} |
0 commit comments