From 9f24dbe7f69f63b1f9eda0ff2e889e0015e4c754 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 21 Oct 2016 21:47:49 +0100
Subject: [PATCH] [untested] Refactor pixelbot code into own class.

---
 PixelHub-Client/PixelBot.cpp        | 105 ++++++++++++++++++++++++++++
 PixelHub-Client/PixelBot.h          |  23 +++++-
 PixelHub-Client/PixelBut.cpp        |  10 ---
 PixelHub-Client/PixelHub-Client.ino | 102 +--------------------------
 4 files changed, 130 insertions(+), 110 deletions(-)
 create mode 100644 PixelHub-Client/PixelBot.cpp
 delete mode 100644 PixelHub-Client/PixelBut.cpp

diff --git a/PixelHub-Client/PixelBot.cpp b/PixelHub-Client/PixelBot.cpp
new file mode 100644
index 0000000..279c0b0
--- /dev/null
+++ b/PixelHub-Client/PixelBot.cpp
@@ -0,0 +1,105 @@
+#include "PixelBot.h"
+
+#include <ESP8266WiFi.h>
+#include <WiFiUdp.h>
+
+#include "Utilities.h"
+
+PixelBot::PixelBot()
+{
+	memset(serverIp, '\0', 16);
+}
+
+PixelBot::~PixelBot()
+{
+	
+}
+
+/// <summary>
+/// Listens for PixelHub beacon pings in order to determine the location of the PixelHub server.
+/// </summary>
+/// <description>
+/// Puts the details it finds into the `serverIp` and `portNumber` data members.
+/// </description>
+void PixelBot::FindServer()
+{
+	Serial.print("Initialising PixelHub auto detection system - ");
+	
+	// The UDP Client for sending and recieving UDP datagrams.
+	WiFiUDP UdpClient;
+	
+	byte datagramBuffer[datagramBufferSize];
+	memset(datagramBuffer, '\0', datagramBufferSize); // Prefill the datagram buffer with zeros for protection later
+	
+	UdpClient.beginMulticast(WiFi.localIP(), beaconAddress, beaconPort);
+	
+	Serial.println("success!");
+
+	Serial.println("Listening for PixelHub beacon pings.");
+	while(true) {
+		int datagramSize = UdpClient.parsePacket();
+		if(!datagramSize) continue;
+		
+		Serial.print("Received datagram #");
+		Serial.print(datagramSize);
+		Serial.print(" bytes in size from ");
+		Serial.print(UdpClient.remoteIP());
+		Serial.print(":");
+		Serial.print(UdpClient.remotePort());
+		if(datagramSize > datagramBufferSize) {
+			Serial.println(", but the message is larger than the datagram buffer size.");
+			continue;
+		}
+		Serial.println(".");
+		
+		UdpClient.read(datagramBuffer, datagramSize);
+		
+		Serial.print("Content as hex: ");
+		for(int i = 0; i < datagramSize; i++) {
+			Serial.print(datagramBuffer[i], HEX);
+			Serial.print(" ");
+		}
+		Serial.println();
+		Serial.print("Raw content: ");
+		Serial.write(datagramBuffer, datagramSize);
+		Serial.println();
+
+		Serial.print("Parsing datagram - ");
+
+		// Parse the recieved message
+		char* datagramStr = (char*)datagramBuffer;
+
+		// Find the positions of the key characters
+		//int atPos = getPosition(datagramStr, datagramSize, '@');
+		//int colonPos = getPosition(datagramStr, datagramSize, ':');
+		int atPos = findChar(datagramStr, '@');
+		int colonPos = findChar(datagramStr, ':');
+		
+		char role[7];
+		char serverPortText[7];
+		memset(role, '\0', 7);
+		memset(serverPortText, '\0', 7);
+		
+		strncpy(role, datagramStr, atPos);
+		strncpy(serverIp, datagramStr + atPos + 1, colonPos - atPos - 1);
+		strncpy(serverPortText, datagramStr + colonPos + 1, datagramSize - colonPos - 1);
+		
+		Serial.println("complete.");
+		
+		Serial.print("atPos: "); Serial.println(atPos);
+		Serial.print("colonPos: "); Serial.println(colonPos);
+		
+		Serial.print("Role: "); Serial.print(role); Serial.print(" ");
+		Serial.print("Remote IP: "); Serial.print(serverIp); Serial.print(" ");
+		Serial.print("Port number: "); Serial.print(serverPortText);
+		Serial.println();
+		
+		// If the advertiser isn't playing the role of a server, then we're not interested
+		if(role != "server") continue;
+
+		serverPort = atoi(serverPortText);
+
+		return;
+	}
+}
+
diff --git a/PixelHub-Client/PixelBot.h b/PixelHub-Client/PixelBot.h
index f1fd2f2..fe39097 100644
--- a/PixelHub-Client/PixelBot.h
+++ b/PixelHub-Client/PixelBot.h
@@ -1,12 +1,33 @@
 #pragma once
 
+#include <ESP8266WiFi.h>
+#include <WiFiUdp.h>
+
 class PixelBot
 {
 public:
 	PixelBot();
 	~PixelBot();
+
+	void FindServer();
   
 private:
-  
+	char serverIp[16];
+	int serverPort = -1;
+	
+	// ~~~ Auto Discovery System ~~~ //
+	
+	// The 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.
+	unsigned int beaconPort = 5050;
+	
+	// The size of the datagram buffer that is used to buffer incoming messages.
+	int datagramBufferSize = 256;
+
+	// ~~~   ~~~ //
+	
+	// The TCP client
+	WiFiClient tcpClient = WiFiClient();
 };
 
