Esempio n. 1
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!");
     }
  }
}