#! /usr/bin/env python
# -*- Encoding: utf-8 -*-

"""
Module to control the dome IP webcams

Hack to remotely control the IR leds and other parameters


2018-05-14 LLG, ES, FH
"""

import sys
import os, os.path

import requests
from requests.auth import HTTPDigestAuth

default_host = '192.168.200.177'
default_login = "admin"
default_password = "dicedice"



def IRled(host = default_host, on = False, login = default_login, password = default_password):
    """
    Method to turn ON/OFF the IR LEDs of the WansView IP Webcam.
    """

    keyword = 'close'
    if on:
        keyword = 'auto'
        
    url = "http://%s/hy-cgi/irctrl.cgi?cmd=setinfrared&infraredstatus=%s&cmd=setircutctrl&ircutctrlstatus=auto&cmd=setirparams&irparams=100" % (host, keyword)
    try:
        rep = requests.get(url, auth=HTTPDigestAuth(login, password))
    except:
        print(("Could not reach the webcams, switch the IR LEDs %s using your browser" % ('ON' if on else 'OFF')))
        return False

    if rep.status_code != 200: # 200 = OK
        return False

    return True


