Skip to content

Commit 85a89fe

Browse files
feat: network settings and camera name (#82)
1 parent b86642c commit 85a89fe

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

Diff for: examples/basic_usage.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
dst = cam.get_dst()
1010
ok = cam.add_user("foo", "bar", "admin")
1111
alarm = cam.get_alarm_motion()
12+
cam.set_device_name(name='my_camera')

Diff for: examples/network_config.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import os
2+
from configparser import RawConfigParser
3+
from reolinkapi import Camera
4+
5+
6+
def read_config(props_path: str) -> dict:
7+
"""Reads in a properties file into variables.
8+
9+
NB! this config file is kept out of commits with .gitignore. The structure of this file is such:
10+
# secrets.cfg
11+
[camera]
12+
ip={ip_address}
13+
username={username}
14+
password={password}
15+
"""
16+
config = RawConfigParser()
17+
assert os.path.exists(props_path), f"Path does not exist: {props_path}"
18+
config.read(props_path)
19+
return config
20+
21+
22+
# Read in your ip, username, & password
23+
# (NB! you'll likely have to create this file. See tests/test_camera.py for details on structure)
24+
config = read_config('camera.cfg')
25+
26+
ip = config.get('camera', 'ip')
27+
un = config.get('camera', 'username')
28+
pw = config.get('camera', 'password')
29+
30+
# Connect to camera
31+
cam = Camera(ip, un, pw)
32+
33+
# Get current network settings
34+
current_settings = cam.get_network_general()
35+
print("Current settings:", current_settings)
36+
37+
# Configure DHCP
38+
cam.set_network_settings(
39+
ip="",
40+
gateway="",
41+
mask="",
42+
dns1="",
43+
dns2="",
44+
mac=current_settings[0]['value']['LocalLink']['mac'],
45+
use_dhcp=True,
46+
auto_dns=True
47+
)
48+
49+
# Configure static IP
50+
# cam.set_network_settings(
51+
# ip="192.168.1.102",
52+
# gateway="192.168.1.1",
53+
# mask="255.255.255.0",
54+
# dns1="8.8.8.8",
55+
# dns2="8.8.4.4",
56+
# mac=current_settings[0]['value']['LocalLink']['mac'],
57+
# use_dhcp=False,
58+
# auto_dns=False
59+
# )

Diff for: reolinkapi/mixins/device.py

+19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ class DeviceAPIMixin:
55
"""API calls for getting device information."""
66
DEFAULT_HDD_ID = [0]
77

8+
def set_device_name(self, name: str) -> bool:
9+
"""
10+
Set the device name of the camera.
11+
:param name: The new name for the device
12+
:return: bool indicating success
13+
"""
14+
body = [{"cmd": "SetDevName", "action": 0, "param": {"DevName": {"name": name}}}]
15+
self._execute_command('SetDevName', body)
16+
print(f"Successfully set device name to: {name}")
17+
return True
18+
19+
def get_device_name(self) -> Dict:
20+
"""
21+
Get the device name of the camera.
22+
:return: Dict containing the device name
23+
"""
24+
body = [{"cmd": "GetDevName", "action": 0, "param": {}}]
25+
return self._execute_command('GetDevName', body)
26+
827
def get_hdd_info(self) -> Dict:
928
"""
1029
Gets all HDD and SD card information from Camera

Diff for: reolinkapi/mixins/network.py

+36
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,42 @@
33

44
class NetworkAPIMixin:
55
"""API calls for network settings."""
6+
def set_network_settings(self, ip: str, gateway: str, mask: str, dns1: str, dns2: str, mac: str,
7+
use_dhcp: bool = True, auto_dns: bool = True) -> Dict:
8+
"""
9+
Set network settings including IP, gateway, subnet mask, DNS, and connection type (DHCP or Static).
10+
11+
:param ip: str
12+
:param gateway: str
13+
:param mask: str
14+
:param dns1: str
15+
:param dns2: str
16+
:param mac: str
17+
:param use_dhcp: bool
18+
:param auto_dns: bool
19+
:return: Dict
20+
"""
21+
body = [{"cmd": "SetLocalLink", "action": 0, "param": {
22+
"LocalLink": {
23+
"dns": {
24+
"auto": 1 if auto_dns else 0,
25+
"dns1": dns1,
26+
"dns2": dns2
27+
},
28+
"mac": mac,
29+
"static": {
30+
"gateway": gateway,
31+
"ip": ip,
32+
"mask": mask
33+
},
34+
"type": "DHCP" if use_dhcp else "Static"
35+
}
36+
}}]
37+
38+
return self._execute_command('SetLocalLink', body)
39+
print("Successfully Set Network Settings")
40+
return True
41+
642
def set_net_port(self, http_port: float = 80, https_port: float = 443, media_port: float = 9000,
743
onvif_port: float = 8000, rtmp_port: float = 1935, rtsp_port: float = 554) -> bool:
844
"""

0 commit comments

Comments
 (0)