001package silicondust.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * TV H/W, EPGs, and Recording
005 * $Id: HDHRPrimeClientTask.java 5999 2020-05-18 16:41:28Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/silicondust/trunk/src/main/java/silicondust/ant/taskdefs/HDHRPrimeClientTask.java $
007 * %%
008 * Copyright (C) 2013 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import ball.http.ant.taskdefs.HTTPTask;
024import ball.util.ant.taskdefs.AntTask;
025import ball.util.ant.taskdefs.NotNull;
026import java.io.File;
027import java.io.FileOutputStream;
028import java.net.InetAddress;
029import lombok.Getter;
030import lombok.NoArgsConstructor;
031import lombok.Setter;
032import lombok.ToString;
033import org.apache.tools.ant.BuildException;
034import silicondust.HDHRPrimeClient;
035import silicondust.LineupEntry;
036
037import static lombok.AccessLevel.PROTECTED;
038
039/**
040 * Abstract {@link.uri http://ant.apache.org/ Ant} base
041 * {@link org.apache.tools.ant.Task} for tasks that invoke
042 * {@link HDHRPrimeClient} methods.
043 *
044 * {@ant.task}
045 *
046 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
047 * @version $Revision: 5999 $
048 */
049@NoArgsConstructor(access = PROTECTED)
050public abstract class HDHRPrimeClientTask extends HTTPTask {
051    @NotNull @Getter
052    private InetAddress address = null;
053    protected HDHRPrimeClient client = null;
054
055    public void setAddress(String address) throws BuildException {
056        try {
057            this.address = InetAddress.getByName(address);
058        } catch (BuildException exception) {
059            throw exception;
060        } catch (RuntimeException exception) {
061            throw exception;
062        } catch (Throwable throwable) {
063            throw new BuildException(throwable);
064        }
065    }
066
067    @Override
068    public void execute() throws BuildException {
069        super.execute();
070
071        try {
072            client = new HDHRPrimeClientImpl();
073        } catch (BuildException exception) {
074            throw exception;
075        } catch (RuntimeException exception) {
076            throw exception;
077        } catch (Throwable throwable) {
078            throw new BuildException(throwable);
079        }
080    }
081
082    /**
083     * {@link.uri http://ant.apache.org/ Ant}
084     * {@link org.apache.tools.ant.Task} to invoke
085     * {@link HDHRPrimeClient#getLineup()}.
086     *
087     * {@ant.task}
088     */
089    @AntTask("hdhr-lineup")
090    @NoArgsConstructor @ToString
091    public static class Lineup extends HDHRPrimeClientTask {
092        @Override
093        public void execute() throws BuildException {
094            super.execute();
095
096            try {
097                for (LineupEntry entry : client.getLineup()) {
098                    log(String.valueOf(entry));
099                }
100            } catch (BuildException exception) {
101                throw exception;
102            } catch (RuntimeException exception) {
103                throw exception;
104            } catch (Throwable throwable) {
105                throw new BuildException(throwable);
106            }
107        }
108    }
109
110    /**
111     * {@link.uri http://ant.apache.org/ Ant}
112     * {@link org.apache.tools.ant.Task} to invoke
113     * {@link HDHRPrimeClient#stream(Integer,Integer,Integer)} and save
114     * the stream to a {@link File}.
115     *
116     * {@ant.task}
117     */
118    @AntTask("hdhr-record")
119    @NoArgsConstructor @ToString
120    public static class Record extends HDHRPrimeClientTask {
121        @Getter @Setter
122        private Integer tuner = null;
123        @NotNull @Getter @Setter
124        private Integer channel = null;
125        @NotNull @Getter @Setter
126        private Integer duration = null;
127        @NotNull @Getter @Setter
128        private File file = null;
129
130        @Override
131        public void execute() throws BuildException {
132            super.execute();
133
134            try (FileOutputStream out = new FileOutputStream(getFile())) {
135                client
136                    .stream(getTuner(), getChannel(), getDuration())
137                    .writeTo(out);
138            } catch (BuildException exception) {
139                throw exception;
140            } catch (RuntimeException exception) {
141                throw exception;
142            } catch (Throwable throwable) {
143                throw new BuildException(throwable);
144            }
145        }
146    }
147
148    private class HDHRPrimeClientImpl extends HDHRPrimeClient {
149        public HDHRPrimeClientImpl() {
150            super(builder(),
151                  HDHRPrimeClientTask.this.getAddress().getHostAddress());
152        }
153    }
154}