-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcameras_set_rec_mode.py
66 lines (55 loc) · 2.1 KB
/
cameras_set_rec_mode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import argparse
import requests
import sys
import json
import base64
# Function for executing HTTP request
def cameras_set_rec_mode(id, token, status):
# HTTP header. Indicates the MIME document type (json) as well as the authorization key
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
}
data = {"rec_mode": status} # Parameters that shoul be changed in an event
params = ( # Parameters of the request.
('token', token),
)
address = 'https://web.skyvr.videoexpertsgroup.com/api/v2/cameras/%s/' % str(id) # End point
# Exception block.
try:
response = requests.put(address, headers=headers, params = params, data=json.dumps(data), timeout=15) # Method PUT
except requests.exceptions.RequestException as e:
print (e)
sys.exit(1)
return response.status_code # Returns the status of an HTTP request
# Parsing the command line
parser = argparse.ArgumentParser()
parser.add_argument('-access_token', '--access_token', help = 'Access token', required=True)
parser.add_argument('-rec_mode', '--rec_mode', help = 'Record mode (on, off, by_event)', required=True)
#
try:
param = parser.parse_args()
except IOError as msg:
parser.error(str(msg))
# Checking that the parameter rec_mode is correct. The values should be on, off or by_event.
if param.rec_mode == "on":
status = "on"
elif param.rec_mode == "off":
status = "off"
elif param.rec_mode == "by_event":
status = "by_event"
else:
print('Specify the parameter "on", "off", "by_event" without quotes')
sys.exit(1)
# Parsing of the access token for retrieving a token and camid.
token_json=''
try:
token = base64.b64decode(param.access_token)
token_json = json.loads(token)
except Exception:
print("Error access token")
sys.exit(1)
# Function call for sending an HTTP request
code= cameras_set_rec_mode(token_json["camid"], token_json['token'], status)
# Output of the status of the request
print ('Request completed. HTTP status code: ' + str(code)+'\n')