def discover(scope=None) -> List: """Discover cameras on network using onvif discovery. Returns: List: List of ips found in network. """ # Get the scopes from the IPs returned by the bash command `hostname -I`. if (scope == None): ips = list() for iface in netifaces.interfaces(): if (netifaces.AF_INET in netifaces.ifaddresses(iface)): ips.append( netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr']) scope = ['.'.join(ip.split('.')[:2]) for ip in ips] # Run WSDiscovery to search the IP from the cameras. wsd = WSDiscovery() wsd.start() ret = wsd.searchServices() wsd.stop() # Get just the services from onvif cameras. onvif_services = [s for s in ret if str(s.getTypes()).find('onvif') >= 0] # Extract the IPs of the onvif cameras. urls = [ip for s in onvif_services for ip in s.getXAddrs()] ips = [ip for url in urls for ip in re.findall(r'\d+\.\d+\.\d+\.\d+', url)] # Return a list with the IPs that correspond to the scope. lst = [ip for ip in ips if any(ip.startswith(sp) for sp in scope)] return sorted(lst)
def wsdiscovery() -> list[Service]: """Get ONVIF Profile S devices from network.""" discovery = WSDiscovery(ttl=4) discovery.start() services = discovery.searchServices( scopes=[Scope("onvif://www.onvif.org/Profile/Streaming")]) discovery.stop() return services
def discover_cameras(scope=None, types='NetworkVideoTransmitter', capture=None): wsd = WSDiscovery(capture=capture) wsd.start() namespace = "http://www.onvif.org/ver10/network/wsdl" timeout = 10 if not scope and not types: svcs = wsd.searchServices(timeout=timeout) elif scope and not types: # we support giving just one scope, for now svcs = wsd.searchServices(scopes=[Scope(scope)], timeout=timeout) elif not scope and types: # we support giving just one scope, for now probetypes = [QName(namespace, types)] svcs = wsd.searchServices(types=probetypes, timeout=timeout) else: # we support giving just one scope, for now probetypes = [QName(namespace, types)] svcs = wsd.searchServices(scopes=[Scope(scope)], types=probetypes, timeout=timeout) cameras = [] for service in svcs: camera = {} url = urlparse(service.getXAddrs()[0]) port = url.port if not port: port = 80 camera['hostname'] = url.hostname camera['port'] = port camera['username'] = url.username camera['password'] = url.password #scopes = service.getScopes() #camera['scopes'] = scopes cameras.append(camera) wsd.stop() if len(cameras) > 0: print("Found {} cameras".format(len(cameras))) for camera in cameras: print(" {}:{}".format(camera['hostname'], camera['port'])) else: print("Did not find any cameras") return cameras
def fetch_devices(): wsd = WSDiscovery() scope1 = Scope("onvif://www.onvif.org/") ttype1 = QName("http://www.onvif.org/ver10/device/wsdl", "Device") # ttype2 = Qname("http://www.onvif.org/ver10/media/wsdl","Media") wsd.start() services = wsd.searchServices(types=[ttype1], scopes=[scope1], timeout=6) # services = wsd.searchServices(scopes=[scope1]) ipaddresses = [] for service in services: #filter those devices that dont have ONVIF service ipaddress = re.search('(\d+|\.)+', str(service.getXAddrs()[0])).group(0) ipaddresses.append(ipaddress) print(f'\nnumber of devices detected: {len(services)}') wsd.stop() return ipaddresses
def discovery(capture=None): wsd = WSDiscovery(capture=capture) wsd.start() yield wsd wsd.stop()
tempDict["serial"] = i[3] except: pass out_list.append(tempDict) return out_list ttype1 = QName("http://www.onvif.org/ver10/device/wsdl", "Device") scope1 = Scope("onvif://www.onvif.org/Model") xAddr1 = "localhost:8080/abc" wsp = WSPublishing() wsp.start() wsp.publishService(types=[ttype1], scopes=[scope1], xAddrs=[xAddr1]) wsd = WSDiscovery() wsd.start() services = wsd.searchServices() adrArr = [] for service in services: if service.getXAddrs()[0] != xAddr1: adr = service.getXAddrs()[0] adrArr.append(adr) wsd.stop() infoList = [] for i in adrArr: try: # IPC session = requests.session() session.auth = HTTPBasicAuth("admin", "") # msg = """<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetScopes xmlns="http://www.onvif.org/ver10/device/wsdl"/></s:Body></s:Envelope>"""
old_devices = {'ssdp': get_saved_peripherals(API_URL, 'SSDP'), 'ws-discovery': get_saved_peripherals(API_URL, 'WS-Discovery'), 'zeroconf': get_saved_peripherals(API_URL, 'Bonjour/Avahi')} logging.info(f'Peripherals registered from the previous run: {old_devices}') try: zeroconf = Zeroconf() except OSError as ex: logging.error(f'Zeroconf failed to start and cannot be fixed without a restart: {str(ex)}') zeroconf = zeroconf_listener = None else: zeroconf_listener = ZeroConfListener() wsdaemon = WSDiscovery() while True: current_devices = network_manager(zeroconf, zeroconf_listener, wsdaemon) logging.info('CURRENT DEVICES: {}'.format(current_devices)) for protocol in current_devices: if current_devices[protocol] != old_devices[protocol]: old_devices_set = set(old_devices[protocol].keys()) current_devices_set = set(current_devices[protocol].keys()) publishing = current_devices_set - old_devices_set removing = old_devices_set - current_devices_set