package com.jentfoo.recorder;

import java.io.File;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.threadly.concurrent.AbstractService;
import org.threadly.concurrent.SchedulerServiceInterface;

public class HomeRunRecordingService extends AbstractService {
  private final SchedulerServiceInterface recordScheduler;
  private final List<HttpStreamRecorder> recorders; 
  
  public HomeRunRecordingService(SchedulerServiceInterface masterScheduler, 
                                 SchedulerServiceInterface recordScheduler, 
                                 InetAddress downloadIp, File savePath, 
                                 List<ChannelSchedule> schedule) {
    this.recordScheduler = recordScheduler;
    this.recorders = new ArrayList<HttpStreamRecorder>(schedule.size());
    Iterator<ChannelSchedule> it = schedule.iterator();
    while (it.hasNext()) {
      ChannelSchedule cs = it.next();
      URL requestURL; 
      try {
        requestURL =  new URL("http://" + downloadIp.getHostAddress() + ":5004/auto/v" + cs.channel + "?dlna");
      } catch (MalformedURLException e) {
        throw new RuntimeException(e);
      }
      recorders.add(new HttpStreamRecorder(masterScheduler, requestURL, savePath, cs));
    }
  }

  @Override
  protected void startupService() {
    Iterator<HttpStreamRecorder> it = recorders.iterator();
    while (it.hasNext()) {
      HttpStreamRecorder recorder = it.next();
      short daysTillRecord = recorder.chanSchedule.daysTillValid();
      if (daysTillRecord == 0) {
        System.out.println("Will start recording channel: " + recorder.chanSchedule.channel + 
                             " in " + makeTimeStr(recorder.getDelayInMillisTillStart()) + " minutes");
      } else {
        System.out.println("Scheduling to start recording channel: " + recorder.chanSchedule.channel + 
                             " in " + daysTillRecord + " days");
      }
      
      long period;
      if (recorder.chanSchedule.days.size() == 1) {
        period = TimeUnit.DAYS.toMillis(7);
      } else {
        period = TimeUnit.DAYS.toMillis(1);
      }
      recordScheduler.scheduleAtFixedRate(recorder, recorder.getDelayInMillisTillStart(), period);
    }
  }
  
  private static String makeTimeStr(long millis) {
    int min = (int)((millis / 1000) / 60);
    int seconds = (int)((millis - (min * 1000 * 60)) / 1000);
    
    return min + ":" + StringFormatter.pad(seconds, 2);
  }

  @Override
  protected void shutdownService() {
    Iterator<HttpStreamRecorder> it = recorders.iterator();
    while (it.hasNext()) {
      recordScheduler.remove(it.next());
    }
    
    synchronized (this) {
      this.notifyAll();
    }
  }
  
  public void blockTillShutdown() throws InterruptedException {
    synchronized (this) {
      while (isRunning()) {
        this.wait();
      }
    }
  }
}

@@ -2093,117 +2093,445 @@
     
-long period;%0A      if (recorder.chanSchedule.days.size() == 1) %7B%0A        period = TimeUnit.DAYS.toMillis(7);%0A
+if (recorder.chanSchedule.days.size() == 1) %7B%0A        long timeDelay = recorder.getDelayInMillisTillStart();%0A        long dayPointInMillis = TimeUnit.HOURS.toMillis(recorder.chanSchedule.hour) + %0A                                  TimeUnit.MINUTES.toMillis(recorder.chanSchedule.minute);%0A        long initialDelay;%0A        if (timeDelay %3E dayPointInMillis) %7B%0A          initialDelay = TimeUnit.DAYS.toMillis(daysTillRecord) + timeDelay;%0A  
     
@@ -2553,16 +2553,158 @@
     
+  
-period =
+initialDelay = TimeUnit.DAYS.toMillis(daysTillRecord - 1) + timeDelay;%0A        %7D%0A        recordScheduler.scheduleAtFixedRate(recorder, initialDelay,
  Tim
@@ -2723,17 +2723,18 @@
 oMillis(
-1
+7)
 );%0A     
@@ -2731,31 +2731,40 @@
 7));%0A      %7D
-%0A
+ else %7B%0A 
       
+ 
 recordSchedu
@@ -2839,16 +2839,43 @@
 (), 
-period);
+TimeUnit.DAYS.toMillis(1));%0A      %7D
 %0A   

Fix defect when scheduled item only runs once per week