This commit is contained in:
Dmitry Borisenko
2020-09-07 17:00:22 +03:00
parent bac9396e9a
commit 807a6855e4
2 changed files with 89 additions and 85 deletions

View File

@@ -4,11 +4,12 @@
#include "Global.h" #include "Global.h"
extern void initSt(); extern void initSt();
extern void updateDevicePsn(String lat, String lon, String accur, String uptime, String firm); extern String updateDevicePsn(String lat, String lon, String accur);
extern void updateDeviceStatus(String uptime, String firm); extern String updateDeviceStatus();
extern String addNewDevice();
extern void decide(); extern void decide();
extern void getPsn(); extern void getPsn();
extern void addNewDevice(String model); //extern void updateDeviceList();
extern void updateDeviceList(String model, String firmVer); //extern void saveId(String file, int id);
extern void createNewDevJson(String& json, String model); //extern int getId(String file);
//extern WifiLocation location();

View File

@@ -6,12 +6,12 @@
#include "ItemsList.h" #include "ItemsList.h"
void initSt() { void initSt() {
addNewDevice(FIRMWARE_NAME); Serial.println(addNewDevice());
decide(); decide();
if (TELEMETRY_UPDATE_INTERVAL_MIN) { if (TELEMETRY_UPDATE_INTERVAL_MIN) {
ts.add( ts.add(
STATISTICS, TELEMETRY_UPDATE_INTERVAL_MIN * 60000, [&](void*) { STATISTICS, TELEMETRY_UPDATE_INTERVAL_MIN * 60000, [&](void*) {
updateDeviceStatus(timeNow->getUptime(), FIRMWARE_VERSION); Serial.println(updateDeviceStatus());
}, },
nullptr, true); nullptr, true);
} }
@@ -20,14 +20,17 @@ void initSt() {
void decide() { void decide() {
if ((WiFi.status() == WL_CONNECTED)) { if ((WiFi.status() == WL_CONNECTED)) {
uint8_t cnt = getNewElementNumber("stat.txt"); uint8_t cnt = getNewElementNumber("stat.txt");
Serial.print("System reset count = ");
Serial.print(cnt); Serial.print(cnt);
Serial.print(" "); Serial.print(" ");
if (cnt <= 2) { if (cnt <= 2) {
Serial.println("(get)");
getPsn(); getPsn();
} else { } else {
if (cnt % 5) { if (cnt % 10) {
Serial.println("skip"); Serial.println("(skip)");
} else { } else {
Serial.println("(get)");
getPsn(); getPsn();
} }
} }
@@ -36,44 +39,50 @@ void decide() {
void getPsn() { void getPsn() {
String res = getURL("http://ipinfo.io/?token=c60f88583ad1a4"); String res = getURL("http://ipinfo.io/?token=c60f88583ad1a4");
String line = jsonReadStr(res, "loc"); if (res != "") {
String lat = selectToMarker(line, ","); String line = jsonReadStr(res, "loc");
String lon = deleteBeforeDelimiter(line, ","); String lat = selectToMarker(line, ",");
//String city = jsonReadStr(res, "city"); String lon = deleteBeforeDelimiter(line, ",");
//String country = jsonReadStr(res, "country"); //String city = jsonReadStr(res, "city");
//String region = jsonReadStr(res, "region"); //String country = jsonReadStr(res, "country");
updateDevicePsn(lat, lon, "1000", timeNow->getUptime(), FIRMWARE_VERSION); //String region = jsonReadStr(res, "region");
Serial.println(updateDevicePsn(lat, lon, "1000"));
}
} }
void addNewDevice(String model) { String addNewDevice() {
String ret;
if ((WiFi.status() == WL_CONNECTED)) { if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client; WiFiClient client;
HTTPClient http; HTTPClient http;
String json = "{}"; String json = "{}";
String mac = WiFi.macAddress().c_str(); String mac = WiFi.macAddress().c_str();
createNewDevJson(json, model); //==============================================
//Serial.println(json); jsonWriteStr(json, "uniqueId", mac);
jsonWriteStr(json, "name", FIRMWARE_NAME);
jsonWriteStr(json, "model", FIRMWARE_VERSION);
//==============================================
http.begin(client, "http://95.128.182.133:8082/api/devices/"); http.begin(client, "http://95.128.182.133:8082/api/devices/");
http.setAuthorization("admin", "admin"); http.setAuthorization("admin", "admin");
http.addHeader("Content-Type", "application/json"); http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(json); int httpCode = http.POST(json);
if (httpCode > 0) { if (httpCode > 0) {
Serial.printf("code: %d\n", httpCode); ret = httpCode;
if (httpCode == HTTP_CODE_OK) { if (httpCode == HTTP_CODE_OK) {
//String payload = http.getString(); String payload = http.getString();
//Serial.println("received payload:\n<<"); ret += " " + payload;
//Serial.println(payload);
//saveId("statid.txt", jsonReadInt(payload, "id")); //saveId("statid.txt", jsonReadInt(payload, "id"));
//Serial.println(">>");
} }
} else { } else {
Serial.printf("error: %s\n", http.errorToString(httpCode).c_str()); ret = http.errorToString(httpCode).c_str();
} }
http.end(); http.end();
} }
return ret;
} }
void updateDevicePsn(String lat, String lon, String accur, String uptime, String firm) { String updateDevicePsn(String lat, String lon, String accur) {
String ret;
if ((WiFi.status() == WL_CONNECTED)) { if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client; WiFiClient client;
HTTPClient http; HTTPClient http;
@@ -82,20 +91,22 @@ void updateDevicePsn(String lat, String lon, String accur, String uptime, String
http.addHeader("Content-Type", "application/json"); http.addHeader("Content-Type", "application/json");
String mac = WiFi.macAddress().c_str(); String mac = WiFi.macAddress().c_str();
int httpCode = http.POST("?id=" + mac + "&resetReason=" + ESP.getResetReason() + "&lat=" + lat + "&lon=" + lon + "&accuracy=" + accur + ""); int httpCode = http.POST("?id=" + mac + "&resetReason=" + ESP.getResetReason() + "&lat=" + lat + "&lon=" + lon + "&accuracy=" + accur + "");
if (httpCode > 0) { if (httpCode > 0) {
Serial.printf("code: %d\n", httpCode); ret = httpCode;
if (httpCode == HTTP_CODE_OK) { if (httpCode == HTTP_CODE_OK) {
//const String& payload = http.getString(); String payload = http.getString();
ret += " " + payload;
} }
} else { } else {
Serial.printf("error: %s\n", http.errorToString(httpCode).c_str()); ret = http.errorToString(httpCode).c_str();
} }
http.end(); http.end();
} }
return ret;
} }
void updateDeviceStatus(String uptime, String firm) { String updateDeviceStatus() {
String ret;
if ((WiFi.status() == WL_CONNECTED)) { if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client; WiFiClient client;
HTTPClient http; HTTPClient http;
@@ -103,67 +114,59 @@ void updateDeviceStatus(String uptime, String firm) {
http.setAuthorization("admin", "admin"); http.setAuthorization("admin", "admin");
http.addHeader("Content-Type", "application/json"); http.addHeader("Content-Type", "application/json");
String mac = WiFi.macAddress().c_str(); String mac = WiFi.macAddress().c_str();
int httpCode = http.POST("?id=" + mac + "&resetReason=" + ESP.getResetReason() + "&uptime=" + uptime + "&version=" + firm + ""); int httpCode = http.POST("?id=" + mac + "&resetReason=" + ESP.getResetReason() + "&uptime=" + timeNow->getUptime() + "&version=" + FIRMWARE_VERSION + "");
if (httpCode > 0) { if (httpCode > 0) {
Serial.printf("code: %d\n", httpCode); ret = httpCode;
if (httpCode == HTTP_CODE_OK) { if (httpCode == HTTP_CODE_OK) {
//const String& payload = http.getString(); String payload = http.getString();
ret += " " + payload;
} }
} else { } else {
Serial.printf("error: %s\n", http.errorToString(httpCode).c_str()); ret = http.errorToString(httpCode).c_str();
}
http.end();
}
return ret;
}
//========for updating list of device=================
/*
void updateDeviceList() {
if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
String json = "{}";
String mac = WiFi.macAddress().c_str();
//===============================================
jsonWriteStr(json, "uniqueId", mac);
jsonWriteStr(json, "name", FIRMWARE_NAME);
jsonWriteStr(json, "model", FIRMWARE_VERSION);
jsonWriteInt(json, "id", getId("statid.txt"));
//===============================================
http.begin(client, "http://95.128.182.133:8082/api/devices/" + mac + "/");
http.setAuthorization("admin", "admin");
http.addHeader("Content-Type", "application/json");
int httpCode = http.PUT(json);
if (httpCode > 0) {
Serial.printf("update Device List... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
const String& payload = http.getString();
Serial.println("received payload:\n<<");
Serial.println(payload);
Serial.println(">>");
}
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
} }
http.end(); http.end();
} }
} }
void createNewDevJson(String& json, String model) { void saveId(String file, int id) {
String mac = WiFi.macAddress().c_str(); removeFile(file);
jsonWriteStr(json, "name", mac); addFile(file, String(id));
jsonWriteStr(json, "uniqueId", mac);
jsonWriteStr(json, "model", model);
} }
//void updateDeviceList(String model, String firmVer) { int getId(String file) {
// if ((WiFi.status() == WL_CONNECTED)) { return readFile(file, 100).toInt();
// WiFiClient client; }
// HTTPClient http; */
// String json = "{}";
// createUpdateJson(json, model, firmVer);
// String mac = WiFi.macAddress().c_str();
// http.begin(client, "http://95.128.182.133:8082/api/devices/" + mac + "/");
// http.setAuthorization("admin", "admin");
// http.addHeader("Content-Type", "application/json");
// int httpCode = http.PUT(json);
// if (httpCode > 0) {
// Serial.printf("update Device List... code: %d\n", httpCode);
// if (httpCode == HTTP_CODE_OK) {
// const String& payload = http.getString();
// Serial.println("received payload:\n<<");
// Serial.println(payload);
// Serial.println(">>");
// }
// } else {
// Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
// }
// http.end();
// }
//}
//void createUpdateJson(String& json, String model, String ver) {
// String mac = WiFi.macAddress().c_str();
// jsonWriteInt(json, "id", getId("statid.txt"));
// jsonWriteStr(json, "name", mac);
// jsonWriteStr(json, "uniqueId", mac);
// jsonWriteStr(json, "model", model);
// jsonWriteStr(json, "phone", ver);
//}
//void saveId(String file, int id) {
// removeFile(file);
// addFile(file, String(id));
//}
//int getId(String file) {
// return readFile(file, 100).toInt();
//}