From 2a4ae9061a282bd339038296d8141380fe51ab83 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Tue, 20 Jun 2017 20:24:58 +0100 Subject: [PATCH] Add edit-timer call --- handler.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++- handler.h | 2 + jsonserver.c | 0 3 files changed, 206 insertions(+), 2 deletions(-) mode change 100755 => 100644 handler.c mode change 100755 => 100644 handler.h mode change 100755 => 100644 jsonserver.c diff --git a/handler.c b/handler.c old mode 100755 new mode 100644 index 28778d4..0286d26 --- a/handler.c +++ b/handler.c @@ -85,6 +85,7 @@ int jsonserver_request_handler(struct mg_connection *conn) 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); + else if (!strcmp(wvrequest, "timeredit")) success = jsonserver_timeredit(js, postData); else if (!strcmp(wvrequest, "tunersstatus")) success = jsonserver_tunersstatus(js, postData); if (!success) return 0; // the specific handler failed badly @@ -885,6 +886,7 @@ bool jsonserver_channellist(Json::Value& js) // else continue; Json::Value oneChannel; + oneChannel["ID"] = (const char *)channel->GetChannelID().ToString(); oneChannel["Number"] = channel->Number(); // oneChannel["Type"] = type; oneChannel["Name"] = channel->Name(); @@ -1227,10 +1229,210 @@ 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) +bool jsonserver_timeredit(Json::Value& js, const char* postData) +{ + Log* log = Log::getInstance(); + log->log("JSONServer", Log::DEBUG, "timeredit"); + + // Get all olds + char oldName[1024]; int mgv1 = mg_get_var(postData, strlen(postData), "OldName", oldName, 1024); + char oldActive[20]; int mgv2 = mg_get_var(postData, strlen(postData), "OldActive", oldActive, 20); + char oldChannelID[50]; int mgv3 = mg_get_var(postData, strlen(postData), "OldChannelID", oldChannelID, 50); + char oldDay[20]; int mgv4 = mg_get_var(postData, strlen(postData), "OldDay", oldDay, 20); + char oldWeekDays[50]; int mgv5 = mg_get_var(postData, strlen(postData), "OldWeekDays", oldWeekDays, 20); + char oldStartTime[20]; int mgv6 = mg_get_var(postData, strlen(postData), "OldStartTime", oldStartTime, 20); + char oldStopTime[20]; int mgv7 = mg_get_var(postData, strlen(postData), "OldStopTime", oldStopTime, 20); + char oldPriority[20]; int mgv8 = mg_get_var(postData, strlen(postData), "OldPriority", oldPriority, 20); + char oldLifetime[20]; int mgv9 = mg_get_var(postData, strlen(postData), "OldLifetime", oldLifetime, 20); + + if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1) || (mgv6 == -1) || (mgv7 == -1) || (mgv8 == -1) || (mgv9 == -1)) + { + log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5, mgv6, mgv7, mgv8, mgv9); + js["Result"] = false; + js["Error"] = "Bad request parameters"; + return true; + } + + log->log("JSONServer", Log::DEBUG, "timeredit: %s %s %s %s %s %s %s %s %s", oldName, oldActive, oldChannelID, oldDay, oldWeekDays, oldStartTime, oldStopTime, oldPriority, oldLifetime); + + + // Get all news + char newName[1024]; int mgv11 = mg_get_var(postData, strlen(postData), "NewName", newName, 1024); + char newActive[20]; int mgv12 = mg_get_var(postData, strlen(postData), "NewActive", newActive, 20); + char newChannelID[50]; int mgv13 = mg_get_var(postData, strlen(postData), "NewChannelID", newChannelID, 50); + char newDay[20]; int mgv14 = mg_get_var(postData, strlen(postData), "NewDay", newDay, 20); + char newWeekDays[50]; int mgv15 = mg_get_var(postData, strlen(postData), "NewWeekDays", newWeekDays, 20); + char newStartTime[20]; int mgv16 = mg_get_var(postData, strlen(postData), "NewStartTime", newStartTime, 20); + char newStopTime[20]; int mgv17 = mg_get_var(postData, strlen(postData), "NewStopTime", newStopTime, 20); + char newPriority[20]; int mgv18 = mg_get_var(postData, strlen(postData), "NewPriority", newPriority, 20); + char newLifetime[20]; int mgv19 = mg_get_var(postData, strlen(postData), "NewLifetime", newLifetime, 20); + + if ( (mgv11 == -1) || (mgv12 == -1) || (mgv13 == -1) || (mgv14 == -1) || (mgv15 == -1) || (mgv16 == -1) || (mgv17 == -1) || (mgv18 == -1) || (mgv19 == -1)) + { + log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i %i %i %i %i", mgv11, mgv12, mgv13, mgv14, mgv15, mgv16, mgv17, mgv18, mgv19); + js["Result"] = false; + js["Error"] = "Bad request parameters"; + return true; + } + + log->log("JSONServer", Log::DEBUG, "timeredit: %s %s %s %s %s %s %s %s %s", newName, newActive, newChannelID, newDay, newWeekDays, newStartTime, newStopTime, newPriority, newLifetime); + + + cTimer* timer = jsonserver_findTimer2(oldName, oldActive, oldChannelID, oldDay, oldWeekDays, oldStartTime, oldStopTime, oldPriority, oldLifetime); + if (!timer) + { + js["Result"] = false; + js["Error"] = "Timer not found"; + return true; + } + + // Old version commented below used to set each thing individually based on whether it had changed. However, since + // the only way to change the timer channel appears to be with the cTimer::Parse function, might as well use that + // for everything it supports + // Except flags. Get current flags, set using Parse, then add/remove active as needed the other way. + + time_t nstt = atoi(newStartTime); + struct tm nstm; + localtime_r(&nstt, &nstm); + int nssf = (nstm.tm_hour * 100) + nstm.tm_min; + + time_t nztt = atoi(newStopTime); + struct tm nztm; + localtime_r(&nztt, &nztm); + int nzsf = (nztm.tm_hour * 100) + nztm.tm_min; + + strreplace(newName, ':', '|'); + + cString parseBuffer = cString::sprintf("%u:%s:%s:%04d:%04d:%d:%d:%s:%s", + timer->Flags(), newChannelID, *(cTimer::PrintDay(atoi(newDay), atoi(newWeekDays), true)), + nssf, nzsf, atoi(newPriority), atoi(newLifetime), newName, timer->Aux() ? timer->Aux() : ""); + + log->log("JSONServer", Log::DEBUG, "timeredit new parse: %s", *parseBuffer); + + bool parseResult = timer->Parse(*parseBuffer); + if (!parseResult) + { + js["Result"] = false; + js["Error"] = "Timer parsing failed"; + return true; + } + + if (timer->HasFlags(tfActive) != !(strcasecmp(newActive, "true"))) + { + log->log("JSONServer", Log::DEBUG, "%i %i timeredit set new active: %s", timer->HasFlags(tfActive), !(strcasecmp(newActive, "true")), newActive); + + if (strcasecmp(newActive, "true") == 0) + { + timer->SetFlags(tfActive); + } + else if (strcasecmp(newActive, "false") == 0) + { + timer->ClrFlags(tfActive); + } + else + { + js["Result"] = false; + js["Error"] = "Bad request parameters"; + return true; + } + } + + js["Result"] = true; + Timers.SetModified(); + return true; + + /* + + if (strcmp(timer->File(), newName)) + { + log->log("JSONServer", Log::DEBUG, "timeredit set new name: %s", newName); + timer->SetFile(newName); + } + + if (timer->Day() != atoi(newDay)) + { + log->log("JSONServer", Log::DEBUG, "timeredit set new day: %s", newDay); + timer->SetDay(atoi(newDay)); + } + + if (timer->WeekDays() != atoi(newWeekDays)) + { + log->log("JSONServer", Log::DEBUG, "timeredit set new week days: %s", newWeekDays); + timer->SetWeekDays(atoi(newWeekDays)); + } + + if (timer->StartTime() != atoi(newStartTime)) + { + log->log("JSONServer", Log::DEBUG, "timeredit set new start time: %s", newStartTime); + time_t nstt = atoi(newStartTime); + struct tm nstm; + localtime_r(&nstt, &nstm); + int nssf = (nstm.tm_hour * 100) + nstm.tm_min; + timer->SetStart(nssf); + } + + if (timer->StopTime() != atoi(newStopTime)) + { + log->log("JSONServer", Log::DEBUG, "timeredit set new stop time: %s", newStopTime); + time_t nztt = atoi(newStopTime); + struct tm nztm; + localtime_r(&nztt, &nztm); + int nzsf = (nztm.tm_hour * 100) + nztm.tm_min; + timer->SetStop(nzsf); + } + + if (timer->Priority() != atoi(newPriority)) + { + log->log("JSONServer", Log::DEBUG, "timeredit set new priority: %s", newPriority); + timer->SetPriority(atoi(newPriority)); + } + + if (timer->Lifetime() != atoi(newLifetime)) + { + log->log("JSONServer", Log::DEBUG, "timeredit set new lifetime: %s", newLifetime); + timer->SetLifetime(atoi(newLifetime)); + } +*/ +} + +cTimer* jsonserver_findTimer2(const char* rName, const char* rActive, const char* rChannelID, const char* rDay, const char* rWeekDays, const char* rStartTime, const char* rStopTime, const char* rPriority, const char* rLifetime) { Log* log = Log::getInstance(); + int numTimers = Timers.Count(); + cTimer* timer; + log->log("JSONServer", Log::DEBUG, "findtimer2: %s %s %s %s %s %s %s %s %s", rName, rActive, rChannelID, rDay, rWeekDays, rStartTime, rStopTime, rPriority, rLifetime); + for (int i = 0; i < numTimers; i++) + { + timer = Timers.Get(i); + + log->log("JSONServer", Log::DEBUG, "findtimer2 search: %s %i %s %i %i %i %i %i %i", timer->File(), timer->HasFlags(tfActive), (const char*)timer->Channel()->GetChannelID().ToString(), + (int)timer->Day(), timer->WeekDays(), (int)timer->StartTime(), (int)timer->StopTime(), + timer->Priority(), timer->Lifetime()); + + if ( (strcmp(timer->File(), rName) == 0) + && (timer->HasFlags(tfActive) == !(strcasecmp(rActive, "true"))) + && (strcmp(timer->Channel()->GetChannelID().ToString(), rChannelID) == 0) + && (timer->Day() == atoi(rDay)) + && (timer->WeekDays() == atoi(rWeekDays)) + && (timer->StartTime() == atoi(rStartTime)) + && (timer->StopTime() == atoi(rStopTime)) + && (timer->Priority() == atoi(rPriority)) + && (timer->Lifetime() == atoi(rLifetime)) + ) + { + log->log("JSONServer", Log::DEBUG, "found"); + return timer; + } + } + log->log("JSONServer", Log::DEBUG, "no timer found"); + return NULL; +} + +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++) @@ -1248,7 +1450,7 @@ cTimer* jsonserver_findTimer(const char* rChannelID, const char* rName, const ch 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) diff --git a/handler.h b/handler.h old mode 100755 new mode 100644 index 32ce1e6..f68c7f1 --- a/handler.h +++ b/handler.h @@ -26,10 +26,12 @@ 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); +bool jsonserver_timeredit(Json::Value& js, const char* postData); bool jsonserver_tunersstatus(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); +cTimer* jsonserver_findTimer2(const char* rName, const char* rActive, const char* rChannelID, const char* rDay, const char* rWeekDays, const char* rStartTime, const char* rStopTime, const char* rPriority, const char* rLifetime); #endif diff --git a/jsonserver.c b/jsonserver.c old mode 100755 new mode 100644 -- 2.39.5