-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlServer.h
More file actions
87 lines (76 loc) · 2.17 KB
/
ControlServer.h
File metadata and controls
87 lines (76 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma once
#include "utils.h"
#include "Configuration.h"
#include <WiFiUdp.h>
#pragma pack(push)
#pragma pack(1)
struct SenderoControlHeader{
enum Command {
REQUEST_CLOCK,
CLOCK_CORRECTION,
CONFIGURATION,
NO_COMMAND,
};
char packetName[7];
union{
struct{
bool requestClockFlag :1;
bool clockCorrectionOffsetFlag :1;
bool configurationFlag :1;
bool closeConnectionFlag :1;
bool requestStatsFlag :1;
bool keepAliveFlag :1;
int padding :2;
};
byte flags;
};
Command type(){
if (requestClockFlag)
return REQUEST_CLOCK;
if (clockCorrectionOffsetFlag)
return CLOCK_CORRECTION;
if (configurationFlag)
return CONFIGURATION;
return NO_COMMAND;
}
int size(){
return sizeof(SenderoControlHeader);
}
String toString(){
char name[8]; name[7] = '\0';
for(int i = 0; i < 7; i++) name[i] = packetName[i];
String headerName = String(name) + "\n";
String v1 = String("isRequestClock = ") + (requestClockFlag ? "Yes" : "No") + "\n";
String v2 = String("clockCorrectionOffsetFlag = ") + (clockCorrectionOffsetFlag ? "Yes" : "No") + "\n";
String v3 = String("configurationFlag = ") + (configurationFlag ? "Yes" : "No") + "\n";
String v4 = String("closeConnectionFlag = ") + (closeConnectionFlag ? "Yes" : "No") + "\n";
String v5 = String("requestStatsFlag = ") + (requestStatsFlag ? "Yes" : "No") + "\n";
return headerName + v1 + v2 + v3 + v4 + v5;
}
};
#pragma pack(pop)
class ControlServer : public ConfigurationObserver{
SINGLETON_H(ControlServer)
private:
WiFiUDP udp;
WiFiServer* server;
WiFiClient client;
Configuration* configuration;
char* buffer;
void registerInServer();
void setup();
bool serverDiscovered;
unsigned long lastPacketTime;
template<typename T> T readBuffer(void* buffer);
template<typename T> void writeBuffer(void* buffer, T data);
public:
bool serverIsAlive;
void externalCommandReceived();
ControlServer();
bool incomingCommand();
SenderoControlHeader processCommand();
void obtainServerEndpoint();
void configurationChanged();
void commandReceivedFlashLed();
~ControlServer();
};