-
-
Notifications
You must be signed in to change notification settings - Fork 9
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 #42 from meshtastic/wip
Wip
- Loading branch information
Showing
10 changed files
with
217 additions
and
104 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
Meshtastic.Cli/CommandHandlers/RequestTelemetryCommandHandler.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,40 @@ | ||
using Meshtastic.Data; | ||
using Meshtastic.Data.MessageFactories; | ||
using Meshtastic.Protobufs; | ||
using Meshtastic.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Meshtastic.Cli.CommandHandlers; | ||
|
||
public class RequestTelemetryCommandHandler : DeviceCommandHandler | ||
{ | ||
public RequestTelemetryCommandHandler(DeviceConnectionContext context, | ||
CommandContext commandContext) : base(context, commandContext) | ||
{ | ||
} | ||
|
||
public async Task<DeviceStateContainer> Handle() | ||
{ | ||
if (Destination is null) | ||
throw new ApplicationException("Destination must be specified to request telemetry"); | ||
|
||
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) | ||
{ | ||
var telemetryMessageFactory = new TelemetryMessageFactory(container, Destination); | ||
var message = telemetryMessageFactory.CreateTelemetryPacket(); | ||
await Connection.WriteToRadio(ToRadioMessageFactory.CreateMeshPacketMessage(message), async (packet, container) => | ||
{ | ||
if (packet.Packet.Decoded.RequestId > 0 && packet.Packet.From == Destination!.Value && packet.GetPayload<Telemetry>()?.DeviceMetrics is not null) { | ||
var metrics = packet.GetPayload<Telemetry>()?.DeviceMetrics; | ||
Logger.LogInformation($"Received telemetry from destination ({Destination.Value}): \n{metrics}"); | ||
} | ||
return await Task.FromResult(false); | ||
}); | ||
} | ||
} |
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 RequestTelemetryCommand : Command | ||
{ | ||
public RequestTelemetryCommand(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) | ||
{ | ||
this.SetHandler(async (context, commandContext) => | ||
{ | ||
var handler = new RequestTelemetryCommandHandler(context, commandContext); | ||
await handler.Handle(); | ||
}, | ||
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
42 changes: 42 additions & 0 deletions
42
Meshtastic/Data/MessageFactories/TelemetryMessageFactory.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,42 @@ | ||
using Google.Protobuf; | ||
using Meshtastic.Protobufs; | ||
using Meshtastic.Extensions; | ||
|
||
namespace Meshtastic.Data.MessageFactories; | ||
|
||
public class TelemetryMessageFactory | ||
{ | ||
private readonly DeviceStateContainer container; | ||
private readonly uint? dest; | ||
|
||
public TelemetryMessageFactory(DeviceStateContainer container, uint? dest = null) | ||
{ | ||
this.container = container; | ||
this.dest = dest; | ||
} | ||
|
||
public MeshPacket CreateTelemetryPacket(uint channel = 0) | ||
{ | ||
var telemetry = container?.FromRadioMessageLog | ||
.Where(fromRadio => fromRadio.Packet?.From == container.MyNodeInfo?.MyNodeNum) | ||
.FirstOrDefault(fromRadio => fromRadio.GetPayload<Telemetry>()?.DeviceMetrics != null)?.GetPayload<Telemetry>()?.DeviceMetrics; | ||
|
||
return new MeshPacket() | ||
{ | ||
Channel = channel, | ||
WantAck = true, | ||
To = dest ?? 0, | ||
Id = (uint)Math.Floor(Random.Shared.Next() * 1e9), | ||
HopLimit = container?.GetHopLimitOrDefault() ?? 3, | ||
Decoded = new Protobufs.Data() | ||
{ | ||
Portnum = PortNum.TelemetryApp, | ||
Payload = new Telemetry() | ||
{ | ||
DeviceMetrics = telemetry ?? new DeviceMetrics() | ||
}.ToByteString(), | ||
WantResponse = true, | ||
}, | ||
}; | ||
} | ||
} |
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.