This repository has been archived on 2019-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
PixelHub/PixelHub-Client/src/PixelBot.h

96 lines
3.4 KiB
C++

#pragma once
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
/// Message Types ///
// Hard-resets the PixelBot. Useful if something has gone wrong, or to reset it's internal state.
const int MESSAGE_RESET = 0;
// May be sent by either party. Indicates that one or the other doesn't like a message that was sent by the other party.
const int MESSAGE_ERROR = 1;
// Sent by a PixelBot upon connection.
const int MESSAGE_HANDSHAKEREQUEST = 5;
// Sent by the server in response to a `HandshakeRequest`.
const int MESSAGE_HANDSHAKERESPONSE = 6;
// Instructs a PixelBot to perform a movement.
const int MESSAGE_MOVE = 11;
// Sent by a pixelbot to indicate that the command sent by the server is ok, and that it will proceed to execute it.
const int MESSAGE_COMMANDOK = 12;
// Sent by a pixelbot to indicate that it has completed executing the command sent previously.
const int MESSAGE_COMMANDCOMPLETE = 13;
// Sent by a pixelbot to indicate that it has failed to complete the execution of a command it received previously.
const int MESSAGE_COMMANDFAILED = 14;
// Sent by the server to request information from a PixelBot. This could be from sensors, configuration, statistics, etc.
const int MESSAGE_INFOREQUEST = 20;
// Sent by a PixelBot in response to an `InfoRequest` message.
const int MESSAGE_INFORESPONSE = 21;
/////////////////////
/// <summary>
/// Connects to a PixelHub server, receives commands, and acts upon them.
/// </summmary>
class PixelBot
{
public:
PixelBot();
~PixelBot();
/// <summary>
/// The current protocol version that this this PixelBot implementation understands.
/// Very important to avoid strange behavious when a PixelBot hasn't has it's code updated.
/// </summary>
static const unsigned short PROTOCOL_VERSION = 1;
/// <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 multicast address that the PixelHub beacon is broadcasting on.
IPAddress beaconAddress = IPAddress(239, 62, 148, 30);
// 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 to use as the PixelHub server transport connection.
WiFiClient tcpClient = WiFiClient();
};