From df7df73be47880597cc4c75b74fc8f17baac3ce4 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Mon, 3 Jun 2013 01:00:24 +0100 Subject: [PATCH] Find event for timer in list even when timer is inactive --- handler.c | 89 +++++++++++++++++++++++++++++++++++++++---------------- handler.h | 4 +++ 2 files changed, 67 insertions(+), 26 deletions(-) diff --git a/handler.c b/handler.c index c19ac0a..7416b27 100644 --- a/handler.c +++ b/handler.c @@ -734,65 +734,85 @@ bool jsonserver_getscheduleevent(Json::Value& js, const char* postData) js["Error"] = "Bad request parameters"; return true; } - + int channelNumber = atoi(sChannelNumber); int eventID = atoi(sEventID); + const cEvent* event = jsonserver_getEvent(js, channelNumber, eventID, 0); + if (!event) + { + js["Result"] = false; + return true; + } + + Json::Value oneEvent; + 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["Description"] = event->Description() ? event->Description() : ""; + oneEvent["HasTimer"] = event->HasTimer(); + oneEvent["RunningStatus"] = event->RunningStatus(); + + js["Result"] = true; + js["Event"] = oneEvent; + return true; +} + +const cEvent* jsonserver_getEvent(Json::Value& js, int channelNumber, int eventID, int aroundTime) +{ + Log* log = Log::getInstance(); + cChannel* channel = NULL; for (channel = Channels.First(); channel; channel = Channels.Next(channel)) { if (channel->GroupSep()) continue; if (channel->Number() == channelNumber) break; } - + if (!channel) { log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber); - js["Result"] = false; js["Error"] = "Could not find channel"; - return true; + return NULL; } - + cSchedulesLock MutexLock; const cSchedules *Schedules = cSchedules::Schedules(MutexLock); if (!Schedules) { log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber); - js["Result"] = false; js["Error"] = "Internal schedules error (1)"; - return true; + return NULL; } + const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID()); if (!Schedule) { log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber); - js["Result"] = false; js["Error"] = "Internal schedules error (2)"; - return true; + return NULL; + } + + const cEvent* event = NULL; + if (eventID) + { + event = Schedule->GetEvent(eventID); + } + else + { + event = Schedule->GetEventAround(aroundTime); } - const cEvent* event = Schedule->GetEvent(eventID); if (!event) { log->log("JSONServer", Log::ERR, "Could not find requested event: %i", eventID); - js["Result"] = false; js["Error"] = "Internal schedules error (3)"; - return true; + return NULL; } - - Json::Value oneEvent; - 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["Description"] = event->Description() ? event->Description() : ""; - oneEvent["HasTimer"] = event->HasTimer(); - oneEvent["RunningStatus"] = event->RunningStatus(); - js["Result"] = true; - js["Event"] = oneEvent; - return true; + return event; } bool jsonserver_timerlist(Json::Value& js) @@ -824,9 +844,26 @@ bool jsonserver_timerlist(Json::Value& js) const cEvent* event = timer->Event(); if (event) + { oneTimer["EventID"] = event->EventID(); + } else - oneTimer["EventID"] = 0; + { + int channelNumber = timer->Channel()->Number(); + int aroundTime = timer->StartTime() + 1; + log->log("JSONServer", Log::DEBUG, "%i", aroundTime); + //((timer->StopTime() - timer->StartTime()) / 2); + + const cEvent* eventAround = jsonserver_getEvent(js, channelNumber, 0, aroundTime); + if (eventAround) + { + oneTimer["EventID"] = eventAround->EventID(); + } + else + { + oneTimer["EventID"] = 0; + } + } jstimers.append(oneTimer); } diff --git a/handler.h b/handler.h index 8d1a6fa..e1f4e7e 100644 --- a/handler.h +++ b/handler.h @@ -4,6 +4,8 @@ #include #include "mongoose.h" +class cEvent; + int jsonserver_request_handler(struct mg_connection* conn); bool jsonserver_gettime(Json::Value& js); @@ -21,5 +23,7 @@ bool jsonserver_timerdel(Json::Value& js, const char* postData); bool jsonserver_timerset(Json::Value& js, const char* postData); bool jsonserver_timersetactive(Json::Value& js, const char* postData); +const cEvent* jsonserver_getEvent(Json::Value& js, int channelNumber, int eventID, int aroundTime); + #endif -- 2.39.5