Skip to content

Commit

Permalink
Everything but the kitchen sink...
Browse files Browse the repository at this point in the history
Fix UI Issues.
Fix button names.
Add toggles for streaming.
Work on method to properly start/stop streaming on toggle.
Fix async issues, cleanup, etc.
Optimizations.
Beautifcation.
I need a vacation...
  • Loading branch information
d8ahazard committed Nov 18, 2019
1 parent 6674eeb commit 315fb99
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 133 deletions.
26 changes: 16 additions & 10 deletions Controllers/HueDataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ namespace HueDream.Controllers {
[Route("api/[controller]")]
[ApiController]
public class HueDataController : ControllerBase {
private DreamSync ds;
private DreamData userData;

public HueDataController() {
ds = new DreamSync();
userData = new DreamData();
CheckSync();
}

// GET: api/HueData/action?action=...
[HttpGet("action")]
public JsonResult Get(string action) {
string message = "Unrecognized action";
DreamData userData = new DreamData();
Console.Write("Now we're cooking: " + action);
if (action == "connectDreamScreen") {
string dsIp = userData.DS_IP;
Expand Down Expand Up @@ -72,13 +80,11 @@ public JsonResult Get(string action) {
// GET: api/HueData/json
[HttpGet("json")]
public IActionResult Get() {
DreamData userData = new DreamData();
return Ok(userData);
}

[HttpGet("lights")]
public IActionResult GetWhatever() {
DreamData userData = new DreamData();
return Ok(userData);
}

Expand All @@ -92,13 +98,14 @@ public static string Get(int id) {
// POST: api/HueData
[HttpPost]
public void Post(string value) {
DreamData userData = new DreamData();
string[] keys = Request.Form.Keys.ToArray<string>();
Console.WriteLine("We have a post: " + value);
bool mapLights = false;
List<KeyValuePair<int, string>> lightMap = new List<KeyValuePair<int, string>>();
foreach (string key in keys) {
Console.WriteLine("We have a key and value: " + key + " " + Request.Form[key]);
if (key.Contains("lightMap")) {
mapLights = true;
int lightId = int.Parse(key.Replace("lightMap", ""));
lightMap.Add(new KeyValuePair<int, string>(lightId, Request.Form[key]));
} else if (key == "ds_ip") {
Expand All @@ -108,28 +115,27 @@ public void Post(string value) {
} else if (key == "hue_sync") {
string val = Request.Form[key];
bool res = false;
if (val == "on") {
if (val == "true") {
res = true;
} else {
res = false;
}
userData.HUE_SYNC = res;
}
}
userData.HUE_MAP = lightMap;
if (mapLights) userData.HUE_MAP = lightMap;
userData.saveData();
CheckSync(userData);
CheckSync();
}

private static void CheckSync(DreamData userData) {
DreamSync ds = new DreamSync();
private void CheckSync() {
if (userData.DS_IP != "0.0.0.0" && userData.HUE_SYNC && !ds.doSync) {
Console.WriteLine("Starting Dreamscreen sync!");
Task.Run(() => ds.startSync());
} else if (!userData.HUE_SYNC && ds.doSync) {
Console.WriteLine("Stopping sync.");
ds.StopSync();
}
}
}
}
}
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ RUN dotnet build "HueDream.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "HueDream.csproj" -c Release -o /app/publish

RUN mkdir -p /etc/huedream
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "HueDream.dll"]
ENTRYPOINT ["dotnet", "HueDream.dll"]

VOLUME /etc/huedream
35 changes: 21 additions & 14 deletions DreamScreenControl/DreamScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace HueDream.DreamScreenControl {
class DreamScreen {
Expand Down Expand Up @@ -34,6 +33,7 @@ class DreamScreen {
private Socket dreamScreenSocket;
private int Port = 8888;
private IPEndPoint endPoint;
private IPEndPoint receiverPort;
private byte groupNumber = 0;
private UdpClient receiver;
public bool listening = false;
Expand All @@ -47,7 +47,7 @@ public DreamScreen(string remoteIP) {
for (int i = 0; i < colors.Length; i++) {
colors[i] = new string[] { "FF", "FF", "FF" };
}
dreamScreenSocket = new Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
dreamScreenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
dreamScreenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, (int)1);
//getMode();
}
Expand All @@ -63,15 +63,16 @@ public void getMode() {
Listen();
}

public void startListening() {
public void StartListening() {
Console.WriteLine("Listen called.");
sendUDPWrite((byte)0x01, (byte)0x0C, new byte[] { (byte)0x01 }, (byte)0x10);
Listen();
}

public void Listen() {
if (!listening) {
// Create UDP client
IPEndPoint receiverPort = new IPEndPoint(IPAddress.Any, 8888); ;
receiverPort = new IPEndPoint(IPAddress.Any, 8888); ;
receiver = new UdpClient();
receiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
receiver.EnableBroadcast = true;
Expand All @@ -84,8 +85,8 @@ public void Listen() {
}

public void StopListening() {
Console.WriteLine("Listening canceled.");
if (listening) {
dreamScreenSocket.Close();
listening = false;
}
}
Expand Down Expand Up @@ -133,7 +134,12 @@ private void DataReceived(IAsyncResult ar) {
}

// Restart listening for udp data packages
c.BeginReceive(DataReceived, ar.AsyncState);
if (listening) {
c.BeginReceive(DataReceived, ar.AsyncState);
} else {
Console.WriteLine("Closing listening socket.");
dreamScreenSocket.Close();
}
}


Expand Down Expand Up @@ -196,9 +202,9 @@ void sendUDPWrite(byte command1, byte command2, byte[] payload, byte flag = (byt

var byteSend = stream.ToArray();
// CRC
response.Write(calculateCrc(byteSend));
response.Write(CalculateCrc(byteSend));
string msg = BitConverter.ToString(stream.ToArray());
sendUDPUnicast(stream.ToArray());
SendUDPUnicast(stream.ToArray());
}
}
}
Expand All @@ -210,6 +216,7 @@ void requestState() {
using (BinaryWriter response = new BinaryWriter(stream)) {
// Magic header
response.Write((byte)0xFC);
// Hacky implementation, but itttt works
response.Write((byte)0x05);
response.Write((byte)0xFF);
response.Write((byte)0x30);
Expand All @@ -218,14 +225,14 @@ void requestState() {
response.Write((byte)0x2A);
var byteSend = stream.ToArray();
// CRC
response.Write(calculateCrc(byteSend));
response.Write(CalculateCrc(byteSend));
string msg = BitConverter.ToString(stream.ToArray());
sendUDPUnicast(stream.ToArray());
SendUDPUnicast(stream.ToArray());
}
}
}

private byte calculateCrc(byte[] data) {
private byte CalculateCrc(byte[] data) {
byte size = (byte)(data[1] + 1);
byte crc = (byte)0;
for (byte cntr = (byte)0; cntr < size; cntr = (byte)(cntr + 1)) {
Expand All @@ -234,7 +241,7 @@ private byte calculateCrc(byte[] data) {
return crc;
}

private void sendUDPUnicast(byte[] data) {
private void SendUDPUnicast(byte[] data) {
dreamScreenSocket.SendTo(data, endPoint);
}

Expand All @@ -250,7 +257,7 @@ public DreamScreenState(string byteString) {
if (magic == "FC") {
string type = bytesIn[bytesIn.Length - 2];
string state = bytesIn[33];

if (type == "01") {
typeString = "DreamScreen";
} else if (type == "02") {
Expand All @@ -274,5 +281,5 @@ public DreamScreenState(string byteString) {
}
}


}
Loading

0 comments on commit 315fb99

Please sign in to comment.