mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-29 07:32:18 +03:00
getData
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
#include "Clock.h"
|
||||
|
||||
#include "Utils/TimeUtils.h"
|
||||
|
||||
static const char* MODULE = "Clock";
|
||||
|
||||
void startTimeSync() {
|
||||
if (!hasTimeSynced()) {
|
||||
pm.info("Start sync");
|
||||
reconfigTime();
|
||||
}
|
||||
}
|
||||
|
||||
void setupSntp() {
|
||||
int tzs = getBiasInSeconds();
|
||||
int tzh = tzs / 3600;
|
||||
tzs -= tzh * 3600;
|
||||
int tzm = tzs / 60;
|
||||
tzs -= tzm * 60;
|
||||
|
||||
String ntp = jsonReadStr(configSetupJson, "ntp");
|
||||
pm.info("Setup ntp: " + ntp);
|
||||
char tzstr[64];
|
||||
snprintf(tzstr, sizeof tzstr, "ESPUSER<%+d:%02d:%02d>", tzh, tzm, tzs);
|
||||
return configTime(tzstr, ntp.c_str(), "pool.ntp.org", "time.nist.gov");
|
||||
}
|
||||
|
||||
void reconfigTime() {
|
||||
#ifdef ESP32
|
||||
uint8_t i = 0;
|
||||
struct tm timeinfo;
|
||||
while (!getLocalTime(&timeinfo) && i <= 4) {
|
||||
Serial.print(".");
|
||||
i++;
|
||||
delay(1000);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ESP8266
|
||||
setupSntp();
|
||||
uint8_t i = 0;
|
||||
while (!hasTimeSynced() && i < 4) {
|
||||
Serial.print(".");
|
||||
i++;
|
||||
delay(30);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (getTimeUnix() != "failed") {
|
||||
pm.info("Time synced " + getDateDigitalFormated() + " " + getTime());
|
||||
} else {
|
||||
pm.error("Failed to obtain");
|
||||
}
|
||||
}
|
||||
54
src/Cmd.cpp
54
src/Cmd.cpp
@@ -13,6 +13,8 @@ Servo myServo1;
|
||||
Servo myServo2;
|
||||
SoftwareSerial *mySerial = nullptr;
|
||||
|
||||
void getData();
|
||||
|
||||
void CMD_init() {
|
||||
sCmd.addCommand("button", button);
|
||||
sCmd.addCommand("buttonSet", buttonSet);
|
||||
@@ -97,36 +99,38 @@ void CMD_init() {
|
||||
sCmd.addCommand("firmwareUpdate", firmwareUpdate);
|
||||
sCmd.addCommand("firmwareVersion", firmwareVersion);
|
||||
|
||||
sCmd.addCommand("getData", getData);
|
||||
|
||||
handle_time_init();
|
||||
}
|
||||
|
||||
//==========================================================================================================
|
||||
//==========================================Модуль кнопок===================================================
|
||||
void button() {
|
||||
pm.info("create button");
|
||||
String button_number = sCmd.next();
|
||||
String button_param = sCmd.next();
|
||||
pm.info("create 'button'");
|
||||
String number = sCmd.next();
|
||||
String param = sCmd.next();
|
||||
String widget = sCmd.next();
|
||||
String page = sCmd.next();
|
||||
String start_state = sCmd.next();
|
||||
String state = sCmd.next();
|
||||
String pageNumber = sCmd.next();
|
||||
|
||||
jsonWriteStr(configOptionJson, "button_param" + button_number, button_param);
|
||||
jsonWriteStr(configLiveJson, "button" + button_number, start_state);
|
||||
jsonWriteStr(configOptionJson, "button_param" + number, param);
|
||||
jsonWriteStr(configLiveJson, "button" + number, state);
|
||||
|
||||
if (isDigitStr(button_param)) {
|
||||
pinMode(button_param.toInt(), OUTPUT);
|
||||
digitalWrite(button_param.toInt(), start_state.toInt());
|
||||
if (isDigitStr(param)) {
|
||||
pinMode(param.toInt(), OUTPUT);
|
||||
digitalWrite(param.toInt(), state.toInt());
|
||||
}
|
||||
|
||||
if (button_param == "scen") {
|
||||
jsonWriteStr(configSetupJson, "scen", start_state);
|
||||
if (param == "scen") {
|
||||
jsonWriteStr(configSetupJson, "scen", state);
|
||||
loadScenario();
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
if (button_param.indexOf("line") != -1) {
|
||||
String str = button_param;
|
||||
if (param.indexOf("line") != -1) {
|
||||
String str = param;
|
||||
while (str.length()) {
|
||||
if (str == "") return;
|
||||
String tmp = selectToMarker(str, ","); //line1,
|
||||
@@ -134,11 +138,11 @@ void button() {
|
||||
number.replace(",", "");
|
||||
Serial.println(number);
|
||||
int number_int = number.toInt();
|
||||
scenario_line_status[number_int] = start_state.toInt();
|
||||
scenario_line_status[number_int] = state.toInt();
|
||||
str = deleteBeforeDelimiter(str, ",");
|
||||
}
|
||||
}
|
||||
createWidget(widget, page, pageNumber, "toggle", "button" + button_number);
|
||||
createWidget(widget, page, pageNumber, "toggle", "button" + number);
|
||||
}
|
||||
|
||||
void buttonSet() {
|
||||
@@ -547,6 +551,14 @@ void serialBegin() {
|
||||
});
|
||||
}
|
||||
|
||||
void getData() {
|
||||
String param = sCmd.next();
|
||||
String res = param.length() ? jsonReadStr(configLiveJson, param) : configLiveJson;
|
||||
if (term) {
|
||||
term->println(res.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void serialWrite() {
|
||||
String payload = sCmd.next();
|
||||
if (term) {
|
||||
@@ -578,11 +590,13 @@ void firmwareUpdate() {
|
||||
}
|
||||
|
||||
void firmwareVersion() {
|
||||
String widget_name = sCmd.next();
|
||||
String page_name = sCmd.next();
|
||||
String page_number = sCmd.next();
|
||||
String widget = sCmd.next();
|
||||
String page = sCmd.next();
|
||||
String pageNumber = sCmd.next();
|
||||
|
||||
jsonWriteStr(configLiveJson, "firmver", FIRMWARE_VERSION);
|
||||
createWidgetByType(widget_name, page_name, page_number, "anydata", "firmver");
|
||||
|
||||
createWidget(widget, page, pageNumber, "anydata", "firmver");
|
||||
}
|
||||
|
||||
void addCommandLoop(const String &cmdStr) {
|
||||
@@ -596,7 +610,7 @@ void loopCmd() {
|
||||
if (order_loop.length()) {
|
||||
String tmp = selectToMarker(order_loop, ","); //выделяем первую команду rel 5 1,
|
||||
sCmd.readStr(tmp); //выполняем
|
||||
Serial.println("[ORDER] => " + order_loop);
|
||||
pm.info("do: " + order_loop);
|
||||
order_loop = deleteBeforeDelimiter(order_loop, ","); //осекаем
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ AsyncWebSocket ws;
|
||||
//AsyncEventSource events;
|
||||
#endif
|
||||
|
||||
Clock* rtc;
|
||||
|
||||
TickerScheduler ts(TEST + 1);
|
||||
|
||||
WiFiClient espClient;
|
||||
|
||||
@@ -8,6 +8,12 @@ String jsonReadStr(String& json, String name) {
|
||||
return root[name].as<String>();
|
||||
}
|
||||
|
||||
boolean jsonReadBool(String& json, String name) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& root = jsonBuffer.parseObject(json);
|
||||
return root[name].as<boolean>();
|
||||
}
|
||||
|
||||
int jsonReadInt(String& json, String name) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& root = jsonBuffer.parseObject(json);
|
||||
|
||||
@@ -23,25 +23,24 @@ static uint32_t total_memory = 52864;
|
||||
static uint32_t total_memory = 362868;
|
||||
#endif
|
||||
|
||||
void printMemoryStatus(String text) {
|
||||
const String printMemoryStatus() {
|
||||
uint32_t free = ESP.getFreeHeap();
|
||||
uint32_t used = total_memory - free;
|
||||
uint32_t memory_load = (used * 100) / total_memory;
|
||||
if (text) {
|
||||
Serial.print(text);
|
||||
}
|
||||
Serial.printf(" used: %d%% free: %s\n", memory_load, prettyBytes(free).c_str());
|
||||
char buf[64];
|
||||
sprintf(buf, "used: %d%% free: %s", memory_load, getHeapStats().c_str());
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
String getHeapStats() {
|
||||
const String getHeapStats() {
|
||||
uint32_t free;
|
||||
uint16_t max;
|
||||
uint8_t frag;
|
||||
ESP.getHeapStats(&free, &max, &frag);
|
||||
String buf;
|
||||
buf += prettyBytes(free);
|
||||
buf += " ";
|
||||
buf += " frag: ";
|
||||
buf += frag;
|
||||
buf += '%';
|
||||
return buf;
|
||||
|
||||
@@ -6,33 +6,17 @@
|
||||
#define ONE_MINUTE_s 60
|
||||
#define ONE_HOUR_s 60 * ONE_MINUTE_s
|
||||
|
||||
int getBiasInSeconds() {
|
||||
return 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
||||
}
|
||||
|
||||
int getBiasInMinutes() {
|
||||
return getBiasInSeconds() / 60;
|
||||
}
|
||||
|
||||
const timezone getTimeZone() {
|
||||
return timezone{getBiasInMinutes(), 0};
|
||||
}
|
||||
|
||||
time_t getSystemTime() {
|
||||
timeval tv{0, 0};
|
||||
timezone tz = getTimeZone();
|
||||
time_t epoch = 0;
|
||||
if (gettimeofday(&tv, &tz) != -1)
|
||||
epoch = tv.tv_sec;
|
||||
return epoch;
|
||||
}
|
||||
|
||||
bool hasTimeSynced() {
|
||||
unsigned long now = time(nullptr);
|
||||
return now > millis();
|
||||
}
|
||||
|
||||
time_t t;
|
||||
struct tm* tm;
|
||||
static const char* wd[7] = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"};
|
||||
String getTimeUnix() {
|
||||
t = time(NULL);
|
||||
tm = localtime(&t);
|
||||
Serial.printf("%04d/%02d/%02d(%s) %02d:%02d:%02d\n",
|
||||
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
|
||||
wd[tm->tm_wday],
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||
delay(1000);
|
||||
time_t now = time(nullptr);
|
||||
if (now < 30000) {
|
||||
return "failed";
|
||||
@@ -149,127 +133,6 @@ const String prettyMillis(unsigned long time_ms) {
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
int timeZoneInSeconds(const byte timeZone) {
|
||||
int res = 0;
|
||||
switch (constrain(timeZone, 1, 38)) {
|
||||
case 1:
|
||||
res = -12 * ONE_HOUR_s;
|
||||
break;
|
||||
case 2:
|
||||
res = -11 * ONE_HOUR_s;
|
||||
break;
|
||||
case 3:
|
||||
res = -10 * ONE_HOUR_s;
|
||||
break;
|
||||
case 4:
|
||||
res = -9 * ONE_HOUR_s - 30 * ONE_MINUTE_s;
|
||||
break;
|
||||
case 5:
|
||||
res = -9 * ONE_HOUR_s;
|
||||
break;
|
||||
case 6:
|
||||
res = -8 * ONE_HOUR_s;
|
||||
break;
|
||||
case 7:
|
||||
res = -7 * ONE_HOUR_s;
|
||||
break;
|
||||
case 8:
|
||||
res = -6 * ONE_HOUR_s;
|
||||
break;
|
||||
case 9:
|
||||
res = -5 * ONE_HOUR_s;
|
||||
break;
|
||||
case 10:
|
||||
res = -4 * ONE_HOUR_s;
|
||||
break;
|
||||
case 11:
|
||||
res = -3 * ONE_HOUR_s - 30 * ONE_MINUTE_s;
|
||||
break;
|
||||
case 12:
|
||||
res = -3 * ONE_HOUR_s;
|
||||
break;
|
||||
case 13:
|
||||
res = -2 * ONE_HOUR_s;
|
||||
break;
|
||||
case 14:
|
||||
res = -1 * ONE_HOUR_s;
|
||||
break;
|
||||
case 15:
|
||||
res = 0;
|
||||
break;
|
||||
case 16:
|
||||
res = 1 * ONE_HOUR_s;
|
||||
break;
|
||||
case 17:
|
||||
res = 2 * ONE_HOUR_s;
|
||||
break;
|
||||
case 18:
|
||||
res = 3 * ONE_HOUR_s;
|
||||
break;
|
||||
case 19:
|
||||
res = 3 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||
break;
|
||||
case 20:
|
||||
res = 4 * ONE_HOUR_s;
|
||||
break;
|
||||
case 21:
|
||||
res = 4 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||
break;
|
||||
case 22:
|
||||
res = 5 * ONE_HOUR_s;
|
||||
break;
|
||||
case 23:
|
||||
res = 5 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||
break;
|
||||
case 24:
|
||||
res = 5 * ONE_HOUR_s + 45 * ONE_MINUTE_s;
|
||||
break;
|
||||
case 25:
|
||||
res = 6 * ONE_HOUR_s;
|
||||
break;
|
||||
case 26:
|
||||
res = 6 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||
break;
|
||||
case 27:
|
||||
res = 7 * ONE_HOUR_s;
|
||||
break;
|
||||
case 28:
|
||||
res = 8 * ONE_HOUR_s;
|
||||
break;
|
||||
case 29:
|
||||
res = 8 * ONE_HOUR_s + 45 * ONE_MINUTE_s;
|
||||
break;
|
||||
case 30:
|
||||
res = 9 * ONE_HOUR_s;
|
||||
break;
|
||||
case 31:
|
||||
res = 9 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||
break;
|
||||
case 32:
|
||||
res = 10 * ONE_HOUR_s;
|
||||
break;
|
||||
case 33:
|
||||
res = 10 * ONE_HOUR_s + 30 * ONE_MINUTE_s;
|
||||
break;
|
||||
case 34:
|
||||
res = 11 * ONE_HOUR_s;
|
||||
break;
|
||||
case 35:
|
||||
res = 12 * ONE_HOUR_s;
|
||||
break;
|
||||
case 36:
|
||||
res = 12 * ONE_HOUR_s + 45 * ONE_MINUTE_s;
|
||||
break;
|
||||
case 37:
|
||||
res = 13 * ONE_HOUR_s;
|
||||
break;
|
||||
case 38:
|
||||
res = 14 * ONE_HOUR_s;
|
||||
break;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
unsigned long millis_since(unsigned long sinse) {
|
||||
return millis_passed(sinse, millis());
|
||||
}
|
||||
|
||||
10
src/Web.cpp
10
src/Web.cpp
@@ -130,15 +130,17 @@ void web_init() {
|
||||
}
|
||||
//--------------------------------------------------------------------------------
|
||||
if (request->hasArg("timezone")) {
|
||||
jsonWriteStr(configSetupJson, "timezone", request->getParam("timezone")->value());
|
||||
String timezoneStr = request->getParam("timezone")->value();
|
||||
jsonWriteStr(configSetupJson, "timezone", timezoneStr);
|
||||
saveConfig();
|
||||
reconfigTime();
|
||||
rtc->setTimezone(timezoneStr.toInt());
|
||||
request->send(200, "text/text", "OK");
|
||||
}
|
||||
if (request->hasArg("ntp")) {
|
||||
jsonWriteStr(configSetupJson, "ntp", request->getParam("ntp")->value());
|
||||
String ntpStr = request->getParam("ntp")->value();
|
||||
jsonWriteStr(configSetupJson, "ntp", ntpStr);
|
||||
saveConfig();
|
||||
reconfigTime();
|
||||
rtc->setNtpPool(ntpStr);
|
||||
request->send(200, "text/text", "OK");
|
||||
}
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
10
src/main.cpp
10
src/main.cpp
@@ -56,10 +56,14 @@ void setup() {
|
||||
pm.info("WebAdmin");
|
||||
web_init();
|
||||
|
||||
pm.info("TimeSync");
|
||||
pm.info("Clock");
|
||||
rtc = new Clock();
|
||||
rtc->setNtpPool(jsonReadStr(configSetupJson, "ntp"));
|
||||
rtc->setTimezone(jsonReadStr(configSetupJson, "timezone").toInt());
|
||||
|
||||
ts.add(
|
||||
TIME_SYNC, 30000, [&](void*) {
|
||||
startTimeSync();
|
||||
rtc->hasSync();
|
||||
},
|
||||
nullptr, true);
|
||||
|
||||
@@ -69,7 +73,7 @@ void setup() {
|
||||
#endif
|
||||
ts.add(
|
||||
TEST, 10000, [&](void*) {
|
||||
printMemoryStatus();
|
||||
pm.info(printMemoryStatus());
|
||||
},
|
||||
nullptr, true);
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ void add_dev_in_list(String fileName, String id, String dev_name, String ip);
|
||||
|
||||
#ifdef UDP_ENABLED
|
||||
void UDP_init() {
|
||||
removeFile("/dev.csv");
|
||||
removeFile("dev.csv");
|
||||
addFile("dev.csv", "device id;device name;ip address");
|
||||
|
||||
#ifdef ESP8266
|
||||
|
||||
Reference in New Issue
Block a user