2020-09-15 23:50:08 +03:00
|
|
|
#include "Utils/StatUtils.h"
|
2020-09-09 16:44:19 +03:00
|
|
|
|
2020-09-07 00:36:03 +03:00
|
|
|
#include <Arduino.h>
|
2020-09-15 23:50:08 +03:00
|
|
|
#include <EEPROM.h>
|
2020-09-09 16:44:19 +03:00
|
|
|
|
2020-09-07 00:36:03 +03:00
|
|
|
#include "Global.h"
|
2020-09-07 02:22:13 +03:00
|
|
|
#include "ItemsList.h"
|
2020-09-06 23:00:33 +03:00
|
|
|
|
2020-09-17 17:27:44 +03:00
|
|
|
|
2020-09-06 23:00:33 +03:00
|
|
|
void initSt() {
|
2020-09-17 17:27:44 +03:00
|
|
|
addNewDevice();
|
2020-09-07 02:22:13 +03:00
|
|
|
decide();
|
2020-09-06 23:00:33 +03:00
|
|
|
if (TELEMETRY_UPDATE_INTERVAL_MIN) {
|
|
|
|
|
ts.add(
|
|
|
|
|
STATISTICS, TELEMETRY_UPDATE_INTERVAL_MIN * 60000, [&](void*) {
|
2020-09-16 15:16:01 +03:00
|
|
|
static bool secondTime = false;
|
2020-09-17 18:32:39 +03:00
|
|
|
if (secondTime) getNextNumber("totalhrs.txt");
|
2020-09-16 15:16:01 +03:00
|
|
|
secondTime = true;
|
2020-09-17 17:27:44 +03:00
|
|
|
updateDeviceStatus();
|
2020-09-06 23:00:33 +03:00
|
|
|
},
|
|
|
|
|
nullptr, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 02:22:13 +03:00
|
|
|
void decide() {
|
2020-09-07 01:32:09 +03:00
|
|
|
if ((WiFi.status() == WL_CONNECTED)) {
|
2020-09-17 18:32:39 +03:00
|
|
|
uint8_t cnt = getNextNumber("stat.txt");
|
2020-09-17 18:04:06 +03:00
|
|
|
SerialPrint("I","Stat","Total resets number: " + String(cnt));
|
2020-09-09 16:44:19 +03:00
|
|
|
if (cnt <= 3) {
|
2020-09-17 17:27:44 +03:00
|
|
|
//Serial.println("(get)");
|
2020-09-07 02:22:13 +03:00
|
|
|
getPsn();
|
|
|
|
|
} else {
|
2020-09-09 16:44:19 +03:00
|
|
|
if (cnt % 5) {
|
2020-09-17 17:27:44 +03:00
|
|
|
//Serial.println("(skip)");
|
2020-09-07 02:22:13 +03:00
|
|
|
} else {
|
2020-09-17 17:27:44 +03:00
|
|
|
//Serial.println("(get)");
|
2020-09-07 02:22:13 +03:00
|
|
|
getPsn();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-07 01:32:09 +03:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-07 02:22:13 +03:00
|
|
|
|
|
|
|
|
void getPsn() {
|
2020-09-15 23:50:08 +03:00
|
|
|
String res = getURL(F("http://ipinfo.io/?token=c60f88583ad1a4"));
|
2020-09-07 17:00:22 +03:00
|
|
|
if (res != "") {
|
|
|
|
|
String line = jsonReadStr(res, "loc");
|
|
|
|
|
String lat = selectToMarker(line, ",");
|
|
|
|
|
String lon = deleteBeforeDelimiter(line, ",");
|
|
|
|
|
//String city = jsonReadStr(res, "city");
|
|
|
|
|
//String country = jsonReadStr(res, "country");
|
|
|
|
|
//String region = jsonReadStr(res, "region");
|
2020-09-17 17:27:44 +03:00
|
|
|
updateDevicePsn(lat, lon, "1000");
|
2020-09-07 17:00:22 +03:00
|
|
|
}
|
2020-09-07 02:22:13 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-07 17:00:22 +03:00
|
|
|
String addNewDevice() {
|
|
|
|
|
String ret;
|
2020-09-06 23:00:33 +03:00
|
|
|
if ((WiFi.status() == WL_CONNECTED)) {
|
|
|
|
|
WiFiClient client;
|
|
|
|
|
HTTPClient http;
|
|
|
|
|
String json = "{}";
|
|
|
|
|
String mac = WiFi.macAddress().c_str();
|
2020-09-07 17:00:22 +03:00
|
|
|
//==============================================
|
|
|
|
|
jsonWriteStr(json, "uniqueId", mac);
|
|
|
|
|
jsonWriteStr(json, "name", FIRMWARE_NAME);
|
2020-09-17 21:33:54 +03:00
|
|
|
jsonWriteInt(json, "model", FIRMWARE_VERSION);
|
2020-09-07 17:00:22 +03:00
|
|
|
//==============================================
|
2020-09-15 23:50:08 +03:00
|
|
|
http.begin(client, F("http://95.128.182.133:8082/api/devices/"));
|
2020-09-06 23:00:33 +03:00
|
|
|
http.setAuthorization("admin", "admin");
|
|
|
|
|
http.addHeader("Content-Type", "application/json");
|
|
|
|
|
int httpCode = http.POST(json);
|
|
|
|
|
if (httpCode > 0) {
|
2020-09-07 17:00:22 +03:00
|
|
|
ret = httpCode;
|
2020-09-06 23:00:33 +03:00
|
|
|
if (httpCode == HTTP_CODE_OK) {
|
2020-09-07 17:00:22 +03:00
|
|
|
String payload = http.getString();
|
|
|
|
|
ret += " " + payload;
|
2020-09-06 23:00:33 +03:00
|
|
|
//saveId("statid.txt", jsonReadInt(payload, "id"));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-09-07 17:00:22 +03:00
|
|
|
ret = http.errorToString(httpCode).c_str();
|
2020-09-06 23:00:33 +03:00
|
|
|
}
|
|
|
|
|
http.end();
|
|
|
|
|
}
|
2020-09-17 18:04:06 +03:00
|
|
|
SerialPrint("I","Stat","New device registaration: " + ret);
|
2020-09-07 17:00:22 +03:00
|
|
|
return ret;
|
2020-09-06 23:00:33 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-07 17:00:22 +03:00
|
|
|
String updateDevicePsn(String lat, String lon, String accur) {
|
|
|
|
|
String ret;
|
2020-09-06 23:00:33 +03:00
|
|
|
if ((WiFi.status() == WL_CONNECTED)) {
|
|
|
|
|
WiFiClient client;
|
|
|
|
|
HTTPClient http;
|
2020-09-15 23:50:08 +03:00
|
|
|
http.begin(client, F("http://95.128.182.133:5055/"));
|
2020-09-06 23:00:33 +03:00
|
|
|
http.setAuthorization("admin", "admin");
|
|
|
|
|
http.addHeader("Content-Type", "application/json");
|
|
|
|
|
String mac = WiFi.macAddress().c_str();
|
2020-09-16 15:16:01 +03:00
|
|
|
int httpCode = http.POST("?id=" + mac +
|
|
|
|
|
"&resetReason=" + ESP.getResetReason() +
|
|
|
|
|
"&lat=" + lat +
|
|
|
|
|
"&lon=" + lon +
|
|
|
|
|
"&accuracy=" + accur + "");
|
2020-09-06 23:00:33 +03:00
|
|
|
if (httpCode > 0) {
|
2020-09-07 17:00:22 +03:00
|
|
|
ret = httpCode;
|
2020-09-07 00:36:03 +03:00
|
|
|
if (httpCode == HTTP_CODE_OK) {
|
2020-09-07 17:00:22 +03:00
|
|
|
String payload = http.getString();
|
|
|
|
|
ret += " " + payload;
|
2020-09-07 00:36:03 +03:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-09-07 17:00:22 +03:00
|
|
|
ret = http.errorToString(httpCode).c_str();
|
2020-09-07 00:36:03 +03:00
|
|
|
}
|
|
|
|
|
http.end();
|
|
|
|
|
}
|
2020-09-17 18:04:06 +03:00
|
|
|
SerialPrint("I","Stat","Update device psn: " + ret);
|
2020-09-07 17:00:22 +03:00
|
|
|
return ret;
|
2020-09-07 00:36:03 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-07 17:00:22 +03:00
|
|
|
String updateDeviceStatus() {
|
|
|
|
|
String ret;
|
2020-09-07 00:36:03 +03:00
|
|
|
if ((WiFi.status() == WL_CONNECTED)) {
|
|
|
|
|
WiFiClient client;
|
|
|
|
|
HTTPClient http;
|
2020-09-15 23:50:08 +03:00
|
|
|
http.begin(client, F("http://95.128.182.133:5055/"));
|
|
|
|
|
http.setAuthorization("admin", "admin");
|
|
|
|
|
http.addHeader("Content-Type", "application/json");
|
|
|
|
|
String mac = WiFi.macAddress().c_str();
|
2020-09-16 15:16:01 +03:00
|
|
|
int httpCode = http.POST("?id=" + mac +
|
|
|
|
|
"&resetReason=" + ESP.getResetReason() +
|
|
|
|
|
"&uptime=" + timeNow->getUptime() +
|
|
|
|
|
"&uptimeTotal=" + getUptimeTotal() +
|
2020-09-17 18:32:39 +03:00
|
|
|
"&version=" + FIRMWARE_VERSION +
|
|
|
|
|
"&resetsTotal=" + String(getCurrentNumber("stat.txt")) + "");
|
2020-09-07 00:36:03 +03:00
|
|
|
if (httpCode > 0) {
|
2020-09-07 17:00:22 +03:00
|
|
|
ret = httpCode;
|
2020-09-06 23:00:33 +03:00
|
|
|
if (httpCode == HTTP_CODE_OK) {
|
2020-09-07 17:00:22 +03:00
|
|
|
String payload = http.getString();
|
|
|
|
|
ret += " " + payload;
|
2020-09-06 23:00:33 +03:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-09-07 17:00:22 +03:00
|
|
|
ret = http.errorToString(httpCode).c_str();
|
2020-09-06 23:00:33 +03:00
|
|
|
}
|
|
|
|
|
http.end();
|
|
|
|
|
}
|
2020-09-17 18:04:06 +03:00
|
|
|
SerialPrint("I","Stat","Update device data: " + ret);
|
2020-09-07 17:00:22 +03:00
|
|
|
return ret;
|
2020-09-06 23:00:33 +03:00
|
|
|
}
|
2020-09-15 23:50:08 +03:00
|
|
|
|
2020-09-16 15:16:01 +03:00
|
|
|
String getUptimeTotal() {
|
2020-09-17 18:32:39 +03:00
|
|
|
uint8_t hrs = getCurrentNumber("totalhrs.txt");
|
2020-09-17 00:21:14 +03:00
|
|
|
String hrsStr = prettySeconds(hrs * 60 * 60);
|
2020-09-17 18:32:39 +03:00
|
|
|
SerialPrint("I","Stat","Total running time: " + hrsStr);
|
2020-09-16 15:16:01 +03:00
|
|
|
return hrsStr;
|
2020-09-15 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-17 18:32:39 +03:00
|
|
|
uint8_t getNextNumber(String file) {
|
|
|
|
|
uint8_t number = readFile(file, 100).toInt();
|
|
|
|
|
number++;
|
|
|
|
|
removeFile(file);
|
|
|
|
|
addFile(file, String(number));
|
|
|
|
|
return number;
|
2020-09-15 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-17 18:32:39 +03:00
|
|
|
uint8_t getCurrentNumber(String file) {
|
|
|
|
|
uint8_t number = readFile(file, 100).toInt();
|
|
|
|
|
return number;
|
2020-09-15 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-17 18:32:39 +03:00
|
|
|
|
|
|
|
|
//String getUptimeTotal() {
|
|
|
|
|
// static int hrs;
|
|
|
|
|
// EEPROM.begin(512);
|
|
|
|
|
// hrs = eeGetInt(0);
|
|
|
|
|
// SerialPrint("I","Stat","Total running hrs: " + String(hrs));
|
|
|
|
|
// String hrsStr = prettySeconds(hrs * 60 * 60);
|
|
|
|
|
// SerialPrint("I","Stat","Total running hrs (f): " + hrsStr);
|
|
|
|
|
// return hrsStr;
|
|
|
|
|
//}
|
|
|
|
|
//int plusOneHour() {
|
|
|
|
|
// static int hrs;
|
|
|
|
|
// EEPROM.begin(512);
|
|
|
|
|
// hrs = eeGetInt(0);
|
|
|
|
|
// hrs++;
|
|
|
|
|
// eeWriteInt(0, hrs);
|
|
|
|
|
// return hrs;
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//void eeWriteInt(int pos, int val) {
|
|
|
|
|
// byte* p = (byte*)&val;
|
|
|
|
|
// EEPROM.write(pos, *p);
|
|
|
|
|
// EEPROM.write(pos + 1, *(p + 1));
|
|
|
|
|
// EEPROM.write(pos + 2, *(p + 2));
|
|
|
|
|
// EEPROM.write(pos + 3, *(p + 3));
|
|
|
|
|
// EEPROM.commit();
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//int eeGetInt(int pos) {
|
|
|
|
|
// int val;
|
|
|
|
|
// byte* p = (byte*)&val;
|
|
|
|
|
// *p = EEPROM.read(pos);
|
|
|
|
|
// *(p + 1) = EEPROM.read(pos + 1);
|
|
|
|
|
// *(p + 2) = EEPROM.read(pos + 2);
|
|
|
|
|
// *(p + 3) = EEPROM.read(pos + 3);
|
|
|
|
|
// if (val < 0) {
|
|
|
|
|
// return 0;
|
|
|
|
|
// } else {
|
|
|
|
|
// return val;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2020-09-07 17:00:22 +03:00
|
|
|
//========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();
|
|
|
|
|
}
|
2020-09-06 23:00:33 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-07 17:00:22 +03:00
|
|
|
void saveId(String file, int id) {
|
|
|
|
|
removeFile(file);
|
|
|
|
|
addFile(file, String(id));
|
|
|
|
|
}
|
2020-09-06 23:00:33 +03:00
|
|
|
|
2020-09-07 17:00:22 +03:00
|
|
|
int getId(String file) {
|
|
|
|
|
return readFile(file, 100).toInt();
|
|
|
|
|
}
|
|
|
|
|
*/
|