27 m_retryTimer(new QTimer(this))
29 setObjectName(
"ZMClient");
46 QString zmserver_host;
52 if (zmserver_host.isEmpty() || zmserver_port == -1)
54 LOG(VB_GENERAL, LOG_INFO,
"ZMClient: no valid IP or port found for mythzmserver");
74 LOG(VB_GENERAL, LOG_INFO,
75 QString(
"Connecting to zm server: %1:%2 (try %3 of 2)")
106 "Have you set the correct IP and port in the settings?"));
125 QStringList origStrList = strList;
133 LOG(VB_GENERAL, LOG_NOTICE,
"Connection to mythzmserver lost");
137 LOG(VB_GENERAL, LOG_ERR,
"Re-connection to mythzmserver failed");
142 strList = origStrList;
154 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
159 if (strList[0] ==
"UNKNOWN_COMMAND")
161 LOG(VB_GENERAL, LOG_ERR,
"Somethings is getting passed to the server "
162 "that it doesn't understand");
167 if (strList[0].startsWith(
"ERROR"))
169 LOG(VB_GENERAL, LOG_ERR,
170 QString(
"The server failed to process the command. "
171 "The error was:- \n\t\t\t%1").arg(strList[0]));
176 return strList[0] ==
"OK";
183 QStringList strList(
"HELLO");
186 LOG(VB_GENERAL, LOG_ERR, QString(
"Server didn't respond to 'HELLO'!!"));
188 ShowOkPopup(tr(
"The mythzmserver didn't respond to our request "
189 "to get the protocol version!!"));
194 if (strList.size() < 2)
196 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
202 LOG(VB_GENERAL, LOG_ERR,
203 QString(
"Protocol version mismatch (plugin=%1, mythzmserver=%2)")
206 ShowOkPopup(QString(
"The mythzmserver uses protocol version %1, "
207 "but this client only understands version %2. "
208 "Make sure you are running compatible versions of "
209 "both the server and plugin.")
214 LOG(VB_GENERAL, LOG_INFO,
260 QStringList strList(
"GET_SERVER_STATUS");
265 if (strList.size() < 4)
267 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
272 cpuStat = strList[2];
273 diskStat = strList[3];
280 QStringList strList(
"GET_MONITOR_STATUS");
285 if (strList.size() < 2)
287 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
292 int monitorCount = strList[1].toInt(&bOK);
295 LOG(VB_GENERAL, LOG_ERR,
296 "ZMClient received bad int in getMonitorStatus()");
302 for (
int x = 0; x < monitorCount; x++)
304 int monID = strList[x * 7 + 2].toInt();
309 mon->
name = strList[x * 7 + 3];
312 mon->
events = strList[x * 7 + 6].toInt();
314 mon->
enabled = (strList[x * 7 + 8].toInt() != 0);
321 QString result =
"UNKNOWN";
351 QStringList strList(
"GET_ALARM_STATES");
356 if (strList.size() < 2)
358 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
363 int monitorCount = strList[1].toInt(&bOK);
366 LOG(VB_GENERAL, LOG_ERR,
367 "ZMClient received bad int in getAlarmStates()");
373 bool changed =
false;
374 for (
int x = 0; x < monitorCount; x++)
376 int monID = strList[x * 2 + 2].toInt();
377 auto state = (
State)strList[x * 2 + 3].toInt();
382 if (mon->
state != state)
385 LOG(VB_GENERAL, LOG_DEBUG,
386 QString(
"ZMClient monitor %1 changed state from %2 to %3")
399 const QString &date,
bool includeContinuous,
400 std::vector<Event*> *eventList)
406 QStringList strList(
"GET_EVENT_LIST");
407 strList << monitorName << (oldestFirst ?
"1" :
"0") ;
409 strList << (includeContinuous ?
"1" :
"0") ;
415 if (strList.size() < 2)
417 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
422 int eventCount = strList[1].toInt(&bOK);
425 LOG(VB_GENERAL, LOG_ERR,
"ZMClient received bad int in getEventList()");
430 if ((strList.size() - 2) / 6 != eventCount)
432 LOG(VB_GENERAL, LOG_ERR,
433 "ZMClient got a mismatch between the number of events and "
434 "the expected number of stringlist items in getEventList()");
438 QStringList::Iterator it = strList.begin();
440 for (
int x = 0; x < eventCount; x++)
442 eventList->push_back(
454 QStringList &dateList)
460 QStringList strList(
"GET_EVENT_DATES");
461 strList << monitorName << (oldestFirst ?
"1" :
"0") ;
467 if (strList.size() < 2)
469 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
474 int dateCount = strList[1].toInt(&bOK);
477 LOG(VB_GENERAL, LOG_ERR,
478 "ZMClient received bad int in getEventDates()");
483 if ((strList.size() - 3) != dateCount)
485 LOG(VB_GENERAL, LOG_ERR,
486 "ZMClient got a mismatch between the number of dates and "
487 "the expected number of stringlist items in getEventDates()");
491 QStringList::Iterator it = strList.begin();
493 for (
int x = 0; x < dateCount; x++)
495 dateList.append(*it++);
505 QStringList strList(
"GET_FRAME_LIST");
506 strList << QString::number(eventID);
511 if (strList.size() < 2)
513 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
518 int frameCount = strList[1].toInt(&bOK);
521 LOG(VB_GENERAL, LOG_ERR,
"ZMClient received bad int in getFrameList()");
526 if ((strList.size() - 2) / 2 != frameCount)
528 LOG(VB_GENERAL, LOG_ERR,
529 "ZMClient got a mismatch between the number of frames and "
530 "the expected number of stringlist items in getFrameList()");
534 QStringList::Iterator it = strList.begin();
536 for (
int x = 0; x < frameCount; x++)
538 auto *item =
new Frame;
540 item->delta = (*it++).toDouble();
541 frameList->push_back(item);
549 QStringList strList(
"DELETE_EVENT");
550 strList << QString::number(eventID);
559 QStringList strList(
"DELETE_EVENT_LIST");
561 std::vector<Event*>::iterator it;
562 for (it = eventList->begin(); it != eventList->end(); ++it)
564 strList << QString::number((*it)->eventID());
569 strList = QStringList(
"DELETE_EVENT_LIST");
578 strList = QStringList(
"RUN_ZMAUDIT");
585 std::chrono::milliseconds errmsgtime { 0ms };
603 LOG(VB_GENERAL, LOG_ERR,
"readData: Error, readBlock");
609 LOG(VB_GENERAL, LOG_ERR,
610 "readData: Error, socket went unconnected");
616 std::chrono::milliseconds elapsed = timer.
elapsed();
619 if ((elapsed - errmsgtime) > 10s)
621 errmsgtime = elapsed;
622 LOG(VB_GENERAL, LOG_ERR,
623 QString(
"m_socket->: Waiting for data: %1 %2")
624 .arg(
read).arg(dataSize));
630 LOG(VB_GENERAL, LOG_ERR,
"Error, readData timeout (readBlock)");
649 QStringList strList(
"GET_EVENT_FRAME");
650 strList << QString::number(event->
monitorID());
651 strList << QString::number(event->
eventID());
652 strList << QString::number(frameNo);
653 strList <<
event->startTime(Qt::LocalTime).toString(
"yy/MM/dd/hh/mm/ss");
658 if (strList.size() < 2)
660 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
665 int imageSize = strList[1].toInt();
668 auto *data =
new unsigned char[imageSize];
671 LOG(VB_GENERAL, LOG_ERR,
672 "ZMClient::getEventFrame(): Failed to get image data");
681 if (!(*image)->loadFromData(data, imageSize,
"JPEG"))
683 LOG(VB_GENERAL, LOG_ERR,
684 "ZMClient::getEventFrame(): Failed to load image from data");
694 QStringList strList(
"GET_ANALYSE_FRAME");
695 strList << QString::number(event->
monitorID());
696 strList << QString::number(event->
eventID());
697 strList << QString::number(frameNo);
698 strList <<
event->startTime(Qt::LocalTime).toString(
"yy/MM/dd/hh/mm/ss");
706 if (strList.size() < 2)
708 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
713 int imageSize = strList[1].toInt();
716 auto *data =
new unsigned char[imageSize];
719 LOG(VB_GENERAL, LOG_ERR,
720 "ZMClient::getAnalyseFrame(): Failed to get image data");
726 if (!image.loadFromData(data, imageSize,
"JPEG"))
728 LOG(VB_GENERAL, LOG_ERR,
729 "ZMClient::getAnalyseFrame(): Failed to load image from data");
741 QStringList strList(
"GET_LIVE_FRAME");
742 strList << QString::number(monitorID);
747 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
753 if (strList[0].startsWith(
"WARNING"))
762 if (strList.size() < 4)
764 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
772 size_t imageSize = strList[3].toInt();
774 if (buffer.size() < imageSize)
776 LOG(VB_GENERAL, LOG_ERR,
777 "ZMClient::getLiveFrame(): Live frame buffer is too small!");
785 if (!
readData(buffer.data(), imageSize))
787 LOG(VB_GENERAL, LOG_ERR,
788 "ZMClient::getLiveFrame(): Failed to get image data");
801 QStringList strList(
"GET_CAMERA_LIST");
806 if (strList.size() < 2)
808 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
813 int cameraCount = strList[1].toInt(&bOK);
816 LOG(VB_GENERAL, LOG_ERR,
817 "ZMClient received bad int in getCameraList()");
822 if (strList.size() < cameraCount + 2)
824 LOG(VB_GENERAL, LOG_ERR, QString(
825 "ZMClient got a mismatch between the number of cameras (%1) and "
826 "the expected number of stringlist items (%2) in getCameraList()")
827 .arg(cameraCount).arg(strList.size()));
831 for (
int x = 0; x < cameraCount; x++)
833 cameraList.append(strList[x + 2]);
874 QStringList strList(
"GET_MONITOR_LIST");
879 if (strList.size() < 2)
881 LOG(VB_GENERAL, LOG_ERR,
"ZMClient response too short");
886 int monitorCount = strList[1].toInt(&bOK);
889 LOG(VB_GENERAL, LOG_ERR,
890 "ZMClient received bad int in getMonitorList()");
895 if ((strList.size() - 2) / 5 != monitorCount)
897 LOG(VB_GENERAL, LOG_ERR,
898 "ZMClient got a mismatch between the number of monitors and "
899 "the expected number of stringlist items in getMonitorList()");
905 QStringList notificationMonitors = s.split(
",");
907 for (
int x = 0; x < monitorCount; x++)
910 item->
id = strList[x * 5 + 2].toInt();
911 item->name = strList[x * 5 + 3];
912 item->width = strList[x * 5 + 4].toInt();
913 item->height = strList[x * 5 + 5].toInt();
914 item->bytes_per_pixel = strList[x * 5 + 6].toInt();
915 item->zmcStatus =
"";
916 item->zmaStatus =
"";
919 item->showNotifications = notificationMonitors.contains(QString::number(item->id));
924 LOG(VB_GENERAL, LOG_NOTICE,
925 QString(
"Monitor: %1 (%2) is using %3 bytes per pixel")
926 .arg(item->name).arg(item->id).arg(item->bytes_per_pixel));
934 QStringList strList(
"SET_MONITOR_FUNCTION");
935 strList << QString::number(monitorID);
937 strList << QString::number(static_cast<int>(enabled));
953 s += QString(
",%1").arg(mon->
id);
955 s = QString(
"%1").arg(mon->
id);
966 auto *me =
dynamic_cast<MythEvent*
>(event);
970 if (me->Message().startsWith(
"ZONEMINDER_NOTIFICATION"))
972 QStringList list = me->
Message().simplified().split(
' ');
977 int monID = list[1].toInt();
982 QObject::customEvent(event);
994 miniPlayer->setAlarmMonitor(monitorID);
996 if (miniPlayer->Create())