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, "tunersstatus")) success = jsonserver_tunersstatus(js, postData);
90 if (!success) return 0; // the specific handler failed badly
92 // Now js will be filled
94 Json::StyledWriter sw;
95 std::string jsonout = sw.write(js);
96 mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n");
97 mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n");
98 mg_write(conn, jsonout.c_str(), jsonout.length());
103 else if (event == MG_EVENT_LOG)
105 if (request_info->status_code == 400) // bad request
107 log->log("Mongoose", Log::DEBUG, "400 BAD REQUEST:");
108 log->log("Mongoose", Log::DEBUG, request_info->request_method);
109 log->log("Mongoose", Log::DEBUG, request_info->uri);
110 log->log("Mongoose", Log::DEBUG, request_info->http_version);
111 log->log("Mongoose", Log::DEBUG, request_info->query_string);
112 log->log("Mongoose", Log::DEBUG, request_info->log_message);
113 for (int i = 0; i < request_info->num_headers; i++)
115 log->log("Mongoose", Log::DEBUG, "%s: %s", request_info->http_headers[i].name, request_info->http_headers[i].value);
120 log->log("Mongoose", Log::DEBUG, request_info->log_message);
121 log->log("Mongoose", Log::DEBUG, request_info->uri);
126 // other events not handled:
127 // MG_HTTP_ERROR, MG_INIT_SSL
128 // Let mongoose do something with those
136 bool jsonserver_gettime(Json::Value& js)
138 Log* log = Log::getInstance();
139 log->log("JSONServer", Log::DEBUG, "gettime");
142 gettimeofday(&tv, NULL);
144 js["Time"] = (Json::UInt64)tv.tv_sec;
145 js["MTime"] = (Json::UInt)(tv.tv_usec/1000);
150 bool jsonserver_diskstats(Json::Value& js)
152 Log* log = Log::getInstance();
153 log->log("JSONServer", Log::DEBUG, "diskstats");
157 int Percent = VideoDiskSpace(&FreeMB, &UsedMB);
159 js["FreeMiB"] = FreeMB;
160 js["UsedMiB"] = UsedMB;
161 js["Percent"] = Percent;
167 bool jsonserver_reclist(Json::Value& js)
169 Log* log = Log::getInstance();
170 log->log("JSONServer", Log::DEBUG, "reclist");
172 Json::Value jsrecordings(Json::arrayValue);
173 cRecordings Recordings;
175 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
178 oneRec["StartTime"] = (Json::UInt)recording->Start();
179 oneRec["Length"] = (Json::UInt)recording->LengthInSeconds();
180 oneRec["IsNew"] = recording->IsNew();
181 oneRec["Name"] = recording->Name();
182 oneRec["Filename"] = recording->FileName();
183 oneRec["FileSizeMB"] = recording->FileSizeMB();
185 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
186 if (rc) oneRec["CurrentlyRecording"] = true;
187 else oneRec["CurrentlyRecording"] = false;
189 jsrecordings.append(oneRec);
191 js["Recordings"] = jsrecordings;
196 bool jsonserver_recinfo(Json::Value& js, const char* postData)
198 Log* log = Log::getInstance();
199 log->log("JSONServer", Log::DEBUG, "recinfo");
201 char reqfilename[1000];
202 int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000);
205 log->log("JSONServer", Log::ERR, "Could not decode filename");
206 js["Result"] = false;
207 js["Error"] = "Could not decode filename";
211 log->log("JSONServer", Log::DEBUG, "%s", reqfilename);
213 cRecordings Recordings;
214 Recordings.Load(); // probably have to do this
215 cRecording *recording = Recordings.GetByName(reqfilename);
219 log->log("JSONServer", Log::ERR, "recinfo found no recording");
220 js["Result"] = false;
224 js["IsNew"] = recording->IsNew();
225 js["LengthInSeconds"] = recording->LengthInSeconds();
226 js["FileSizeMB"] = recording->FileSizeMB();
227 js["Name"] = recording->Name() ? recording->Name() : Json::Value::null;
228 js["Priority"] = recording->Priority();
229 js["LifeTime"] = recording->Lifetime();
230 js["Start"] = (Json::UInt)recording->Start();
232 js["ResumePoint"] = 0;
233 cResumeFile ResumeFile(recording->FileName(), recording->IsPesRecording());
234 if (ResumeFile.Read() >= 0) js["ResumePoint"] = ResumeFile.Read();
236 js["CurrentlyRecordingStart"] = 0;
237 js["CurrentlyRecordingStop"] = 0;
238 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
241 js["CurrentlyRecordingStart"] = (Json::UInt)rc->Timer()->StartTime();
242 js["CurrentlyRecordingStop"] = (Json::UInt)rc->Timer()->StopTime();
245 const cRecordingInfo *info = recording->Info();
248 js["ChannelName"] = info->ChannelName() ? info->ChannelName() : Json::Value::null;
249 js["Title"] = info->Title() ? info->Title() : Json::Value::null;
250 js["ShortText"] = info->ShortText() ? info->ShortText() : Json::Value::null;
251 js["Description"] = info->Description() ? info->Description() : Json::Value::null;
253 const cComponents* components = info->Components();
256 js["Components"] = Json::Value::null;
260 Json::Value jscomponents;
262 tComponent* component;
263 for (int i = 0; i < components->NumComponents(); i++)
265 component = components->Component(i);
267 Json::Value oneComponent;
268 oneComponent["Stream"] = component->stream;
269 oneComponent["Type"] = component->type;
270 oneComponent["Language"] = component->language ? component->language : Json::Value::null;
271 oneComponent["Description"] = component->description ? component->description : Json::Value::null;
272 jscomponents.append(oneComponent);
275 js["Components"] = jscomponents;
283 bool jsonserver_recstop(Json::Value& js, const char* postData)
285 Log* log = Log::getInstance();
286 log->log("JSONServer", Log::DEBUG, "recstop");
288 char reqfilename[1000];
289 int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000);
292 log->log("JSONServer", Log::ERR, "Could not decode filename");
293 js["Result"] = false;
294 js["Error"] = "Could not decode filename";
298 log->log("JSONServer", Log::DEBUG, "%s", reqfilename);
300 cRecordings Recordings;
301 Recordings.Load(); // probably have to do this
302 cRecording *recording = Recordings.GetByName(reqfilename);
306 log->log("JSONServer", Log::ERR, "recstop found no recording");
307 js["Result"] = false;
311 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
314 log->log("JSONServer", Log::ERR, "recstop - not currently recording");
315 js["Result"] = false;
319 if (Timers.BeingEdited())
321 log->log("JSONServer", Log::ERR, "recstop - timers being edited elsewhere");
322 js["Result"] = false;
326 cTimer* timer = rc->Timer();
329 log->log("JSONServer", Log::ERR, "recstop - timer not found");
330 js["Result"] = false;
334 timer->ClrFlags(tfActive);
335 Timers.SetModified();
341 bool jsonserver_recdel(Json::Value& js, const char* postData)
343 Log* log = Log::getInstance();
344 log->log("JSONServer", Log::DEBUG, "recdel");
346 char reqfilename[1000];
347 int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000);
350 log->log("JSONServer", Log::ERR, "Could not decode filename");
351 js["Result"] = false;
352 js["Error"] = "Could not decode filename";
356 log->log("JSONServer", Log::DEBUG, "%s", reqfilename);
358 cRecordings Recordings;
359 Recordings.Load(); // probably have to do this
360 cRecording *recording = Recordings.GetByName(reqfilename);
364 js["Result"] = false;
365 js["Error"] = "Could not find recording to delete";
369 log->log("JSONServer", Log::DEBUG, "Deleting recording: %s", recording->Name());
370 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
373 js["Result"] = false;
374 js["Error"] = "This recording is still recording.. ho ho";
378 if (recording->Delete())
380 ::Recordings.DelByName(recording->FileName());
385 js["Result"] = false;
386 js["Error"] = "Failed to delete recording";
392 bool jsonserver_recmove(Json::Value& js, const char* postData)
394 Log* log = Log::getInstance();
395 log->log("JSONServer", Log::DEBUG, "recmove");
397 char* fileNameToMove = NULL;
398 char* requestedNewPath = NULL;
399 char* dateDirName = NULL;
400 char* titleDirName = NULL;
401 char* folderName = NULL;
402 char* newContainer = NULL;
407 int postDataLen = strlen(postData)+1;
408 fileNameToMove = new char[postDataLen];
409 int mgv1 = mg_get_var(postData, postDataLen-1, "filename", fileNameToMove, postDataLen);
410 requestedNewPath = new char[postDataLen];
411 int mgv2 = mg_get_var(postData, postDataLen-1, "newpath", requestedNewPath, postDataLen);
413 if ((mgv1 == -1) || (mgv2 == -1) || !strlen(fileNameToMove) || !strlen(requestedNewPath))
415 log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2);
419 cRecordings Recordings;
420 Recordings.Load(); // probably have to do this
421 cRecording* recordingObj = Recordings.GetByName(fileNameToMove);
422 if (!recordingObj) throw 2;
424 cRecordControl *rc = cRecordControls::GetRecordControl(recordingObj->FileName());
427 log->log("JSONServer", Log::DEBUG, "moving recording: %s", recordingObj->Name());
428 log->log("JSONServer", Log::DEBUG, "moving recording: %s", recordingObj->FileName());
429 log->log("JSONServer", Log::DEBUG, "to: %s", requestedNewPath);
431 const char* t = recordingObj->FileName();
435 // Find the datedirname
436 for(k = strlen(t) - 1; k >= 0; k--)
440 log->log("JSONServer", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
441 dateDirName = new char[strlen(&t[k+1]) + 1];
442 strcpy(dateDirName, &t[k+1]);
447 // Find the titledirname
449 for(j = k-1; j >= 0; j--)
453 log->log("JSONServer", Log::DEBUG, "l2: %i", k - j);
454 titleDirName = new char[k - j];
455 memcpy(titleDirName, &t[j+1], k - j - 1);
456 titleDirName[k - j - 1] = '\0';
461 // Find the foldername
463 log->log("JSONServer", Log::DEBUG, "j = %u, strlenvd = %u", j, strlen(VideoDirectory));
464 if (j > (int)strlen(VideoDirectory)) // Rec is in a subfolder now
466 for(m = j-1; m >= 0; m--)
470 log->log("JSONServer", Log::DEBUG, "l3: %i", j - m);
471 folderName = new char[j - m];
472 memcpy(folderName, &t[m+1], j - m - 1);
473 folderName[j - m - 1] = '\0';
479 ExchangeChars(requestedNewPath, true);
481 log->log("JSONServer", Log::DEBUG, "datedirname: %s", dateDirName);
482 log->log("JSONServer", Log::DEBUG, "titledirname: %s", titleDirName);
483 log->log("JSONServer", Log::DEBUG, "viddir: %s", VideoDirectory);
484 if (folderName) log->log("JSONServer", Log::DEBUG, "folderName: %s", folderName);
485 log->log("JSONServer", Log::DEBUG, "EC: %s", requestedNewPath);
487 // Could be a new path - construct that first and test
488 newContainer = new char[strlen(VideoDirectory) + strlen(requestedNewPath) + strlen(titleDirName) + 1];
489 sprintf(newContainer, "%s%s", VideoDirectory, requestedNewPath);
490 log->log("JSONServer", Log::DEBUG, "NPT: %s", newContainer);
492 int statret = stat(newContainer, &dstat);
493 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
495 log->log("JSONServer", Log::DEBUG, "new path does not exist (1)");
496 int mkdirret = mkdir(newContainer, 0755);
497 if (mkdirret != 0) throw 4;
499 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR)))
501 // Something exists but it's not a dir
505 // New path now created or was there already
507 sprintf(newContainer, "%s%s%s", VideoDirectory, requestedNewPath, titleDirName);
508 log->log("JSONServer", Log::DEBUG, "%s", newContainer);
510 statret = stat(newContainer, &dstat);
511 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
513 log->log("JSONServer", Log::DEBUG, "new dir does not exist (2)");
514 int mkdirret = mkdir(newContainer, 0755);
515 if (mkdirret != 0) throw 6;
517 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR)))
519 // Something exists but it's not a dir
523 // Ok, the directory container has been made, or it pre-existed.
525 newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
526 sprintf(newDir, "%s/%s", newContainer, dateDirName);
528 log->log("JSONServer", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
529 if (rename(t, newDir) != 0) throw 8;
531 // Success. Test for remove old dir containter
532 char* tempOldTitleDir = new char[k+1];
533 memcpy(tempOldTitleDir, t, k);
534 tempOldTitleDir[k] = '\0';
535 log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(tempOldTitleDir), tempOldTitleDir);
536 rmdir(tempOldTitleDir); // can't do anything about a fail result at this point.
537 delete[] tempOldTitleDir;
539 // Test for remove old foldername
542 char* tempOldFolderName = new char[j+1];
543 memcpy(tempOldFolderName, t, j);
544 tempOldFolderName[j] = '\0';
545 log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldfoldername: %s", j+1, j, strlen(tempOldFolderName), tempOldFolderName);
548 rmdir() deletes a directory, which must be empty.
549 ENOTEMPTY - pathname contains entries other than . and ..
550 So, should be safe to call rmdir on non-empty dir
552 rmdir(tempOldFolderName); // can't do anything about a fail result at this point.
553 delete[] tempOldFolderName;
556 ::Recordings.Update();
558 js["NewRecordingFileName"] = newDir;
562 js["Result"] = false;
565 log->log("JSONServer", Log::ERR, "Bad parameters");
566 js["Error"] = "Bad request parameters";
570 log->log("JSONServer", Log::ERR, "Could not find recording to move");
571 js["Error"] = "Bad filename";
575 log->log("JSONServer", Log::ERR, "Could not move recording, it is still recording");
576 js["Error"] = "Cannot move recording in progress";
580 log->log("JSONServer", Log::ERR, "Failed to make new dir (1)");
581 js["Error"] = "Failed to create new directory (1)";
585 log->log("JSONServer", Log::ERR, "Something already exists? (1)");
586 js["Error"] = "Something already exists at the new path (1)";
590 log->log("JSONServer", Log::ERR, "Failed to make new dir (2)");
591 js["Error"] = "Failed to create new directory (2)";
595 log->log("JSONServer", Log::ERR, "Something already exists?");
596 js["Error"] = "Something already exists at the new path";
600 log->log("JSONServer", Log::ERR, "Rename failed");
601 js["Error"] = "Move failed";
605 if (fileNameToMove) delete[] fileNameToMove;
606 if (requestedNewPath) delete[] requestedNewPath;
607 if (dateDirName) delete[] dateDirName;
608 if (titleDirName) delete[] titleDirName;
609 if (folderName) delete[] folderName;
610 if (newContainer) delete[] newContainer;
611 if (newDir) delete[] newDir;
616 bool jsonserver_recrename(Json::Value& js, const char* postData)
618 Log* log = Log::getInstance();
619 log->log("JSONServer", Log::DEBUG, "recrename");
621 char* fileNameToRename = NULL;
622 char* requestedNewName = NULL;
623 char* dateDirName = NULL;
624 char* titleDirName = NULL;
625 char* folderName = NULL;
626 char* newContainer = NULL;
631 int postDataLen = strlen(postData)+1;
632 fileNameToRename = new char[postDataLen];
633 int mgv1 = mg_get_var(postData, postDataLen-1, "filename", fileNameToRename, postDataLen);
634 requestedNewName = new char[postDataLen];
635 int mgv2 = mg_get_var(postData, postDataLen-1, "newname", requestedNewName, postDataLen);
637 if ((mgv1 == -1) || (mgv2 == -1) || !strlen(fileNameToRename) || !strlen(requestedNewName))
639 log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2);
643 cRecordings Recordings;
644 Recordings.Load(); // probably have to do this
645 cRecording* recordingObj = Recordings.GetByName(fileNameToRename);
646 if (!recordingObj) throw 2;
648 cRecordControl *rc = cRecordControls::GetRecordControl(recordingObj->FileName());
651 log->log("JSONServer", Log::DEBUG, "renaming recording: %s", recordingObj->Name());
652 log->log("JSONServer", Log::DEBUG, "renaming recording: %s", recordingObj->FileName());
653 log->log("JSONServer", Log::DEBUG, "to: %s", requestedNewName);
655 const char* t = recordingObj->FileName();
659 // Find the datedirname
660 for(k = strlen(t) - 1; k >= 0; k--)
664 log->log("JSONServer", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
665 dateDirName = new char[strlen(&t[k+1]) + 1];
666 strcpy(dateDirName, &t[k+1]);
671 // Find the titledirname
673 for(j = k-1; j >= 0; j--)
677 log->log("JSONServer", Log::DEBUG, "l2: %i", k - j);
678 titleDirName = new char[k - j];
679 memcpy(titleDirName, &t[j+1], k - j - 1);
680 titleDirName[k - j - 1] = '\0';
685 // Find the foldername
687 log->log("JSONServer", Log::DEBUG, "j = %u, strlenvd = %u", j, strlen(VideoDirectory));
688 if (j > (int)strlen(VideoDirectory)) // Rec is in a subfolder now
690 for(m = j-1; m >= 0; m--)
694 log->log("JSONServer", Log::DEBUG, "l3: %i", j - m);
695 folderName = new char[j - m];
696 memcpy(folderName, &t[m+1], j - m - 1);
697 folderName[j - m - 1] = '\0';
703 ExchangeChars(requestedNewName, true);
705 log->log("JSONServer", Log::DEBUG, "datedirname: %s", dateDirName);
706 log->log("JSONServer", Log::DEBUG, "titledirname: %s", titleDirName);
707 log->log("JSONServer", Log::DEBUG, "viddir: %s", VideoDirectory);
708 if (folderName) log->log("JSONServer", Log::DEBUG, "folderName: %s", folderName);
709 log->log("JSONServer", Log::DEBUG, "EC: %s", requestedNewName);
711 // Could be a new path - construct that first and test
715 newContainer = new char[strlen(VideoDirectory) + 1 + strlen(folderName) + 1 + strlen(requestedNewName) + 1];
716 sprintf(newContainer, "%s/%s/%s", VideoDirectory, folderName, requestedNewName);
720 newContainer = new char[strlen(VideoDirectory) + 1 + strlen(requestedNewName) + 1];
721 sprintf(newContainer, "%s/%s", VideoDirectory, requestedNewName);
723 log->log("JSONServer", Log::DEBUG, "NPT: %s", newContainer);
725 int statret = stat(newContainer, &dstat);
726 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
728 log->log("JSONServer", Log::DEBUG, "new path does not exist (1)");
729 int mkdirret = mkdir(newContainer, 0755);
730 if (mkdirret != 0) throw 4;
732 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR)))
734 // Something exists but it's not a dir
738 // New path now created or was there already
740 newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
741 sprintf(newDir, "%s/%s", newContainer, dateDirName);
743 log->log("JSONServer", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
744 if (rename(t, newDir) != 0) throw 8;
746 // Success. Test for remove old dir containter
747 char* tempOldTitleDir = new char[k+1];
748 memcpy(tempOldTitleDir, t, k);
749 tempOldTitleDir[k] = '\0';
750 log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(tempOldTitleDir), tempOldTitleDir);
751 rmdir(tempOldTitleDir); // can't do anything about a fail result at this point.
752 delete[] tempOldTitleDir;
754 ::Recordings.Update();
756 js["NewRecordingFileName"] = newDir;
760 js["Result"] = false;
763 log->log("JSONServer", Log::ERR, "Bad parameters");
764 js["Error"] = "Bad request parameters";
768 log->log("JSONServer", Log::ERR, "Could not find recording to move");
769 js["Error"] = "Bad filename";
773 log->log("JSONServer", Log::ERR, "Could not move recording, it is still recording");
774 js["Error"] = "Cannot move recording in progress";
778 log->log("JSONServer", Log::ERR, "Failed to make new dir (1)");
779 js["Error"] = "Failed to create new directory (1)";
783 log->log("JSONServer", Log::ERR, "Something already exists? (1)");
784 js["Error"] = "Something already exists at the new path (1)";
788 log->log("JSONServer", Log::ERR, "Rename failed");
789 js["Error"] = "Move failed";
793 if (fileNameToRename) delete[] fileNameToRename;
794 if (requestedNewName) delete[] requestedNewName;
795 if (dateDirName) delete[] dateDirName;
796 if (titleDirName) delete[] titleDirName;
797 if (folderName) delete[] folderName;
798 if (newContainer) delete[] newContainer;
799 if (newDir) delete[] newDir;
804 bool jsonserver_recresetresume(Json::Value& js, const char* postData)
806 Log* log = Log::getInstance();
807 log->log("JSONServer", Log::DEBUG, "recresetresume");
809 char reqfilename[1000];
810 int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000);
813 log->log("JSONServer", Log::ERR, "Could not decode filename");
814 js["Result"] = false;
815 js["Error"] = "Could not decode filename";
819 log->log("JSONServer", Log::DEBUG, "%s", reqfilename);
821 cRecordings Recordings;
822 Recordings.Load(); // probably have to do this
823 cRecording *recording = Recordings.GetByName(reqfilename);
827 js["Result"] = false;
828 js["Error"] = "Could not find recording to reset resume";
832 log->log("JSONServer", Log::DEBUG, "Reset resume for: %s", recording->Name());
834 cResumeFile ResumeFile(recording->FileName(), recording->IsPesRecording());
835 if (ResumeFile.Read() >= 0)
843 js["Result"] = false;
844 js["Error"] = "Recording has no resume point";
849 bool jsonserver_channellist(Json::Value& js)
851 Log* log = Log::getInstance();
852 log->log("JSONServer", Log::DEBUG, "channellist");
854 Json::Value jschannels(Json::arrayValue);
857 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
859 if (!channel->GroupSep())
861 // log->log("JSONServer", Log::DEBUG, "name: '%s'", channel->Name());
863 // if (channel->Vpid()) type = 1;
864 // else if (channel->Apid(0)) type = 2;
867 Json::Value oneChannel;
868 oneChannel["Number"] = channel->Number();
869 // oneChannel["Type"] = type;
870 oneChannel["Name"] = channel->Name();
871 //#if VDRVERSNUM < 10703
872 // oneChannel["VType"] = 2;
874 // oneChannel["VType"] = channel->Vtype();
876 jschannels.append(oneChannel);
879 js["Channels"] = jschannels;
884 bool jsonserver_channelschedule(Json::Value& js, const char* postData)
886 Log* log = Log::getInstance();
887 log->log("JSONServer", Log::DEBUG, "channelschedule '%s'", postData);
889 char sChannelNumber[15]; int mgv1 = mg_get_var(postData, strlen(postData), "channelnumber", sChannelNumber, 15);
890 char sStartTime[15]; int mgv2 = mg_get_var(postData, strlen(postData), "starttime", sStartTime, 15);
891 char sDuration[15]; int mgv3 = mg_get_var(postData, strlen(postData), "duration", sDuration, 15);
893 if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) )
895 log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i", mgv1, mgv2, mgv3);
896 js["Result"] = false;
897 js["Error"] = "Bad request parameters";
901 int channelNumber = atoi(sChannelNumber);
902 int startTime = atoi(sStartTime);
903 int duration = atoi(sDuration);
905 cChannel* channel = NULL;
906 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
908 if (channel->GroupSep()) continue;
909 if (channel->Number() == channelNumber) break;
914 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
915 js["Result"] = false;
916 js["Error"] = "Could not find channel";
920 cSchedulesLock MutexLock;
921 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
924 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
925 js["Result"] = false;
926 js["Error"] = "Internal schedules error (1)";
929 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
932 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
933 js["Result"] = false;
934 js["Error"] = "Internal schedules error (2)";
938 Json::Value jsevents(Json::arrayValue);
940 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
943 if ((event->StartTime() + event->Duration()) < time(NULL)) continue;
945 if ((event->StartTime() + event->Duration()) <= startTime) continue;
947 if (event->StartTime() >= (startTime + duration)) continue;
949 Json::Value oneEvent;
950 oneEvent["ID"] = event->EventID();
951 oneEvent["Time"] = (Json::UInt)event->StartTime();
952 oneEvent["Duration"] = event->Duration();
953 oneEvent["Title"] = event->Title() ? event->Title() : "";
954 oneEvent["ShortText"] = event->ShortText() ? event->ShortText() : "";
955 oneEvent["HasTimer"] = event->HasTimer();
956 //oneEvent["Description"] = event->Description() ? event->Description() : "";
957 jsevents.append(oneEvent);
961 js["Events"] = jsevents;
965 bool jsonserver_getscheduleevent(Json::Value& js, const char* postData)
967 Log* log = Log::getInstance();
968 log->log("JSONServer", Log::DEBUG, "getscheduleevent '%s'", postData);
970 char sChannelNumber[15]; int mgv1 = mg_get_var(postData, strlen(postData), "channelnumber", sChannelNumber, 15);
971 char sEventID[15]; int mgv2 = mg_get_var(postData, strlen(postData), "eventid", sEventID, 15);
973 if ( (mgv1 == -1) || (mgv2 == -1) )
975 log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2);
976 js["Result"] = false;
977 js["Error"] = "Bad request parameters";
981 int channelNumber = atoi(sChannelNumber);
982 int eventID = atoi(sEventID);
984 const cEvent* event = jsonserver_getEvent(js, channelNumber, eventID, 0);
987 js["Result"] = false;
991 Json::Value oneEvent;
992 oneEvent["ID"] = event->EventID();
993 oneEvent["Time"] = (Json::UInt)event->StartTime();
994 oneEvent["Duration"] = event->Duration();
995 oneEvent["Title"] = event->Title() ? event->Title() : "";
996 oneEvent["ShortText"] = event->ShortText() ? event->ShortText() : "";
997 oneEvent["Description"] = event->Description() ? event->Description() : "";
998 oneEvent["HasTimer"] = event->HasTimer();
999 oneEvent["RunningStatus"] = event->RunningStatus();
1001 js["Result"] = true;
1002 js["Event"] = oneEvent;
1006 const cEvent* jsonserver_getEvent(Json::Value& js, int channelNumber, int eventID, int aroundTime)
1008 Log* log = Log::getInstance();
1010 cChannel* channel = NULL;
1011 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
1013 if (channel->GroupSep()) continue;
1014 if (channel->Number() == channelNumber) break;
1019 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
1020 js["Error"] = "Could not find channel";
1024 cSchedulesLock MutexLock;
1025 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
1028 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
1029 js["Error"] = "Internal schedules error (1)";
1033 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
1036 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
1037 js["Error"] = "Internal schedules error (2)";
1041 const cEvent* event = NULL;
1044 event = Schedule->GetEvent(eventID);
1048 event = Schedule->GetEventAround(aroundTime);
1053 log->log("JSONServer", Log::ERR, "Could not find requested event: %i", eventID);
1054 js["Error"] = "Internal schedules error (3)";
1061 bool jsonserver_timerlist(Json::Value& js)
1063 Log* log = Log::getInstance();
1064 log->log("JSONServer", Log::DEBUG, "timerlist");
1066 Json::Value jstimers(Json::arrayValue);
1069 int numTimers = Timers.Count();
1071 for (int i = 0; i < numTimers; i++)
1073 timer = Timers.Get(i);
1074 Json::Value oneTimer;
1075 oneTimer["Active"] = timer->HasFlags(tfActive);
1076 oneTimer["Recording"] = timer->Recording();
1077 oneTimer["Pending"] = timer->Pending();
1078 oneTimer["Priority"] = timer->Priority();
1079 oneTimer["Lifetime"] = timer->Lifetime();
1080 oneTimer["ChannelNumber"] = timer->Channel()->Number();
1081 oneTimer["ChannelID"] = (const char *)timer->Channel()->GetChannelID().ToString();
1082 oneTimer["StartTime"] = (int)timer->StartTime();
1083 oneTimer["StopTime"] = (int)timer->StopTime();
1084 oneTimer["Day"] = (int)timer->Day();
1085 oneTimer["WeekDays"] = timer->WeekDays();
1086 oneTimer["Name"] = timer->File();
1088 const cEvent* event = timer->Event();
1091 oneTimer["EventID"] = event->EventID();
1095 int channelNumber = timer->Channel()->Number();
1096 int aroundTime = timer->StartTime() + 1;
1097 log->log("JSONServer", Log::DEBUG, "%i", aroundTime);
1099 const cEvent* eventAround = jsonserver_getEvent(js, channelNumber, 0, aroundTime);
1102 oneTimer["EventID"] = eventAround->EventID();
1106 oneTimer["EventID"] = 0;
1110 jstimers.append(oneTimer);
1113 js["Timers"] = jstimers;
1114 js["Result"] = true;
1118 bool jsonserver_timerset(Json::Value& js, const char* postData)
1120 Log* log = Log::getInstance();
1121 log->log("JSONServer", Log::DEBUG, "timerset");
1123 char sTimerString[1024]; int mgv1 = mg_get_var(postData, strlen(postData), "timerstring", sTimerString, 1024);
1127 log->log("JSONServer", Log::ERR, "Could not get timerstring");
1128 js["Result"] = false;
1129 js["Error"] = "Bad request parameters";
1133 log->log("JSONServer", Log::DEBUG, "'%s'", sTimerString);
1134 cTimer *timer = new cTimer;
1135 if (!timer->Parse(sTimerString))
1138 js["Result"] = false;
1139 js["Error"] = "Failed to parse timer request details";
1143 cTimer *t = Timers.GetTimer(timer);
1147 js["Result"] = false;
1148 js["Error"] = "Timer already exists";
1153 Timers.SetModified();
1154 js["Result"] = true;
1158 bool jsonserver_timersetactive(Json::Value& js, const char* postData)
1160 Log* log = Log::getInstance();
1161 log->log("JSONServer", Log::DEBUG, "timersetactive");
1163 char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50);
1164 char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024);
1165 char rStartTime[20]; int mgv3 = mg_get_var(postData, strlen(postData), "StartTime", rStartTime, 20);
1166 char rStopTime[20]; int mgv4 = mg_get_var(postData, strlen(postData), "StopTime", rStopTime, 20);
1167 char rWeekDays[50]; int mgv5 = mg_get_var(postData, strlen(postData), "WeekDays", rWeekDays, 20);
1168 char tNewActive[20]; int mgv6 = mg_get_var(postData, strlen(postData), "SetActive", tNewActive, 20);
1170 if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1) || (mgv6 == -1))
1172 log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5, mgv6);
1173 js["Result"] = false;
1174 js["Error"] = "Bad request parameters";
1178 log->log("JSONServer", Log::DEBUG, "timersetactive: %s %s:%s:%s:%s:%s", tNewActive, rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1180 cTimer* timer = jsonserver_findTimer(rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1183 if (strcmp(tNewActive, "true") == 0)
1185 timer->SetFlags(tfActive);
1187 else if (strcmp(tNewActive, "false") == 0)
1189 timer->ClrFlags(tfActive);
1193 js["Result"] = false;
1194 js["Error"] = "Bad request parameters";
1198 js["Result"] = true;
1200 Timers.SetModified();
1205 js["Result"] = false;
1206 js["Error"] = "Timer not found";
1210 cTimer* jsonserver_findTimer(const char* rChannelID, const char* rName, const char* rStartTime, const char* rStopTime, const char* rWeekDays)
1212 Log* log = Log::getInstance();
1214 int numTimers = Timers.Count();
1216 for (int i = 0; i < numTimers; i++)
1218 timer = Timers.Get(i);
1220 log->log("JSONServer", Log::DEBUG, "findtimer current: %s", (const char*)timer->ToText(true));
1221 log->log("JSONServer", Log::DEBUG, "findtimer: %s", (const char*)timer->Channel()->GetChannelID().ToString());
1222 log->log("JSONServer", Log::DEBUG, "findtimer: %s", rChannelID);
1223 log->log("JSONServer", Log::DEBUG, "findtimer: %s", timer->File());
1224 log->log("JSONServer", Log::DEBUG, "findtimer: %s", rName);
1225 log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->StartTime());
1226 log->log("JSONServer", Log::DEBUG, "findtimer: %s", rStartTime);
1227 log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->StopTime());
1228 log->log("JSONServer", Log::DEBUG, "findtimer: %s", rStopTime);
1229 log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->WeekDays());
1230 log->log("JSONServer", Log::DEBUG, "findtimer: %s", rWeekDays);
1233 (strcmp(timer->Channel()->GetChannelID().ToString(), rChannelID) == 0)
1234 && (strcmp(timer->File(), rName) == 0)
1235 && (timer->StartTime() == atoi(rStartTime))
1236 && (timer->StopTime() == atoi(rStopTime))
1237 && (timer->WeekDays() == atoi(rWeekDays))
1240 log->log("JSONServer", Log::DEBUG, "found");
1244 log->log("JSONServer", Log::DEBUG, "no timer found");
1248 bool jsonserver_timerdel(Json::Value& js, const char* postData)
1250 Log* log = Log::getInstance();
1251 log->log("JSONServer", Log::DEBUG, "timerdel");
1253 char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50);
1254 char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024);
1255 char rStartTime[20]; int mgv3 = mg_get_var(postData, strlen(postData), "StartTime", rStartTime, 20);
1256 char rStopTime[20]; int mgv4 = mg_get_var(postData, strlen(postData), "StopTime", rStopTime, 20);
1257 char rWeekDays[50]; int mgv5 = mg_get_var(postData, strlen(postData), "WeekDays", rWeekDays, 20);
1259 if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1))
1261 log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5);
1262 js["Result"] = false;
1263 js["Error"] = "Bad request parameters";
1267 log->log("JSONServer", Log::DEBUG, "timerdel: %s:%s:%s:%s:%s", rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1269 if (Timers.BeingEdited())
1271 log->log("JSONServer", Log::ERR, "Unable to delete timer - timers being edited at VDR");
1272 js["Result"] = false;
1273 js["Error"] = "Timers being edited at VDR";
1277 cTimer* timer = jsonserver_findTimer(rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1280 if (timer->Recording())
1282 log->log("JSONServer", Log::ERR, "Unable to delete timer - timer is running");
1283 js["Result"] = false;
1284 js["Error"] = "Timer is running";
1291 Timers.SetModified();
1292 js["Result"] = true;
1296 js["Result"] = false;
1297 js["Error"] = "Timer not found";
1301 bool jsonserver_timerisrecording(Json::Value& js, const char* postData)
1303 Log* log = Log::getInstance();
1304 log->log("JSONServer", Log::DEBUG, "timerisrecording");
1306 char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50);
1307 char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024);
1308 char rStartTime[20]; int mgv3 = mg_get_var(postData, strlen(postData), "StartTime", rStartTime, 20);
1309 char rStopTime[20]; int mgv4 = mg_get_var(postData, strlen(postData), "StopTime", rStopTime, 20);
1310 char rWeekDays[50]; int mgv5 = mg_get_var(postData, strlen(postData), "WeekDays", rWeekDays, 20);
1312 if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1))
1314 log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5);
1315 js["Result"] = false;
1316 js["Error"] = "Bad request parameters";
1320 log->log("JSONServer", Log::DEBUG, "timerisrecording: %s:%s:%s:%s:%s", rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1322 cTimer* timer = jsonserver_findTimer(rChannelID, rName, rStartTime, rStopTime, rWeekDays);
1325 js["Recording"] = timer->Recording();
1326 js["Pending"] = timer->Pending();
1327 js["Result"] = true;
1331 js["Result"] = false;
1332 js["Error"] = "Timer not found";
1336 bool jsonserver_tunersstatus(Json::Value& js, const char* postData)
1338 Log* log = Log::getInstance();
1339 log->log("JSONServer", Log::DEBUG, "tunerstatus");
1341 js["NumDevices"] = cDevice::NumDevices();
1343 Json::Value jsdevices(Json::arrayValue);
1345 for (int i = 0; i < cDevice::NumDevices(); i++)
1347 Json::Value oneDevice;
1348 cDevice *d = cDevice::GetDevice(i);
1349 oneDevice["Number"] = d->DeviceNumber();
1350 oneDevice["Type"] = (const char*)d->DeviceType();
1351 oneDevice["Name"] = (const char*)d->DeviceName();
1352 oneDevice["IsPrimary"] = d->IsPrimaryDevice();
1354 const cChannel* cchannel = d->GetCurrentlyTunedTransponder();
1357 oneDevice["Frequency"] = cchannel->Frequency();
1358 oneDevice["SignalStrength"] = d->SignalStrength();
1359 oneDevice["SignalQuality"] = d->SignalQuality();
1364 oneDevice["Frequency"] = 0;
1365 oneDevice["SignalStrength"] = 0;
1366 oneDevice["SignalQuality"] = 0;
1369 jsdevices.append(oneDevice);
1372 js["Devices"] = jsdevices;
1375 Json::Value jstimers(Json::arrayValue);
1376 int numTimers = Timers.Count();
1378 for (int i = 0; i < numTimers; i++)
1380 timer = Timers.Get(i);
1382 if (timer->Recording())
1384 Json::Value oneTimer;
1385 oneTimer["Recording"] = timer->Recording();
1386 oneTimer["StartTime"] = (int)timer->StartTime();
1387 oneTimer["StopTime"] = (int)timer->StopTime();
1388 oneTimer["File"] = timer->File();
1390 cRecordControl* crc = cRecordControls::GetRecordControl(timer);
1393 cDevice* crcd = crc->Device();
1394 oneTimer["DeviceNumber"] = crcd->DeviceNumber();
1398 oneTimer["DeviceNumber"] = Json::Value::null;
1401 const cChannel* channel = timer->Channel();
1404 oneTimer["ChannelName"] = channel->Name();
1408 oneTimer["ChannelName"] = Json::Value::null;
1411 jstimers.append(oneTimer);
1415 js["CurrentRecordings"] = jstimers;
1418 js["Result"] = true;