1
0
mirror of https://github.com/moparisthebest/rcrdit synced 2024-12-21 23:08:57 -05:00

Add web service function to get the schedule.

This commit is contained in:
Jeff Lamb 2017-03-03 22:12:18 -05:00
parent 352e8ed1ad
commit d95a9ec9e1
3 changed files with 158 additions and 4 deletions

View File

@ -27,6 +27,7 @@ import com.moparisthebest.rcrdit.autorec.ProgramAutoRec;
import com.moparisthebest.rcrdit.tuner.HDHomerun;
import com.moparisthebest.rcrdit.tuner.Tuner;
import com.moparisthebest.rcrdit.tuner.Tuners;
import com.moparisthebest.rcrdit.xmltv.Channel;
import com.moparisthebest.rcrdit.xmltv.Program;
import com.moparisthebest.rcrdit.xmltv.Tv;
import net.fortuna.ical4j.data.CalendarOutputter;
@ -49,8 +50,10 @@ import org.slf4j.LoggerFactory;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileOutputStream;
@ -64,10 +67,13 @@ import java.sql.DriverManager;
import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import com.moparisthebest.rcrdit.requestbeans.GetScheduleRequest;
/**
* Created by mopar on 2/16/17.
@ -135,7 +141,7 @@ public class RcrdIt extends ResourceConfig implements AutoCloseable {
System.out.print("\"" + name + "\", ");
*/
}
private static void addMeeting(final Calendar calendar, final VTimeZone tz,
final Instant start, final Instant stop,
final ProgramAutoRec prog, final MessageDigest md, final boolean skipped) {
@ -426,12 +432,34 @@ public class RcrdIt extends ResourceConfig implements AutoCloseable {
return "pong";
}
@GET
@POST
@Path("getSchedule")
@Produces(MediaType.APPLICATION_JSON)
public Tv getSchedule() {
return schedule;
@Consumes(MediaType.APPLICATION_JSON)
//List<Channel>
public List<Channel> getSchedule(GetScheduleRequest scheduleRequest) {
List<Channel> channelList = new ArrayList<>();
try{
int firstItemToLoad = ((scheduleRequest.getPageNum()-1) * scheduleRequest.getChannelsPerPage());
if(schedule.getChannels().size() > firstItemToLoad){
for(int i=firstItemToLoad; i<schedule.getChannels().size() && channelList.size() <scheduleRequest.getChannelsPerPage() ;i++){
channelList.add(new Channel(schedule.getChannels().get(i),scheduleRequest.getStartTime(),scheduleRequest.getEndTime()));
}
}
}catch(Exception e){
log.error("Error in getSchedule",e);
}
return channelList;
}
/* @GET
@Path("getSchedule")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void getSchedule() {
return schedule;
}*/
public static void main(String[] args) throws Exception {
final File cfg;
@ -441,6 +469,7 @@ public class RcrdIt extends ResourceConfig implements AutoCloseable {
return;
}
log.debug("rcrdit starting");
final RcrdIt rcrdIt = new RcrdIt(cfg);

View File

@ -0,0 +1,112 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.moparisthebest.rcrdit.requestbeans;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
/**
*
* @author Jeff
*/
public class GetScheduleRequest {
protected String startTimeString;
protected String endTimeString;
protected int channelsPerPage = 100;
protected int pageNum = 1;
protected Instant startTime;
protected Instant endTime;
private final String dateTimeFormatPattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public GetScheduleRequest() {
LocalDateTime now = LocalDateTime.now();
LocalDateTime topOfHour = now.withMinute(0);
setStartTime(topOfHour.toInstant(ZoneOffset.systemDefault().getRules().getOffset(topOfHour)));
LocalDateTime endOfNextHour = now.plusHours(1).withMinute(59).withSecond(59);
setEndTime(endOfNextHour.toInstant(ZoneOffset.systemDefault().getRules().getOffset(endOfNextHour)));
}
public String getStartTimeString() {
return startTimeString;
}
public void setStartTimeString(String startTimeString, boolean setStartTimeObject){
if(setStartTimeObject){
setStartTimeString(startTimeString);
}else{
this.startTimeString = startTimeString;
}
}
public void setStartTimeString(String startTimeString) {
System.out.println("In setStartTimeString");
this.startTimeString = startTimeString;
if(startTimeString != null){
startTime = Instant.parse(startTimeString);
}
}
public String getEndTimeString() {
return endTimeString;
}
public void setEndTimeString(String endTime) {
System.out.println("In setEndTimeString");
this.endTimeString = endTime;
if(endTimeString != null){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormatPattern).withZone( ZoneOffset.UTC );
startTime = Instant.parse(endTimeString);
}
}
public int getChannelsPerPage() {
return channelsPerPage;
}
public void setChannelsPerPage(int channelsPerPage) {
this.channelsPerPage = channelsPerPage;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public Instant getStartTime() {
return startTime;
}
public final void setStartTime(Instant startTime) {
System.out.println("In setStartTime");
this.startTime = startTime;
if(this.startTime != null){
this.startTimeString = DateTimeFormatter.ofPattern(dateTimeFormatPattern).withZone(ZoneOffset.UTC).format(this.startTime);
}
}
public Instant getEndTime() {
return endTime;
}
public final void setEndTime(Instant endTime) {
System.out.println("In setEndTime");
this.endTime = endTime;
if(this.endTime != null){
this.endTimeString = DateTimeFormatter.ofPattern(dateTimeFormatPattern).withZone(ZoneOffset.UTC).format(this.endTime);
}
}
}

View File

@ -18,6 +18,7 @@
package com.moparisthebest.rcrdit.xmltv;
import java.time.Instant;
import java.util.*;
/**
@ -36,6 +37,18 @@ public class Channel {
final List<Program> programs = new ArrayList<>();
private final List<Program> finalPrograms = Collections.unmodifiableList(programs);
public Channel(Channel fromChannel, Instant startTime, Instant stopTime){
this.displayName = fromChannel.displayName;
this.displayNames = fromChannel.displayNames;
this.id = fromChannel.id;
List<Program> programsForNewChannel = new ArrayList<>();
fromChannel.getPrograms().stream().filter((program) -> ((program.getStop().isAfter(startTime) && program.getStop().isBefore(stopTime)) || (program.getStart().isBefore(stopTime) && program.getStart().isAfter(startTime) ) || program.getStart().equals(startTime) || program.getStop().equals(stopTime) )).forEach((program) -> {
programsForNewChannel.add(program);
});
this.programs.addAll(programsForNewChannel);
}
public Channel(final String id, final Set<String> displayNames) {
this.id = id;
this.displayNames = Collections.unmodifiableSet(displayNames);