|
|
|
@ -1,15 +1,12 @@
|
|
|
|
|
package ru.ulstu.utils.timetable;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import org.springframework.web.client.RestClientException;
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
import ru.ulstu.utils.timetable.errors.TimetableClientException;
|
|
|
|
|
import ru.ulstu.utils.timetable.model.Lesson;
|
|
|
|
|
import ru.ulstu.utils.timetable.model.TimetableResponse;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
@ -17,10 +14,10 @@ import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class TimetableService {
|
|
|
|
|
private static final String TIMETABLE_URL = "http://timetable.athene.tech";
|
|
|
|
|
private static final String TIMETABLE_URL = "http://timetable.athene.tech/api/1.0/timetable?filter=%s";
|
|
|
|
|
private SimpleDateFormat lessonTimeFormat = new SimpleDateFormat("hh:mm");
|
|
|
|
|
|
|
|
|
|
private long[] lessonsStarts = new long[]{
|
|
|
|
|
private long[] lessonsStarts = new long[] {
|
|
|
|
|
lessonTimeFormat.parse("8:00:00").getTime(),
|
|
|
|
|
lessonTimeFormat.parse("9:40:00").getTime(),
|
|
|
|
|
lessonTimeFormat.parse("11:30:00").getTime(),
|
|
|
|
@ -30,7 +27,8 @@ public class TimetableService {
|
|
|
|
|
lessonTimeFormat.parse("18:10:00").getTime(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public TimetableService() throws ParseException { }
|
|
|
|
|
public TimetableService() throws ParseException {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int getCurrentDay() {
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
@ -42,6 +40,7 @@ public class TimetableService {
|
|
|
|
|
long lessonDuration = 90 * 60000;
|
|
|
|
|
Date now = new Date();
|
|
|
|
|
long timeNow = now.getTime() % (24 * 60 * 60 * 1000L);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < lessonsStarts.length; i++) {
|
|
|
|
|
if (timeNow > lessonsStarts[i] && timeNow < lessonsStarts[i] + lessonDuration) {
|
|
|
|
|
return i;
|
|
|
|
@ -56,31 +55,26 @@ public class TimetableService {
|
|
|
|
|
return (cal.get(Calendar.WEEK_OF_YEAR) + 1) % 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TimetableResponse getTimetableForUser(String userFIO) throws IOException {
|
|
|
|
|
URL url = new URL(TIMETABLE_URL + "/api/1.0/timetable?filter=" + URLEncoder.encode(userFIO, "UTF-8"));
|
|
|
|
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
|
|
|
con.setRequestMethod("GET");
|
|
|
|
|
private TimetableResponse getTimetableForUser(String userFIO) throws RestClientException {
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
return restTemplate.getForObject(String.format(TIMETABLE_URL, userFIO), TimetableResponse.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BufferedReader in = new BufferedReader(
|
|
|
|
|
new InputStreamReader(con.getInputStream()));
|
|
|
|
|
String inputLine;
|
|
|
|
|
StringBuilder content = new StringBuilder();
|
|
|
|
|
while ((inputLine = in.readLine()) != null) {
|
|
|
|
|
content.append(inputLine);
|
|
|
|
|
public Lesson getCurrentLesson(String userFio) {
|
|
|
|
|
TimetableResponse response;
|
|
|
|
|
try {
|
|
|
|
|
response = getTimetableForUser(userFio);
|
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new TimetableClientException(userFio);
|
|
|
|
|
}
|
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
|
|
return new ObjectMapper().readValue(content.toString(), TimetableResponse.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Lesson getCurrentLesson(String userFio) throws IOException {
|
|
|
|
|
TimetableResponse response = getTimetableForUser(userFio);
|
|
|
|
|
int lessonNumber = getCurrentLessonNumber();
|
|
|
|
|
if (lessonNumber < 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<Object> lessons = response
|
|
|
|
|
List<Lesson> lessons = response
|
|
|
|
|
.getResponse()
|
|
|
|
|
.getWeeks()
|
|
|
|
|
.get(getCurrentWeek())
|
|
|
|
|