From 98daa35b46d864ba31d7b0df64008502ff2b2690 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 17 Nov 2016 19:36:17 +0000
Subject: [PATCH] Bugfix the pixelhub client code - it WORKS!!!!!!

---
 PixelHub-Client/PixelBot.cpp        | 45 +++++++++++++++++++++++++++--
 PixelHub-Client/PixelBot.h          | 39 +++++++++++++++++++++----
 PixelHub-Client/PixelHub-Client.ino |  3 +-
 PixelHub-Client/Utilities.cpp       | 14 +++++++++
 PixelHub-Client/Utilities.h         |  8 +++++
 5 files changed, 100 insertions(+), 9 deletions(-)

diff --git a/PixelHub-Client/PixelBot.cpp b/PixelHub-Client/PixelBot.cpp
index 279c0b0..23f3504 100644
--- a/PixelHub-Client/PixelBot.cpp
+++ b/PixelHub-Client/PixelBot.cpp
@@ -95,11 +95,50 @@ void PixelBot::FindServer()
 		Serial.println();
 		
 		// If the advertiser isn't playing the role of a server, then we're not interested
-		if(role != "server") continue;
-
+		if(strcmp(role, desiredRemoteRole) != 0)
+		{
+			Serial.print("Incompatible role "); Serial.print(role); Serial.println(".");
+			continue;
+		}
+		Serial.println("Role ok!");
 		serverPort = atoi(serverPortText);
-
+		
 		return;
 	}
 }
 
+bool PixelBot::Connect()
+{
+	Serial.print("Connecting to "); Serial.print(serverIp); Serial.print(":"); Serial.print(serverPort); Serial.print(" - ");
+	if(!tcpClient.connect(serverIp, serverPort)) {
+		Serial.println("failed!");
+		return false;
+	}
+	Serial.println("success!");
+	return true;
+}
+
+void PixelBot::Listen()
+{
+	while(true) {
+		if(!Tick()) {
+			Serial.println("[PixelBot/Server] Exiting main loop.");
+			break;
+		}
+	}
+}
+
+bool PixelBot::Tick()
+{
+	// Make sure that there's something to read
+	if(tcpClient.available() == 0) {
+		delay(10);
+		return true;
+	}
+	
+	String nextLine = tcpClient.readStringUntil('\n');
+	Serial.print(" > "); Serial.println(nextLine);
+
+	return true;
+}
+
diff --git a/PixelHub-Client/PixelBot.h b/PixelHub-Client/PixelBot.h
index fe39097..446bb7f 100644
--- a/PixelHub-Client/PixelBot.h
+++ b/PixelHub-Client/PixelBot.h
@@ -3,31 +3,60 @@
 #include <ESP8266WiFi.h>
 #include <WiFiUdp.h>
 
+/// <summary>
+/// Connects to a PixelHub server, receives commands, and acts upon them.
+/// </summmary>
 class PixelBot
 {
 public:
 	PixelBot();
 	~PixelBot();
-
+	
+	/// <summary>
+	/// Listens for beacon pings that reveal the location of the PixelHub server.
+	/// </summary>
+	/// <description>
+	/// Upon receiving a ping it parses the message to figure out the location of the PixelHub server.
+	/// Once done it fills out a few private variables ready for the TCP connection.
+	/// </description>
 	void FindServer();
+	
+	/// <summary>
+	/// Connects the TCP transport connection to the PixelHub server.
+	/// </summary>
+	bool Connect();
+	/// <summary>
+	/// Starts the main listening loop that handles commands from the PixelHub server.
+	/// </summary>
+	void Listen();
+	/// <summary>
+	/// Performs a single tick of the listen loop.
+	/// Very useful if you want to do other things at the same time as handling commands from the PixelHub server.
+	/// </summary>
+	bool Tick();
   
 private:
+	// ~~~ Manual Settings ~~~ //
+	char desiredRemoteRole[7] = { 's', 'e', 'r', 'v', 'e', 'r', '\0' };
+	// ~~~ Automatic Settings ~~~ //
+	// The IP address of the remote PixelHub server. Autofilled by the FindServer() method.
 	char serverIp[16];
+	// The port number that the PixelHub server is running on. Autofilled by the FindServer() method.
 	int serverPort = -1;
 	
 	// ~~~ Auto Discovery System ~~~ //
 	
-	// The address that the PixelHub beacon is broadcasting on.
+	// The multicast address that the PixelHub beacon is broadcasting on.
 	IPAddress beaconAddress = IPAddress(239, 62, 148, 30);
-	// The port number that the PixelHub beacon is broadcasting on.
+	// The multicast port number that the PixelHub beacon is broadcasting on.
 	unsigned int beaconPort = 5050;
 	
 	// The size of the datagram buffer that is used to buffer incoming messages.
 	int datagramBufferSize = 256;
 
-	// ~~~   ~~~ //
+	// ~~~ PixelHub Server Client ~~~ //
 	
-	// The TCP client
+	// The TCP client to use as the PixelHub server transport connection.
 	WiFiClient tcpClient = WiFiClient();
 };
 
diff --git a/PixelHub-Client/PixelHub-Client.ino b/PixelHub-Client/PixelHub-Client.ino
index 94c84b8..574e87b 100644
--- a/PixelHub-Client/PixelHub-Client.ino
+++ b/PixelHub-Client/PixelHub-Client.ino
@@ -37,12 +37,13 @@ void setup()
 	
 	printWiFiInfoLocal();
 	pixelBot.FindServer();
+	pixelBot.Connect();
 }
 
 
 void loop()
 {
-	
+	pixelBot.Tick();
 }
 
 
diff --git a/PixelHub-Client/Utilities.cpp b/PixelHub-Client/Utilities.cpp
index f9a057a..3f7c949 100644
--- a/PixelHub-Client/Utilities.cpp
+++ b/PixelHub-Client/Utilities.cpp
@@ -1,5 +1,9 @@
 //#include <string.h>
 //#include <algorithm>
+#include <stdlib.h>
+
+#include "Utilities.h"
+#include "Arduino.h"
 
 int findChar(char* str, char targetChar)
 {
@@ -16,3 +20,13 @@ int getPosition(const char *array, size_t size, char c)
   return (end == match)? -1 : (match-array);
 }
 */
+
+void Utilities::PrintCharsAsHex(char* charArray, size_t length)
+{
+	for(int i = 0; i < length; i++)
+	{
+		Serial.print((int)(charArray[i]), HEX);
+		Serial.print(" ");
+	}
+}
+
diff --git a/PixelHub-Client/Utilities.h b/PixelHub-Client/Utilities.h
index 41eae61..c09eae1 100644
--- a/PixelHub-Client/Utilities.h
+++ b/PixelHub-Client/Utilities.h
@@ -9,7 +9,15 @@
 
 #pragma once
 
+#include <stdlib.h>
+
 int findChar(char* str, char targetChar);
 
 int getPosition(const char *array, int size, char c);
 
+class Utilities
+{
+public:
+	static void PrintCharsAsHex(char* charArray, size_t length);
+};
+
