/*
This file is part of JFLICKS.
JFLICKS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JFLICKS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JFLICKS. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jflicks.tv.recorder.hdhr;
import org.jflicks.job.JobContainer;
import org.jflicks.job.JobEvent;
import org.jflicks.job.JobManager;
import org.jflicks.job.SystemJob;
/**
* This job will set a HDHR device to a particular "program". A program
* is a particular stream on a frequency. For example, an OTA broadcaster
* has it's main channel and often one or more sub-channels. After the
* HDHR is set to a frequency, this job will tune it to a particular
* stream - either the main channel one of it's sub-channels.
*
* @author Doug Barnum
* @version 1.0
*/
public class StreamInfoJob extends BaseHDHRJob {
private String program;
private String programId;
/**
* Simple no argument constructor.
*/
public StreamInfoJob() {
setProgramId("3");
}
/**
* The program value is the digital channel name. For example "2.1" or
* "2.2".
*
* @return The program as a String.
*/
public String getProgram() {
return (program);
}
/**
* The program value is the digital channel name. For example "2.1" or
* "2.2".
*
* @param s The program as a String.
*/
public void setProgram(String s) {
program = s;
}
public String getProgramId() {
return (programId);
}
public void setProgramId(String s) {
programId = s;
fireJobEvent(JobEvent.UPDATE, "ProgramId: <" + s + ">");
}
/**
* {@inheritDoc}
*/
public void start() {
setTerminate(false);
}
/**
* {@inheritDoc}
*/
public void run() {
SystemJob job = SystemJob.getInstance("hdhomerun_config "
+ getId() + " get /tuner" + getTuner() + "/streaminfo");
fireJobEvent(JobEvent.UPDATE, "command: <" + job.getCommand() + ">");
setSystemJob(job);
job.addJobListener(this);
JobContainer jc = JobManager.getJobContainer(job);
setJobContainer(jc);
jc.start();
while (!isTerminate()) {
JobManager.sleep(getSleepTime());
}
fireJobEvent(JobEvent.COMPLETE);
}
/**
* {@inheritDoc}
*/
public void stop() {
setTerminate(true);
JobContainer jc = getJobContainer();
if (jc != null) {
jc.stop();
setJobContainer(null);
}
}
/**
* {@inheritDoc}
*/
public void jobUpdate(JobEvent event) {
if (event.getType() == JobEvent.COMPLETE) {
SystemJob job = getSystemJob();
if (job != null) {
fireJobEvent(JobEvent.UPDATE, "ProgramJob: exit: "
+ job.getExitValue());
String text = job.getOutputText();
if (text != null) {
String[] lines = text.split("\n");
if (lines != null) {
for (int i = 0; i < lines.length; i++) {
//if (lines[i].indexOf(getProgram()) != -1) {
if (isProgramLine(lines[i])) {
int cindex = lines[i].indexOf(":");
if (cindex != -1) {
setProgramId(lines[i].substring(0, cindex));
break;
}
}
}
}
}
stop();
}
}
}
private boolean isProgramLine(String line) {
boolean result = false;
if (line != null) {
int index = line.indexOf(":");
if (index != -1) {
// Get the "rest" of the line.
fireJobEvent(JobEvent.UPDATE, "line: <" + line + ">");
line = line.substring(index + 1);
fireJobEvent(JobEvent.UPDATE, "line now: <" + line + ">");
line = line.trim();
fireJobEvent(JobEvent.UPDATE, "line trim: <" + line + ">");
result = line.startsWith(getProgram());
fireJobEvent(JobEvent.UPDATE, "startsWith: <" + getProgram()
+ "> " + result);
}
}
return (result);
}
}