Last active
March 22, 2019 01:43
-
-
Save DennisTT/0ac8acde8edcf2d1ade33f232283a8b3 to your computer and use it in GitHub Desktop.
Synology Surveillance Station - Move Camera to PTZ Preset Depending on Home Mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Scroll to the bottom and change your IP address (if running the script from another host), Surveillance Station account and password, | |
the camera name and presets. It shouldn't be too hard to add some other cameras and presets if you know a bit of programming. | |
Add a task in DSM Task Scheduler to execute this file every minute. | |
In case you want to extend this script further to do other stuff, the complete documentation for the Surveillance Station Web API can be found at: | |
https://global.download.synology.com/download/Document/DeveloperGuide/Surveillance_Station_Web_API_v2.8.pdf | |
""" | |
import logging | |
import requests | |
import urllib | |
logging.basicConfig(level=logging.INFO) | |
class ApiException(Exception): | |
def __init__(self, error_code, data): | |
super(ApiException, self).__init__("API returned error code {error_code}: {data}".format(error_code=error_code, data=data)) | |
self.error_code = error_code | |
self.data = data | |
class SurveillanceStationSession(object): | |
def __init__(self, url_base, account, passwd): | |
super(SurveillanceStationSession, self).__init__() | |
self.url_base = url_base | |
self.api_session = requests.Session() | |
self.__api_paths = {} | |
self.auth(account=account, passwd=passwd) | |
def __get(self, api_path, params): | |
querystring = urllib.parse.urlencode(params) | |
url = "{url_base}{api_path}?{querystring}".format(url_base=self.url_base, api_path=api_path, querystring=querystring) | |
logging.debug("Request: {url}".format(url=url)) | |
request = self.api_session.get(url) | |
logging.debug("Response: {response}".format(response=request.text)) | |
result = request.json() | |
success = result['success'] | |
if success: | |
return result.get('data') | |
else: | |
raise ApiException(error_code=result['error'].get('code'), data=result['error']) | |
def __get_api_path(self, api): | |
if api not in self.__api_paths: | |
result = self.__get('query.cgi', { | |
'api': 'SYNO.API.Info', | |
'method': 'Query', | |
'version': '1', | |
'query': api, | |
}) | |
path = result.get(api, {}).get('path') | |
self.__api_paths[api] = path | |
return self.__api_paths[api] | |
def api(self, api, method, version, **kwargs): | |
api_path = self.__get_api_path(api=api) | |
params = dict(kwargs) | |
params['api'] = api | |
params['method'] = method | |
params['version'] = version | |
result = self.__get(api_path=api_path, params=params) | |
return result | |
def auth(self, account, passwd): | |
self.api(api='SYNO.API.Auth', method='Login', version=6, | |
account=account, | |
passwd=passwd, | |
session='SurveillanceStation', | |
format='cookie') | |
def is_home_mode_enabled(self): | |
result = self.api(api='SYNO.SurveillanceStation.HomeMode', method='GetInfo', version=1) | |
return result['on'] | |
def get_camera_list(self): | |
result = self.api(api='SYNO.SurveillanceStation.Camera', method='List', version=9) | |
return result['cameras'] | |
def get_camera_id(self, name): | |
camera_list = self.get_camera_list() | |
for camera in camera_list: | |
if camera['newName'] == name: | |
return camera['id'] | |
raise ValueError("Camera {name} does not exist".format(name=name)) | |
def get_ptz_preset_list(self, camera_id): | |
result = self.api(api='SYNO.SurveillanceStation.PTZ', method='ListPreset', version=1, | |
cameraId=camera_id) | |
return result['presets'] | |
def get_ptz_preset_id(self, camera_id, preset_name): | |
preset_list = self.get_ptz_preset_list(camera_id=camera_id) | |
for preset in preset_list: | |
if preset['name'] == preset_name: | |
return preset['id'] | |
raise ValueError("PTZ preset {preset_name} does not exist on camera {camera_id}".format(preset_name=preset_name, camera_id=camera_id)) | |
def move_camera_to_ptz_preset(self, camera_name, preset_name): | |
camera_id = self.get_camera_id(name=camera_name) | |
preset_id = self.get_ptz_preset_id(camera_id=camera_id, preset_name=preset_name) | |
self.api(api='SYNO.SurveillanceStation.PTZ', method='GoPreset', version=1, | |
cameraId=camera_id, | |
presetId=preset_id) | |
session = SurveillanceStationSession( | |
url_base="http://127.0.0.1:5000/webapi/", | |
account="********", | |
passwd="********" | |
) | |
camera_name = '<< YOUR CAMERA NAME >>' | |
if session.is_home_mode_enabled(): | |
logging.info("Home Mode is enabled") | |
session.move_camera_to_ptz_preset(camera_name=camera_name, preset_name='<< PRESET NAME >>') | |
else: | |
logging.info("Home Mode is disabled") | |
session.move_camera_to_ptz_preset(camera_name=camera_name, preset_name='<< OTHER PRESET NAME >>') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment