Skip to content

Commit

Permalink
Fix for IDevice interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nzbr committed Oct 12, 2020
1 parent e4184b4 commit d5c02a6
Showing 1 changed file with 41 additions and 68 deletions.
109 changes: 41 additions & 68 deletions Project-Aurora/Project-Aurora/Devices/UDP/UDPDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Aurora.Devices.UDP
{
public class UDPDevice : Device
public class UdpDevice : IDevice
{
private const string deviceName = "UDP";
private bool isInitialized = false;
public string DeviceName => "UDP";
public bool IsInitialized { get; private set; } = false;

private VariableRegistry defaultRegistry = null;

Expand All @@ -31,67 +28,60 @@ public class UDPDevice : Device
private Color lastColor;
private long lastUpdateTime = long.MaxValue;

public string GetDeviceDetails()
{
return deviceName + (isInitialized ? ": Initialized" : ": Not initialized");
}
public string DeviceDetails => DeviceName + (IsInitialized ? ": Initialized" : ": Not initialized");
public string DeviceUpdatePerformance => (IsInitialized ? lastUpdateTime + " ms" : "");

public string GetDeviceName()
public VariableRegistry RegisteredVariables
{
return deviceName;
get
{
if (defaultRegistry == null)
{
var devKeysEnumAsEnumerable = Enum.GetValues(typeof(DeviceKeys)).Cast<DeviceKeys>();

defaultRegistry = new VariableRegistry();
defaultRegistry.Register($"{DeviceName}_devicekey", DeviceKeys.Peripheral, "Key Color to Use",
devKeysEnumAsEnumerable.Max(), devKeysEnumAsEnumerable.Min());
defaultRegistry.Register($"{DeviceName}_led_count", 300, "LED Count");
defaultRegistry.Register($"{DeviceName}_ip", "", "IP Adresses (Comma separated)");
defaultRegistry.Register($"{DeviceName}_port", 19446, "UDP Port");
}

return defaultRegistry;
}
}


public bool Initialize()
{
if (isInitialized) return true;
if (IsInitialized) return true;

deviceKey = Global.Configuration.VarRegistry.GetVariable<DeviceKeys>($"{deviceName}_devicekey");
ledCount = Global.Configuration.VarRegistry.GetVariable<int>($"{deviceName}_led_count");
deviceKey = Global.Configuration.VarRegistry.GetVariable<DeviceKeys>($"{DeviceName}_devicekey");
ledCount = Global.Configuration.VarRegistry.GetVariable<int>($"{DeviceName}_led_count");

var udpPort = Global.Configuration.VarRegistry.GetVariable<int>($"{deviceName}_port");
var ips = Global.Configuration.VarRegistry.GetVariable<string>($"{deviceName}_ip").Split(',');
var udpPort = Global.Configuration.VarRegistry.GetVariable<int>($"{DeviceName}_port");
var ips = Global.Configuration.VarRegistry.GetVariable<string>($"{DeviceName}_ip").Split(',');

endpoints = new List<IPEndPoint>();
foreach (var ip in ips)
{
try
{
endpoints.Add(new IPEndPoint(IPAddress.Parse(ip), udpPort));
} catch (FormatException) {} // Don't crash on malformed IPs
}
catch (FormatException)
{
} // Don't crash on malformed IPs
}

udpClient = new UdpClient();
lastColor = Color.Black;
redrawWatch.Start();

isInitialized = true;
IsInitialized = true;
return true;
}

public bool IsConnected()
{
return isInitialized; // UDP has no connections
}

public bool IsInitialized()
{
return isInitialized;
}

public bool IsKeyboardConnected()
{
return (isInitialized && deviceKey != DeviceKeys.Peripheral);
}

public bool IsPeripheralConnected()
{
return (isInitialized && deviceKey == DeviceKeys.Peripheral);
}

public bool Reconnect()
{
return true; // UDP has no connections
}

public void Reset()
{
Expand All @@ -106,7 +96,7 @@ public void Shutdown()
udpClient = null;
redrawWatch.Stop();

isInitialized = false;
IsInitialized = false;
}

public bool UpdateDevice(DeviceColorComposition colorComposition, DoWorkEventArgs e, bool forced = false)
Expand All @@ -116,26 +106,28 @@ public bool UpdateDevice(DeviceColorComposition colorComposition, DoWorkEventArg

public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, DoWorkEventArgs e, bool forced = false)
{
if (!isInitialized) return false;
if (!IsInitialized) return false;

if (keyColors.ContainsKey(deviceKey))
{
updateStopwatch.Restart();

var c = keyColors[deviceKey];
if (redrawWatch.ElapsedMilliseconds < 1000) // Only send the color when it changes or a full second has passed
if (redrawWatch.ElapsedMilliseconds < 1000
) // Only send the color when it changes or a full second has passed
{
if (c == lastColor && !forced) return true;
}
else
{
redrawWatch.Restart();
}

lastColor = c;

// Build a payload by repeating the color ledCount times
var payload = new byte[3 * ledCount];
for (int i = 0; i < ledCount * 3; i += 3)
for (var i = 0; i < ledCount * 3; i += 3)
{
payload[i + 0] = c.R;
payload[i + 1] = c.G;
Expand All @@ -151,27 +143,8 @@ public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, DoWorkEventArg
updateStopwatch.Stop();
lastUpdateTime = updateStopwatch.ElapsedMilliseconds;
}
return true;
}

public string GetDeviceUpdatePerformance()
{
return (isInitialized ? lastUpdateTime + " ms" : "");
}

public VariableRegistry GetRegisteredVariables()
{
if (defaultRegistry == null)
{
var devKeysEnumAsEnumerable = Enum.GetValues(typeof(DeviceKeys)).Cast<DeviceKeys>();

defaultRegistry = new VariableRegistry();
defaultRegistry.Register($"{deviceName}_devicekey", DeviceKeys.Peripheral, "Key Color to Use", devKeysEnumAsEnumerable.Max(), devKeysEnumAsEnumerable.Min());
defaultRegistry.Register($"{deviceName}_led_count", 300, "LED Count");
defaultRegistry.Register($"{deviceName}_ip", "", "IP Adresses (Comma separated)");
defaultRegistry.Register($"{deviceName}_port", 19446, "UDP Port");
}
return defaultRegistry;
return true;
}
}
}
}

0 comments on commit d5c02a6

Please sign in to comment.