Skip to content

feat: network settings and camera name #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/basic_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
dst = cam.get_dst()
ok = cam.add_user("foo", "bar", "admin")
alarm = cam.get_alarm_motion()
cam.set_device_name(name='my_camera')
59 changes: 59 additions & 0 deletions examples/network_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
from configparser import RawConfigParser
from reolinkapi import Camera


def read_config(props_path: str) -> dict:
"""Reads in a properties file into variables.

NB! this config file is kept out of commits with .gitignore. The structure of this file is such:
# secrets.cfg
[camera]
ip={ip_address}
username={username}
password={password}
"""
config = RawConfigParser()
assert os.path.exists(props_path), f"Path does not exist: {props_path}"
config.read(props_path)
return config


# Read in your ip, username, & password
# (NB! you'll likely have to create this file. See tests/test_camera.py for details on structure)
config = read_config('camera.cfg')

ip = config.get('camera', 'ip')
un = config.get('camera', 'username')
pw = config.get('camera', 'password')

# Connect to camera
cam = Camera(ip, un, pw)

# Get current network settings
current_settings = cam.get_network_general()
print("Current settings:", current_settings)

# Configure DHCP
cam.set_network_settings(
ip="",
gateway="",
mask="",
dns1="",
dns2="",
mac=current_settings[0]['value']['LocalLink']['mac'],
use_dhcp=True,
auto_dns=True
)

# Configure static IP
# cam.set_network_settings(
# ip="192.168.1.102",
# gateway="192.168.1.1",
# mask="255.255.255.0",
# dns1="8.8.8.8",
# dns2="8.8.4.4",
# mac=current_settings[0]['value']['LocalLink']['mac'],
# use_dhcp=False,
# auto_dns=False
# )
19 changes: 19 additions & 0 deletions reolinkapi/mixins/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ class DeviceAPIMixin:
"""API calls for getting device information."""
DEFAULT_HDD_ID = [0]

def set_device_name(self, name: str) -> bool:
"""
Set the device name of the camera.
:param name: The new name for the device
:return: bool indicating success
"""
body = [{"cmd": "SetDevName", "action": 0, "param": {"DevName": {"name": name}}}]
self._execute_command('SetDevName', body)
print(f"Successfully set device name to: {name}")
return True

def get_device_name(self) -> Dict:
"""
Get the device name of the camera.
:return: Dict containing the device name
"""
body = [{"cmd": "GetDevName", "action": 0, "param": {}}]
return self._execute_command('GetDevName', body)

def get_hdd_info(self) -> Dict:
"""
Gets all HDD and SD card information from Camera
Expand Down
36 changes: 36 additions & 0 deletions reolinkapi/mixins/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@

class NetworkAPIMixin:
"""API calls for network settings."""
def set_network_settings(self, ip: str, gateway: str, mask: str, dns1: str, dns2: str, mac: str,
use_dhcp: bool = True, auto_dns: bool = True) -> Dict:
"""
Set network settings including IP, gateway, subnet mask, DNS, and connection type (DHCP or Static).

:param ip: str
:param gateway: str
:param mask: str
:param dns1: str
:param dns2: str
:param mac: str
:param use_dhcp: bool
:param auto_dns: bool
:return: Dict
"""
body = [{"cmd": "SetLocalLink", "action": 0, "param": {
"LocalLink": {
"dns": {
"auto": 1 if auto_dns else 0,
"dns1": dns1,
"dns2": dns2
},
"mac": mac,
"static": {
"gateway": gateway,
"ip": ip,
"mask": mask
},
"type": "DHCP" if use_dhcp else "Static"
}
}}]

return self._execute_command('SetLocalLink', body)
print("Successfully Set Network Settings")
return True

def set_net_port(self, http_port: float = 80, https_port: float = 443, media_port: float = 9000,
onvif_port: float = 8000, rtmp_port: float = 1935, rtsp_port: float = 554) -> bool:
"""
Expand Down