001package com.github.sarxos.webcam.ds.ipcam.device.zavio;
002
003import java.awt.Dimension;
004import java.net.MalformedURLException;
005import java.net.URL;
006
007import com.github.sarxos.webcam.WebcamException;
008import com.github.sarxos.webcam.ds.ipcam.IpCamDevice;
009import com.github.sarxos.webcam.ds.ipcam.IpCamMode;
010
011
012/**
013 * B7210 2M Bullet IP Camera from Zavio.
014 * 
015 * @author Bartosz Firyn (SarXos)
016 */
017public class B7210 extends IpCamDevice {
018
019        public static final Dimension SIZE_HD_1080 = new Dimension(1280, 1024);
020        public static final Dimension SIZE_43_960 = new Dimension(1280, 960);
021        public static final Dimension SIZE_QVGA = new Dimension(320, 240);
022
023        //@formatter:off
024        private static final Dimension[] SIZES = new Dimension[] { 
025                SIZE_HD_1080, 
026                SIZE_43_960, 
027                SIZE_QVGA, 
028        };
029        //@formatter:on
030
031        private URL base = null;
032
033        public B7210(String name, String urlBase) {
034                this(name, toURL(urlBase));
035        }
036
037        public B7210(String name, URL base) {
038                super(name, (URL) null, IpCamMode.PULL);
039                this.base = base;
040        }
041
042        @Override
043        public Dimension[] getResolutions() {
044                return SIZES;
045        }
046
047        @Override
048        public void setResolution(Dimension size) {
049
050                int index = -1;
051                for (int i = 0; i < SIZES.length; i++) {
052                        if (SIZES[i].equals(size)) {
053                                index = i;
054                                break;
055                        }
056                }
057
058                if (index == -1) {
059                        throw new IllegalArgumentException(String.format("Incorrect size %s", size));
060                }
061
062                super.setResolution(size);
063        }
064
065        @Override
066        public URL getURL() {
067
068                int index = -1;
069                for (int i = 0; i < SIZES.length; i++) {
070                        if (SIZES[i].equals(getResolution())) {
071                                index = i;
072                                break;
073                        }
074                }
075
076                int res = 0;
077                switch (index) {
078                        case 0:
079                                res = 0;
080                                break;
081                        case 1:
082                                res = 3;
083                                break;
084                        case 2:
085                                res = 4;
086                                break;
087                }
088
089                long time = System.currentTimeMillis();
090
091                String url = String.format("%s/cgi-bin/view/image?pro_%d&%d", base, res, time);
092                try {
093                        return new URL(url);
094                } catch (MalformedURLException e) {
095                        throw new WebcamException(String.format("Incorrect URL %s", url), e);
096                }
097        }
098
099}