Skip to content

Commit

Permalink
Merge pull request #1413 from domoticz/Python-Plugins
Browse files Browse the repository at this point in the history
Resolved crash on Raspberry Pi when added icon zip files via a plugin
  • Loading branch information
gizmocuz committed Mar 28, 2017
2 parents 28ec1dd + 74b193b commit 3a2f81b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 27 deletions.
26 changes: 2 additions & 24 deletions hardware/plugins/PythonObjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,7 @@ namespace Plugins {
if (sFileName.length())
{
self->pPlugin = pModState->pPlugin;
//
// Check file exists in plugin home folder and load it
//
sFileName = self->pPlugin->m_HomeFolder + sFileName;
if (self->pPlugin->m_bDebug)
{
_log.Log(LOG_NORM, "(%s) Opening image file '%s'.", self->pPlugin->Name.c_str(), sFileName.c_str());
}
std::ifstream infile(sFileName.c_str(), std::ios::in | std::ios::binary);
if (infile.is_open())
{
std::stringstream ssImages;
ssImages << infile.rdbuf();
infile.close();
self->ZipFile = ssImages.str();
if (self->pPlugin->m_bDebug)
{
_log.Log(LOG_NORM, "(%s) Loaded %d bytes from image file.", self->pPlugin->Name.c_str(), self->ZipFile.length());
}
}
else
_log.Log(LOG_ERROR, "CImage:%s, File read failed on '%s'.", __func__, sFileName.c_str());

Py_DECREF(self->Filename);
self->Filename = PyUnicode_FromString(sFileName.c_str());
}
Expand Down Expand Up @@ -166,7 +144,7 @@ namespace Plugins {
std::string sFilename = PyUnicode_AsUTF8(self->Filename);
if (self->ImageID == -1)
{
if (self->ZipFile.length())
if (sFilename.length())
{
if (self->pPlugin->m_bDebug)
{
Expand All @@ -177,7 +155,7 @@ namespace Plugins {
// Call code to do insert here
//
std::string ErrorMessage;
if (!m_sql.InsertCustomIconFromZip(self->ZipFile, ErrorMessage))
if (!m_sql.InsertCustomIconFromZipFile(sFilename, ErrorMessage))
{
_log.Log(LOG_ERROR, "(%s) Insert Custom Icon From Zip failed on file '%s' with error '%s'.", self->pPlugin->Name.c_str(), sFilename.c_str(), ErrorMessage.c_str());
}
Expand Down
1 change: 0 additions & 1 deletion hardware/plugins/PythonObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Plugins {
PyObject* Name;
PyObject* Description;
PyObject* Filename;
std::string ZipFile;
CPlugin* pPlugin;
} CImage;

Expand Down
9 changes: 7 additions & 2 deletions main/SQLHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7218,7 +7218,12 @@ bool CSQLHelper::InsertCustomIconFromZip(const std::string &szZip, std::string &
outfile.flush();
outfile.close();

clx::basic_unzip<char> in(outputfile);
return InsertCustomIconFromZipFile(outputfile, ErrorMessage);
}

bool CSQLHelper::InsertCustomIconFromZipFile(const std::string &szZipFile, std::string &ErrorMessage)
{
clx::basic_unzip<char> in(szZipFile);
if (!in.is_open())
{
ErrorMessage = "Error opening zip file";
Expand All @@ -7236,7 +7241,7 @@ bool CSQLHelper::InsertCustomIconFromZip(const std::string &szZip, std::string &
continue;

int ipos = fpath.find("icons.txt");
if ( ipos != std::string::npos)
if (ipos != std::string::npos)
{
std::string rpath;
if (ipos > 0)
Expand Down
1 change: 1 addition & 0 deletions main/SQLHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ class CSQLHelper
void AllowNewHardwareTimer(const int iTotMinutes);

bool InsertCustomIconFromZip(const std::string &szZip, std::string &ErrorMessage);
bool InsertCustomIconFromZipFile(const std::string & szZipFile, std::string & ErrorMessage);

std::map<std::string, std::string> BuildDeviceOptions(const std::string & options, const bool decode = true);
std::map<std::string, std::string> GetDeviceOptions(const std::string & idx);
Expand Down

0 comments on commit 3a2f81b

Please sign in to comment.