001 package com.github.sarxos.webcam.ds.ipcam; 002 003 import java.net.MalformedURLException; 004 import java.net.URL; 005 import java.util.ArrayList; 006 import java.util.Collections; 007 import java.util.Iterator; 008 import java.util.List; 009 010 import com.github.sarxos.webcam.Webcam; 011 import com.github.sarxos.webcam.WebcamDevice; 012 import com.github.sarxos.webcam.WebcamException; 013 014 015 /** 016 * Class used to register IP camera devices. 017 * 018 * @author Bartosz Firyn (SarXos) 019 */ 020 public class IpCamDeviceRegistry { 021 022 /** 023 * Contains IP cameras. 024 */ 025 private static final List<IpCamDevice> DEVICES = new ArrayList<IpCamDevice>(); 026 027 /** 028 * Register IP camera. 029 * 030 * @param ipcam the IP camera to be register 031 */ 032 public static IpCamDevice register(IpCamDevice ipcam) { 033 034 for (WebcamDevice d : DEVICES) { 035 String name = ipcam.getName(); 036 if (d.getName().equals(name)) { 037 throw new WebcamException(String.format("Webcam with name '%s' is already registered", name)); 038 } 039 } 040 041 DEVICES.add(ipcam); 042 043 // run discovery service once to trigger new webcam discovery event 044 // and keep webcams list up-to-date 045 046 Webcam.getDiscoveryService().scan(); 047 048 return ipcam; 049 } 050 051 public static IpCamDevice register(String name, String url, IpCamMode mode) throws MalformedURLException { 052 return register(new IpCamDevice(name, url, mode)); 053 } 054 055 public static IpCamDevice register(String name, URL url, IpCamMode mode) { 056 return register(new IpCamDevice(name, url, mode)); 057 } 058 059 public static IpCamDevice register(String name, String url, IpCamMode mode, IpCamAuth auth) throws MalformedURLException { 060 return register(new IpCamDevice(name, url, mode, auth)); 061 } 062 063 public static IpCamDevice register(String name, URL url, IpCamMode mode, IpCamAuth auth) { 064 return register(new IpCamDevice(name, url, mode, auth)); 065 } 066 067 public static boolean isRegistered(IpCamDevice ipcam) { 068 Iterator<IpCamDevice> di = DEVICES.iterator(); 069 while (di.hasNext()) { 070 if (di.next().getName().equals(ipcam.getName())) { 071 return true; 072 } 073 } 074 return false; 075 } 076 077 public static boolean isRegistered(String name) { 078 Iterator<IpCamDevice> di = DEVICES.iterator(); 079 while (di.hasNext()) { 080 if (di.next().equals(name)) { 081 return true; 082 } 083 } 084 return false; 085 } 086 087 public static boolean isRegistered(URL url) { 088 for (IpCamDevice d : DEVICES) { 089 if (d.getURL().equals(url)) { 090 return true; 091 } 092 } 093 return false; 094 } 095 096 /** 097 * Unregister IP camera. 098 * 099 * @param ipcam the IP camera to be unregister 100 */ 101 public static boolean unregister(IpCamDevice ipcam) { 102 boolean removed = DEVICES.remove(ipcam); 103 104 // run discovery service once if device has been removed to 105 // trigger disconnected webcam discovery event and keep webcams 106 // list up-to-date 107 108 if (removed) { 109 Webcam.getDiscoveryService().scan(); 110 } 111 112 return removed; 113 } 114 115 /** 116 * Unregister IP camera with given name. 117 * 118 * @param ipcam the IP camera to be unregister 119 */ 120 public static boolean unregister(String name) { 121 Iterator<IpCamDevice> di = DEVICES.iterator(); 122 while (di.hasNext()) { 123 IpCamDevice d = di.next(); 124 if (d.getName().equals(name)) { 125 di.remove(); 126 Webcam.getDiscoveryService().scan(); 127 return true; 128 } 129 } 130 return false; 131 } 132 133 /** 134 * Get all registered IP cameras. 135 * 136 * @return Collection of registered IP cameras 137 */ 138 public static List<IpCamDevice> getIpCameras() { 139 return Collections.unmodifiableList(DEVICES); 140 } 141 }