Added User management and osd

This commit is contained in:
Alano Terblanche
2019-08-11 13:24:35 +02:00
parent 696b3ac58f
commit f23c5e8c29

View File

@@ -32,11 +32,11 @@ class APIHandler:
# Token # Token
def login(self): def login(self) -> bool:
""" """
Get login token Get login token
Must be called first, before any other operation can be performed Must be called first, before any other operation can be performed
:return: :return: bool
""" """
try: try:
body = [{"cmd": "Login", "action": 0, body = [{"cmd": "Login", "action": 0,
@@ -49,9 +49,12 @@ class APIHandler:
if int(code) == 0: if int(code) == 0:
self.token = data["value"]["Token"]["name"] self.token = data["value"]["Token"]["name"]
print("Login success") print("Login success")
return True
print(self.token) print(self.token)
return False
else: else:
print("Failed to login\nStatus Code:", response.status_code) print("Failed to login\nStatus Code:", response.status_code)
return False
except Exception as e: except Exception as e:
print("Error Login\n", e) print("Error Login\n", e)
raise raise
@@ -63,43 +66,48 @@ class APIHandler:
########### ###########
# SET Network # SET Network
########### ###########
def set_net_port(self, httpPort=80, httpsPort=443, mediaPort=9000, onvifPort=8000, rtmpPort=1935, rtspPort=554): def set_net_port(self, http_port=80, https_port=443, media_port=9000, onvif_port=8000, rtmp_port=1935,
rtsp_port=554) -> bool:
""" """
Set network ports Set network ports
If nothing is specified, the default values will be used If nothing is specified, the default values will be used
:param httpPort: :param rtsp_port: int
:param httpsPort: :param rtmp_port: int
:param mediaPort: :param onvif_port: int
:param onvifPort: :param media_port: int
:param rtmpPort: :param https_port: int
:param rtspPort: :type http_port: int
:return: :return: bool
""" """
try: try:
if self.token is None: if self.token is None:
raise ValueError("Login first") raise ValueError("Login first")
body = [{"cmd": "SetNetPort", "action": 0, "param": {"NetPort": { body = [{"cmd": "SetNetPort", "action": 0, "param": {"NetPort": {
"httpPort": httpPort, "httpPort": http_port,
"httpsPort": httpsPort, "httpsPort": https_port,
"mediaPort": mediaPort, "mediaPort": media_port,
"onvifPort": onvifPort, "onvifPort": onvif_port,
"rtmpPort": rtmpPort, "rtmpPort": rtmp_port,
"rtspPort": rtspPort "rtspPort": rtsp_port
}}}] }}}]
param = {"token": self.token} param = {"token": self.token}
response = Request.post(self.url, data=body, params=param) response = Request.post(self.url, data=body, params=param)
if response is not None: if response is not None:
if response.status_code == 200: if response.status_code == 200:
print("Successfully Set Network Ports") print("Successfully Set Network Ports")
return True
else: else:
print("Something went wront\nStatus Code:", response.status_code) print("Something went wront\nStatus Code:", response.status_code)
return False
return False
except Exception as e: except Exception as e:
print("Setting Network Port Error\n", e) print("Setting Network Port Error\n", e)
raise raise
def set_wifi(self, ssid, password): def set_wifi(self, ssid, password) -> json or None:
try: try:
if self.token is None: if self.token is None:
raise ValueError("Login first") raise ValueError("Login first")
@@ -185,18 +193,119 @@ class APIHandler:
########### ###########
# GET # GET
########### ###########
def get_general_system(self): def get_general_system(self) -> json or None:
try: try:
if self.token is None: if self.token is None:
raise ValueError("Login first") raise ValueError("Login first")
body = [{"cmd": "GetTime", "action": 1, "param": {}}, {"cmd": "GetNorm", "action": 1, "param": {}}] body = [{"cmd": "GetTime", "action": 1, "param": {}}, {"cmd": "GetNorm", "action": 1, "param": {}}]
param = {"token": self.token} param = {"token": self.token}
response = Request.post(self.url, data=body, params=param) response = Request.post(self.url, data=body, params=param)
if response.status_code == 200:
return json.loads(response.text) return json.loads(response.text)
print("Could not retrieve general information from camera successfully. Status:", response.status_code)
return None
except Exception as e: except Exception as e:
print("Could not get General System settings\n", e) print("Could not get General System settings\n", e)
raise raise
def get_osd(self) -> json or None:
try:
param = {"cmd": "GetOsd", "token": self.token}
body = [{"cmd": "GetOsd", "action": 1, "param": {"channel": 0}}]
response = Request.post(self.url, data=body, params=param)
if response.status_code == 200:
return json.loads(response.text)
print("Could not retrieve OSD from camera successfully. Status:", response.status_code)
return None
except Exception as e:
print("Could not get OSD", e)
raise
##########
# User
##########
##########
# GET
##########
def get_online_user(self) -> json or None:
try:
param = {"cmd": "GetOnline", "token": self.token}
body = [{"cmd": "GetOnline", "action": 1, "param": {}}]
response = Request.post(self.url, data=body, params=param)
if response.status_code == 200:
return json.loads(response.text)
print("Could not retrieve online user from camera. Status:", response.status_code)
return None
except Exception as e:
print("Could not get online user", e)
raise
def get_users(self) -> json or None:
try:
param = {"cmd": "GetUser", "token": self.token}
body = [{"cmd": "GetUser", "action": 1, "param": {}}]
response = Request.post(self.url, data=body, params=param)
if response.status_code == 200:
return json.loads(response.text)
print("Could not retrieve users from camera. Status:", response.status_code)
return None
except Exception as e:
print("Could not get users", e)
raise
##########
# SET
##########
def add_user(self, username: str, password: str, level: str = "guest") -> bool:
try:
param = {"cmd": "AddUser", "token": self.token}
body = [{"cmd": "AddUser", "action": 0,
"param": {"User": {"userName": username, "password": password, "level": level}}}]
response = Request.post(self.url, data=body, params=param)
if response.status_code == 200:
r_data = json.loads(response.text)
if r_data["value"]["rspCode"] == "200":
return True
print("Could not add user. Camera responded with:", r_data["value"])
return False
print("Something went wrong. Could not add user. Status:", response.status_code)
return False
except Exception as e:
print("Could not add user", e)
raise
def modify_user(self, username: str, password: str) -> bool:
try:
param = {"cmd": "ModifyUser", "token": self.token}
body = [{"cmd": "ModifyUser", "action": 0, "param": {"User": {"userName": username, "password": password}}}]
response = Request.post(self.url, data=body, params=param)
if response.status_code == 200:
r_data = json.loads(response.text)
if r_data["value"]["rspCode"] == "200":
return True
print("Could not modify user:", username, "\nCamera responded with:", r_data["value"])
print("Something went wrong. Could not modify user. Status:", response.status_code)
return False
except Exception as e:
print("Could not modify user", e)
raise
def delete_user(self, username: str) -> bool:
try:
param = {"cmd": "DelUser", "token": self.token}
body = [{"cmd": "DelUser", "action": 0, "param": {"User": {"userName": username}}}]
response = Request.post(self.url, data=body, params=param)
if response.status_code == 200:
r_data = json.loads(response.text)
if r_data["value"]["rspCode"] == "200":
return True
print("Could not delete user:", username, "\nCamera responded with:", r_data["value"])
return False
except Exception as e:
print("Could not delete user", e)
raise
########## ##########
# Image Data # Image Data
########## ##########
@@ -222,4 +331,3 @@ class APIHandler:
except Exception as e: except Exception as e:
print("Could not get Image data\n", e) print("Could not get Image data\n", e)
raise raise