mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 14:12:16 +03:00
добавил получение времени NTP
This commit is contained in:
@@ -35,6 +35,9 @@
|
||||
#define USE_LITTLEFS false
|
||||
#endif
|
||||
|
||||
#define MIN_DATETIME 1575158400
|
||||
#define LEAP_YEAR(Y) (((1970 + Y) > 0) && !((1970 + Y) % 4) && (((1970 + Y) % 100) || !((1970 + Y) % 400)))
|
||||
|
||||
//задачи таскера
|
||||
enum TimerTask_t { WIFI_SCAN,
|
||||
WIFI_MQTT_CONNECTION_CHECK,
|
||||
|
||||
@@ -115,6 +115,23 @@ extern String prex;
|
||||
extern String all_widgets;
|
||||
extern String scenario;
|
||||
|
||||
// Time
|
||||
struct Time_t {
|
||||
uint8_t second;
|
||||
uint8_t minute;
|
||||
uint8_t hour;
|
||||
uint8_t day_of_week;
|
||||
uint8_t day_of_month;
|
||||
uint8_t month;
|
||||
uint16_t day_of_year;
|
||||
uint16_t year;
|
||||
unsigned long days;
|
||||
unsigned long valid;
|
||||
};
|
||||
|
||||
extern Time_t _time_local;
|
||||
extern Time_t _time_utc;
|
||||
|
||||
// extern DynamicJsonDocument settingsFlashJsonDoc;
|
||||
// extern DynamicJsonDocument paramsFlashJsonDoc;
|
||||
// extern DynamicJsonDocument paramsHeapJsonDoc;
|
||||
@@ -13,3 +13,4 @@
|
||||
#include "PeriodicTasks.h"
|
||||
#include "classes/IoTScenario.h"
|
||||
#include "EventsAndOrders.h"
|
||||
#include "NTP.h"
|
||||
|
||||
13
include/NTP.h
Normal file
13
include/NTP.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include "Arduino.h"
|
||||
#include "Global.h"
|
||||
|
||||
static const uint8_t days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
|
||||
extern void breakEpochToTime(unsigned long epoch, Time_t& tm);
|
||||
extern void ntpInit();
|
||||
extern time_t getSystemTime();
|
||||
extern void synchTime();
|
||||
extern const String getTimeLocal_hhmm();
|
||||
extern const String getTimeLocal_hhmmss();
|
||||
extern const String getDateTimeDotFormated();
|
||||
@@ -1,65 +1,5 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "Global.h"
|
||||
|
||||
#define ONE_MINUTE_s 60
|
||||
#define ONE_HOUR_m 60
|
||||
#define ONE_HOUR_s 60 * ONE_MINUTE_s
|
||||
#define LEAP_YEAR(Y) (((1970 + Y) > 0) && !((1970 + Y) % 4) && (((1970 + Y) % 100) || !((1970 + Y) % 400)))
|
||||
#define MIN_DATETIME 1575158400
|
||||
#define ONE_SECOND_ms 1000
|
||||
|
||||
/*
|
||||
* Время (мс) прошедщее с @since
|
||||
*/
|
||||
unsigned long millis_since(unsigned long sinse);
|
||||
|
||||
/*
|
||||
* Интерввал времени (мс) между @start и @finish
|
||||
*/
|
||||
unsigned long millis_passed(unsigned long start, unsigned long finish);
|
||||
|
||||
/*
|
||||
* Форматиронное время интервала (мс)
|
||||
* "чч:мм:cc",
|
||||
* "дд чч:мм", если > 24 часов
|
||||
*/
|
||||
const String prettyMillis(unsigned long time_ms = millis());
|
||||
|
||||
/*
|
||||
* Форматиронное время интервала (c)
|
||||
* "чч:мм:cc",
|
||||
* "дд чч:мм", если > 24 часов
|
||||
*/
|
||||
const String prettySeconds(unsigned long time_s);
|
||||
|
||||
/*
|
||||
* Тайм зона в секундах
|
||||
*/
|
||||
int getOffsetInSeconds(int timezone);
|
||||
|
||||
/*
|
||||
* Тайм зона в минутах
|
||||
*/
|
||||
int getOffsetInMinutes(int timezone);
|
||||
|
||||
/*
|
||||
* Разбивает время на составляющие
|
||||
*/
|
||||
|
||||
struct Time_t {
|
||||
uint8_t second;
|
||||
uint8_t minute;
|
||||
uint8_t hour;
|
||||
uint8_t day_of_week;
|
||||
uint8_t day_of_month;
|
||||
uint8_t month;
|
||||
uint16_t day_of_year;
|
||||
uint16_t year;
|
||||
unsigned long days;
|
||||
unsigned long valid;
|
||||
};
|
||||
|
||||
void breakEpochToTime(unsigned long epoch, Time_t& tm);
|
||||
|
||||
// void timeInit();
|
||||
extern const String prettySeconds(unsigned long time_s);
|
||||
extern const String prettyMillis(unsigned long time_ms);
|
||||
@@ -64,6 +64,10 @@ String all_widgets = "";
|
||||
String scenario = "";
|
||||
String mqttRootDevice = "";
|
||||
|
||||
// Time
|
||||
Time_t _time_local;
|
||||
Time_t _time_utc;
|
||||
|
||||
// DynamicJsonDocument settingsFlashJsonDoc(JSON_BUFFER_SIZE);
|
||||
// DynamicJsonDocument paramsFlashJsonDoc(JSON_BUFFER_SIZE);
|
||||
// DynamicJsonDocument paramsHeapJsonDoc(JSON_BUFFER_SIZE);
|
||||
@@ -47,6 +47,9 @@ void setup() {
|
||||
//инициализация mqtt
|
||||
mqttInit();
|
||||
|
||||
// NTP
|
||||
ntpInit();
|
||||
|
||||
//настраиваем микроконтроллер
|
||||
configure("/config.json");
|
||||
|
||||
@@ -72,13 +75,10 @@ void setup() {
|
||||
// симуляция добавления внешних событий
|
||||
// IoTItems.push_back((IoTItem*)new externalVariable("{\"id\":\"rel1\",\"val\":10,\"int\":20}"));
|
||||
// IoTItems.push_back((IoTItem*)new externalVariable("{\"id\":\"rel4\",\"val\":34,\"int\":30}"));
|
||||
|
||||
// пример получения JSON всех Items
|
||||
// Serial.println(getParamsJson());
|
||||
|
||||
//чтение одного параметра
|
||||
// Serial.println(findIoTItem("t1")->getValue());
|
||||
|
||||
//тест перебора пинов из расширения
|
||||
// for (int i = 109; i < 112; i++) {
|
||||
// IoTgpio.pinMode(i, OUTPUT);
|
||||
|
||||
112
src/NTP.cpp
Normal file
112
src/NTP.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "NTP.h"
|
||||
|
||||
#include "Global.h"
|
||||
#include "Utils\SerialPrint.h"
|
||||
|
||||
void ntpInit() {
|
||||
synchTime();
|
||||
|
||||
ts.add(
|
||||
TIME, 1000, [&](void*) {
|
||||
unsigned long unixTime = getSystemTime();
|
||||
if (unixTime < MIN_DATETIME) {
|
||||
// SerialPrint("E", "NTP", "Time not synched");
|
||||
synchTime();
|
||||
return;
|
||||
}
|
||||
breakEpochToTime(unixTime + 3 * 60 * 60, _time_local);
|
||||
breakEpochToTime(unixTime, _time_utc);
|
||||
String timenow = getTimeLocal_hhmm();
|
||||
static String prevTime;
|
||||
if (prevTime != timenow) {
|
||||
prevTime = timenow;
|
||||
// jsonWriteStr(configLiveJson, "timenow", timenow);
|
||||
SerialPrint("I", F("NTP"), getDateTimeDotFormated());
|
||||
}
|
||||
},
|
||||
nullptr, true);
|
||||
|
||||
SerialPrint("I", F("NTP"), F("Handle time init"));
|
||||
}
|
||||
|
||||
void synchTime() {
|
||||
configTime(0, 0, "pool.ntp.org", "ru.pool.ntp.org", "pool.ntp.org");
|
||||
}
|
||||
|
||||
time_t getSystemTime() {
|
||||
timeval tv{0, 0};
|
||||
timezone tz = timezone{0, 0};
|
||||
time_t epoch = 0;
|
||||
if (gettimeofday(&tv, &tz) != -1) {
|
||||
epoch = tv.tv_sec;
|
||||
}
|
||||
return epoch;
|
||||
}
|
||||
|
||||
void breakEpochToTime(unsigned long epoch, Time_t& tm) {
|
||||
// break the given time_input into time components
|
||||
// this is a more compact version of the C library localtime function
|
||||
|
||||
unsigned long time = epoch;
|
||||
tm.second = time % 60;
|
||||
time /= 60; // now it is minutes
|
||||
tm.minute = time % 60;
|
||||
time /= 60; // now it is hours
|
||||
tm.hour = time % 24;
|
||||
time /= 24; // now it is days
|
||||
tm.days = time;
|
||||
tm.day_of_week = ((time + 4) % 7) + 1; // Sunday is day 1
|
||||
|
||||
uint8_t year = 0;
|
||||
unsigned long days = 0;
|
||||
|
||||
while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
|
||||
year++;
|
||||
}
|
||||
tm.year = year - 30;
|
||||
|
||||
days -= LEAP_YEAR(year) ? 366 : 365;
|
||||
time -= days; // now it is days in this year, starting at 0
|
||||
tm.day_of_year = time;
|
||||
|
||||
uint8_t month;
|
||||
uint8_t month_length;
|
||||
for (month = 0; month < 12; month++) {
|
||||
if (1 == month) { // february
|
||||
if (LEAP_YEAR(year)) {
|
||||
month_length = 29;
|
||||
} else {
|
||||
month_length = 28;
|
||||
}
|
||||
} else {
|
||||
month_length = days_in_month[month];
|
||||
}
|
||||
|
||||
if (time >= month_length) {
|
||||
time -= month_length;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
tm.month = month + 1; // jan is month 1
|
||||
tm.day_of_month = time + 1; // day of month
|
||||
tm.valid = (epoch > MIN_DATETIME);
|
||||
}
|
||||
|
||||
const String getTimeLocal_hhmm() {
|
||||
char buf[32];
|
||||
sprintf(buf, "%02d:%02d", _time_local.hour, _time_local.minute);
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
const String getTimeLocal_hhmmss() {
|
||||
char buf[32];
|
||||
sprintf(buf, "%02d:%02d:%02d", _time_local.hour, _time_local.minute, _time_local.second);
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
const String getDateTimeDotFormated() {
|
||||
char buf[32];
|
||||
sprintf(buf, "%02d.%02d.%02d %02d:%02d:%02d", _time_local.day_of_month, _time_local.month, _time_local.year, _time_local.hour, _time_local.minute, _time_local.second);
|
||||
return String(buf);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "utils/TimeUtils.h"
|
||||
|
||||
static const uint8_t days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
#include "Utils\TimeUtils.h"
|
||||
#include "Global.h"
|
||||
#include "Utils\StringUtils.h"
|
||||
|
||||
static const char* TIME_FORMAT PROGMEM = "%02d:%02d:%02d";
|
||||
static const char* TIME_FORMAT_WITH_DAYS PROGMEM = "%dd %02d:%02d";
|
||||
@@ -32,101 +32,4 @@ const String prettySeconds(unsigned long time_s) {
|
||||
|
||||
const String prettyMillis(unsigned long time_ms) {
|
||||
return prettySeconds(time_ms / 1000);
|
||||
}
|
||||
|
||||
unsigned long millis_since(unsigned long sinse) {
|
||||
return millis_passed(sinse, millis());
|
||||
}
|
||||
|
||||
unsigned long millis_passed(unsigned long start, unsigned long finish) {
|
||||
unsigned long result = 0;
|
||||
if (start <= finish) {
|
||||
unsigned long passed = finish - start;
|
||||
if (passed <= __LONG_MAX__) {
|
||||
result = static_cast<long>(passed);
|
||||
} else {
|
||||
result = static_cast<long>((__LONG_MAX__ - finish) + start + 1u);
|
||||
}
|
||||
} else {
|
||||
unsigned long passed = start - finish;
|
||||
if (passed <= __LONG_MAX__) {
|
||||
result = static_cast<long>(passed);
|
||||
result = -1 * result;
|
||||
} else {
|
||||
result = static_cast<long>((__LONG_MAX__ - start) + finish + 1u);
|
||||
result = -1 * result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int getOffsetInSeconds(int timezone) {
|
||||
return getOffsetInMinutes(timezone) * ONE_MINUTE_s;
|
||||
}
|
||||
|
||||
int getOffsetInMinutes(int timezone) {
|
||||
return timezone * ONE_HOUR_m;
|
||||
}
|
||||
|
||||
void breakEpochToTime(unsigned long epoch, Time_t& tm) {
|
||||
unsigned long time = epoch;
|
||||
tm.second = time % 60;
|
||||
time /= 60; // now it is minutes
|
||||
tm.minute = time % 60;
|
||||
time /= 60; // now it is hours
|
||||
tm.hour = time % 24;
|
||||
time /= 24; // now it is days
|
||||
tm.days = time;
|
||||
tm.day_of_week = ((time + 4) % 7) + 1; // Sunday is day 1
|
||||
|
||||
uint8_t year = 0;
|
||||
unsigned long days = 0;
|
||||
|
||||
while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
|
||||
year++;
|
||||
}
|
||||
tm.year = year - 30;
|
||||
|
||||
days -= LEAP_YEAR(year) ? 366 : 365;
|
||||
time -= days; // now it is days in this year, starting at 0
|
||||
tm.day_of_year = time;
|
||||
|
||||
uint8_t month;
|
||||
uint8_t month_length;
|
||||
for (month = 0; month < 12; month++) {
|
||||
if (1 == month) { // february
|
||||
if (LEAP_YEAR(year)) {
|
||||
month_length = 29;
|
||||
} else {
|
||||
month_length = 28;
|
||||
}
|
||||
} else {
|
||||
month_length = days_in_month[month];
|
||||
}
|
||||
|
||||
if (time >= month_length) {
|
||||
time -= month_length;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
tm.month = month + 1;
|
||||
tm.day_of_month = time + 1;
|
||||
tm.valid = (epoch > MIN_DATETIME);
|
||||
}
|
||||
|
||||
// void timeInit() {
|
||||
// ts.add(
|
||||
// TIME, 1000, [&](void*) {
|
||||
// String timenow = timeNow->getTimeWOsec();
|
||||
// static String prevTime;
|
||||
// if (prevTime != timenow) {
|
||||
// prevTime = timenow;
|
||||
// jsonWriteStr(paramsFlashJson, "timenow", timenow);
|
||||
// eventGen2("timenow", timenow);
|
||||
// SerialPrint("i", F("NTP"), timenow);
|
||||
// }
|
||||
// },
|
||||
// nullptr, true);
|
||||
// SerialPrint("i", F("NTP"), F("Handle time init"));
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user