This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.h
124 lines (92 loc) · 2.82 KB
/
server.h
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* server.h
* Copyright Joseph R. Freeston (MIT License)
*
* API Wrapper for CubeServer
*
*/
#ifndef CUBESERVER_SERVER_H
#define CUBESERVER_SERVER_H
#include <Arduino.h>
#ifdef ARDUINO_ARCH_ESP8266 // ESP8266 version--
# include <ESP8266WiFi.h>
# include <ESP8266HTTPClient.h>
#elif ARDUINO_ARCH_ESP32 // ESP32 version--
#include <WiFi.h>
#include <HTTPClient.h>
#else
# error "Unsupported Architecture."
#endif
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
#include "server_options.h"
#include "compile_time.h"
#define VERIFICATION_OK 1
#define VERIFICATION_FAILED -1
extern WiFiClientSecure client;
extern HTTPClient http;
typedef struct CubeServerConfig {
const char *AP_SSID;
const char *API_CN;
const char *API_HOST;
int API_PORT;
} CubeServerConfig;
#ifdef CLIENT_CONF_H
const CubeServerConfig CUBESERVER_DEFAULT_CONFIG = {
CONF_AP_SSID,
CONF_API_CN,
CONF_API_HOST,
API_PORT
};
#else
const CubeServerConfig CUBESERVER_DEFAULT_CONFIG = {
"CubeServer-API",
"api.local",
"https://api.local",
8081
};
#endif
typedef struct GameStatus {
unsigned long int unix_time;
int score;
int strikes;
} GameStatus;
class CubeServer {
private:
const char * _team_name;
const char * _team_secret;
CubeServerConfig _conf;
const char *_server_fingerprint;
unsigned int _timestamp;
int _begin_client(String path);
public:
CubeServer(const char * team_name, const char * team_secret, const char * server_fingerprint, long unsigned int timestamp, CubeServerConfig conf);
CubeServer(const char * team_name, const char * team_secret, const char * server_fingerprint, CubeServerConfig conf) : CubeServer(team_name, team_secret, server_fingerprint, UNIX_TIMESTAMP, CUBESERVER_DEFAULT_CONFIG) {};
CubeServer(const char * team_name, const char * team_secret, const char * server_fingerprint) : CubeServer(team_name, team_secret, server_fingerprint, CUBESERVER_DEFAULT_CONFIG) {};
#ifdef CLIENT_CONF_H
#ifdef ARDUINO_ARCH_ESP32 // Only ESP32 can handle the sha256 fingerprint:
CubeServer() : CubeServer(TEAM_NAME, TEAM_SECRET, SERVER_FINGERPRINT_SHA256) {};
#else
CubeServer() : CubeServer(TEAM_NAME, TEAM_SECRET, SERVER_FINGERPRINT) {};
#endif
#endif
// connect to the wifi access point:
int connect(bool (*connection_wait_loop)());
int connect();
// get status:
int get_status(GameStatus* stats_var);
// post some data:
int post(char *json);
int postTemperature(int value);
int postTemperature(double value);
int postHumidity(int value);
int postHumidity(double value);
int postPressure(int value);
int postPressure(double value);
int postLightIntensity(int value);
int postLightIntensity(double value);
int postComment(String value);
int postComment(char *value);
int postComment(const char *value);
};
#endif