package uk.me.steev.java.heating.io.http;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import uk.me.steev.java.heating.controller.Heating;
import uk.me.steev.java.heating.controller.TemperatureEvent;

public class SetOverrideServlet extends HeatingServlet {
  private static final long serialVersionUID = -2479268631077208608L;
  static final Logger logger = LogManager.getLogger(CurrentTempServlet.class.getName());
  
  public SetOverrideServlet(Heating heating) {
    super(heating);
  }
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    response.setContentType("text/plain");

    String pathInfo = null;
    if (null != request.getPathInfo()) {
      pathInfo = request.getPathInfo().replaceFirst("/", "");
      Float overrideDegrees = Float.parseFloat(pathInfo);
      List<TemperatureEvent> timesDueOn = heating.getProcessor().getTimesDueOn();
      if (null != timesDueOn && timesDueOn.size() > 0) {
        for (TemperatureEvent te : timesDueOn) {
          if (te.getStartTime().isBefore(LocalDateTime.now()))
            continue;
    
          heating.setOverrideEnd(te.getStartTime());
          break;
        }
      }

      heating.setOverrideDegrees(overrideDegrees);
      heating.getProcessor().process();

      response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    }
  }
}

@@ -1238,16 +1238,69 @@
 size() %3E
+ 0 &&%0A          Float.compare(0f, overrideDegrees) ==
  0) %7B%0A  
@@ -1407,17 +1407,19 @@
 e.now())
-)
+ &&
 %0A       
@@ -1427,23 +1427,60 @@
     
-continue;%0A  
+  te.getEndTime().isAfter(LocalDateTime.now())) %7B%0A
   
-%0A
     
@@ -1510,29 +1510,27 @@
 deEnd(te.get
-Start
+End
 Time());%0A   
@@ -1526,16 +1526,18 @@
 ime());%0A
+  
         
@@ -1553,17 +1553,99 @@
         
-%7D
+  %7D%0A        %7D%0A      %7D else %7B%0A          heating.setOverrideEnd(LocalDateTime.now());
 %0A      %7D

Fix override temp/time to end of current event