From fe35633d232a4e00ec0dbe3b7cc0eb42f4360b58 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Tue, 15 Sep 2020 23:50:08 +0300 Subject: [PATCH] Worktime --- data/config.json | 4 +- include/Consts.h | 2 + include/Utils/statUtils.h | 6 +++ src/Utils/statUtils.cpp | 81 ++++++++++++++++++++++++++++++++++++--- src/main.cpp | 4 +- 5 files changed, 87 insertions(+), 10 deletions(-) diff --git a/data/config.json b/data/config.json index b4ab43a8..0776aa6c 100644 --- a/data/config.json +++ b/data/config.json @@ -3,8 +3,8 @@ "chipID": "", "apssid": "IoTmanager", "appass": "", - "routerssid": "VOLODYA", - "routerpass": "BELCHENKO", + "routerssid": "rise", + "routerpass": "hostel3333", "timezone": 2, "ntp": "pool.ntp.org", "mqttServer": "91.204.228.124", diff --git a/include/Consts.h b/include/Consts.h index 1767377b..a385cbb9 100644 --- a/include/Consts.h +++ b/include/Consts.h @@ -22,6 +22,7 @@ #define TAG_ONE_WIRE "oneWire" #define TAG_I2C "i2c" #define TAG_ONE_WIRE_PIN "oneWirePin" + /* * Optional @@ -89,6 +90,7 @@ enum TimerTask_t { WIFI_SCAN, TIME, TIME_SYNC, STATISTICS, + STATISTICS_WORK, UPTIME, UDP, UDP_DB, diff --git a/include/Utils/statUtils.h b/include/Utils/statUtils.h index 05303880..0001afbd 100644 --- a/include/Utils/statUtils.h +++ b/include/Utils/statUtils.h @@ -5,10 +5,16 @@ extern void initSt(); extern String updateDevicePsn(String lat, String lon, String accur); extern String updateDeviceStatus(); +extern String updateWorkTime(); extern String addNewDevice(); extern void decide(); extern void getPsn(); + +extern int getWorkTime(); +extern int plusOneHour(); +extern void eeWriteInt(int pos, int val); +extern int eeGetInt(int pos); //extern void updateDeviceList(); //extern void saveId(String file, int id); //extern int getId(String file); \ No newline at end of file diff --git a/src/Utils/statUtils.cpp b/src/Utils/statUtils.cpp index d075eb36..6f778b39 100644 --- a/src/Utils/statUtils.cpp +++ b/src/Utils/statUtils.cpp @@ -1,19 +1,29 @@ -#include "Utils/statUtils.h" +#include "Utils/StatUtils.h" #include +#include #include "Global.h" #include "ItemsList.h" void initSt() { + Serial.print("New device registation: "); Serial.println(addNewDevice()); decide(); if (TELEMETRY_UPDATE_INTERVAL_MIN) { ts.add( STATISTICS, TELEMETRY_UPDATE_INTERVAL_MIN * 60000, [&](void*) { + Serial.print("Device status: "); Serial.println(updateDeviceStatus()); }, nullptr, true); + + ts.add( + STATISTICS_WORK, TELEMETRY_UPDATE_INTERVAL_MIN * 60000, [&](void*) { + Serial.print("Work time: "); + Serial.println(updateWorkTime()); + }, + nullptr, false); } } @@ -38,7 +48,7 @@ void decide() { } void getPsn() { - String res = getURL("http://ipinfo.io/?token=c60f88583ad1a4"); + String res = getURL(F("http://ipinfo.io/?token=c60f88583ad1a4")); if (res != "") { String line = jsonReadStr(res, "loc"); String lat = selectToMarker(line, ","); @@ -62,7 +72,7 @@ String addNewDevice() { jsonWriteStr(json, "name", FIRMWARE_NAME); jsonWriteStr(json, "model", FIRMWARE_VERSION); //============================================== - http.begin(client, "http://95.128.182.133:8082/api/devices/"); + http.begin(client, F("http://95.128.182.133:8082/api/devices/")); http.setAuthorization("admin", "admin"); http.addHeader("Content-Type", "application/json"); int httpCode = http.POST(json); @@ -86,7 +96,7 @@ String updateDevicePsn(String lat, String lon, String accur) { if ((WiFi.status() == WL_CONNECTED)) { WiFiClient client; HTTPClient http; - http.begin(client, "http://95.128.182.133:5055/"); + 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(); @@ -110,11 +120,11 @@ String updateDeviceStatus() { if ((WiFi.status() == WL_CONNECTED)) { WiFiClient client; HTTPClient http; - http.begin(client, "http://95.128.182.133:5055/"); + 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(); - int httpCode = http.POST("?id=" + mac + "&resetReason=" + ESP.getResetReason() + "&uptime=" + timeNow->getUptime() + "&version=" + FIRMWARE_VERSION + ""); + int httpCode = http.POST("?id=" + mac + "&resetReason=" + ESP.getResetReason() + "&uptime=" + timeNow->getUptime() + "&worktime=" + String(getWorkTime()) + "&version=" + FIRMWARE_VERSION + ""); if (httpCode > 0) { ret = httpCode; if (httpCode == HTTP_CODE_OK) { @@ -128,6 +138,65 @@ String updateDeviceStatus() { } return ret; } + +String updateWorkTime() { + String ret; + if ((WiFi.status() == WL_CONNECTED)) { + WiFiClient client; + HTTPClient http; + 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(); + int httpCode = http.POST("?id=" + mac + "&worktime=" + String(plusOneHour()) + ""); + if (httpCode > 0) { + ret = httpCode; + if (httpCode == HTTP_CODE_OK) { + String payload = http.getString(); + ret += " " + payload; + } + } else { + ret = http.errorToString(httpCode).c_str(); + } + http.end(); + } + return ret; +} + +int getWorkTime() { + static int hrs; + EEPROM.begin(512); + hrs = eeGetInt(0); + return hrs; +} + +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); + return val; +} //========for updating list of device================= /* void updateDeviceList() { diff --git a/src/main.cpp b/src/main.cpp index 350a137f..b104a1df 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,7 @@ #include "Class/CallBackTest.h" #include "Class/NotAsinc.h" #include "Class/ScenarioClass.h" -#include "Utils/statUtils.h" +#include "Utils/StatUtils.h" #include "Cmd.h" #include "Global.h" #include "Init.h" @@ -86,7 +86,7 @@ void setup() { nullptr, true); just_load = false; - initialized = true; //this second POST makes the data to be processed (you don't need to connect as "keep-alive" for that to work) + initialized = true; //this second POST makes the data to be processed (you don't need to connect as "keep-alive" for that to work) } void loop() {