Skip to content

Commit

Permalink
#112 add versions to /ping
Browse files Browse the repository at this point in the history
  • Loading branch information
yakumo-saki committed Dec 13, 2022
1 parent 5fd787a commit 74c9aa0
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 48 deletions.
3 changes: 3 additions & 0 deletions include/network/http_api_ping.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <Arduino.h>

void http_api_ping_setup();
6 changes: 5 additions & 1 deletion include/network/http_api_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

bool parseBooleanString(const String value);
std::vector<String> stringSplit(String value, String delimiter);
String jsonToString(DynamicJsonDocument& json);
String jsonToString(DynamicJsonDocument& json);
String http_normal_ping_json();

// millisecond -> 00:00:00 形式の日付文字列を返す
String getTimeString(unsigned long ms);
1 change: 1 addition & 0 deletions include/network/http_not_found.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void http_not_found_setup();
12 changes: 0 additions & 12 deletions src/network/api/http_api_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ void http_handle_data() {
server.send(200, MimeType::JSON, message);
}

void http_handle_ping() {
String message = http_normal_ping_json();
server.send(200, MimeType::JSON, message);
}

void http_handle_stastics() {
server.send(200, MimeType::JSON, stasticsJSON);
}
Expand All @@ -39,17 +34,10 @@ void http_handle_goto_setup() {
server.send(200, MimeType::JSON, jsonStr);
}

void http_handle_not_found() {
String message = http_normal_not_found_html();
server.send(404, MimeType::HTML, message);
}

void http_api_base_setup() {
server.on ( "/ping", HTTP_GET, http_handle_ping);
server.on ( "/", HTTP_GET, http_handle_data );
server.on ( "/stastics", HTTP_GET, http_handle_stastics );
server.on ( "/goto_setup", HTTP_POST, http_handle_goto_setup );

server.onNotFound ( http_handle_not_found );
apilog("API Base initialized.");
}
37 changes: 2 additions & 35 deletions src/network/api/http_api_base_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,12 @@
#include "log.h"
#include "global.h"

String http_normal_not_found_html() {
String message = "File Not Found\n\n";
return message;
}

String get_time_string(unsigned long ms) {
unsigned long second = ms / 1000;
unsigned long minute = second / 60;

int sec = second % 60;
int min = (second / 60) % 60;
int hr = minute / 60;

char buf[15];
snprintf (buf, sizeof buf,"%02d:%02d:%02d", hr, min, sec);
return String(buf);
}
#include "network/http_api_util.h"

String http_normal_data_json() {

unsigned long ms = millis();
String timeString = get_time_string(ms);
String timeString = getTimeString(ms);

char temp[10], hum[10], pres[10];
char lux[10], luxIr[10],ppm[10];
Expand All @@ -48,20 +32,3 @@ String http_normal_data_json() {
serializeJson(doc, json);
return json;
}

String http_normal_ping_json() {

unsigned long ms = millis();
String timeString = get_time_string(ms);

DynamicJsonDocument doc(2000);
doc["product"] = product;
doc["uptime"] = timeString;
doc["uptimeMills"] = ms;

String json;
serializeJson(doc, json);

httplog(json);
return json;
}
44 changes: 44 additions & 0 deletions src/network/api/http_api_ping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <Arduino.h>
#include <ArduinoJson.h>

#include "config.h"

#include "log.h"
#include "global.h"
#include "display/display.h"
#include "network/http_api.h"
#include "network/http_api_util.h"

extern HTTPWEBSERVER server;


String http_normal_ping_json() {

unsigned long ms = millis();
String timeString = getTimeString(ms);

DynamicJsonDocument doc(2000);
doc["product"] = product;
doc["uptime"] = timeString;
doc["uptimeMills"] = ms;
doc["majorVer"] = ver;
doc["minorVer"] = minorVer;
doc["settingId"] = SETTING_ID;

String json;
serializeJson(doc, json);

httplog(json);
return json;
}


void http_handle_ping() {
String message = http_normal_ping_json();
server.send(200, MimeType::JSON, message);
}

void http_api_ping_setup() {
server.on ( "/ping", HTTP_GET, http_handle_ping);
apilog("API Ping initialized.");
}
14 changes: 14 additions & 0 deletions src/network/api/http_api_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@

#include "log.h"

// millisecond -> 00:00:00 形式の日付文字列を返す
String getTimeString(unsigned long ms) {
unsigned long second = ms / 1000;
unsigned long minute = second / 60;

int sec = second % 60;
int min = (second / 60) % 60;
int hr = minute / 60;

char buf[15];
snprintf (buf, sizeof buf,"%02d:%02d:%02d", hr, min, sec);
return String(buf);
}

String jsonToString(DynamicJsonDocument& json) {
String jsonStr;
serializeJson(json, jsonStr);
Expand Down
4 changes: 4 additions & 0 deletions src/network/http_normal_web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#include "http_normal.h"

#include "network/http_not_found.h"
#include "network/http_api.h"
#include "network/http_api_ping.h"
#include "network/http_api_base.h"
#include "network/http_api_display.h"
#include "network/http_api_mhz.h"
Expand All @@ -21,6 +23,8 @@ extern HTTPWEBSERVER server;
void http_setup_normal() {
httplog("HTTP web server initializing");

http_not_found_setup();
http_api_ping_setup();
http_api_display_setup();
http_api_base_setup();
http_api_mhz_setup();
Expand Down
21 changes: 21 additions & 0 deletions src/network/http_not_found.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <Arduino.h>
#include <ArduinoJson.h>

#include "config.h"

#include "log.h"
#include "global.h"
#include "display/display.h"
#include "network/http_api.h"
#include "network/http_api_base_json.h"

extern HTTPWEBSERVER server;

void http_handle_not_found() {
String message = "404 File Not Found\n\n";
server.send(404, MimeType::HTML, message);
}

void http_not_found_setup() {
server.onNotFound ( http_handle_not_found );
}

0 comments on commit 74c9aa0

Please sign in to comment.