From f0a16d8bc7187c8e497a1308f62183b74725577b Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sun, 11 Aug 2013 16:53:53 +0100 Subject: [PATCH] Match timers using many fields, not ToText, add istimerrecording call --- handler.c | 184 +++++++++++++++++++++++++++++++++++++----------------- handler.h | 3 + 2 files changed, 129 insertions(+), 58 deletions(-) diff --git a/handler.c b/handler.c index 9c60610..1364ca9 100644 --- a/handler.c +++ b/handler.c @@ -83,6 +83,8 @@ int jsonserver_request_handler(struct mg_connection *conn) else if (!strcmp(wvrequest, "timerdel")) success = jsonserver_timerdel(js, postData); else if (!strcmp(wvrequest, "timerset")) success = jsonserver_timerset(js, postData); else if (!strcmp(wvrequest, "timersetactive")) success = jsonserver_timersetactive(js, postData); + else if (!strcmp(wvrequest, "timerisrecording")) success = jsonserver_timerisrecording(js, postData); + if (!success) return 0; // the specific handler failed badly @@ -1030,7 +1032,6 @@ bool jsonserver_timerlist(Json::Value& js) oneTimer["Day"] = (int)timer->Day(); oneTimer["WeekDays"] = timer->WeekDays(); oneTimer["Name"] = timer->File(); - oneTimer["ToText"] = (const char *)timer->ToText(true); const cEvent* event = timer->Event(); if (event) @@ -1107,51 +1108,46 @@ bool jsonserver_timersetactive(Json::Value& js, const char* postData) Log* log = Log::getInstance(); log->log("JSONServer", Log::DEBUG, "timersetactive"); - char tToText[1024]; int mgv1 = mg_get_var(postData, strlen(postData), "ToText", tToText, 1024); - char tNewActive[15]; int mgv2 = mg_get_var(postData, strlen(postData), "SetActive", tNewActive, 15); + char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50); + char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024); + char rStartTime[20]; int mgv3 = mg_get_var(postData, strlen(postData), "StartTime", rStartTime, 20); + char rStopTime[20]; int mgv4 = mg_get_var(postData, strlen(postData), "StopTime", rStopTime, 20); + char rWeekDays[50]; int mgv5 = mg_get_var(postData, strlen(postData), "WeekDays", rWeekDays, 20); + char tNewActive[20]; int mgv6 = mg_get_var(postData, strlen(postData), "SetActive", tNewActive, 20); - if ( (mgv1 == -1) || (mgv2 == -1) ) + if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1) || (mgv6 == -1)) { - log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2); + log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5, mgv6); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; } - log->log("JSONServer", Log::DEBUG, "timersetactive: %s %s", tToText, tNewActive); + log->log("JSONServer", Log::DEBUG, "timersetactive: %s %s:%s:%s:%s:%s", tNewActive, rChannelID, rName, rStartTime, rStopTime, rWeekDays); - cTimer *timer; - int numTimers = Timers.Count(); - for (int i = 0; i < numTimers; i++) + cTimer* timer = jsonserver_findTimer(rChannelID, rName, rStartTime, rStopTime, rWeekDays); + if (timer) { - timer = Timers.Get(i); - - if (strcmp(timer->ToText(true), tToText) == 0) + if (strcmp(tNewActive, "true") == 0) { - // Found - - if (strcmp(tNewActive, "true") == 0) - { - timer->SetFlags(tfActive); - } - else if (strcmp(tNewActive, "false") == 0) - { - timer->ClrFlags(tfActive); - } - else - { - js["Result"] = false; - js["Error"] = "Bad request parameters"; - return true; - } - - js["ToText"] = (const char*)timer->ToText(true); - js["Result"] = true; - - Timers.SetModified(); - + timer->SetFlags(tfActive); + } + else if (strcmp(tNewActive, "false") == 0) + { + timer->ClrFlags(tfActive); + } + else + { + js["Result"] = false; + js["Error"] = "Bad request parameters"; return true; } + + js["Result"] = true; + + Timers.SetModified(); + + return true; } js["Result"] = false; @@ -1159,23 +1155,65 @@ bool jsonserver_timersetactive(Json::Value& js, const char* postData) return true; } +cTimer* jsonserver_findTimer(const char* rChannelID, const char* rName, const char* rStartTime, const char* rStopTime, const char* rWeekDays) +{ + Log* log = Log::getInstance(); + + int numTimers = Timers.Count(); + cTimer* timer; + for (int i = 0; i < numTimers; i++) + { + timer = Timers.Get(i); + + log->log("JSONServer", Log::DEBUG, "findtimer current: %s", (const char*)timer->ToText(true)); + log->log("JSONServer", Log::DEBUG, "findtimer: %s", (const char*)timer->Channel()->GetChannelID().ToString()); + log->log("JSONServer", Log::DEBUG, "findtimer: %s", rChannelID); + log->log("JSONServer", Log::DEBUG, "findtimer: %s", timer->File()); + log->log("JSONServer", Log::DEBUG, "findtimer: %s", rName); + log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->StartTime()); + log->log("JSONServer", Log::DEBUG, "findtimer: %s", rStartTime); + log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->StopTime()); + log->log("JSONServer", Log::DEBUG, "findtimer: %s", rStopTime); + log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->WeekDays()); + log->log("JSONServer", Log::DEBUG, "findtimer: %s", rWeekDays); + + if ( + (strcmp(timer->Channel()->GetChannelID().ToString(), rChannelID) == 0) + && (strcmp(timer->File(), rName) == 0) + && (timer->StartTime() == atoi(rStartTime)) + && (timer->StopTime() == atoi(rStopTime)) + && (timer->WeekDays() == atoi(rWeekDays)) + ) + { + log->log("JSONServer", Log::DEBUG, "found"); + return timer; + } + } + log->log("JSONServer", Log::DEBUG, "no timer found"); + return NULL; +} + bool jsonserver_timerdel(Json::Value& js, const char* postData) { Log* log = Log::getInstance(); log->log("JSONServer", Log::DEBUG, "timerdel"); - char tToText[1024]; int mgv1 = mg_get_var(postData, strlen(postData), "ToText", tToText, 1024); + char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50); + char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024); + char rStartTime[20]; int mgv3 = mg_get_var(postData, strlen(postData), "StartTime", rStartTime, 20); + char rStopTime[20]; int mgv4 = mg_get_var(postData, strlen(postData), "StopTime", rStopTime, 20); + char rWeekDays[50]; int mgv5 = mg_get_var(postData, strlen(postData), "WeekDays", rWeekDays, 20); - if (mgv1 == -1) + if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1)) { - log->log("JSONServer", Log::ERR, "request mgvs: %i", mgv1); + log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; } - log->log("JSONServer", Log::DEBUG, "timerdel: %s", tToText); - + log->log("JSONServer", Log::DEBUG, "timerdel: %s:%s:%s:%s:%s", rChannelID, rName, rStartTime, rStopTime, rWeekDays); + if (Timers.BeingEdited()) { log->log("JSONServer", Log::ERR, "Unable to delete timer - timers being edited at VDR"); @@ -1183,29 +1221,59 @@ bool jsonserver_timerdel(Json::Value& js, const char* postData) js["Error"] = "Timers being edited at VDR"; return true; } - - cTimer* ti = NULL; - for (ti = Timers.First(); ti; ti = Timers.Next(ti)) + + cTimer* timer = jsonserver_findTimer(rChannelID, rName, rStartTime, rStopTime, rWeekDays); + if (timer) { - if (strcmp(ti->ToText(true), tToText) == 0) + if (timer->Recording()) { - // Found - - if (ti->Recording()) - { - log->log("JSONServer", Log::ERR, "Unable to delete timer - timer is running"); - js["Result"] = false; - js["Error"] = "Timer is running"; - return true; - } - - // delete - - Timers.Del(ti); - Timers.SetModified(); - js["Result"] = true; + log->log("JSONServer", Log::ERR, "Unable to delete timer - timer is running"); + js["Result"] = false; + js["Error"] = "Timer is running"; return true; } + + // delete + + Timers.Del(timer); + Timers.SetModified(); + js["Result"] = true; + return true; + } + + js["Result"] = false; + js["Error"] = "Timer not found"; + return true; +} + +bool jsonserver_timerisrecording(Json::Value& js, const char* postData) +{ + Log* log = Log::getInstance(); + log->log("JSONServer", Log::DEBUG, "timerisrecording"); + + char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50); + char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024); + char rStartTime[20]; int mgv3 = mg_get_var(postData, strlen(postData), "StartTime", rStartTime, 20); + char rStopTime[20]; int mgv4 = mg_get_var(postData, strlen(postData), "StopTime", rStopTime, 20); + char rWeekDays[50]; int mgv5 = mg_get_var(postData, strlen(postData), "WeekDays", rWeekDays, 20); + + if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1)) + { + log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5); + js["Result"] = false; + js["Error"] = "Bad request parameters"; + return true; + } + + log->log("JSONServer", Log::DEBUG, "timerisrecording: %s:%s:%s:%s:%s", rChannelID, rName, rStartTime, rStopTime, rWeekDays); + + cTimer* timer = jsonserver_findTimer(rChannelID, rName, rStartTime, rStopTime, rWeekDays); + if (timer) + { + js["Recording"] = timer->Recording(); + js["Pending"] = timer->Pending(); + js["Result"] = true; + return true; } js["Result"] = false; diff --git a/handler.h b/handler.h index fc23372..aa3159d 100644 --- a/handler.h +++ b/handler.h @@ -5,6 +5,7 @@ #include "mongoose.h" class cEvent; +class cTimer; int jsonserver_request_handler(struct mg_connection* conn); @@ -23,8 +24,10 @@ bool jsonserver_timerlist(Json::Value& js); 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); +bool jsonserver_timerisrecording(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); #endif -- 2.39.5