Skip to content

Commit 126c5f5

Browse files
committed
Added: Floorplans dynamically added to appcache if not present
1 parent 078c7ea commit 126c5f5

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

main/WebServer.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ namespace http {
151151
}
152152
}
153153

154+
CheckAppCache(serverpath);
154155

155156
if (m_pWebEm != NULL)
156157
delete m_pWebEm;
@@ -544,6 +545,110 @@ namespace http {
544545
}
545546
}
546547

548+
void CWebServer::CheckAppCache(const std::string &serverpath)
549+
{
550+
_log.Log(LOG_NORM, "AppCache: Check starting...");
551+
try {
552+
/*
553+
** Load floor plan images from database
554+
*/
555+
std::stringstream szQuery;
556+
std::vector<std::vector<std::string> > result;
557+
std::vector<std::string> images;
558+
szQuery.clear();
559+
szQuery.str("");
560+
szQuery << "SELECT ImageFile FROM Floorplans ORDER BY [Order]";
561+
result = m_sql.query(szQuery.str());
562+
if (result.size() > 0)
563+
{
564+
int ii = 0;
565+
CURLEncode* oEncoder = new CURLEncode();
566+
for (std::vector<std::vector<std::string> >::const_iterator itt = result.begin(); itt != result.end(); ++itt)
567+
{
568+
std::string sImage = oEncoder->URLEncode((*itt)[0]);
569+
for (std::string::size_type found = sImage.find("%2F"); found!=std::string::npos; found = sImage.find("%2F"))
570+
sImage.replace(found, 3, "/");
571+
images.insert(images.end(), sImage);
572+
// _log.Log(LOG_NORM, "AppCache: Encoded image '%s'.", sImage.c_str());
573+
}
574+
}
575+
else return; // if no floorplans then bail out
576+
577+
/*
578+
** Scan manifest file and remove those images already in the file
579+
** Images will be at the end of the file after the '# Floorplans' comment
580+
*/
581+
std::string sLine = "";
582+
std::ifstream infile;
583+
bool bFoundComment = false;
584+
std::string manifestfile = serverpath + "/html5.appcache";
585+
infile.open(manifestfile.c_str());
586+
if (infile.is_open())
587+
{
588+
while (!infile.eof())
589+
{
590+
getline(infile, sLine);
591+
if (sLine.size() != 0)
592+
{
593+
if (!bFoundComment)
594+
{
595+
if (sLine == "# Floorplans")
596+
{
597+
bFoundComment = true;
598+
// _log.Log(LOG_NORM, "AppCache: Found comment '# Floorplans'.");
599+
}
600+
}
601+
else
602+
{
603+
for (std::vector<std::string>::iterator itt = images.begin(); itt != images.end(); ++itt)
604+
{
605+
if (sLine == *itt)
606+
{
607+
_log.Log(LOG_NORM, "AppCache: Image '%s' found in cache file.", itt->c_str());
608+
images.erase(itt); // clear images already in file to avoid duplication
609+
break;
610+
}
611+
}
612+
}
613+
}
614+
}
615+
infile.close();
616+
}
617+
else
618+
{
619+
_log.Log(LOG_ERROR, "AppCache: Cache file '%s' failed to open for read.", manifestfile.c_str());
620+
return;
621+
}
622+
623+
if (images.size() > 0)
624+
{
625+
std::ofstream outfile;
626+
outfile.open(manifestfile.c_str(), std::ios::out | std::ios::app);
627+
if (outfile.is_open())
628+
{
629+
if (!bFoundComment)
630+
{
631+
_log.Log(LOG_NORM, "AppCache: Inserting comment '# Floorplans'.");
632+
outfile << "# Floorplans" << std::endl << "CACHE:" << std::endl;
633+
}
634+
std::vector<std::string>::const_iterator itt;
635+
for (itt = images.begin(); itt != images.end(); ++itt)
636+
{
637+
_log.Log(LOG_NORM, "AppCache: Inserting image '%s' into cache file.", itt->c_str());
638+
outfile << itt->c_str() << std::endl;
639+
}
640+
outfile.flush();
641+
outfile.close();
642+
}
643+
else _log.Log(LOG_ERROR, "AppCache: Cache file '%s' failed to open for append.", manifestfile.c_str());
644+
}
645+
}
646+
catch (...) {
647+
_log.Log(LOG_ERROR, "AppCache: Exception during floorplan check.");
648+
}
649+
_log.Log(LOG_NORM, "AppCache: Check complete.");
650+
};
651+
547652
void CWebServer::Cmd_AddHardware(Json::Value &root)
548653
{
549654
if (m_pWebEm->m_actualuser_rights != 2)

main/WebServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class CWebServer
6060
//JSon
6161
void GetJSonDevices(Json::Value &root, const std::string &rused, const std::string &rfilter, const std::string &order, const std::string &rowid, const std::string &planID, const std::string &floorID, const bool bDisplayHidden, const time_t LastUpdate);
6262
private:
63+
void CheckAppCache(const std::string &serverpath);
6364
void HandleCommand(const std::string &cparam, Json::Value &root);
6465
void HandleRType(const std::string &rtype, Json::Value &root);
6566

0 commit comments

Comments
 (0)