mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-27 06:32:19 +03:00
добавил централизованную функцию serial print и утилиты времени
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
|
||||
bool fileSystemInit() {
|
||||
if (!FileFS.begin()) {
|
||||
Serial.println("FS Init ERROR, may be FS was not flashed");
|
||||
SerialPrint(F("E"), F("FS"), F("Init ERROR, may be FS was not flashed"));
|
||||
return false;
|
||||
}
|
||||
Serial.println("FS Init completed");
|
||||
SerialPrint(F("i"), F("FS"), F("Init completed"));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ File seekFile(const String& filename, size_t position) {
|
||||
String path = filepath(filename);
|
||||
auto file = FileFS.open(path, "r");
|
||||
if (!file) {
|
||||
Serial.println("[E] file error");
|
||||
SerialPrint(F("E"), F("FS"), F("seek file error"));
|
||||
}
|
||||
file.seek(position, SeekSet);
|
||||
return file;
|
||||
@@ -53,9 +53,9 @@ const String filepath(const String& filename) {
|
||||
bool cutFile(const String& src, const String& dst) {
|
||||
String srcPath = filepath(src);
|
||||
String dstPath = filepath(dst);
|
||||
Serial.println("cut " + srcPath + " to " + dstPath);
|
||||
SerialPrint(F("i"), F("FS"), "cut " + srcPath + " to " + dstPath);
|
||||
if (!FileFS.exists(srcPath)) {
|
||||
Serial.println("not exist: " + srcPath);
|
||||
SerialPrint(F("E"), F("FS"), "not exist: " + srcPath);
|
||||
return false;
|
||||
}
|
||||
if (FileFS.exists(dstPath)) {
|
||||
@@ -63,7 +63,6 @@ bool cutFile(const String& src, const String& dst) {
|
||||
}
|
||||
auto srcFile = FileFS.open(srcPath, "r");
|
||||
auto dstFile = FileFS.open(dstPath, "w");
|
||||
|
||||
uint8_t buf[512];
|
||||
while (srcFile.available()) {
|
||||
size_t len = srcFile.read(buf, 512);
|
||||
|
||||
9
src/utils/SerialPrint.cpp
Normal file
9
src/utils/SerialPrint.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
#include "Utils\SerialPrint.h"
|
||||
|
||||
void SerialPrint(String errorLevel, String module, String msg) {
|
||||
Serial.println(prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg);
|
||||
String tosend = "[" + errorLevel + "] [" + module + "] " + msg;
|
||||
|
||||
// ws.textAll(tosend);
|
||||
}
|
||||
132
src/utils/TimeUtils.cpp
Normal file
132
src/utils/TimeUtils.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#include "Utils/TimeUtils.h"
|
||||
|
||||
static const uint8_t days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
|
||||
static const char* TIME_FORMAT PROGMEM = "%02d:%02d:%02d";
|
||||
static const char* TIME_FORMAT_WITH_DAYS PROGMEM = "%dd %02d:%02d";
|
||||
|
||||
const String prettySeconds(unsigned long time_s) {
|
||||
unsigned long tmp = time_s;
|
||||
unsigned long seconds;
|
||||
unsigned long minutes;
|
||||
unsigned long hours;
|
||||
unsigned long days;
|
||||
seconds = tmp % 60;
|
||||
tmp = tmp / 60;
|
||||
|
||||
minutes = tmp % 60;
|
||||
tmp = tmp / 60;
|
||||
|
||||
hours = tmp % 24;
|
||||
days = tmp / 24;
|
||||
|
||||
char buf[32];
|
||||
|
||||
if (days) {
|
||||
sprintf_P(buf, TIME_FORMAT_WITH_DAYS, days, hours, minutes, seconds);
|
||||
} else {
|
||||
sprintf_P(buf, TIME_FORMAT, hours, minutes, seconds);
|
||||
}
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
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(configLiveJson, "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