6 #include <jsoncpp/json/json.h>
9 #include <vdr/videodir.h>
10 #include <vdr/recording.h>
12 #include <vdr/timers.h>
16 int jsonserver_request_handler(struct mg_connection *conn)
18 Log* log = Log::getInstance();
19 const struct mg_request_info *request_info = mg_get_request_info(conn);
21 if (strcmp(request_info->uri, "/jsonserver")) return 0; // not for us
24 int wvrl = mg_get_var(request_info->query_string, strlen(request_info->query_string), "req", wvrequest, 20);
27 log->log("JSONServer", Log::ERR, "Could not decode req");
32 if (!strcmp(request_info->request_method, "OPTIONS"))
34 mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n");
35 mg_printf(conn, "%s", "Access-Control-Allow-Origin: *\r\n");
36 mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n");
44 if (!strcmp(request_info->request_method, "POST"))
46 const char* contentLength = mg_get_header(conn, "Content-Length");
47 int contentLengthI = atoi(contentLength);
48 log->log("JSONServer", Log::DEBUG, "POST data content length: %i", contentLengthI);
49 if (contentLengthI > 10000)
51 log->log("JSONServer", Log::DEBUG, "Length > 10000, rejecting");
55 if (contentLengthI > 0)
57 // FIXME - assume for now that all post data will be small enough to have arrived immediately
58 int bytesRead = mg_read(conn, postData, contentLengthI);
59 if (bytesRead != contentLengthI)
61 log->log("JSONServer", Log::DEBUG, "Could not read up to contentLength");
64 postData[contentLengthI] = '\0';
71 if (!strcmp(wvrequest, "gettime")) success = jsonserver_gettime(js);
72 else if (!strcmp(wvrequest, "diskstats")) success = jsonserver_diskstats(js);
73 else if (!strcmp(wvrequest, "reclist")) success = jsonserver_reclist(js);
74 else if (!strcmp(wvrequest, "recinfo")) success = jsonserver_recinfo(js, postData);
75 else if (!strcmp(wvrequest, "recdel")) success = jsonserver_recdel(js, postData);
76 else if (!strcmp(wvrequest, "recmove")) success = jsonserver_recmove(js, postData);
77 else if (!strcmp(wvrequest, "recrename")) success = jsonserver_recrename(js, postData);
78 else if (!strcmp(wvrequest, "recstop")) success = jsonserver_recstop(js, postData);
79 else if (!strcmp(wvrequest, "recresetresume")) success = jsonserver_recresetresume(js, postData);
80 else if (!strcmp(wvrequest, "channellist")) success = jsonserver_channellist(js);
81 else if (!strcmp(wvrequest, "channelschedule")) success = jsonserver_channelschedule(js, postData);
82 else if (!strcmp(wvrequest, "getscheduleevent")) success = jsonserver_getscheduleevent(js, postData);
83 else if (!strcmp(wvrequest, "timerlist")) success = jsonserver_timerlist(js);
84 else if (!strcmp(wvrequest, "timerdel")) success = jsonserver_timerdel(js, postData);
85 else if (!strcmp(wvrequest, "timerset")) success = jsonserver_timerset(js, postData);
86 else if (!strcmp(wvrequest, "timersetactive")) success = jsonserver_timersetactive(js, postData);
87 else if (!strcmp(wvrequest, "timerisrecording")) success = jsonserver_timerisrecording(js, postData);
88 else if (!strcmp(wvrequest, "timeredit")) success = jsonserver_timeredit(js, postData);
89 else if (!strcmp(wvrequest, "tunersstatus")) success = jsonserver_tunersstatus(js, postData);
91 if (!success) return 0; // the specific handler failed badly
93 // Now js will be filled
95 Json::StyledWriter sw;
96 std::string jsonout = sw.write(js);
97 mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n");
98 mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n");
99 mg_write(conn, jsonout.c_str(), jsonout.length());
104 else if (event == MG_EVENT_LOG)
106 if (request_info->status_code == 400) // bad request
108 log->log("Mongoose", Log::DEBUG, "400 BAD REQUEST:");
109 log->log("Mongoose", Log::DEBUG, request_info->request_method);
110 log->log("Mongoose", Log::DEBUG, request_info->uri);
111 log->log("Mongoose", Log::DEBUG, request_info->http_version);
112 log->log("Mongoose", Log::DEBUG, request_info->query_string);
113 log->log("Mongoose", Log::DEBUG, request_info->log_message);
114 for (int i = 0; i < request_info->num_headers; i++)
116 log->log("Mongoose", Log::DEBUG, "%s: %s", request_info->http_headers[i].name, request_info->http_headers[i].value);
121 log->log("Mongoose", Log::DEBUG, request_info->log_message);
122 log->log("Mongoose", Log::DEBUG, request_info->uri);
127 // other events not handled:
128 // MG_HTTP_ERROR, MG_INIT_SSL
129 // Let mongoose do something with those
137 bool jsonserver_gettime(Json::Value& js)
139 Log* log = Log::getInstance();
140 log->log("JSONServer", Log::DEBUG, "gettime");
143 gettimeofday(&tv, NULL);
145 js["Time"] = (Json::UInt64)tv.tv_sec;
146 js["MTime"] = (Json::UInt)(tv.tv_usec/1000);
151 bool jsonserver_diskstats(Json::Value& js)
153 Log* log = Log::getInstance();
154 log->log("JSONServer", Log::DEBUG, "diskstats");
159 #if APIVERSNUM > 20101
160 int Percent = cVideoDirectory::VideoDiskSpace(&FreeMB, &UsedMB);
162 int Percent = VideoDiskSpace(&FreeMB, &UsedMB);
165 js["FreeMiB"] = FreeMB;
166 js["UsedMiB"] = UsedMB;
167 js["Percent"] = Percent;
173 bool jsonserver_reclist(Json::Value& js)
175 Log* log = Log::getInstance();
176 log->log("JSONServer", Log::DEBUG, "reclist");
178 Json::Value jsrecordings(Json::arrayValue);
179 cRecordings Recordings;
181 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
184 oneRec["StartTime"] = (Json::UInt)recording->Start();
185 oneRec["Length"] = (Json::UInt)recording->LengthInSeconds();
186 oneRec["IsNew"] = recording->IsNew();
187 oneRec["Name"] = recording->Name();
188 oneRec["Filename"] = recording->FileName();
189 oneRec["FileSizeMB"] = recording->FileSizeMB();
191 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
192 if (rc) oneRec["CurrentlyRecording"] = true;
193 else oneRec["CurrentlyRecording"] = false;
195 jsrecordings.append(oneRec);
197 js["Recordings"] = jsrecordings;
202 bool jsonserver_recinfo(Json::Value& js, const char* postData)
204 Log* log = Log::getInstance();
205 log->log("JSONServer", Log::DEBUG, "recinfo");
207 char reqfilename[1000];
208 int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000);
211 log->log("JSONServer", Log::ERR, "Could not decode filename");
212 js["Result"] = false;
213 js["Error"] = "Could not decode filename";
217 log->log("JSONServer", Log::DEBUG, "%s", reqfilename);
219 cRecordings Recordings;
220 Recordings.Load(); // probably have to do this
221 cRecording *recording = Recordings.GetByName(reqfilename);
225 log->log("JSONServer", Log::ERR, "recinfo found no recording");
226 js["Result"] = false;
230 js["IsNew"] = recording->IsNew();
231 js["LengthInSeconds"] = recording->LengthInSeconds();
232 js["FileSizeMB"] = recording->FileSizeMB();
233 js["Name"] = recording->Name() ? recording->Name() : Json::Value::null;
234 js["Priority"] = recording->Priority();
235 js["LifeTime"] = recording->Lifetime();
236 js["Start"] = (Json::UInt)recording->Start();
238 js["CurrentlyRecordingStart"] = 0;
239 js["CurrentlyRecordingStop"] = 0;
240 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
243 js["CurrentlyRecordingStart"] = (Json::UInt)rc->Timer()->StartTime();
244 js["CurrentlyRecordingStop"] = (Json::UInt)rc->Timer()->StopTime();
247 js["ResumePoint"] = 0;
249 const cRecordingInfo *info = recording->Info();
252 js["ChannelName"] = info->ChannelName() ? info->ChannelName() : Json::Value::null;
253 js["Title"] = info->Title() ? info->Title() : Json::Value::null;
254 js["ShortText"] = info->ShortText() ? info->ShortText() : Json::Value::null;
255 js["Description"] = info->Description() ? info->Description() : Json::Value::null;
257 const cComponents* components = info->Components();
260 js["Components"] = Json::Value::null;
264 Json::Value jscomponents;
266 tComponent* component;
267 for (int i = 0; i < components->NumComponents(); i++)
269 component = components->Component(i);
271 Json::Value oneComponent;
272 oneComponent["Stream"] = component->stream;
273 oneComponent["Type"] = component->type;
274 oneComponent["Language"] = component->language ? component->language : Json::Value::null;
275 oneComponent["Description"] = component->description ? component->description : Json::Value::null;
276 jscomponents.append(oneComponent);
279 js["Components"] = jscomponents;
282 cResumeFile ResumeFile(recording->FileName(), recording->IsPesRecording());
283 if (ResumeFile.Read() >= 0) js["ResumePoint"] = floor(ResumeFile.Read() / info->FramesPerSecond());
290 bool jsonserver_recstop(Json::Value& js, const char* postData)
292 Log* log = Log::getInstance();
293 log->log("JSONServer", Log::DEBUG, "recstop");
295 char reqfilename[1000];
296 int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000);
299 log->log("JSONServer", Log::ERR, "Could not decode filename");
300 js["Result"] = false;
301 js["Error"] = "Could not decode filename";
305 log->log("JSONServer", Log::DEBUG, "%s", reqfilename);
307 cRecordings Recordings;
308 Recordings.Load(); // probably have to do this
309 cRecording *recording = Recordings.GetByName(reqfilename);
313 log->log("JSONServer", Log::ERR, "recstop found no recording");
314 js["Result"] = false;
318 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
321 log->log("JSONServer", Log::ERR, "recstop - not currently recording");
322 js["Result"] = false;
326 if (Timers.BeingEdited())
328 log->log("JSONServer", Log::ERR, "recstop - timers being edited elsewhere");
329 js["Result"] = false;
333 cTimer* timer = rc->Timer();
336 log->log("JSONServer", Log::ERR, "recstop - timer not found");
337 js["Result"] = false;
341 timer->ClrFlags(tfActive);
342 Timers.SetModified();
348 bool jsonserver_recdel(Json::Value& js, const char* postData)
350 Log* log = Log::getInstance();
351 log->log("JSONServer", Log::DEBUG, "recdel");
353 char reqfilename[1000];
354 int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000);
357 log->log("JSONServer", Log::ERR, "Could not decode filename");
358 js["Result"] = false;
359 js["Error"] = "Could not decode filename";
363 log->log("JSONServer", Log::DEBUG, "%s", reqfilename);
365 cRecordings Recordings;
366 Recordings.Load(); // probably have to do this
367 cRecording *recording = Recordings.GetByName(reqfilename);
371 js["Result"] = false;
372 js["Error"] = "Could not find recording to delete";
376 log->log("JSONServer", Log::DEBUG, "Deleting recording: %s", recording->Name());
377 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
380 js["Result"] = false;
381 js["Error"] = "This recording is still recording.. ho ho";
385 if (recording->Delete())
387 ::Recordings.DelByName(recording->FileName());
392 js["Result"] = false;
393 js["Error"] = "Failed to delete recording";
399 bool jsonserver_recmove(Json::Value& js, const char* postData)
401 Log* log = Log::getInstance();
402 log->log("JSONServer", Log::DEBUG, "recmove");
404 char* fileNameToMove = NULL;
405 char* requestedNewPath = NULL;
406 char* dateDirName = NULL;
407 char* titleDirName = NULL;
408 char* folderName = NULL;
409 char* newContainer = NULL;
414 int postDataLen = strlen(postData)+1;
415 fileNameToMove = new char[postDataLen];
416 int mgv1 = mg_get_var(postData, postDataLen-1, "filename", fileNameToMove, postDataLen);
417 requestedNewPath = new char[postDataLen];
418 int mgv2 = mg_get_var(postData, postDataLen-1, "newpath", requestedNewPath, postDataLen);
420 if ((mgv1 == -1) || (mgv2 == -1) || !strlen(fileNameToMove) || !strlen(requestedNewPath))
422 log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2);
426 cRecordings Recordings;
427 Recordings.Load(); // probably have to do this
428 cRecording* recordingObj = Recordings.GetByName(fileNameToMove);
429 if (!recordingObj) throw 2;
431 cRecordControl *rc = cRecordControls::GetRecordControl(recordingObj->FileName());
434 log->log("JSONServer", Log::DEBUG, "moving recording: %s", recordingObj->Name());
435 log->log("JSONServer", Log::DEBUG, "moving recording: %s", recordingObj->FileName());
436 log->log("JSONServer", Log::DEBUG, "to: %s", requestedNewPath);
438 const char* t = recordingObj->FileName();
442 // Find the datedirname
443 for(k = strlen(t) - 1; k >= 0; k--)
447 log->log("JSONServer", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
448 dateDirName = new char[strlen(&t[k+1]) + 1];
449 strcpy(dateDirName, &t[k+1]);
454 // Find the titledirname
456 for(j = k-1; j >= 0; j--)
460 log->log("JSONServer", Log::DEBUG, "l2: %i", k - j);
461 titleDirName = new char[k - j];
462 memcpy(titleDirName, &t[j+1], k - j - 1);
463 titleDirName[k - j - 1] = '\0';
468 // Find the foldername
470 #if APIVERSNUM > 20101
471 const char* vidDirStr = cVideoDirectory::Name();
473 const char* vidDirStr = VideoDirectory;
475 int vidDirStrLen = strlen(vidDirStr);
477 log->log("JSONServer", Log::DEBUG, "j = %u, strlenvd = %u", j, vidDirStrLen);
478 if (j > vidDirStrLen) // Rec is in a subfolder now
480 for(m = j-1; m >= 0; m--)
484 log->log("JSONServer", Log::DEBUG, "l3: %i", j - m);
485 folderName = new char[j - m];
486 memcpy(folderName, &t[m+1], j - m - 1);
487 folderName[j - m - 1] = '\0';
493 ExchangeChars(requestedNewPath, true);
495 log->log("JSONServer", Log::DEBUG, "datedirname: %s", dateDirName);
496 log->log("JSONServer", Log::DEBUG, "titledirname: %s", titleDirName);
497 log->log("JSONServer", Log::DEBUG, "viddir: %s", vidDirStr);
498 if (folderName) log->log("JSONServer", Log::DEBUG, "folderName: %s", folderName);
499 log->log("JSONServer", Log::DEBUG, "EC: %s", requestedNewPath);
501 // Could be a new path - construct that first and test
502 newContainer = new char[vidDirStrLen + strlen(requestedNewPath) + strlen(titleDirName) + 1];
503 sprintf(newContainer, "%s%s", vidDirStr, requestedNewPath);
504 log->log("JSONServer", Log::DEBUG, "NPT: %s", newContainer);
506 int statret = stat(newContainer, &dstat);
507 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
509 log->log("JSONServer", Log::DEBUG, "new path does not exist (1)");
510 int mkdirret = mkdir(newContainer, 0755);
511 if (mkdirret != 0) throw 4;
513 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR)))
515 // Something exists but it's not a dir
519 // New path now created or was there already
521 sprintf(newContainer, "%s%s%s", vidDirStr, requestedNewPath, titleDirName);
522 log->log("JSONServer", Log::DEBUG, "%s", newContainer);
524 statret = stat(newContainer, &dstat);
525 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
527 log->log("JSONServer", Log::DEBUG, "new dir does not exist (2)");
528 int mkdirret = mkdir(newContainer, 0755);
529 if (mkdirret != 0) throw 6;
531 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR)))
533 // Something exists but it's not a dir
537 // Ok, the directory container has been made, or it pre-existed.
539 newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
540 sprintf(newDir, "%s/%s", newContainer, dateDirName);
542 log->log("JSONServer", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
543 if (rename(t, newDir) != 0) throw 8;
545 // Success. Test for remove old dir containter
546 char* tempOldTitleDir = new char[k+1];
547 memcpy(tempOldTitleDir, t, k);
548 tempOldTitleDir[k] = '\0';
549 log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(tempOldTitleDir), tempOldTitleDir);
550 rmdir(tempOldTitleDir); // can't do anything about a fail result at this point.
551 delete[] tempOldTitleDir;
553 // Test for remove old foldername
556 char* tempOldFolderName = new char[j+1];
557 memcpy(tempOldFolderName, t, j);
558 tempOldFolderName[j] = '\0';
559 log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldfoldername: %s", j+1, j, strlen(tempOldFolderName), tempOldFolderName);
562 rmdir() deletes a directory, which must be empty.
563 ENOTEMPTY - pathname contains entries other than . and ..
564 So, should be safe to call rmdir on non-empty dir
566 rmdir(tempOldFolderName); // can't do anything about a fail result at this point.
567 delete[] tempOldFolderName;
570 ::Recordings.Update();
572 js["NewRecordingFileName"] = newDir;
576 js["Result"] = false;
579 log->log("JSONServer", Log::ERR, "Bad parameters");
580 js["Error"] = "Bad request parameters";
584 log->log("JSONServer", Log::ERR, "Could not find recording to move");
585 js["Error"] = "Bad filename";
589 log->log("JSONServer", Log::ERR, "Could not move recording, it is still recording");
590 js["Error"] = "Cannot move recording in progress";
594 log->log("JSONServer", Log::ERR, "Failed to make new dir (1)");
595 js["Error"] = "Failed to create new directory (1)";
599 log->log("JSONServer", Log::ERR, "Something already exists? (1)");
600 js["Error"] = "Something already exists at the new path (1)";
604 log->log("JSONServer", Log::ERR, "Failed to make new dir (2)");
605 js["Error"] = "Failed to create new directory (2)";
609 log->log("JSONServer", Log::ERR, "Something already exists?");
610 js["Error"] = "Something already exists at the new path";
614 log->log("JSONServer", Log::ERR, "Rename failed");
615 js["Error"] = "Move failed";
619 if (fileNameToMove) delete[] fileNameToMove;
620 if (requestedNewPath) delete[] requestedNewPath;
621 if (dateDirName) delete[] dateDirName;
622 if (titleDirName) delete[] titleDirName;
623 if (folderName) delete[] folderName;
624 if (newContainer) delete[] newContainer;
625 if (newDir) delete[] newDir;
630 bool jsonserver_recrename(Json::Value& js, const char* postData)
632 Log* log = Log::getInstance();
633 log->log("JSONServer", Log::DEBUG, "recrename");
635 char* fileNameToRename = NULL;
636 char* requestedNewName = NULL;
637 char* dateDirName = NULL;
638 char* titleDirName = NULL;
639 char* folderName = NULL;
640 char* newContainer = NULL;
645 int postDataLen = strlen(postData)+1;
646 fileNameToRename = new char[postDataLen];
647 int mgv1 = mg_get_var(postData, postDataLen-1, "filename", fileNameToRename, postDataLen);
648 requestedNewName = new char[postDataLen];
649 int mgv2 = mg_get_var(postData, postDataLen-1, "newname", requestedNewName, postDataLen);
651 if ((mgv1 == -1) || (mgv2 == -1) || !strlen(fileNameToRename) || !strlen(requestedNewName))
653 log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2);
657 cRecordings Recordings;
658 Recordings.Load(); // probably have to do this
659 cRecording* recordingObj = Recordings.GetByName(fileNameToRename);
660 if (!recordingObj) throw 2;
662 cRecordControl *rc = cRecordControls::GetRecordControl(recordingObj->FileName());
665 log->log("JSONServer", Log::DEBUG, "renaming recording: %s", recordingObj->Name());
666 log->log("JSONServer", Log::DEBUG, "renaming recording: %s", recordingObj->FileName());
667 log->log("JSONServer", Log::DEBUG, "to: %s", requestedNewName);
669 const char* t = recordingObj->FileName();
673 // Find the datedirname
674 for(k = strlen(t) - 1; k >= 0; k--)
678 log->log("JSONServer", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
679 dateDirName = new char[strlen(&t[k+1]) + 1];
680 strcpy(dateDirName, &t[k+1]);
685 // Find the titledirname
687 for(j = k-1; j >= 0; j--)
691 log->log("JSONServer", Log::DEBUG, "l2: %i", k - j);
692 titleDirName = new char[k - j];
693 memcpy(titleDirName, &t[j+1], k - j - 1);
694 titleDirName[k - j - 1] = '\0';
699 // Find the foldername
701 #if APIVERSNUM > 20101
702 const char* vidDirStr = cVideoDirectory::Name();
704 const char* vidDirStr = VideoDirectory;
706 int vidDirStrLen = strlen(vidDirStr);
708 log->log("JSONServer", Log::DEBUG, "j = %u, strlenvd = %u", j, vidDirStrLen);
709 if (j > vidDirStrLen) // Rec is in a subfolder now
711 for(m = j-1; m >= 0; m--)
715 log->log("JSONServer", Log::DEBUG, "l3: %i", j - m);
716 folderName = new char[j - m];
717 memcpy(folderName, &t[m+1], j - m - 1);
718 folderName[j - m - 1] = '\0';
724 ExchangeChars(requestedNewName, true);
726 log->log("JSONServer", Log::DEBUG, "datedirname: %s", dateDirName);
727 log->log("JSONServer", Log::DEBUG, "titledirname: %s", titleDirName);
728 log->log("JSONServer", Log::DEBUG, "viddir: %s", vidDirStr);
729 if (folderName) log->log("JSONServer", Log::DEBUG, "folderName: %s", folderName);
730 log->log("JSONServer", Log::DEBUG, "EC: %s", requestedNewName);
732 // Could be a new path - construct that first and test
736 newContainer = new char[vidDirStrLen + 1 + strlen(folderName) + 1 + strlen(requestedNewName) + 1];
737 sprintf(newContainer, "%s/%s/%s", vidDirStr, folderName, requestedNewName);
741 newContainer = new char[vidDirStrLen + 1 + strlen(requestedNewName) + 1];
742 sprintf(newContainer, "%s/%s", vidDirStr, requestedNewName);
744 log->log("JSONServer", Log::DEBUG, "NPT: %s", newContainer);
746 int statret = stat(newContainer, &dstat);
747 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
749 log->log("JSONServer", Log::DEBUG, "new path does not exist (1)");
750 int mkdirret = mkdir(newContainer, 0755);
751 if (mkdirret != 0) throw 4;
753 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR)))
755 // Something exists but it's not a dir
759 // New path now created or was there already
761 newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
762 sprintf(newDir, "%s/%s", newContainer, dateDirName);
764 log->log("JSONServer", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
765 if (rename(t, newDir) != 0) throw 8;
767 // Success. Test for remove old dir containter
768 char* tempOldTitleDir = new char[k+1];
769 memcpy(tempOldTitleDir, t, k);
770 tempOldTitleDir[k] = '\0';
771 log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(tempOldTitleDir), tempOldTitleDir);
772 rmdir(tempOldTitleDir); // can't do anything about a fail result at this point.
773 delete[] tempOldTitleDir;
775 ::Recordings.Update();
777 js["NewRecordingFileName"] = newDir;
781 js["Result"] = false;
784 log->log("JSONServer", Log::ERR, "Bad parameters");
785 js["Error"] = "Bad request parameters";
789 log->log("JSONServer", Log::ERR, "Could not find recording to move");
790 js["Error"] = "Bad filename";
794 log->log("JSONServer", Log::ERR, "Could not move recording, it is still recording");
795 js["Error"] = "Cannot move recording in progress";
799 log->log("JSONServer", Log::ERR, "Failed to make new dir (1)");
800 js["Error"] = "Failed to create new directory (1)";
804 log->log("JSONServer", Log::ERR, "Something already exists? (1)");
805 js["Error"] = "Something already exists at the new path (1)";
809 log->log("JSONServer", Log::ERR, "Rename failed");
810 js["Error"] = "Move failed";
814 if (fileNameToRename) delete[] fileNameToRename;
815 if (requestedNewName) delete[] requestedNewName;
816 if (dateDirName) delete[] dateDirName;
817 if (titleDirName) delete[] titleDirName;
818 if (folderName) delete[] folderName;
819 if (newContainer) delete[] newContainer;
820 if (newDir) delete[] newDir;
825 bool jsonserver_recresetresume(Json::Value& js, const char* postData)
827 Log* log = Log::getInstance();
828 log->log("JSONServer", Log::DEBUG, "recresetresume");
830 char reqfilename[1000];
831 int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000);
834 log->log("JSONServer", Log::ERR, "Could not decode filename");
835 js["Result"] = false;
836 js["Error"] = "Could not decode filename";
840 log->log("JSONServer", Log::DEBUG, "%s", reqfilename);
842 cRecordings Recordings;
843 Recordings.Load(); // probably have to do this
844 cRecording *recording = Recordings.GetByName(reqfilename);
848 js["Result"] = false;
849 js["Error"] = "Could not find recording to reset resume";
853 log->log("JSONServer", Log::DEBUG, "Reset resume for: %s", recording->Name());
855 cResumeFile ResumeFile(recording->FileName(), recording->IsPesRecording());
856 if (ResumeFile.Read() >= 0)
864 js["Result"] = false;
865 js["Error"] = "Recording has no resume point";
870 bool jsonserver_channellist(Json::Value& js)
872 Log* log = Log::getInstance();
873 log->log("JSONServer", Log::DEBUG, "channellist");
875 Json::Value jschannels(Json::arrayValue);
878 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
880 if (!channel->GroupSep())
882 // log->log("JSONServer", Log::DEBUG, "name: '%s'", channel->Name());
884 // if (channel->Vpid()) type = 1;
885 // else if (channel->Apid(0)) type = 2;
888 Json::Value oneChannel;
889 oneChannel["ID"] = (const char *)channel->GetChannelID().ToString();
890 oneChannel["Number"] = channel->Number();
891 // oneChannel["Type"] = type;
892 oneChannel["Name"] = channel->Name();
893 //#if VDRVERSNUM < 10703
894 // oneChannel["VType"] = 2;
896 // oneChannel["VType"] = channel->Vtype();
898 jschannels.append(oneChannel);
901 js["Channels"] = jschannels;
906 bool jsonserver_channelschedule(Json::Value& js, const char* postData)
908 Log* log = Log::getInstance();
909 log->log("JSONServer", Log::DEBUG, "channelschedule '%s'", postData);
911 char sChannelNumber[15]; int mgv1 = mg_get_var(postData, strlen(postData), "channelnumber", sChannelNumber, 15);
912 char sStartTime[15]; int mgv2 = mg_get_var(postData, strlen(postData), "starttime", sStartTime, 15);
913 char sDuration[15]; int mgv3 = mg_get_var(postData, strlen(postData), "duration", sDuration, 15);
915 if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) )
917 log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i", mgv1, mgv2, mgv3);
918 js["Result"] = false;
919 js["Error"] = "Bad request parameters";
923 int channelNumber = atoi(sChannelNumber);
924 int startTime = atoi(sStartTime);
925 int duration = atoi(sDuration);
927 cChannel* channel = NULL;
928 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
930 if (channel->GroupSep()) continue;
931 if (channel->Number() == channelNumber) break;
936 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
937 js["Result"] = false;
938 js["Error"] = "Could not find channel";
942 cSchedulesLock MutexLock;
943 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
946 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
947 js["Result"] = false;
948 js["Error"] = "Internal schedules error (1)";
951 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
954 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
955 js["Result"] = false;
956 js["Error"] = "Internal schedules error (2)";
960 Json::Value jsevents(Json::arrayValue);
962 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
965 if ((event->StartTime() + event->Duration()) < time(NULL)) continue;
967 if ((event->StartTime() + event->Duration()) <= startTime) continue;
969 if (event->StartTime() >= (startTime + duration)) continue;
971 Json::Value oneEvent;
972 oneEvent["ID"] = event->EventID();
973 oneEvent["Time"] = (Json::UInt)event->StartTime();
974 oneEvent["Duration"] = event->Duration();
975 oneEvent["Title"] = event->Title() ? event->Title() : "";
976 oneEvent["ShortText"] = event->ShortText() ? event->ShortText() : "";
977 oneEvent["HasTimer"] = event->HasTimer();
978 //oneEvent["Description"] = event->Description() ? event->Description() : "";
979 jsevents.append(oneEvent);
983 js["Events"] = jsevents;
987 bool jsonserver_getscheduleevent(Json::Value& js, const char* postData)
989 Log* log = Log::getInstance();
990 log->log("JSONServer", Log::DEBUG, "getscheduleevent '%s'", postData);
992 char sChannelNumber[15]; int mgv1 = mg_get_var(postData, strlen(postData), "channelnumber", sChannelNumber, 15);
993 char sEventID[15]; int mgv2 = mg_get_var(postData, strlen(postData), "eventid", sEventID, 15);
995 if ( (mgv1 == -1) || (mgv2 == -1) )
997 log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2);
998 js["Result"] = false;
999 js["Error"] = "Bad request parameters";
1003 int channelNumber = atoi(sChannelNumber);
1004 int eventID = atoi(sEventID);
1006 const cEvent* event = jsonserver_getEvent(js, channelNumber, eventID, 0);
1009 js["Result"] = false;
1013 Json::Value oneEvent;
1014 oneEvent["ID"] = event->EventID();
1015 oneEvent["Time"] = (Json::UInt)event->StartTime();
1016 oneEvent["Duration"] = event->Duration();
1017 oneEvent["Title"] = event->Title() ? event->Title() : "";
1018 oneEvent["ShortText"] = event->ShortText() ? event->ShortText() : "";
1019 oneEvent["Description"] = event->Description() ? event->Description() : "";
1020 oneEvent["HasTimer"] = event->HasTimer();
1021 oneEvent["RunningStatus"] = event->RunningStatus();
1023 js["Result"] = true;
1024 js["Event"] = oneEvent;
1028 const cEvent* jsonserver_getEvent(Json::Value& js, int channelNumber, int eventID, int aroundTime)
1030 Log* log = Log::getInstance();
1032 cChannel* channel = NULL;
1033 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
1035 if (channel->GroupSep()) continue;
1036 if (channel->Number() == channelNumber) break;
1041 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
1042 js["Error"] = "Could not find channel";
1046 cSchedulesLock MutexLock;
1047 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
1050 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
1051 js["Error"] = "Internal schedules error (1)";
1055 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
1058 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
1059 js["Error"] = "Internal schedules error (2)";
1063 const cEvent* event = NULL;
1066 event = Schedule->GetEvent(eventID);
1070 event = Schedule->GetEventAround(aroundTime);
1075 log->log("JSONServer", Log::ERR, "Could not find requested event: %i", eventID);
1076 js["Error"] = "Internal schedules error (3)";
1083 bool jsonserver_timerlist(Json::Value& js)
1085 Log* log = Log::getInstance();
1086 log->log("JSONServer", Log::DEBUG, "timerlist");
1088 Json::Value jstimers(Json::arrayValue);
1091 int numTimers = Timers.Count();
1093 for (int i = 0; i < numTimers; i++)
1095 timer = Timers.Get(i);
1096 Json::Value oneTimer;
1097 oneTimer["Active"] = timer->HasFlags(tfActive);
1098 oneTimer["Recording"] = timer->Recording();
1099 oneTimer["Pending"] = timer->Pending();
1100 oneTimer["Priority"] = timer->Priority();
1101 oneTimer["Lifetime"] = timer->Lifetime();
1102 oneTimer["ChannelNumber"] = timer->Channel()->Number();
1103 oneTimer["ChannelID"] = (const char *)timer->Channel()->GetChannelID().ToString();
1104 oneTimer["StartTime"] = (int)timer->StartTime();
1105 oneTimer["StopTime"] = (int)timer->StopTime();
1106 oneTimer["Day"] = (int)timer->Day();
1107 oneTimer["WeekDays"] = timer->WeekDays();
1108 oneTimer["Name"] = timer->File();
1110 const cEvent* event = timer->Event();
1113 oneTimer["EventID"] = event->EventID();
1117 int channelNumber = timer->Channel()->Number();
1118 int aroundTime = timer->StartTime() + 1;
1119 log->log("JSONServer", Log::DEBUG, "%i", aroundTime);
1121 const cEvent* eventAround = jsonserver_getEvent(js, channelNumber, 0, aroundTime);
1124 oneTimer["EventID"] = eventAround->EventID();
1128 oneTimer["EventID"] = 0;
1132 jstimers.append(oneTimer);
1135 js["Timers"] = jstimers;
1136 js["Result"] = true;
1140 bool jsonserver_timerset(Json::Value& js, const char* postData)
1142 Log* log = Log::getInstance();
1143 log->log("JSONServer", Log::DEBUG, "timerset");
1145 char sTimerString[1024]; int mgv1 = mg_get_var(postData, strlen(postData), "timerstring", sTimerString, 1024);
1149 log->log("JSONServer", Log::ERR, "Could not get timerstring");
1150 js["Result"] = false;
1151 js["Error"] = "Bad request parameters";
1155 log->log("JSONServer", Log::DEBUG, "'%s'", sTimerString);
1156 cTimer *timer = new cTimer;
1157 if (!timer->Parse(sTimerString))
1160 js["Result"] = false;
1161 js["Error"] = "Failed to parse timer request details";
1165 cTimer *t = Timers.GetTimer(timer);
1169 js["Result"] = false;
1170 js["Error"] = "Timer already exists";
1175 Timers.SetModified();
1176 js["Result"] = true;
1180 bool jsonserver_timersetactive(Json::Value& js, const char* postData)
1182 Log* log = Log::getInstance();
1183 log->log("JSONServer", Log::DEBUG, "timersetactive");
1185 char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50);
1186 char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024);
1187 char rStartTime[20]; int mgv3 = mg_get_var(postData, strlen(postData), "StartTime", rStartTime, 20);
1188 char rStopTime[20]; int mgv4 = mg_get_var(postData, strlen(postData), "StopTime", rStopTime, 20);
1189 char rWeekDays[50]; int mgv5 = mg_get_var(postData, strlen(postData), "WeekDays", rWeekDays, 20);
1190 char tNewActive[20]; int mgv6 = mg_get_var(postData, strlen(postData), "SetActive", tNewActive, 20);
1192 if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1) || (mgv6 == -1))
1194 log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5, mgv6);
1195 js["Result"] = false;
1196 js["Error"] = "Bad request parameters";
1200 log->log("JSONServer", Log::DEBUG, "timersetactive: %s %s:%s:%s:%s:%s", tNewActive, rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1202 cTimer* timer = jsonserver_findTimer(rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1205 if (strcmp(tNewActive, "true") == 0)
1207 timer->SetFlags(tfActive);
1209 else if (strcmp(tNewActive, "false") == 0)
1211 timer->ClrFlags(tfActive);
1215 js["Result"] = false;
1216 js["Error"] = "Bad request parameters";
1220 js["Result"] = true;
1222 Timers.SetModified();
1227 js["Result"] = false;
1228 js["Error"] = "Timer not found";
1232 bool jsonserver_timeredit(Json::Value& js, const char* postData)
1234 Log* log = Log::getInstance();
1235 log->log("JSONServer", Log::DEBUG, "timeredit");
1238 char oldName[1024]; int mgv1 = mg_get_var(postData, strlen(postData), "OldName", oldName, 1024);
1239 char oldActive[20]; int mgv2 = mg_get_var(postData, strlen(postData), "OldActive", oldActive, 20);
1240 char oldChannelID[50]; int mgv3 = mg_get_var(postData, strlen(postData), "OldChannelID", oldChannelID, 50);
1241 char oldDay[20]; int mgv4 = mg_get_var(postData, strlen(postData), "OldDay", oldDay, 20);
1242 char oldWeekDays[50]; int mgv5 = mg_get_var(postData, strlen(postData), "OldWeekDays", oldWeekDays, 20);
1243 char oldStartTime[20]; int mgv6 = mg_get_var(postData, strlen(postData), "OldStartTime", oldStartTime, 20);
1244 char oldStopTime[20]; int mgv7 = mg_get_var(postData, strlen(postData), "OldStopTime", oldStopTime, 20);
1245 char oldPriority[20]; int mgv8 = mg_get_var(postData, strlen(postData), "OldPriority", oldPriority, 20);
1246 char oldLifetime[20]; int mgv9 = mg_get_var(postData, strlen(postData), "OldLifetime", oldLifetime, 20);
1248 if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1) || (mgv6 == -1) || (mgv7 == -1) || (mgv8 == -1) || (mgv9 == -1))
1250 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);
1251 js["Result"] = false;
1252 js["Error"] = "Bad request parameters";
1256 log->log("JSONServer", Log::DEBUG, "timeredit: %s %s %s %s %s %s %s %s %s", oldName, oldActive, oldChannelID, oldDay, oldWeekDays, oldStartTime, oldStopTime, oldPriority, oldLifetime);
1260 char newName[1024]; int mgv11 = mg_get_var(postData, strlen(postData), "NewName", newName, 1024);
1261 char newActive[20]; int mgv12 = mg_get_var(postData, strlen(postData), "NewActive", newActive, 20);
1262 char newChannelID[50]; int mgv13 = mg_get_var(postData, strlen(postData), "NewChannelID", newChannelID, 50);
1263 char newDay[20]; int mgv14 = mg_get_var(postData, strlen(postData), "NewDay", newDay, 20);
1264 char newWeekDays[50]; int mgv15 = mg_get_var(postData, strlen(postData), "NewWeekDays", newWeekDays, 20);
1265 char newStartTime[20]; int mgv16 = mg_get_var(postData, strlen(postData), "NewStartTime", newStartTime, 20);
1266 char newStopTime[20]; int mgv17 = mg_get_var(postData, strlen(postData), "NewStopTime", newStopTime, 20);
1267 char newPriority[20]; int mgv18 = mg_get_var(postData, strlen(postData), "NewPriority", newPriority, 20);
1268 char newLifetime[20]; int mgv19 = mg_get_var(postData, strlen(postData), "NewLifetime", newLifetime, 20);
1270 if ( (mgv11 == -1) || (mgv12 == -1) || (mgv13 == -1) || (mgv14 == -1) || (mgv15 == -1) || (mgv16 == -1) || (mgv17 == -1) || (mgv18 == -1) || (mgv19 == -1))
1272 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);
1273 js["Result"] = false;
1274 js["Error"] = "Bad request parameters";
1278 log->log("JSONServer", Log::DEBUG, "timeredit: %s %s %s %s %s %s %s %s %s", newName, newActive, newChannelID, newDay, newWeekDays, newStartTime, newStopTime, newPriority, newLifetime);
1281 cTimer* timer = jsonserver_findTimer2(oldName, oldActive, oldChannelID, oldDay, oldWeekDays, oldStartTime, oldStopTime, oldPriority, oldLifetime);
1284 js["Result"] = false;
1285 js["Error"] = "Timer not found";
1289 // Old version commented below used to set each thing individually based on whether it had changed. However, since
1290 // the only way to change the timer channel appears to be with the cTimer::Parse function, might as well use that
1291 // for everything it supports
1292 // Except flags. Get current flags, set using Parse, then add/remove active as needed the other way.
1294 time_t nstt = atoi(newStartTime);
1296 localtime_r(&nstt, &nstm);
1297 int nssf = (nstm.tm_hour * 100) + nstm.tm_min;
1299 time_t nztt = atoi(newStopTime);
1301 localtime_r(&nztt, &nztm);
1302 int nzsf = (nztm.tm_hour * 100) + nztm.tm_min;
1304 strreplace(newName, ':', '|');
1306 cString parseBuffer = cString::sprintf("%u:%s:%s:%04d:%04d:%d:%d:%s:%s",
1307 timer->Flags(), newChannelID, *(cTimer::PrintDay(atoi(newDay), atoi(newWeekDays), true)),
1308 nssf, nzsf, atoi(newPriority), atoi(newLifetime), newName, timer->Aux() ? timer->Aux() : "");
1310 log->log("JSONServer", Log::DEBUG, "timeredit new parse: %s", *parseBuffer);
1312 bool parseResult = timer->Parse(*parseBuffer);
1315 js["Result"] = false;
1316 js["Error"] = "Timer parsing failed";
1320 if (timer->HasFlags(tfActive) != !(strcasecmp(newActive, "true")))
1322 log->log("JSONServer", Log::DEBUG, "%i %i timeredit set new active: %s", timer->HasFlags(tfActive), !(strcasecmp(newActive, "true")), newActive);
1324 if (strcasecmp(newActive, "true") == 0)
1326 timer->SetFlags(tfActive);
1328 else if (strcasecmp(newActive, "false") == 0)
1330 timer->ClrFlags(tfActive);
1334 js["Result"] = false;
1335 js["Error"] = "Bad request parameters";
1340 js["Result"] = true;
1341 Timers.SetModified();
1346 if (strcmp(timer->File(), newName))
1348 log->log("JSONServer", Log::DEBUG, "timeredit set new name: %s", newName);
1349 timer->SetFile(newName);
1352 if (timer->Day() != atoi(newDay))
1354 log->log("JSONServer", Log::DEBUG, "timeredit set new day: %s", newDay);
1355 timer->SetDay(atoi(newDay));
1358 if (timer->WeekDays() != atoi(newWeekDays))
1360 log->log("JSONServer", Log::DEBUG, "timeredit set new week days: %s", newWeekDays);
1361 timer->SetWeekDays(atoi(newWeekDays));
1364 if (timer->StartTime() != atoi(newStartTime))
1366 log->log("JSONServer", Log::DEBUG, "timeredit set new start time: %s", newStartTime);
1367 time_t nstt = atoi(newStartTime);
1369 localtime_r(&nstt, &nstm);
1370 int nssf = (nstm.tm_hour * 100) + nstm.tm_min;
1371 timer->SetStart(nssf);
1374 if (timer->StopTime() != atoi(newStopTime))
1376 log->log("JSONServer", Log::DEBUG, "timeredit set new stop time: %s", newStopTime);
1377 time_t nztt = atoi(newStopTime);
1379 localtime_r(&nztt, &nztm);
1380 int nzsf = (nztm.tm_hour * 100) + nztm.tm_min;
1381 timer->SetStop(nzsf);
1384 if (timer->Priority() != atoi(newPriority))
1386 log->log("JSONServer", Log::DEBUG, "timeredit set new priority: %s", newPriority);
1387 timer->SetPriority(atoi(newPriority));
1390 if (timer->Lifetime() != atoi(newLifetime))
1392 log->log("JSONServer", Log::DEBUG, "timeredit set new lifetime: %s", newLifetime);
1393 timer->SetLifetime(atoi(newLifetime));
1398 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)
1400 Log* log = Log::getInstance();
1402 int numTimers = Timers.Count();
1404 log->log("JSONServer", Log::DEBUG, "findtimer2: %s %s %s %s %s %s %s %s %s", rName, rActive, rChannelID, rDay, rWeekDays, rStartTime, rStopTime, rPriority, rLifetime);
1405 for (int i = 0; i < numTimers; i++)
1407 timer = Timers.Get(i);
1409 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(),
1410 (int)timer->Day(), timer->WeekDays(), (int)timer->StartTime(), (int)timer->StopTime(),
1411 timer->Priority(), timer->Lifetime());
1413 if ( (strcmp(timer->File(), rName) == 0)
1414 && (timer->HasFlags(tfActive) == !(strcasecmp(rActive, "true")))
1415 && (strcmp(timer->Channel()->GetChannelID().ToString(), rChannelID) == 0)
1416 && (timer->Day() == atoi(rDay))
1417 && (timer->WeekDays() == atoi(rWeekDays))
1418 && (timer->StartTime() == atoi(rStartTime))
1419 && (timer->StopTime() == atoi(rStopTime))
1420 && (timer->Priority() == atoi(rPriority))
1421 && (timer->Lifetime() == atoi(rLifetime))
1424 log->log("JSONServer", Log::DEBUG, "found");
1428 log->log("JSONServer", Log::DEBUG, "no timer found");
1432 cTimer* jsonserver_findTimer(const char* rChannelID, const char* rName, const char* rStartTime, const char* rStopTime, const char* rWeekDays)
1434 Log* log = Log::getInstance();
1436 int numTimers = Timers.Count();
1438 for (int i = 0; i < numTimers; i++)
1440 timer = Timers.Get(i);
1442 log->log("JSONServer", Log::DEBUG, "findtimer current: %s", (const char*)timer->ToText(true));
1443 log->log("JSONServer", Log::DEBUG, "findtimer: %s", (const char*)timer->Channel()->GetChannelID().ToString());
1444 log->log("JSONServer", Log::DEBUG, "findtimer: %s", rChannelID);
1445 log->log("JSONServer", Log::DEBUG, "findtimer: %s", timer->File());
1446 log->log("JSONServer", Log::DEBUG, "findtimer: %s", rName);
1447 log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->StartTime());
1448 log->log("JSONServer", Log::DEBUG, "findtimer: %s", rStartTime);
1449 log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->StopTime());
1450 log->log("JSONServer", Log::DEBUG, "findtimer: %s", rStopTime);
1451 log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->WeekDays());
1452 log->log("JSONServer", Log::DEBUG, "findtimer: %s", rWeekDays);
1455 (strcmp(timer->Channel()->GetChannelID().ToString(), rChannelID) == 0)
1456 && (strcmp(timer->File(), rName) == 0)
1457 && (timer->StartTime() == atoi(rStartTime))
1458 && (timer->StopTime() == atoi(rStopTime))
1459 && (timer->WeekDays() == atoi(rWeekDays))
1462 log->log("JSONServer", Log::DEBUG, "found");
1466 log->log("JSONServer", Log::DEBUG, "no timer found");
1470 bool jsonserver_timerdel(Json::Value& js, const char* postData)
1472 Log* log = Log::getInstance();
1473 log->log("JSONServer", Log::DEBUG, "timerdel");
1475 char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50);
1476 char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024);
1477 char rStartTime[20]; int mgv3 = mg_get_var(postData, strlen(postData), "StartTime", rStartTime, 20);
1478 char rStopTime[20]; int mgv4 = mg_get_var(postData, strlen(postData), "StopTime", rStopTime, 20);
1479 char rWeekDays[50]; int mgv5 = mg_get_var(postData, strlen(postData), "WeekDays", rWeekDays, 20);
1481 if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1))
1483 log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5);
1484 js["Result"] = false;
1485 js["Error"] = "Bad request parameters";
1489 log->log("JSONServer", Log::DEBUG, "timerdel: %s:%s:%s:%s:%s", rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1491 if (Timers.BeingEdited())
1493 log->log("JSONServer", Log::ERR, "Unable to delete timer - timers being edited at VDR");
1494 js["Result"] = false;
1495 js["Error"] = "Timers being edited at VDR";
1499 cTimer* timer = jsonserver_findTimer(rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1502 if (timer->Recording())
1504 log->log("JSONServer", Log::ERR, "Unable to delete timer - timer is running");
1505 js["Result"] = false;
1506 js["Error"] = "Timer is running";
1513 Timers.SetModified();
1514 js["Result"] = true;
1518 js["Result"] = false;
1519 js["Error"] = "Timer not found";
1523 bool jsonserver_timerisrecording(Json::Value& js, const char* postData)
1525 Log* log = Log::getInstance();
1526 log->log("JSONServer", Log::DEBUG, "timerisrecording");
1528 char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50);
1529 char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024);
1530 char rStartTime[20]; int mgv3 = mg_get_var(postData, strlen(postData), "StartTime", rStartTime, 20);
1531 char rStopTime[20]; int mgv4 = mg_get_var(postData, strlen(postData), "StopTime", rStopTime, 20);
1532 char rWeekDays[50]; int mgv5 = mg_get_var(postData, strlen(postData), "WeekDays", rWeekDays, 20);
1534 if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1))
1536 log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5);
1537 js["Result"] = false;
1538 js["Error"] = "Bad request parameters";
1542 log->log("JSONServer", Log::DEBUG, "timerisrecording: %s:%s:%s:%s:%s", rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1544 cTimer* timer = jsonserver_findTimer(rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1547 js["Recording"] = timer->Recording();
1548 js["Pending"] = timer->Pending();
1549 js["Result"] = true;
1553 js["Result"] = false;
1554 js["Error"] = "Timer not found";
1558 bool jsonserver_tunersstatus(Json::Value& js, const char* postData)
1560 Log* log = Log::getInstance();
1561 log->log("JSONServer", Log::DEBUG, "tunerstatus");
1563 js["NumDevices"] = cDevice::NumDevices();
1565 Json::Value jsdevices(Json::arrayValue);
1567 for (int i = 0; i < cDevice::NumDevices(); i++)
1569 Json::Value oneDevice;
1570 cDevice *d = cDevice::GetDevice(i);
1571 oneDevice["Number"] = d->DeviceNumber();
1572 oneDevice["Type"] = (const char*)d->DeviceType();
1573 oneDevice["Name"] = (const char*)d->DeviceName();
1574 oneDevice["IsPrimary"] = d->IsPrimaryDevice();
1576 const cChannel* cchannel = d->GetCurrentlyTunedTransponder();
1579 oneDevice["Frequency"] = cchannel->Frequency();
1580 oneDevice["SignalStrength"] = d->SignalStrength();
1581 oneDevice["SignalQuality"] = d->SignalQuality();
1586 oneDevice["Frequency"] = 0;
1587 oneDevice["SignalStrength"] = 0;
1588 oneDevice["SignalQuality"] = 0;
1591 jsdevices.append(oneDevice);
1594 js["Devices"] = jsdevices;
1597 Json::Value jstimers(Json::arrayValue);
1598 int numTimers = Timers.Count();
1600 for (int i = 0; i < numTimers; i++)
1602 timer = Timers.Get(i);
1604 if (timer->Recording())
1606 Json::Value oneTimer;
1607 oneTimer["Recording"] = timer->Recording();
1608 oneTimer["StartTime"] = (int)timer->StartTime();
1609 oneTimer["StopTime"] = (int)timer->StopTime();
1610 oneTimer["File"] = timer->File();
1612 cRecordControl* crc = cRecordControls::GetRecordControl(timer);
1615 cDevice* crcd = crc->Device();
1616 oneTimer["DeviceNumber"] = crcd->DeviceNumber();
1620 oneTimer["DeviceNumber"] = Json::Value::null;
1623 const cChannel* channel = timer->Channel();
1626 oneTimer["ChannelName"] = channel->Name();
1630 oneTimer["ChannelName"] = Json::Value::null;
1633 jstimers.append(oneTimer);
1637 js["CurrentRecordings"] = jstimers;
1640 js["Result"] = true;