add camsutil
This commit is contained in:
parent
c754f48718
commit
82f12e7083
0
camsutil/__init__.py
Normal file
0
camsutil/__init__.py
Normal file
144
camsutil/ptz.py
Normal file
144
camsutil/ptz.py
Normal file
|
@ -0,0 +1,144 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
import urllib.request
|
||||
from os import path, sep
|
||||
|
||||
|
||||
class API:
|
||||
"""
|
||||
Creation of a request to the camera API based on the prepared template.
|
||||
"""
|
||||
def __init__(self, host: str, user: str, password: str, template: str,
|
||||
protocol: str = 'http', port: int = 80, channel: int = 101, vid: int = 1,
|
||||
x: int = 0, y: int = 0, z: int = 0, speed: int = 1, time: int = 100000,
|
||||
text: str = '', enabled: str = 'true'):
|
||||
"""
|
||||
Object constructor
|
||||
:param host: hostname or ip address
|
||||
:param user: valid user
|
||||
:param password: valid password
|
||||
:param template: the name of an existing template
|
||||
:param protocol: http, https, etc.
|
||||
:param port: port number
|
||||
:param channel: ptz channel number
|
||||
:param vid: video channel id
|
||||
:param x: horizontal positioning: azimuth, pan
|
||||
:param y: vertical positioning: elevation, tilt
|
||||
:param z: zoom direction, absolute zoom
|
||||
:param speed: positioning speed: from 1 to 7
|
||||
:param time: momentary duration, max 100000ms
|
||||
:param text: overlay text content
|
||||
:param enabled: enabled (true) or disabled (false) overlay text
|
||||
"""
|
||||
self._host = host
|
||||
self._user = user
|
||||
self._pswd = password
|
||||
self._temp = path.dirname(path.abspath(__file__)) + sep + 'templates' + sep + template
|
||||
self._protocol = protocol
|
||||
self._port = port
|
||||
self._channel = channel
|
||||
self._id = vid
|
||||
self._x = x
|
||||
self._y = y
|
||||
self._z = z
|
||||
self._speed = speed
|
||||
self._time = time
|
||||
self._message = text
|
||||
self._enabledMessage = enabled
|
||||
self._data = ''
|
||||
self._type = ''
|
||||
self._url = self._protocol + '://' + self._host + ':' + str(self._port)
|
||||
self._method = ''
|
||||
|
||||
with open(self._temp) as file:
|
||||
content = file.read() \
|
||||
.replace('@CHANNEL@', str(self._channel)) \
|
||||
.replace('@ID@', str(self._id)) \
|
||||
.replace('@XXXX@', str(self._x)) \
|
||||
.replace('@YYYY@', str(self._y)) \
|
||||
.replace('@ZZZZ@', str(self._z)) \
|
||||
.replace('@TTTT@', str(self._time)) \
|
||||
.replace('@SSSS@', str(self._speed)) \
|
||||
.replace('@MSG@', str(self._message)) \
|
||||
.replace('@ENABLED@', str(self._enabledMessage))
|
||||
for line in content.splitlines():
|
||||
if not ('<!--' in line):
|
||||
self._data += line + '\n'
|
||||
elif 'PATH:' in line:
|
||||
self._url += line.split(' ')[2]
|
||||
elif 'METHOD:' in line:
|
||||
self._method += line.split(' ')[2]
|
||||
elif 'TYPE:' in line:
|
||||
self._type += line.split(' ')[2]
|
||||
|
||||
def call(self):
|
||||
"""
|
||||
Call a request to the camera api
|
||||
:return: response from the camera api
|
||||
"""
|
||||
# Preparing authorization
|
||||
pswd = urllib.request.HTTPPasswordMgrWithDefaultRealm()
|
||||
pswd.add_password(None, self._url, self._user, self._pswd)
|
||||
auth = urllib.request.HTTPDigestAuthHandler(pswd)
|
||||
urllib.request.install_opener(urllib.request.build_opener(auth))
|
||||
|
||||
# Request
|
||||
request = urllib.request.Request(url=self._url, data=bytes(self._data.encode('utf-8')), method=self._method)
|
||||
request.add_header('Content-Type', self._type)
|
||||
|
||||
# Response
|
||||
response = urllib.request.urlopen(request).read()
|
||||
if response.startswith(b'\xff\xd8'):
|
||||
return response
|
||||
else:
|
||||
return str(response.decode('utf-8'))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from argparse import ArgumentParser
|
||||
|
||||
args = ArgumentParser(
|
||||
prog='PTZ',
|
||||
description='Creation of a request to the camera API based on the prepared template',
|
||||
epilog='Dependencies: Python 3 (tested version 3.9.5)'
|
||||
)
|
||||
args.add_argument('--host', type=str, required=True,
|
||||
help='hostname or ip address')
|
||||
args.add_argument('--user', type=str, required=True,
|
||||
help='valid user')
|
||||
args.add_argument('--password', type=str, required=True,
|
||||
help='valid password')
|
||||
args.add_argument('--template', type=str, required=True,
|
||||
help='the name of an existing template')
|
||||
args.add_argument('--protocol', type=str, default='http', required=False,
|
||||
help='http, https, etc.')
|
||||
args.add_argument('--port', type=int, default=80, required=False,
|
||||
help='port number')
|
||||
args.add_argument('--channel', type=int, default=101, required=False,
|
||||
help='ptz channel number')
|
||||
args.add_argument('--vid', type=int, default=1, required=False,
|
||||
help='video channel id')
|
||||
args.add_argument('--x', type=int, default=0, required=False,
|
||||
help='horizontal positioning: azimuth, pan')
|
||||
args.add_argument('--y', type=int, default=0, required=False,
|
||||
help='vertical positioning: elevation, tilt')
|
||||
args.add_argument('--z', type=int, default=0, required=False,
|
||||
help='zoom direction, absolute zoom')
|
||||
args.add_argument('--speed', type=int, default=1, required=False,
|
||||
help='positioning speed: from 1 to 7')
|
||||
args.add_argument('--time', type=int, default=100000, required=False,
|
||||
help='momentary duration, max 100000ms')
|
||||
args.add_argument('--text', type=str, default='', required=False,
|
||||
help='overlay text content')
|
||||
args.add_argument('--enabled', type=str, default='true', required=False,
|
||||
help='enabled (true) or disabled (false) overlay text')
|
||||
args = args.parse_args()
|
||||
|
||||
action = API(
|
||||
host=args.host, user=args.user, password=args.password, template=args.template,
|
||||
protocol=args.protocol, port=args.port, channel=args.channel, vid=args.vid,
|
||||
x=args.x, y=args.y, z=args.z, speed=args.speed, time=args.time,
|
||||
text=args.text, enabled=args.enabled
|
||||
)
|
||||
print(action.call())
|
3
camsutil/templates/Hikvision_GetCapabilities.html
Normal file
3
camsutil/templates/Hikvision_GetCapabilities.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
<!-- PATH: /ISAPI/PTZCtrl/channels/1/capabilities -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
3
camsutil/templates/Hikvision_GetJPEG.html
Normal file
3
camsutil/templates/Hikvision_GetJPEG.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
<!-- PATH: /Streaming/channels/@ID@/picture?snapShotImageType=JPEG&videoResolutionWidth=@XXXX@&videoResolutionHeight=@YYYY@ -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
3
camsutil/templates/Hikvision_GetPosition.html
Normal file
3
camsutil/templates/Hikvision_GetPosition.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
<!-- PATH: /ISAPI/PTZCtrl/channels/@CHANNEL@/status -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
3
camsutil/templates/Hikvision_GetReboot.html
Normal file
3
camsutil/templates/Hikvision_GetReboot.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
<!-- PATH: /System/reboot -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
12
camsutil/templates/Hikvision_GoAbsolute.xml
Normal file
12
camsutil/templates/Hikvision_GoAbsolute.xml
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<PTZData>
|
||||
<AbsoluteHigh>
|
||||
<elevation>@YYYY@</elevation>
|
||||
<azimuth>@XXXX@</azimuth>
|
||||
<absoluteZoom>@ZZZZ@</absoluteZoom>
|
||||
</AbsoluteHigh>
|
||||
</PTZData>
|
||||
<!-- PATH: /ISAPI/PTZCtrl/channels/@CHANNEL@/absolute -->
|
||||
<!-- METHOD: PUT -->
|
||||
<!-- TYPE: text/xml -->
|
||||
<!-- XXXX: 0..3600 YYYY: -900..2700 ZZZZ: 0..1000 -->
|
12
camsutil/templates/Hikvision_GoContinuous.xml
Normal file
12
camsutil/templates/Hikvision_GoContinuous.xml
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<PTZData>
|
||||
<pan>@XXXX@</pan>
|
||||
<tilt>@YYYY@</tilt>
|
||||
<zoom>@ZZZZ@</zoom>
|
||||
</PTZData>
|
||||
<!-- PATH: /ISAPI/PTZCtrl/channels/@CHANNEL@/continuous -->
|
||||
<!-- METHOD: PUT -->
|
||||
<!-- TYPE: text/xml -->
|
||||
<!-- XXXX: -100..100 YYYY: -100..100 ZZZZ: -100..100 -->
|
||||
<!-- x360* = 3300..10 sec x1* = 9,167..0,028 sec -->
|
||||
<!-- Max action time: 180sec -->
|
5
camsutil/templates/Hikvision_GoHomeposition.xml
Normal file
5
camsutil/templates/Hikvision_GoHomeposition.xml
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<PTZData></PTZData>
|
||||
<!-- PATH: /ISAPI/PTZCtrl/channels/@CHANNEL@/homeposition/goto -->
|
||||
<!-- METHOD: PUT -->
|
||||
<!-- TYPE: text/xml -->
|
13
camsutil/templates/Hikvision_GoMomentary.xml
Normal file
13
camsutil/templates/Hikvision_GoMomentary.xml
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<PTZData>
|
||||
<pan>@XXXX@</pan>
|
||||
<tilt>@YYYY@</tilt>
|
||||
<zoom>@ZZZZ@</zoom>
|
||||
<Momentary>
|
||||
<duration>@TTTT@</duration>
|
||||
</Momentary>
|
||||
</PTZData>
|
||||
<!-- PATH: /ISAPI/PTZCtrl/channels/@CHANNEL@/momentary -->
|
||||
<!-- METHOD: PUT -->
|
||||
<!-- TYPE: text/xml -->
|
||||
<!-- XXXX: -100..100 YYYY: -100..100 ZZZZ: -100..100 TTTT: -->
|
4
camsutil/templates/Hikvision_PtzDown.html
Normal file
4
camsutil/templates/Hikvision_PtzDown.html
Normal file
|
@ -0,0 +1,4 @@
|
|||
<!-- PATH: /PTZ/channels/1/PTZControl?command=TILT_DOWN&speed=@SSSS@&mode=start -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
||||
<!-- SSSS: 1..7 -->
|
4
camsutil/templates/Hikvision_PtzLeft.html
Normal file
4
camsutil/templates/Hikvision_PtzLeft.html
Normal file
|
@ -0,0 +1,4 @@
|
|||
<!-- PATH: /PTZ/channels/1/PTZControl?command=PAN_LEFT&speed=@SSSS@&mode=start -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
||||
<!-- SSSS: 1..7 -->
|
4
camsutil/templates/Hikvision_PtzPreset.html
Normal file
4
camsutil/templates/Hikvision_PtzPreset.html
Normal file
|
@ -0,0 +1,4 @@
|
|||
<!-- PATH: /PTZ/channels/1/PTZControl?command=GOTO_PRESET&presetNo=@ID@&speed=@SSSS@&mode=start -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
||||
<!-- SSSS: 1..7 -->
|
4
camsutil/templates/Hikvision_PtzRight.html
Normal file
4
camsutil/templates/Hikvision_PtzRight.html
Normal file
|
@ -0,0 +1,4 @@
|
|||
<!-- PATH: /PTZ/channels/1/PTZControl?command=PAN_RIGHT&speed=@SSSS@&mode=start -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
||||
<!-- SSSS: 1..7 -->
|
3
camsutil/templates/Hikvision_PtzStop.html
Normal file
3
camsutil/templates/Hikvision_PtzStop.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
<!-- PATH: /PTZ/channels/1/PTZControl?command=GOTO_PRESET&mode=stop -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
4
camsutil/templates/Hikvision_PtzUp.html
Normal file
4
camsutil/templates/Hikvision_PtzUp.html
Normal file
|
@ -0,0 +1,4 @@
|
|||
<!-- PATH: /PTZ/channels/1/PTZControl?command=TILT_UP&speed=@SSSS@&mode=start -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
||||
<!-- SSSS: 1..7 -->
|
4
camsutil/templates/Hikvision_PtzZoomIn.html
Normal file
4
camsutil/templates/Hikvision_PtzZoomIn.html
Normal file
|
@ -0,0 +1,4 @@
|
|||
<!-- PATH: /PTZ/channels/1/PTZControl?command=ZOOM_OUT&speed=@SSSS@&mode=start -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
||||
<!-- SSSS: 1..7 -->
|
4
camsutil/templates/Hikvision_PtzZoomOut.html
Normal file
4
camsutil/templates/Hikvision_PtzZoomOut.html
Normal file
|
@ -0,0 +1,4 @@
|
|||
<!-- PATH: /PTZ/channels/1/PTZControl?command=ZOOM_IN&speed=@SSSS@&mode=start -->
|
||||
<!-- METHOD: GET -->
|
||||
<!-- TYPE: application/x-www-form-urlencoded -->
|
||||
<!-- SSSS: 1..7 -->
|
5
camsutil/templates/Hikvision_SetHomeposition.xml
Normal file
5
camsutil/templates/Hikvision_SetHomeposition.xml
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<PTZData></PTZData>
|
||||
<!-- PATH: /ISAPI/PTZCtrl/channels/@CHANNEL@/homeposition -->
|
||||
<!-- METHOD: PUT -->
|
||||
<!-- TYPE: text/xml -->
|
13
camsutil/templates/Hikvision_SetTextOverlay.xml
Normal file
13
camsutil/templates/Hikvision_SetTextOverlay.xml
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<TextOverlayList version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema">
|
||||
<TextOverlay>
|
||||
<id>@ID@</id>
|
||||
<enabled>@ENABLED@</enabled>
|
||||
<posX>@XXXX@</posX>
|
||||
<posY>@YYYY@</posY>
|
||||
<message>@MSG@</message>
|
||||
</TextOverlay>
|
||||
</TextOverlayList>
|
||||
<!-- PATH: /Video/inputs/channels/@CHANNEL@/overlays/text -->
|
||||
<!-- METHOD: PUT -->
|
||||
<!-- TYPE: text/xml -->
|
Loading…
Reference in New Issue
Block a user