-
-
Notifications
You must be signed in to change notification settings - Fork 72
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 #525 from Macro-Deck-App/qr-code
Add QR Code for Quick Setup
- Loading branch information
Showing
7 changed files
with
228 additions
and
155 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace SuchByte.MacroDeck.DataTypes.QrCode; | ||
|
||
public class QuickConnectQrCodeData | ||
{ | ||
public string InstanceName { get; set; } | ||
|
||
public List<string> NetworkInterfaces { get; set; } | ||
|
||
public int Port { get; set; } | ||
|
||
public bool Ssl { get; set; } | ||
|
||
public QuickConnectQrCodeData(string instanceName, List<string> networkInterfaces, int port, bool ssl) | ||
{ | ||
InstanceName = instanceName; | ||
NetworkInterfaces = networkInterfaces; | ||
Port = port; | ||
Ssl = ssl; | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.IO; | ||
using System.Net.NetworkInformation; | ||
using System.Net.Sockets; | ||
using System.Text.Json; | ||
using QRCoder; | ||
using SuchByte.MacroDeck.DataTypes.QrCode; | ||
using SuchByte.MacroDeck.Logging; | ||
|
||
namespace SuchByte.MacroDeck.Services; | ||
|
||
public class QrCodeService | ||
{ | ||
public static readonly QrCodeService Instance = new(); | ||
|
||
private byte[]? _quickSetupQrCode; | ||
|
||
public Image GetQuickSetupQrCode() | ||
{ | ||
if (_quickSetupQrCode is not null) | ||
{ | ||
return FromSavedBytes(); | ||
} | ||
|
||
var networkInterfaces = new List<string>(); | ||
try | ||
{ | ||
networkInterfaces.AddRange(NetworkInterface.GetAllNetworkInterfaces() | ||
.Select(adapter => adapter.GetIPProperties() | ||
.UnicastAddresses.FirstOrDefault(x => x.Address.AddressFamily == AddressFamily.InterNetwork) | ||
?.Address.ToString()) | ||
.Where(address => !string.IsNullOrWhiteSpace(address) && address != "127.0.0.1")); | ||
} | ||
catch (Exception ex) | ||
{ | ||
MacroDeckLogger.Warning($"Error while searching for network interfaces\n{ex.Message}"); | ||
} | ||
|
||
var data = new QuickConnectQrCodeData(Environment.MachineName, | ||
networkInterfaces, | ||
MacroDeck.Configuration.HostPort, | ||
MacroDeck.Configuration.EnableSsl); | ||
|
||
var dataJson = JsonSerializer.Serialize(data); | ||
var dataBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(dataJson)); | ||
var qrCodeLink = $"https://macro-deck.app/quick-setup/{dataBase64}"; | ||
|
||
using var qrGenerator = new QRCodeGenerator(); | ||
using var qrCodeData = qrGenerator.CreateQrCode(qrCodeLink, QRCodeGenerator.ECCLevel.L); | ||
using var qrCode = new BitmapByteQRCode(qrCodeData); | ||
|
||
_quickSetupQrCode = qrCode.GetGraphic(20); | ||
return FromSavedBytes(); | ||
|
||
Image FromSavedBytes() | ||
{ | ||
using var ms = new MemoryStream(_quickSetupQrCode); | ||
return Image.FromStream(ms); | ||
} | ||
} | ||
} |