From 31a1fd3dda7d1b3e9d3ac0fc92dd03fbf1febe93 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Wed, 5 Jul 2017 20:55:20 +0100 Subject: [PATCH] EPG search call --- handler.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ handler.h | 1 + 2 files changed, 77 insertions(+) diff --git a/handler.c b/handler.c index cb2acce..4b047b1 100644 --- a/handler.c +++ b/handler.c @@ -89,6 +89,7 @@ int jsonserver_request_handler(struct mg_connection *conn) else if (!strcmp(wvrequest, "tunersstatus")) success = jsonserver_tunersstatus(js, postData); else if (!strcmp(wvrequest, "epgsearchsame")) success = jsonserver_epgsearchsame(js, postData); else if (!strcmp(wvrequest, "epgdownload")) success = jsonserver_epgdownload(js, postData); + else if (!strcmp(wvrequest, "epgsearch")) success = jsonserver_epgsearch(js, postData); if (!success) return 0; // the specific handler failed badly @@ -1747,3 +1748,78 @@ bool jsonserver_epgdownload(Json::Value& js, const char* postData) js["Events"] = jsevents; return true; } + +bool jsonserver_epgsearch(Json::Value& js, const char* postData) +{ + Log* log = Log::getInstance(); + log->log("JSONServer", Log::DEBUG, "epgsearch '%s'", postData); + + char searchfor[1024]; int mgv1 = mg_get_var(postData, strlen(postData), "searchfor", searchfor, 1024); + + if (mgv1 == -1) + { + log->log("JSONServer", Log::ERR, "request mgvs: %i", mgv1); + js["Result"] = false; + js["Error"] = "Bad request parameters"; + return true; + } + + log->log("JSONServer", Log::DEBUG, "search for: %s", searchfor); + + cSchedulesLock MutexLock; + const cSchedules *Schedules = cSchedules::Schedules(MutexLock); + + if (!Schedules) + { + log->log("JSONServer", Log::ERR, "Could not get Schedules object"); + js["Result"] = false; + js["Error"] = "Internal schedules error (1)"; + return true; + } + + Json::Value jsevents(Json::arrayValue); + + for (cChannel* channel = Channels.First(); channel; channel = Channels.Next(channel)) + { + if (channel->GroupSep()) continue; + + const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID()); + if (!Schedule) continue; + + bool foundtitle; + bool founddescription; + for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event)) + { + foundtitle = false; + founddescription = false; + + if (event->Title() && strcasestr(event->Title(), searchfor)) foundtitle = true; + + if (!foundtitle && event->Description()) + if (strcasestr(event->Description(), searchfor)) founddescription = true; + + if (foundtitle || founddescription) + { + Json::Value oneEvent; + oneEvent["ChannelNumber"] = channel->Number(); + oneEvent["ChannelID"] = (const char*)event->ChannelID().ToString(); + oneEvent["ID"] = event->EventID(); + oneEvent["Time"] = (Json::UInt)event->StartTime(); + oneEvent["Duration"] = event->Duration(); + oneEvent["Title"] = event->Title() ? event->Title() : ""; + oneEvent["ShortText"] = event->ShortText() ? event->ShortText() : ""; + oneEvent["HasTimer"] = event->HasTimer(); + oneEvent["Description"] = event->Description() ? event->Description() : ""; + if (founddescription) + oneEvent["FoundInDesc"] = true; + else + oneEvent["FoundInDesc"] = false; + jsevents.append(oneEvent); + } + } + } + + js["Result"] = true; + js["Events"] = jsevents; + return true; +} diff --git a/handler.h b/handler.h index c7836b8..92fb10d 100644 --- a/handler.h +++ b/handler.h @@ -30,6 +30,7 @@ bool jsonserver_timeredit(Json::Value& js, const char* postData); bool jsonserver_tunersstatus(Json::Value& js, const char* postData); bool jsonserver_epgsearchsame(Json::Value& js, const char* postData); bool jsonserver_epgdownload(Json::Value& js, const char* postData); +bool jsonserver_epgsearch(Json::Value& js, const char* postData); const cEvent* jsonserver_getEvent(Json::Value& js, int channelNumber, int eventID, int aroundTime); cTimer* jsonserver_findTimer(const char* rChannelID, const char* rName, const char* rStartTime, const char* rStopTime, const char* rWeekDays); -- 2.39.2