Skip to content

Commit

Permalink
limit reading of old log files if cache is newer
Browse files Browse the repository at this point in the history
  • Loading branch information
William C Bonner committed Sep 29, 2023
1 parent 426baaa commit 2cfdb8e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (POLICY CMP0141)
endif()

project (GoveeBTTempLogger
VERSION 2.20230928.11
VERSION 2.20230928.12
DESCRIPTION "Listen and log Govee Thermometer Bluetooth Low Energy Advertisments"
HOMEPAGE_URL https://github.com/wcbonner/GoveeBTTempLogger
)
Expand Down
47 changes: 32 additions & 15 deletions goveebttemplogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ void GenerateCacheFile(std::map<bdaddr_t, std::vector<Govee_Temp>> &AddressTempe
}
void ReadCacheDirectory(void)
{
const std::regex CacheFileRegex("gvh-[[:xdigit:]]{12}-cache\.txt");
const std::regex CacheFileRegex("gvh-[[:xdigit:]]{12}-cache.txt");
if (!CacheDirectory.empty())
{
if (ConsoleVerbosity > 1)
Expand Down Expand Up @@ -1715,27 +1715,44 @@ void ReadLoggedData(const std::filesystem::path& filename)
ssBTAddress.insert(index, ":");
bdaddr_t TheBlueToothAddress;
str2ba(ssBTAddress.c_str(), &TheBlueToothAddress);
std::ifstream TheFile(filename);
if (TheFile.is_open())

// Only read the file if it's newer than what we may have cached
bool bReadFile = true;
struct stat64 FileStat;
FileStat.st_mtim.tv_sec = 0;
if (0 == stat64(filename.c_str(), &FileStat)) // returns 0 if the file-status information is obtained
{
std::vector<std::string> SortableFile;
std::string TheLine;
while (std::getline(TheFile, TheLine))
SortableFile.push_back(TheLine);
TheFile.close();
sort(SortableFile.begin(), SortableFile.end());
for (auto iter = SortableFile.begin(); iter != SortableFile.end(); iter++)
auto it = GoveeMRTGLogs.find(TheBlueToothAddress);
if (it != GoveeMRTGLogs.end())
if (!it->second.empty())
if (FileStat.st_mtim.tv_sec < (it->second.begin()->Time)) // only read the file if it more recent than existing data
bReadFile = false;
}

if (bReadFile)
{
std::ifstream TheFile(filename);
if (TheFile.is_open())
{
Govee_Temp TheValue(*iter);
if (TheValue.IsValid())
UpdateMRTGData(TheBlueToothAddress, TheValue);
std::vector<std::string> SortableFile;
std::string TheLine;
while (std::getline(TheFile, TheLine))
SortableFile.push_back(TheLine);
TheFile.close();
sort(SortableFile.begin(), SortableFile.end());
for (auto iter = SortableFile.begin(); iter != SortableFile.end(); iter++)
{
Govee_Temp TheValue(*iter);
if (TheValue.IsValid())
UpdateMRTGData(TheBlueToothAddress, TheValue);
}
}
}
}
// Finds log files specific to this program then reads the contents into the memory mapped structure simulating MRTG log files.
void ReadLoggedData(void)
{
const std::regex LogFileRegex("gvh-[[:xdigit:]]{12}-[[:digit:]]{4}-[[:digit:]]{2}\.txt");
const std::regex LogFileRegex("gvh-[[:xdigit:]]{12}-[[:digit:]]{4}-[[:digit:]]{2}.txt");
if (!LogDirectory.empty())
{
if (ConsoleVerbosity > 1)
Expand Down Expand Up @@ -1863,7 +1880,7 @@ void WriteAllSVG()
}
void WriteSVGIndex(const std::filesystem::path LogDirectory, const std::filesystem::path SVGIndexFilename)
{
const std::regex LogFileRegex("gvh-[[:xdigit:]]{12}-[[:digit:]]{4}-[[:digit:]]{2}\.txt");
const std::regex LogFileRegex("gvh-[[:xdigit:]]{12}-[[:digit:]]{4}-[[:digit:]]{2}.txt");
if (!LogDirectory.empty())
{
if (ConsoleVerbosity > 0)
Expand Down

0 comments on commit 2cfdb8e

Please sign in to comment.