mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-27 18:02:17 +03:00
Migrate from astyle to clang-format (#8464)
This commit is contained in:
committed by
Max Prokhorov
parent
46190b61f1
commit
19b7a29720
@ -42,11 +42,11 @@ extern "C" {
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
const char *ssid = STASSID;
|
||||
const char *password = STAPSK;
|
||||
const unsigned int port = 80;
|
||||
|
||||
ESP8266WebServer server(port);
|
||||
@ -76,20 +76,16 @@ void handleNotFound() {
|
||||
message += "\nArguments: ";
|
||||
message += server.args();
|
||||
message += "\n";
|
||||
for (uint8_t i = 0; i < server.args(); i++) {
|
||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||
}
|
||||
for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; }
|
||||
server.send(404, "text/plain", message);
|
||||
}
|
||||
|
||||
void SSEKeepAlive() {
|
||||
for (uint8_t i = 0; i < SSE_MAX_CHANNELS; i++) {
|
||||
if (!(subscription[i].clientIP)) {
|
||||
continue;
|
||||
}
|
||||
if (!(subscription[i].clientIP)) { continue; }
|
||||
if (subscription[i].client.connected()) {
|
||||
Serial.printf_P(PSTR("SSEKeepAlive - client is still listening on channel %d\n"), i);
|
||||
subscription[i].client.println(F("event: event\ndata: { \"TYPE\":\"KEEP-ALIVE\" }\n")); // Extra newline required by SSE standard
|
||||
subscription[i].client.println(F("event: event\ndata: { \"TYPE\":\"KEEP-ALIVE\" }\n")); // Extra newline required by SSE standard
|
||||
} else {
|
||||
Serial.printf_P(PSTR("SSEKeepAlive - client not listening on channel %d, remove subscription\n"), i);
|
||||
subscription[i].keepAliveTimer.detach();
|
||||
@ -106,15 +102,15 @@ void SSEKeepAlive() {
|
||||
void SSEHandler(uint8_t channel) {
|
||||
WiFiClient client = server.client();
|
||||
SSESubscription &s = subscription[channel];
|
||||
if (s.clientIP != client.remoteIP()) { // IP addresses don't match, reject this client
|
||||
if (s.clientIP != client.remoteIP()) { // IP addresses don't match, reject this client
|
||||
Serial.printf_P(PSTR("SSEHandler - unregistered client with IP %s tries to listen\n"), server.client().remoteIP().toString().c_str());
|
||||
return handleNotFound();
|
||||
}
|
||||
client.setNoDelay(true);
|
||||
client.setSync(true);
|
||||
Serial.printf_P(PSTR("SSEHandler - registered client with IP %s is listening\n"), IPAddress(s.clientIP).toString().c_str());
|
||||
s.client = client; // capture SSE server client connection
|
||||
server.setContentLength(CONTENT_LENGTH_UNKNOWN); // the payload can go on forever
|
||||
s.client = client; // capture SSE server client connection
|
||||
server.setContentLength(CONTENT_LENGTH_UNKNOWN); // the payload can go on forever
|
||||
server.sendContent_P(PSTR("HTTP/1.1 200 OK\nContent-Type: text/event-stream;\nConnection: keep-alive\nCache-Control: no-cache\nAccess-Control-Allow-Origin: *\n\n"));
|
||||
s.keepAliveTimer.attach_scheduled(30.0, SSEKeepAlive); // Refresh time every 30s for demo
|
||||
}
|
||||
@ -122,28 +118,20 @@ void SSEHandler(uint8_t channel) {
|
||||
void handleAll() {
|
||||
const char *uri = server.uri().c_str();
|
||||
const char *restEvents = PSTR("/rest/events/");
|
||||
if (strncmp_P(uri, restEvents, strlen_P(restEvents))) {
|
||||
return handleNotFound();
|
||||
}
|
||||
uri += strlen_P(restEvents); // Skip the "/rest/events/" and get to the channel number
|
||||
if (strncmp_P(uri, restEvents, strlen_P(restEvents))) { return handleNotFound(); }
|
||||
uri += strlen_P(restEvents); // Skip the "/rest/events/" and get to the channel number
|
||||
unsigned int channel = atoi(uri);
|
||||
if (channel < SSE_MAX_CHANNELS) {
|
||||
return SSEHandler(channel);
|
||||
}
|
||||
if (channel < SSE_MAX_CHANNELS) { return SSEHandler(channel); }
|
||||
handleNotFound();
|
||||
};
|
||||
|
||||
void SSEBroadcastState(const char *sensorName, unsigned short prevSensorValue, unsigned short sensorValue) {
|
||||
for (uint8_t i = 0; i < SSE_MAX_CHANNELS; i++) {
|
||||
if (!(subscription[i].clientIP)) {
|
||||
continue;
|
||||
}
|
||||
if (!(subscription[i].clientIP)) { continue; }
|
||||
String IPaddrstr = IPAddress(subscription[i].clientIP).toString();
|
||||
if (subscription[i].client.connected()) {
|
||||
Serial.printf_P(PSTR("broadcast status change to client IP %s on channel %d for %s with new state %d\n"),
|
||||
IPaddrstr.c_str(), i, sensorName, sensorValue);
|
||||
subscription[i].client.printf_P(PSTR("event: event\ndata: {\"TYPE\":\"STATE\", \"%s\":{\"state\":%d, \"prevState\":%d}}\n\n"),
|
||||
sensorName, sensorValue, prevSensorValue);
|
||||
Serial.printf_P(PSTR("broadcast status change to client IP %s on channel %d for %s with new state %d\n"), IPaddrstr.c_str(), i, sensorName, sensorValue);
|
||||
subscription[i].client.printf_P(PSTR("event: event\ndata: {\"TYPE\":\"STATE\", \"%s\":{\"state\":%d, \"prevState\":%d}}\n\n"), sensorName, sensorValue, prevSensorValue);
|
||||
} else {
|
||||
Serial.printf_P(PSTR("SSEBroadcastState - client %s registered on channel %d but not listening\n"), IPaddrstr.c_str(), i);
|
||||
}
|
||||
@ -152,7 +140,7 @@ void SSEBroadcastState(const char *sensorName, unsigned short prevSensorValue, u
|
||||
|
||||
// Simulate sensors
|
||||
void updateSensor(sensorType &sensor) {
|
||||
unsigned short newVal = (unsigned short)RANDOM_REG32; // (not so good) random value for the sensor
|
||||
unsigned short newVal = (unsigned short)RANDOM_REG32; // (not so good) random value for the sensor
|
||||
Serial.printf_P(PSTR("update sensor %s - previous state: %d, new state: %d\n"), sensor.name, sensor.value, newVal);
|
||||
if (sensor.value != newVal) {
|
||||
SSEBroadcastState(sensor.name, sensor.value, newVal); // only broadcast if state is different
|
||||
@ -167,7 +155,7 @@ void handleSubscribe() {
|
||||
}
|
||||
|
||||
uint8_t channel;
|
||||
IPAddress clientIP = server.client().remoteIP(); // get IP address of client
|
||||
IPAddress clientIP = server.client().remoteIP(); // get IP address of client
|
||||
String SSEurl = F("http://");
|
||||
SSEurl += WiFi.localIP().toString();
|
||||
SSEurl += F(":");
|
||||
@ -176,14 +164,12 @@ void handleSubscribe() {
|
||||
SSEurl += F("/rest/events/");
|
||||
|
||||
++subscriptionCount;
|
||||
for (channel = 0; channel < SSE_MAX_CHANNELS; channel++) // Find first free slot
|
||||
if (!subscription[channel].clientIP) {
|
||||
break;
|
||||
}
|
||||
subscription[channel] = {clientIP, server.client(), Ticker()};
|
||||
for (channel = 0; channel < SSE_MAX_CHANNELS; channel++) // Find first free slot
|
||||
if (!subscription[channel].clientIP) { break; }
|
||||
subscription[channel] = { clientIP, server.client(), Ticker() };
|
||||
SSEurl += channel;
|
||||
Serial.printf_P(PSTR("Allocated channel %d, on uri %s\n"), channel, SSEurl.substring(offset).c_str());
|
||||
//server.on(SSEurl.substring(offset), std::bind(SSEHandler, &(subscription[channel])));
|
||||
// server.on(SSEurl.substring(offset), std::bind(SSEHandler, &(subscription[channel])));
|
||||
Serial.printf_P(PSTR("subscription for client IP %s: event bus location: %s\n"), clientIP.toString().c_str(), SSEurl.c_str());
|
||||
server.send_P(200, "text/plain", SSEurl.c_str());
|
||||
}
|
||||
@ -200,16 +186,14 @@ void setup(void) {
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
Serial.println("");
|
||||
while (WiFi.status() != WL_CONNECTED) { // Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) { // Wait for connection
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.printf_P(PSTR("\nConnected to %s with IP address: %s\n"), ssid, WiFi.localIP().toString().c_str());
|
||||
if (MDNS.begin("esp8266")) {
|
||||
Serial.println("MDNS responder started");
|
||||
}
|
||||
if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); }
|
||||
|
||||
startServers(); // start web and SSE servers
|
||||
startServers(); // start web and SSE servers
|
||||
sensor[0].name = "sensorA";
|
||||
sensor[1].name = "sensorB";
|
||||
updateSensor(sensor[0]);
|
||||
|
Reference in New Issue
Block a user