summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2024-02-17 13:39:01 +0300
committerEvgeny Zinoviev <me@ch1p.io>2024-02-17 13:39:01 +0300
commit77b80dd9b3500539b24b7dc6258c02c23fd4d015 (patch)
treecb9382f937feffc3f18aec894bde2aa3b27a8bc6 /bin
parentc5e69cf2c9b89d546ad7a4f6bb26aef47021dd50 (diff)
fix ipcam_ntp_util
Diffstat (limited to 'bin')
-rwxr-xr-xbin/ipcam_ntp_util.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/bin/ipcam_ntp_util.py b/bin/ipcam_ntp_util.py
index 0268a06..b27995c 100755
--- a/bin/ipcam_ntp_util.py
+++ b/bin/ipcam_ntp_util.py
@@ -151,6 +151,23 @@ class HikvisionISAPIClient:
ntp_port: int = 123):
format = 'ipaddress' if validate_ipv4(ntp_host) else 'hostname'
+ # test ntp server first
+ data = f'<?xml version="1.0" encoding="UTF-8"?><NTPTestDescription><addressingFormatType>{format}</addressingFormatType><ipAddress>{ntp_host}</ipAddress><portNo>{ntp_port}</portNo></NTPTestDescription>'
+ r = requests.post(self.isapi_uri('System/time/ntpServers/test'), data=data, headers={
+ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
+ 'X-Requested-With': 'XMLHttpRequest',
+ }, cookies=self.cookies)
+ r.raise_for_status()
+
+ resp = xml_to_dict(r.text)['NTPTestResult']
+
+ error_code = int(resp['errorCode'][0])
+ error_description = resp['errorDescription'][0]
+
+ if error_code != 0 or error_description.lower() != 'ok':
+ raise ResponseError('response status looks bad')
+
+ # then set it
data = '<?xml version="1.0" encoding="UTF-8"?>'
data += f'<NTPServer><id>1</id><addressingFormatType>{format}</addressingFormatType><ipAddress>{ntp_host}</ipAddress><portNo>{ntp_port}</portNo><synchronizeInterval>1440</synchronizeInterval></NTPServer>'
@@ -194,6 +211,7 @@ def process_hikvision_camera(host: str,
print(f'[{host}] {client.get_ntp_server()}')
return
client.set_ntp_server(ntp_server)
+ print(f'[{host}] done')
except AuthError as e:
print(f'[{host}] ({str(e)})')
except ResponseError as e: