Skip to content

Commit

Permalink
feat: changed EPG scanning strategy -> is faster now
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendrik2319 committed Jul 2, 2023
1 parent f1ba09e commit 13553ba
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ static RangeTime get(int time_s) {
return null;
}
@Override public String toString() { return label; }

static RangeTime getMax() { return _48h; }
}

private enum RowHeight {
Expand All @@ -105,6 +107,7 @@ public static RowHeight get(int value) {
private final TimersPanel timers;
private final Timers timerData;
private final BouquetsNStations bouquetsNStations;
private final Bouquet bouquet;
private final Vector<SubService> stations;
private final LoadEPGThread loadEPGThread;
private final EPGView epgView;
Expand Down Expand Up @@ -159,14 +162,16 @@ private EPGDialog(
super(parent, getTitle(bouquet), modality, repeatedUseOfDialogObject);
this.epg = epg;
this.timers = timers;
this.bouquet = bouquet;
this.timerData = timerData==null && this.timers!=null ? this.timers.getData() : timerData;
this.bouquetsNStations = bouquetsNStations;
this.stations = bouquet.subservices;
this.stations = this.bouquet.subservices;

JLabel statusOutput = new JLabel("");
statusOutput.setBorder(BorderFactory.createLoweredSoftBevelBorder());

loadEPGThread = new LoadEPGThread(baseURL, this.epg, this.stations) {
//loadEPGThread = new LoadEPGThread(baseURL, this.epg, this.stations) {
loadEPGThread = new LoadEPGThread(baseURL, this.epg, this.bouquet) {
@Override public void setStatusOutput(String text) { statusOutput.setText(text); }
@Override public void updateEPGView () { EPGDialog.this.updateEPGView(); }
@Override public void reconfigureHorizScrollBar() { EPGDialog.this.reconfigureEPGViewHorizScrollBar(); }
Expand All @@ -188,7 +193,8 @@ private EPGDialog(

leadTime_s = OpenWebifController.settings.getInt(ValueKey.EPGDialog_LeadTime , LeadTime._1_30h.time_s);
rangeTime_s = OpenWebifController.settings.getInt(ValueKey.EPGDialog_RangeTime, RangeTime._4h .time_s);
loadEPGThread.setLeadTime(leadTime_s);
loadEPGThread.setLeadTime (leadTime_s);
loadEPGThread.setRangeTime(rangeTime_s<0 ? RangeTime.getMax().time_s : rangeTime_s);

JComboBox<LeadTime > cmbbxLeadTime = OpenWebifController.createComboBox(LeadTime.values(), LeadTime.get(leadTime_s), e->{
int oldLeadTime_s = leadTime_s;
Expand All @@ -215,6 +221,7 @@ private EPGDialog(
});
JComboBox<RangeTime> cmbbxRangeTime = OpenWebifController.createComboBox(RangeTime.values(), RangeTime.get(rangeTime_s), e->{
OpenWebifController.settings.putInt(ValueKey.EPGDialog_RangeTime, rangeTime_s = e.time_s);
loadEPGThread.setRangeTime(rangeTime_s<0 ? RangeTime.getMax().time_s : rangeTime_s);
if (!loadEPGThread.isRunning()) {
updateEPGView();
reconfigureEPGViewHorizScrollBar();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.schwarzbaer.java.tools.openwebifcontroller.epg;

import java.util.Objects;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.SwingUtilities;

import net.schwarzbaer.java.lib.openwebif.Bouquet;
import net.schwarzbaer.java.lib.openwebif.EPG;
import net.schwarzbaer.java.lib.openwebif.Bouquet.SubService;
import net.schwarzbaer.java.tools.openwebifcontroller.OpenWebifController;
Expand All @@ -13,15 +15,24 @@ abstract class LoadEPGThread {
private final String baseURL;
private final EPG epg;
private final Vector<SubService> stations;
private final Bouquet bouquet;
private final JButton button;

private Thread thread;
private boolean isRunning;
private int leadTime_s;
private int rangeTime_s;

LoadEPGThread(String baseURL, EPG epg, Bouquet bouquet) {
this(baseURL, epg, Objects.requireNonNull(bouquet), null);
}
LoadEPGThread(String baseURL, EPG epg, Vector<SubService> stations) {
this.baseURL = baseURL;
this.epg = epg;
this(baseURL, epg, null, Objects.requireNonNull(stations));
}
private LoadEPGThread(String baseURL, EPG epg, Bouquet bouquet, Vector<SubService> stations) {
this.baseURL = Objects.requireNonNull(baseURL);
this.epg = Objects.requireNonNull(epg);
this.bouquet = bouquet;
this.stations = stations;
isRunning = false;
thread = null;
Expand All @@ -36,6 +47,10 @@ void setLeadTime(int leadTime_s) {
this.leadTime_s = leadTime_s;
}

void setRangeTime(int rangeTime_s) {
this.rangeTime_s = rangeTime_s;
}

JButton getButton() {
return button;
}
Expand All @@ -50,6 +65,50 @@ private void loadEPG(String baseURL) {

long now_ms = System.currentTimeMillis();

if (stations!=null) scanEPGbyStations(baseURL, now_ms);
if (bouquet !=null) scanEPGbyTimeBlocks(baseURL, now_ms);


synchronized (this) {
isRunning = false;
button.setText("Load EPG");
button.setEnabled(true);
}
}

private void scanEPGbyTimeBlocks(String baseURL, long now_ms)
{
int blockSize_mins = 400;
int overlap_mins = 20;

System.out.printf("Scan EPG for Bouquet \"%s\": %d min - %d min%n", bouquet.name, -leadTime_s/60, rangeTime_s/60);
for (int blockStart_s = -leadTime_s; blockStart_s < rangeTime_s; blockStart_s += (blockSize_mins-overlap_mins)*60)
{
int blockStart_mins = blockStart_s/60;
int blockEnd_mins = blockStart_mins + blockSize_mins;

boolean isInterrupted = Thread.currentThread().isInterrupted();
System.out.printf("EPG for Bouquet \"%s\" (%d min - %d min)%s%n", bouquet.name, blockStart_mins, blockEnd_mins, isInterrupted ? " -> omitted" : "");
if (isInterrupted) continue;

long beginTime_UnixTS = now_ms/1000 + blockStart_s;
long endTime_Minutes = blockSize_mins;
epg.readEPGforBouquet(baseURL, bouquet, beginTime_UnixTS, endTime_Minutes, taskTitle->{
SwingUtilities.invokeLater(()->{
setStatusOutput(String.format("EPG for Bouquet \"%s\" (%d min - %d min): %s", bouquet.name, blockStart_mins, blockEnd_mins, taskTitle));
});
});
updateEPGView();
SwingUtilities.invokeLater(this::reconfigureHorizScrollBar);
}
System.out.println("... done");
SwingUtilities.invokeLater(()->{
setStatusOutput("");
});
}

private void scanEPGbyStations(String baseURL, long now_ms)
{
for (SubService subservice:stations)
if (!subservice.isMarker()) {
boolean isInterrupted = Thread.currentThread().isInterrupted();
Expand All @@ -68,13 +127,6 @@ private void loadEPG(String baseURL) {
SwingUtilities.invokeLater(()->{
setStatusOutput("");
});


synchronized (this) {
isRunning = false;
button.setText("Load EPG");
button.setEnabled(true);
}
}

synchronized boolean isRunning() {
Expand Down

0 comments on commit 13553ba

Please sign in to comment.