Skip to content

Commit

Permalink
tidy: Convert some zmserver macros into static inline functions.
Browse files Browse the repository at this point in the history
Had to add a semicolon after every reference to these functions.
  • Loading branch information
linuxdude42 committed Jul 10, 2022
1 parent 11a0ece commit 3e628dd
Showing 1 changed file with 63 additions and 61 deletions.
124 changes: 63 additions & 61 deletions mythplugins/mythzoneminder/mythzmserver/zmserver.cpp
Expand Up @@ -52,8 +52,10 @@ static constexpr int MSG_NOSIGNAL { 0 }; // Apple also has SO_NOSIGPIPE?
// the version of the protocol we understand
static constexpr const char* ZM_PROTOCOL_VERSION { "11" };

#define ADD_STR(list,s) list += (s); (list) += "[]:[]";
#define ADD_INT(list,n) (list) += std::to_string(n); (list) += "[]:[]";
static inline void ADD_STR(std::string& list, const std::string& s)
{ list += s; list += "[]:[]"; };
static inline void ADD_INT(std::string& list, int n)
{ list += std::to_string(n); list += "[]:[]"; };

// error messages
static constexpr const char* ERROR_TOKEN_COUNT { "Invalid token count" };
Expand Down Expand Up @@ -772,24 +774,24 @@ long long ZMServer::getDiskSpace(const std::string &filename, long long &total,
void ZMServer::handleGetServerStatus(void)
{
std::string outStr;
ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

// server status
std::string status = runCommand(g_binPath + "/zmdc.pl check");
ADD_STR(outStr, status)
ADD_STR(outStr, status);

// get load averages
std::array<double,3> loads {};
if (getloadavg(loads.data(), 3) == -1)
{
ADD_STR(outStr, "Unknown")
ADD_STR(outStr, "Unknown");
}
else
{
// to_string gives six decimal places. Drop last four.
std::string buf = std::to_string(loads[0]);
buf.resize(buf.size() - 4);
ADD_STR(outStr, buf)
ADD_STR(outStr, buf);
}

// get free space on the disk where the events are stored
Expand All @@ -798,26 +800,26 @@ void ZMServer::handleGetServerStatus(void)
std::string eventsDir = g_webPath + "/events/";
getDiskSpace(eventsDir, total, used);
std::string buf = std::to_string(static_cast<int>((used * 100) / total)) + "%";
ADD_STR(outStr, buf)
ADD_STR(outStr, buf);

send(outStr);
}

void ZMServer::handleGetAlarmStates(void)
{
std::string outStr;
ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

// add the monitor count
ADD_INT(outStr, (int)m_monitors.size())
ADD_INT(outStr, (int)m_monitors.size());

for (auto *monitor : m_monitors)
{
// add monitor ID
ADD_INT(outStr, monitor->m_monId)
ADD_INT(outStr, monitor->m_monId);

// add monitor status
ADD_INT(outStr, monitor->getState())
ADD_INT(outStr, monitor->getState());
}

send(outStr);
Expand All @@ -841,7 +843,7 @@ void ZMServer::handleGetEventList(std::vector<std::string> tokens)
if (m_debug)
std::cout << "Loading events for monitor: " << monitor << ", date: " << date << std::endl;

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

std::string sql("SELECT E.Id, E.Name, M.Id AS MonitorID, M.Name AS MonitorName, E.StartTime, "
"E.Length, M.Width, M.Height, M.DefaultRate, M.DefaultScale "
Expand Down Expand Up @@ -886,20 +888,20 @@ void ZMServer::handleGetEventList(std::vector<std::string> tokens)
if (m_debug)
std::cout << "Got " << eventCount << " events" << std::endl;

ADD_INT(outStr, eventCount)
ADD_INT(outStr, eventCount);

for (int x = 0; x < eventCount; x++)
{
MYSQL_ROW row = mysql_fetch_row(res);
if (row)
{
ADD_STR(outStr, row[0]) // eventID
ADD_STR(outStr, row[1]) // event name
ADD_STR(outStr, row[2]) // monitorID
ADD_STR(outStr, row[3]) // monitor name
ADD_STR(outStr, row[0]); // eventID
ADD_STR(outStr, row[1]); // event name
ADD_STR(outStr, row[2]); // monitorID
ADD_STR(outStr, row[3]); // monitor name
row[4][10] = 'T';
ADD_STR(outStr, row[4]) // start time
ADD_STR(outStr, row[5]) // length
ADD_STR(outStr, row[4]); // start time
ADD_STR(outStr, row[5]); // length
}
else
{
Expand Down Expand Up @@ -930,7 +932,7 @@ void ZMServer::handleGetEventDates(std::vector<std::string> tokens)
if (m_debug)
std::cout << "Loading event dates for monitor: " << monitor << std::endl;

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

std::string sql("SELECT DISTINCT DATE(E.StartTime) "
"from Events as E inner join Monitors as M on E.MonitorId = M.Id ");
Expand All @@ -956,14 +958,14 @@ void ZMServer::handleGetEventDates(std::vector<std::string> tokens)
if (m_debug)
std::cout << "Got " << dateCount << " dates" << std::endl;

ADD_INT(outStr, dateCount)
ADD_INT(outStr, dateCount);

for (int x = 0; x < dateCount; x++)
{
MYSQL_ROW row = mysql_fetch_row(res);
if (row)
{
ADD_STR(outStr, row[0]) // event date
ADD_STR(outStr, row[0]); // event date
}
else
{
Expand All @@ -981,7 +983,7 @@ void ZMServer::handleGetEventDates(std::vector<std::string> tokens)
void ZMServer::handleGetMonitorStatus(void)
{
std::string outStr;
ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

// get monitor list
// Function is reserverd word so but ticks around it
Expand All @@ -1002,7 +1004,7 @@ void ZMServer::handleGetMonitorStatus(void)
if (m_debug)
std::cout << "Got " << monitorCount << " monitors" << std::endl;

ADD_INT(outStr, monitorCount)
ADD_INT(outStr, monitorCount);

for (int x = 0; x < monitorCount; x++)
{
Expand Down Expand Up @@ -1048,13 +1050,13 @@ void ZMServer::handleGetMonitorStatus(void)
}
}

ADD_STR(outStr, id)
ADD_STR(outStr, name)
ADD_STR(outStr, zmcStatus)
ADD_STR(outStr, zmaStatus)
ADD_STR(outStr, events)
ADD_STR(outStr, function)
ADD_STR(outStr, enabled)
ADD_STR(outStr, id);
ADD_STR(outStr, name);
ADD_STR(outStr, zmcStatus);
ADD_STR(outStr, zmaStatus);
ADD_STR(outStr, events);
ADD_STR(outStr, function);
ADD_STR(outStr, enabled);

mysql_free_result(res2);
}
Expand Down Expand Up @@ -1160,7 +1162,7 @@ void ZMServer::handleGetEventFrame(std::vector<std::string> tokens)

std::string outStr;

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

// try to find the frame file
std::string filepath;
Expand Down Expand Up @@ -1213,7 +1215,7 @@ void ZMServer::handleGetEventFrame(std::vector<std::string> tokens)
std::cout << "Frame size: " << fileSize << std::endl;

// get the file size
ADD_INT(outStr, fileSize)
ADD_INT(outStr, fileSize);

// send the data
send(outStr, s_buffer.data(), fileSize);
Expand Down Expand Up @@ -1335,7 +1337,7 @@ void ZMServer::handleGetAnalysisFrame(std::vector<std::string> tokens)
filepath = g_webPath + "/events/" + monitorID + "/" + eventID + "/";
}

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

FILE *fd = nullptr;
int fileSize = 0;
Expand All @@ -1355,7 +1357,7 @@ void ZMServer::handleGetAnalysisFrame(std::vector<std::string> tokens)
std::cout << "Frame size: " << fileSize << std::endl;

// get the file size
ADD_INT(outStr, fileSize)
ADD_INT(outStr, fileSize);

// send the data
send(outStr, s_buffer.data(), fileSize);
Expand Down Expand Up @@ -1383,7 +1385,7 @@ void ZMServer::handleGetAnalysisFrame(std::vector<std::string> tokens)
std::cout << "Frame size: " << fileSize << std::endl;

// get the file size
ADD_INT(outStr, fileSize)
ADD_INT(outStr, fileSize);

// send the data
send(outStr, s_buffer.data(), fileSize);
Expand Down Expand Up @@ -1412,10 +1414,10 @@ void ZMServer::handleGetLiveFrame(std::vector<std::string> tokens)

std::string outStr;

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

// echo the monitor id
ADD_INT(outStr, monitorID)
ADD_INT(outStr, monitorID);

// try to find the correct MONITOR
if (m_monitorMap.find(monitorID) == m_monitorMap.end())
Expand Down Expand Up @@ -1448,10 +1450,10 @@ void ZMServer::handleGetLiveFrame(std::vector<std::string> tokens)
}

// add status
ADD_STR(outStr, monitor->m_status)
ADD_STR(outStr, monitor->m_status);

// send the data size
ADD_INT(outStr, dataSize)
ADD_INT(outStr, dataSize);

// send the data
send(outStr, s_buffer.data(), dataSize);
Expand All @@ -1473,7 +1475,7 @@ void ZMServer::handleGetFrameList(std::vector<std::string> tokens)
if (m_debug)
std::cout << "Loading frames for event: " << eventID << std::endl;

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

// check to see what type of event this is
std::string sql = "SELECT Cause, Length, Frames FROM Events ";
Expand Down Expand Up @@ -1509,16 +1511,16 @@ void ZMServer::handleGetFrameList(std::vector<std::string> tokens)
if (m_debug)
std::cout << "Got " << frameCount << " frames (continuous event)" << std::endl;

ADD_INT(outStr, frameCount)
ADD_INT(outStr, frameCount);

if (frameCount > 0)
{
double delta = length / frameCount;

for (int x = 0; x < frameCount; x++)
{
ADD_STR(outStr, "Normal") // Type
ADD_STR(outStr, std::to_string(delta)) // Delta
ADD_STR(outStr, "Normal"); // Type
ADD_STR(outStr, std::to_string(delta)); // Delta
}
}
}
Expand All @@ -1541,15 +1543,15 @@ void ZMServer::handleGetFrameList(std::vector<std::string> tokens)
if (m_debug)
std::cout << "Got " << frameCount << " frames" << std::endl;

ADD_INT(outStr, frameCount)
ADD_INT(outStr, frameCount);

for (int x = 0; x < frameCount; x++)
{
row = mysql_fetch_row(res);
if (row)
{
ADD_STR(outStr, row[0]) // Type
ADD_STR(outStr, row[1]) // Delta
ADD_STR(outStr, row[0]); // Type
ADD_STR(outStr, row[1]); // Delta
}
else
{
Expand All @@ -1569,13 +1571,13 @@ void ZMServer::handleGetCameraList(void)
{
std::string outStr;

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

ADD_INT(outStr, (int)m_monitors.size())
ADD_INT(outStr, (int)m_monitors.size());

for (auto & monitor : m_monitors)
{
ADD_STR(outStr, monitor->m_name)
ADD_STR(outStr, monitor->m_name);
}

send(outStr);
Expand All @@ -1585,20 +1587,20 @@ void ZMServer::handleGetMonitorList(void)
{
std::string outStr;

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

if (m_debug)
std::cout << "We have " << m_monitors.size() << " monitors" << std::endl;

ADD_INT(outStr, (int)m_monitors.size())
ADD_INT(outStr, (int)m_monitors.size());;

for (auto *mon : m_monitors)
{
ADD_INT(outStr, mon->m_monId)
ADD_STR(outStr, mon->m_name)
ADD_INT(outStr, mon->m_width)
ADD_INT(outStr, mon->m_height)
ADD_INT(outStr, mon->m_bytesPerPixel)
ADD_INT(outStr, mon->m_monId);
ADD_STR(outStr, mon->m_name);
ADD_INT(outStr, mon->m_width);
ADD_INT(outStr, mon->m_height);
ADD_INT(outStr, mon->m_bytesPerPixel);

if (m_debug)
{
Expand Down Expand Up @@ -1632,7 +1634,7 @@ void ZMServer::handleDeleteEvent(std::vector<std::string> tokens)
if (m_debug)
std::cout << "Deleting event: " << eventID << std::endl;

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");

std::string sql;
sql += "DELETE FROM Events WHERE Id = " + eventID;
Expand Down Expand Up @@ -1684,7 +1686,7 @@ void ZMServer::handleDeleteEventList(std::vector<std::string> tokens)
return;
}

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");
send(outStr);
}

Expand All @@ -1702,7 +1704,7 @@ void ZMServer::handleRunZMAudit(void)
if (system(command.c_str()) < 0 && errno)
std::cerr << "Failed to run '" << command << "'" << std::endl;

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");
send(outStr);
}

Expand Down Expand Up @@ -2042,7 +2044,7 @@ void ZMServer::handleSetMonitorFunction(std::vector<std::string> tokens)
else
std::cout << "Not updating monitor function as identical to existing configuration" << std::endl;

ADD_STR(outStr, "OK")
ADD_STR(outStr, "OK");
send(outStr);
}

Expand Down

0 comments on commit 3e628dd

Please sign in to comment.