Skip to content

Commit

Permalink
Add sample code for decoding string and binary payloads (#66)
Browse files Browse the repository at this point in the history
* Add sample for decoding binary payload

- Add ByteArrayToString method
- Add example decoders for string and binary payloads
- Remove unnecessary using statements

* Update LoraDecoders.cs

- Move both examples to one sample decoder: DecoderValueSensor
  • Loading branch information
TechPreacher authored and ronniesa committed Dec 18, 2018
1 parent 6232bea commit 76baf91
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 26 deletions.
36 changes: 36 additions & 0 deletions Samples/DecoderSample/Classes/ConversionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Text;

namespace SensorDecoderModule.Classes
{
public static class ConversionHelper
{
/// <summary>
/// Method enabling to convert a hex string to a byte array.
/// </summary>
/// <param name="hex">Input hex string</param>
/// <returns></returns>
public static byte[] StringToByteArray(string hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}

public static string ByteArrayToString(byte[] bytes)
{
StringBuilder Result = new StringBuilder(bytes.Length * 2);
string HexAlphabet = "0123456789ABCDEF";

foreach (byte B in bytes)
{
Result.Append(HexAlphabet[(int)(B >> 4)]);
Result.Append(HexAlphabet[(int)(B & 0xF)]);
}

return Result.ToString();
}
}
}
21 changes: 12 additions & 9 deletions Samples/DecoderSample/Classes/LoraDecoders.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;
using Newtonsoft.Json;

namespace SensorDecoderModule.Classes
Expand All @@ -12,8 +7,16 @@ internal static class LoraDecoders
{
private static string DecoderValueSensor(byte[] payload, uint fport)
{
var result = Encoding.UTF8.GetString(payload);
// EITHER: Convert a payload containing a string back to string format for further processing
var result = Encoding.UTF8.GetString(payload);

// OR: Convert a payload containing binary data to HEX string for further processing
var result_binary = ConversionHelper.ByteArrayToString(payload);

// Decode the payload

// Return a JSON string containing the decoded data
return JsonConvert.SerializeObject(new { value = result });
}
}
}
}
}
6 changes: 1 addition & 5 deletions Samples/DecoderSample/Classes/Validator.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Net;

namespace SensorDecoderModule.Classes
{
Expand Down
6 changes: 0 additions & 6 deletions Samples/DecoderSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace SensorDecoderModule
{
Expand Down
6 changes: 0 additions & 6 deletions Samples/DecoderSample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace SensorDecoderModule
{
Expand Down

0 comments on commit 76baf91

Please sign in to comment.