diff --git a/PixelHub-Client/PixelBut.cpp b/PixelHub-Client/PixelBut.cpp
deleted file mode 100644
index 464ecb9..0000000
--- a/PixelHub-Client/PixelBut.cpp
+++ /dev/null
@@ -1,10 +0,0 @@
-#include "PixelBot.h"
-
-PixelBot::PixelBot()
-{
-}
-
-PixelBot::~PixelBot()
-{
-}
-
diff --git a/PixelHub-Client/PixelHub-Client.ino b/PixelHub-Client/PixelHub-Client.ino
index 75bbb98..06f8605 100644
--- a/PixelHub-Client/PixelHub-Client.ino
+++ b/PixelHub-Client/PixelHub-Client.ino
@@ -4,28 +4,14 @@
  * Originally adapted from https://www.arduino.cc/en/Tutorial/ConnectWithWPA
  * by Starbeamrainbowlabs
  */
-
 #include <ESP8266WiFi.h>
-#include <WiFiUdp.h>
 
-#include "Utilities.h"
+#include "PixelBot.h"
 
 char ssid[] = "ZimWeaver";
 char password[] = "***REMOVED***";
 
-// The address that the PixelHub beacon is broadcasting on.
-IPAddress beaconAddress(239, 62, 148, 30);
-// The port number that the PixelHub beacon is broadcasting on.
-unsigned int beaconPort = 5050;
-
-// A UDP Client to allow us to recieve UDP datagrams.
-WiFiUDP UdpClient;
-// The size of the datagram buffer that is used to buffer incoming messages.
-int datagramBufferSize = 256;
-
-WiFiClient tcpClient;
-char pixelHubServerIp[16];
-int pixelHubPortNumber;
+PixelBot pixelBot;
 
 void setup()
 {
@@ -50,7 +36,7 @@ void setup()
 	Serial.println("success!");
 	
 	printWiFiInfoLocal();
-	findServer();
+	pixelBot.FindServer();
 }
 
 
@@ -73,86 +59,4 @@ void printWiFiInfoLocal()
 
 // ~~~ PixelHub auto discovery system ~~~ //
 
-/// <summary>
-/// Listens for PixelHub beacon pings in order to determine the location of the PixelHub server.
-/// </summary>
-/// <description>
-/// Puts the details it finds into the `pixelHubServerIp` and `pixelHubPortNumber`
-/// </description>
-void findServer()
-{
-	Serial.print("Initialising PixelHub auto detection system - ");
-	byte datagramBuffer[datagramBufferSize];
-	memset(datagramBuffer, '\0', datagramBufferSize); // Prefill the buffer with zeros for protection later
-	UdpClient.beginMulticast(WiFi.localIP(), beaconAddress, beaconPort);
-	Serial.println("success!");
-
-	Serial.println("Listening for PixelHub beacon pings.");
-	while(true) {
-		int datagramSize = UdpClient.parsePacket();
-		if(!datagramSize) continue;
-		
-		Serial.print("Received datagram #");
-		Serial.print(datagramSize);
-		Serial.print(" bytes in size from ");
-		Serial.print(UdpClient.remoteIP());
-		Serial.print(":");
-		Serial.print(UdpClient.remotePort());
-		if(datagramSize > datagramBufferSize) {
-			Serial.println(", but the message is larger than the datagram buffer size.");
-			continue;
-		}
-		Serial.println(".");
-		
-		UdpClient.read(datagramBuffer, datagramSize);
-		
-		Serial.print("Content as hex: ");
-		for(int i = 0; i < datagramSize; i++) {
-			Serial.print(datagramBuffer[i], HEX);
-			Serial.print(" ");
-		}
-		Serial.println();
-		Serial.print("Raw content: ");
-		Serial.write(datagramBuffer, datagramSize);
-		Serial.println();
-
-		Serial.print("Parsing datagram - ");
-
-		// Parse the recieved message
-		char* datagramStr = (char*)datagramBuffer;
-
-		// Find the positions of the key characters
-		//int atPos = getPosition(datagramStr, datagramSize, '@');
-		//int colonPos = getPosition(datagramStr, datagramSize, ':');
-		int atPos = findChar(datagramStr, '@');
-		int colonPos = findChar(datagramStr, ':');
-		
-		char role[7];
-		char portNumberText[7];
-		memset(role, '\0', 7);
-		memset(pixelHubServerIp, '\0', 16);
-		memset(portNumberText, '\0', 7);
-		
-		strncpy(role, datagramStr, atPos);
-		strncpy(pixelHubServerIp, datagramStr + atPos + 1, colonPos - atPos - 1);
-		strncpy(portNumberText, datagramStr + colonPos + 1, datagramSize - colonPos - 1);
-		
-		Serial.println("complete.");
-		
-		Serial.print("atPos: "); Serial.println(atPos);
-		Serial.print("colonPos: "); Serial.println(colonPos);
-		
-		Serial.print("Role: "); Serial.print(role); Serial.print(" ");
-		Serial.print("Remote IP: "); Serial.print(serverIp); Serial.print(" ");
-		Serial.print("Port number: "); Serial.print(portNumberText);
-		Serial.println();
-		
-		// If the advertiser isn't playing the role of a server, then we're not interested
-		if(role != "server") continue;
-
-		pixelHubPortNumber = atoi(portNumberText);
-
-		return;
-	}
-}
 
