Esempio n. 1
0
void JsonTimerList::addTimer(cTimer* timer)
{
  if ( filtered() ) return;
  static TimerValues v;

  SerTimer serTimer;
  serTimer.Id = StringExtension::UTF8Decode(VdrExtension::getTimerID(timer));
  serTimer.Flags = timer->Flags();
  serTimer.Start = timer->Start();
  serTimer.Stop = timer->Stop();
  serTimer.Priority = timer->Priority();
  serTimer.Lifetime = timer->Lifetime();
  serTimer.EventID = timer->Event() != NULL ? timer->Event()->EventID() : -1;
  serTimer.WeekDays = StringExtension::UTF8Decode(v.ConvertWeekdays(timer->WeekDays()));
  serTimer.Day = StringExtension::UTF8Decode(v.ConvertDay(timer->Day()));
  serTimer.Channel = StringExtension::UTF8Decode((const char*)timer->Channel()->GetChannelID().ToString());
  serTimer.IsRecording = timer->Recording();
  serTimer.IsPending = timer->Pending();
  serTimer.FileName = StringExtension::UTF8Decode(timer->File());
  serTimer.ChannelName = StringExtension::UTF8Decode(timer->Channel()->Name());
  serTimer.IsActive = timer->Flags() & 0x01 == 0x01 ? true : false;

  int tstart = timer->Day() - ( timer->Day() % 3600 ) + ((int)(timer->Start()/100)) * 3600 + ((int)(timer->Start()%100)) * 60;
  int tstop = timer->Day() - ( timer->Day() % 3600 ) + ((int)(timer->Stop()/100)) * 3600 + ((int)(timer->Stop()%100)) * 60;
  // if a timer starts before and ends after midnight, add a day to tstop
  if ( (int)(timer->Start()) > (int)(timer->Stop()) )
     tstop += 86400;

  serTimer.StartTimeStamp = StringExtension::UTF8Decode(StringExtension::dateToString((time_t)tstart));
  serTimer.StopTimeStamp = StringExtension::UTF8Decode(StringExtension::dateToString((time_t)tstop));

  serTimers.push_back(serTimer);
}
Esempio n. 2
0
void TimersResponder::deleteTimer(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{
  QueryHandler q("/timers", request);

  if ( Timers.BeingEdited() ) {
     reply.httpReturn(502, "Timers are being edited - try again later");
     return;
  }

  TimerValues v;

  cTimer* timer = v.ConvertTimer(q.getParamAsString(0));
 
  if ( timer == NULL) {
     reply.httpReturn(404, "Timer id invalid!");
  } else {
     if ( timer->Recording() ) {
        timer->Skip();
        cRecordControls::Process(time(NULL));
     }
     Timers.Del(timer);
     Timers.SetModified();
     reply.httpReturn(200, "Timer deleted."); 
  }
}
Esempio n. 3
0
void JsonTimerList::addTimer(const cTimer* timer)
{
  if ( filtered() ) return;
  static TimerValues v;

  SerTimer serTimer;
  serTimer.Id = StringExtension::UTF8Decode(VdrExtension::getTimerID(timer));
  serTimer.Index = timer->Index() + 1;
  serTimer.Flags = timer->Flags();
  serTimer.Start = timer->Start();
  serTimer.Stop = timer->Stop();
  serTimer.Priority = timer->Priority();
  serTimer.Lifetime = timer->Lifetime();
  serTimer.EventID = timer->Event() != NULL ? timer->Event()->EventID() : -1;
  serTimer.WeekDays = StringExtension::UTF8Decode(v.ConvertWeekdays(timer->WeekDays()));
  serTimer.Day = StringExtension::UTF8Decode(v.ConvertDay(timer->Day()));
  serTimer.Channel = StringExtension::UTF8Decode((const char*)timer->Channel()->GetChannelID().ToString());
  serTimer.IsRecording = timer->Recording();
  serTimer.IsPending = timer->Pending();
  serTimer.FileName = StringExtension::UTF8Decode(timer->File());
  serTimer.ChannelName = StringExtension::UTF8Decode(timer->Channel()->Name());
  serTimer.IsActive = (timer->Flags() & tfActive) == tfActive ? true : false;
  serTimer.Aux = StringExtension::UTF8Decode(timer->Aux() != NULL ? timer->Aux() : "");

  serTimer.StartTimeStamp = v.GetStartStopTimestamp(timer);
  serTimer.StopTimeStamp = v.GetStartStopTimestamp(timer, true);

  serTimers.push_back(serTimer);
}
Esempio n. 4
0
void XmlTimerList::addTimer(cTimer* timer)
{
  if ( filtered() ) return;
  static TimerValues v;

  s->write(" <timer>\n");
  s->write(cString::sprintf("  <param name=\"id\">%s</param>\n", StringExtension::encodeToXml(VdrExtension::getTimerID(timer)).c_str()));
  s->write(cString::sprintf("  <param name=\"flags\">%i</param>\n", timer->Flags()));
  s->write(cString::sprintf("  <param name=\"start\">%i</param>\n", timer->Start()) );
  s->write(cString::sprintf("  <param name=\"stop\">%i</param>\n", timer->Stop()) );

  int tstart = timer->Day() - ( timer->Day() % 3600 ) + ((int)(timer->Start()/100)) * 3600 + ((int)(timer->Start()%100)) * 60;
  int tstop = timer->Day() - ( timer->Day() % 3600 ) + ((int)(timer->Stop()/100)) * 3600 + ((int)(timer->Stop()%100)) * 60;
  // if a timer starts before and ends after midnight, add a day to tstop
  if ( (int)(timer->Start()) > (int)(timer->Stop()) )
     tstop += 86400;

  s->write(cString::sprintf("  <param name=\"start_timestamp\">%s</param>\n", StringExtension::encodeToXml(StringExtension::dateToString(tstart)).c_str()));
  s->write(cString::sprintf("  <param name=\"stop_timestamp\">%s</param>\n", StringExtension::encodeToXml(StringExtension::dateToString(tstop)).c_str()));

  s->write(cString::sprintf("  <param name=\"priority\">%i</param>\n", timer->Priority()) );
  s->write(cString::sprintf("  <param name=\"lifetime\">%i</param>\n", timer->Lifetime()) );
  s->write(cString::sprintf("  <param name=\"event_id\">%i</param>\n", timer->Event() != NULL ? timer->Event()->EventID() : -1) );
  s->write(cString::sprintf("  <param name=\"weekdays\">%s</param>\n", StringExtension::encodeToXml(v.ConvertWeekdays(timer->WeekDays())).c_str()));
  s->write(cString::sprintf("  <param name=\"day\">%s</param>\n", StringExtension::encodeToXml(v.ConvertDay(timer->Day())).c_str()));
  s->write(cString::sprintf("  <param name=\"channel\">%s</param>\n", StringExtension::encodeToXml((const char*)timer->Channel()->GetChannelID().ToString()).c_str()) );
  s->write(cString::sprintf("  <param name=\"is_recording\">%s</param>\n", timer->Recording() ? "true" : "false" ) );
  s->write(cString::sprintf("  <param name=\"is_pending\">%s</param>\n", timer->Pending() ? "true" : "false" ));
  s->write(cString::sprintf("  <param name=\"file_name\">%s</param>\n", StringExtension::encodeToXml(timer->File()).c_str()) );
  s->write(cString::sprintf("  <param name=\"channel_name\">%s</param>\n", StringExtension::encodeToXml(timer->Channel()->Name()).c_str()));
  s->write(cString::sprintf("  <param name=\"is_active\">%s</param>\n", timer->Flags() & 0x01 == 0x01 ? "true" : "false" ));
  s->write(" </timer>\n");
}
Esempio n. 5
0
void XmlTimerList::addTimer(const cTimer* timer)
{
  if ( filtered() ) return;
  static TimerValues v;

  s->write(" <timer>\n");
  s->write(cString::sprintf("  <param name=\"id\">%s</param>\n", StringExtension::encodeToXml(VdrExtension::getTimerID(timer)).c_str()));
  s->write(cString::sprintf("  <param name=\"index\">%i</param>\n", timer->Index() + 1));
  s->write(cString::sprintf("  <param name=\"flags\">%i</param>\n", timer->Flags()));
  s->write(cString::sprintf("  <param name=\"start\">%i</param>\n", timer->Start()) );
  s->write(cString::sprintf("  <param name=\"stop\">%i</param>\n", timer->Stop()) );

  s->write(cString::sprintf("  <param name=\"start_timestamp\">%s</param>\n", StringExtension::encodeToXml(v.GetStartStopTimestamp(timer)).c_str()));
  s->write(cString::sprintf("  <param name=\"stop_timestamp\">%s</param>\n", StringExtension::encodeToXml(v.GetStartStopTimestamp(timer, true)).c_str()));

  s->write(cString::sprintf("  <param name=\"priority\">%i</param>\n", timer->Priority()) );
  s->write(cString::sprintf("  <param name=\"lifetime\">%i</param>\n", timer->Lifetime()) );
  s->write(cString::sprintf("  <param name=\"event_id\">%i</param>\n", timer->Event() != NULL ? timer->Event()->EventID() : -1) );
  s->write(cString::sprintf("  <param name=\"weekdays\">%s</param>\n", StringExtension::encodeToXml(v.ConvertWeekdays(timer->WeekDays())).c_str()));
  s->write(cString::sprintf("  <param name=\"day\">%s</param>\n", StringExtension::encodeToXml(v.ConvertDay(timer->Day())).c_str()));
  s->write(cString::sprintf("  <param name=\"channel\">%s</param>\n", StringExtension::encodeToXml((const char*)timer->Channel()->GetChannelID().ToString()).c_str()) );
  s->write(cString::sprintf("  <param name=\"is_recording\">%s</param>\n", timer->Recording() ? "true" : "false" ) );
  s->write(cString::sprintf("  <param name=\"is_pending\">%s</param>\n", timer->Pending() ? "true" : "false" ));
  s->write(cString::sprintf("  <param name=\"file_name\">%s</param>\n", StringExtension::encodeToXml(timer->File()).c_str()) );
  s->write(cString::sprintf("  <param name=\"channel_name\">%s</param>\n", StringExtension::encodeToXml(timer->Channel()->Name()).c_str()));
  s->write(cString::sprintf("  <param name=\"is_active\">%s</param>\n", (timer->Flags() & tfActive) == tfActive ? "true" : "false" ));
  s->write(cString::sprintf("  <param name=\"aux\">%s</param>\n", (timer->Aux() != NULL ? StringExtension::encodeToXml(timer->Aux()).c_str() : "")));
  s->write(" </timer>\n");
}
Esempio n. 6
0
void TimersResponder::createOrUpdateTimer(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply, bool update)
{
  QueryHandler q("/timers", request);

  if ( Timers.BeingEdited() ) {
     reply.httpReturn(502, "Timers are being edited - try again later");
     return;
  }

  int error = false;
  string error_values = "";
  static TimerValues v;

  int flags = v.ConvertFlags(q.getBodyAsString("flags"));
  string aux = v.ConvertAux(q.getBodyAsString("aux"));
  string file = v.ConvertFile(q.getBodyAsString("file"));
  int lifetime = v.ConvertLifetime(q.getBodyAsString("lifetime"));
  int priority = v.ConvertPriority(q.getBodyAsString("priority"));
  int stop = v.ConvertStop(q.getBodyAsString("stop"));
  int start = v.ConvertStart(q.getBodyAsString("start"));
  string weekdays = q.getBodyAsString("weekdays");
  string day = v.ConvertDay(q.getBodyAsString("day"));
  cChannel* chan = v.ConvertChannel(q.getBodyAsString("channel"));
  cTimer* timer_orig = v.ConvertTimer(q.getBodyAsString("timer_id"));
  
  if ( update == false ) { //create
     int eventid = q.getBodyAsInt("eventid");
     int minpre = q.getBodyAsInt("minpre");
     int minpost = q.getBodyAsInt("minpost");
     if (eventid >= 0 && chan != NULL) {
        cEvent* event = VdrExtension::GetEventById((tEventID)eventid, chan);
      
        if (event == NULL) {
           reply.httpReturn(407, "eventid invalid");
           return;
        } else {
           if (minpre < 0) minpre = 0;
           if (minpost < 0) minpost = 0;
           if (!v.IsFlagsValid(flags)) flags = 1;
           if (!v.IsFileValid(file)) file = (string)event->Title();
           if (!v.IsWeekdaysValid(weekdays)) weekdays = "-------";
           if (!v.IsLifetimeValid(lifetime)) lifetime = 50;
           if (!v.IsPriorityValid(priority)) priority = 99;
           chan = VdrExtension::getChannel((const char*)event->ChannelID().ToString());
           if (!v.IsStartValid(start) || !v.IsStopValid(stop) || !v.IsDayValid(day)) {
              time_t estart = event->StartTime();
              time_t estop = event->EndTime();
              struct tm *starttime = localtime(&estart);
            
              ostringstream daystream;
              daystream << StringExtension::addZeros((starttime->tm_year + 1900), 4) << "-"
                        << StringExtension::addZeros((starttime->tm_mon + 1), 2) << "-"
                        << StringExtension::addZeros((starttime->tm_mday), 2);
              day = daystream.str();
 
              start = starttime->tm_hour * 100 + starttime->tm_min - ((int)(minpre/60))*100 - minpre%60;

              struct tm *stoptime = localtime(&estop);
              stop = stoptime->tm_hour * 100 + stoptime->tm_min + ((int)(minpost/60))*100 + minpost%60;
           }
        }
     } else {
        if ( !v.IsFlagsValid(flags) ) { flags = 1; }
        if ( !v.IsFileValid(file) ) { error = true; error_values += "file, "; }
        if ( !v.IsLifetimeValid(lifetime) ) { lifetime = 50; }
        if ( !v.IsPriorityValid(priority) ) { priority = 99; }
        if ( !v.IsStopValid(stop) ) { error = true; error_values += "stop, "; }
        if ( !v.IsStartValid(start) ) { error = true; error_values += "start, "; }
        if ( !v.IsWeekdaysValid(weekdays) ) { error = true; error_values += "weekdays, "; }
        if ( !v.IsDayValid(day)&& !day.empty() ) { error = true; error_values += "day, "; }
        if ( chan == NULL ) { error = true; error_values += "channel, "; }
     }
  } else { //update
     if ( timer_orig == NULL ) { error = true; error_values += "timer_id, "; }
     if ( !error ) {
        if ( !v.IsFlagsValid(flags) ) { flags = timer_orig->Flags(); }
        if ( !v.IsFileValid(file) ) { file = (string)timer_orig->File(); }
        if ( !v.IsLifetimeValid(lifetime) ) { lifetime = timer_orig->Lifetime(); }
        if ( !v.IsPriorityValid(priority) ) { priority = timer_orig->Priority(); }
        if ( !v.IsStopValid(stop) ) { stop = timer_orig->Stop(); }
        if ( !v.IsStartValid(start) ) { start = timer_orig->Start(); }
        if ( !v.IsWeekdaysValid(weekdays) ) { weekdays = v.ConvertWeekdays(timer_orig->WeekDays()); }
        if ( !v.IsDayValid(day) ) { day = v.ConvertDay(timer_orig->Day()); }
        if ( chan == NULL ) { chan = (cChannel*)timer_orig->Channel(); }
     }
  }

  if (error) {
     string error_message = (string)"The following parameters aren't valid: " + error_values.substr(0, error_values.length()-2) + (string)"!";
     reply.httpReturn(403, error_message);
     return;
  }
 
  ostringstream builder;
  builder << flags << ":"
          << (const char*)chan->GetChannelID().ToString() << ":"
	  << ( weekdays != "-------" ? weekdays : "" )
          << ( weekdays == "-------" || day.empty() ? "" : "@" ) << day << ":"
          << start << ":"
          << stop << ":"
          << priority << ":"
          << lifetime << ":"
          << file << ":" 
          << aux;

  dsyslog("restfulapi: /%s/ ", builder.str().c_str());
  chan = NULL;
  if ( update == false ) { // create timer
     cTimer* timer = new cTimer();
     if ( timer->Parse(builder.str().c_str()) ) { 
        cTimer* checkTimer = Timers.GetTimer(timer);
        if ( checkTimer != NULL ) {
           delete timer;
           reply.httpReturn(403, "Timer already defined!"); 
           esyslog("restfulapi: Timer already defined!");
        } else {
           replyCreatedId(timer, request, reply, out);
           timer->SetEventFromSchedule();
           Timers.Add(timer);
           Timers.SetModified();
           esyslog("restfulapi: timer created!");
        }
     } else {
        reply.httpReturn(403, "Creating timer failed!");
        esyslog("restfulapi: timer creation failed!");
     }
  } else {
     if ( timer_orig->Parse(builder.str().c_str()) ) {
        timer_orig->SetEventFromSchedule();
        Timers.SetModified();
        replyCreatedId(timer_orig, request, reply, out);
        esyslog("restfulapi: updating timer successful!");
     } else { 
        reply.httpReturn(403, "updating timer failed!");
        esyslog("restfulapi: updating timer failed!");
     }
  }
}
Esempio n. 7
0
void TimersResponder::replyBulkdelete(std::ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply) {

  QueryHandler q("/timers/bulkdelete", request);

#if APIVERSNUM > 20300
    LOCK_TIMERS_WRITE;
    cTimers& timers = *Timers;
#else
    cTimers& timers = Timers;

	if ( timers.BeingEdited() ) {
		reply.httpReturn(502, "Timers are being edited - try again later");
		return;
	}
#endif

  TimerDeletedList* list;

  if ( q.isFormat(".json") ) {
     reply.addHeader("Content-Type", "application/json; charset=utf-8");
     list = (TimerDeletedList*)new JsonTimerDeletedList(&out);
  } else if ( q.isFormat(".html") ) {
     reply.addHeader("Content-Type", "text/html; charset=utf-8");
     list = (TimerDeletedList*)new HtmlTimerDeletedList(&out);
  } else if ( q.isFormat(".xml") ) {
     reply.addHeader("Content-Type", "text/xml; charset=utf-8");
     list = (TimerDeletedList*)new XmlTimerDeletedList(&out);
  } else {
     reply.httpReturn(404, "Resources are not available for the selected format. (Use: .json, .html or .xml)");
     return;
  }

  TimerValues v;
  cTimer* timer;

  vector< string > deleteTimers = q.getBodyAsStringArray("timers");
  vector< SerBulkDeleted > results;
  SerBulkDeleted result;

  size_t i;

  list->init();

  for ( i = 0; i < deleteTimers.size(); i++ ) {
    timer = v.ConvertTimer(deleteTimers[i]);
    result.id = deleteTimers[i];
    if ( timer == NULL ) {
	result.deleted = false;
    } else {
      if ( timer->Recording() ) {
	timer->Skip();
#if APIVERSNUM > 20300
        cRecordControls::Process(Timers, time(NULL));
#else
        cRecordControls::Process(time(NULL));
#endif
      }
      timers.Del(timer);
      timers.SetModified();
      result.deleted = true;
    }
    list->addDeleted(result);
  }
  list->setTotal((int)deleteTimers.size());
  list->finish();
  delete list;

};