Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
parkpow
GitHub Repository: parkpow/deep-license-plate-recognition
Path: blob/master/stream/ipro-adam-app/installer.py
643 views
1
import argparse
2
import logging
3
import os
4
from pathlib import Path
5
6
import requests
7
import tqdm
8
from requests.auth import HTTPDigestAuth
9
from requests_toolbelt.multipart.encoder import (
10
MultipartEncoder,
11
MultipartEncoderMonitor,
12
)
13
14
# tqdm-4.67.1
15
# requests-toolbelt-1.0.0
16
17
lgr = logging.getLogger(__name__)
18
19
20
def make_request(method, params=None, data=None, headers=None, use_install_url=False):
21
if use_install_url:
22
url = f"{camera_url}/cgi-bin/adam_install.cgi"
23
else:
24
url = f"{camera_url}/cgi-bin/adam.cgi"
25
lgr.debug(f"url: {url}")
26
lgr.debug(f"headers: {headers}")
27
28
response = requests.request(
29
method,
30
url,
31
params=params,
32
data=data,
33
headers=headers,
34
auth=HTTPDigestAuth(username, password),
35
)
36
37
lgr.info(f"response: {response}")
38
lgr.debug(f"response: {response.text}")
39
40
return response
41
42
43
def list_applications(name):
44
applications = make_request("GET", {"methodName": "getApplicationList"}).json()
45
for a in applications["appList"]:
46
if a["appInfo"]["appNameList"][0]["name"] == name:
47
return a
48
return None
49
50
51
def upload_adam_app(ext_file_path):
52
"""
53
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary9dLmSwnFFi6H43X6
54
Body Format:
55
------WebKitFormBoundaryKeEUnMHC1xkj75DO
56
Content-Disposition: form-data; name="methodName"
57
58
installApplication
59
------WebKitFormBoundaryKeEUnMHC1xkj75DO
60
Content-Disposition: form-data; name="applicationPackage"; filename="stream-adam_1.1.ext"
61
Content-Type: application/octet-stream
62
63
------WebKitFormBoundaryKeEUnMHC1xkj75DO--
64
65
:param ext_file_path:
66
:return:
67
"""
68
file_size = os.path.getsize(ext_file_path)
69
progress_bar = tqdm.tqdm(
70
desc=f"Uploading App File:[{ext_file_path}]",
71
total=file_size,
72
unit="B",
73
unit_scale=True,
74
unit_divisor=1024,
75
)
76
77
def progress_callback(monitor):
78
# uploaded = monitor.bytes_read
79
# total = monitor.len
80
progress_bar.update(monitor.bytes_read - progress_bar.n)
81
82
with open(ext_file_path, "rb") as fp:
83
try:
84
# Construct the multipart body
85
encoder = MultipartEncoder(
86
fields={
87
"methodName": "installApplication",
88
"applicationPackage": (
89
os.path.basename(ext_file_path),
90
fp,
91
"application/octet-stream",
92
),
93
}
94
)
95
# Wrap encoder with a monitor for progress tracking
96
monitor = MultipartEncoderMonitor(encoder, progress_callback)
97
headers = {"Content-Type": monitor.content_type}
98
response = make_request(
99
"POST", use_install_url=True, data=monitor, headers=headers
100
)
101
102
if response.status_code == 204:
103
print("Application Installed!!!")
104
print(response.headers)
105
else:
106
print(
107
f"Application Install Failed - status code: {response.status_code}"
108
)
109
print(response.text)
110
except Exception:
111
raise
112
113
114
def uninstall(install_id):
115
data = {"methodName": "uninstallApplication", "installId": install_id}
116
res = make_request("POST", data=data)
117
if res.status_code != 200:
118
lgr.error(res.text)
119
else:
120
print("Application Uninstalled!!!")
121
122
123
def stop(install_id):
124
params = {"methodName": "stopApplication", "installId": install_id}
125
res = make_request("GET", params)
126
127
if res.status_code != 200:
128
# {"faultCode":"3","faultString":"Invalid Install ID"}
129
lgr.error(res.text)
130
else:
131
print("Application Stopped!!!")
132
133
134
def start(install_id):
135
params = {"methodName": "startApplication", "installId": install_id}
136
res = make_request("GET", params)
137
if res.status_code != 200:
138
# {"faultCode":"3","faultString":"Invalid Install ID"}
139
raise Exception(res.text)
140
print("Application Started!!!")
141
142
143
if __name__ == "__main__":
144
parser = argparse.ArgumentParser(
145
prog="AdamAPP Installer",
146
description="Quick Install or Replace AdamAPP(.ext) on i-PRO",
147
epilog="Upload Adam APP to i-PRO with Progress",
148
)
149
150
camera_url = "http://CAMERA_IP:80"
151
username = "CAMERA_USERNAME"
152
password = "CAMERA_PASSWORD"
153
ext_file = "/tmp/app-build-dir/stream-adam_1.1.ext"
154
installed_app_name = "Platerecognizer Stream"
155
156
parser.add_argument(
157
"--ext-file",
158
type=Path,
159
default=ext_file,
160
help="App ext file to install",
161
required=False,
162
)
163
parser.add_argument(
164
"--app-name",
165
type=str,
166
default=installed_app_name,
167
help="app_name after install, used to find app app install ID",
168
required=False,
169
)
170
parser.add_argument(
171
"--camera-url",
172
type=str,
173
default=camera_url,
174
help="camera access url including port and scheme",
175
required=False,
176
)
177
parser.add_argument(
178
"--username",
179
type=str,
180
# default=username,
181
help="username of camera login",
182
required=False,
183
)
184
parser.add_argument(
185
"--password",
186
type=str,
187
# default=password,
188
help="password of camera login",
189
required=False,
190
)
191
# Add skip param to skip directly to upload
192
args = parser.parse_args()
193
if username != args.username:
194
username = args.username
195
if password != args.password:
196
password = args.password
197
if camera_url != args.camera_url:
198
camera_url = args.camera_url
199
200
# list applications
201
application = list_applications(args.app_name)
202
print(f"Uninstall application: {application}")
203
if application is not None:
204
app_install_id = application["appInfo"]["installId"]
205
# Stop
206
stop(app_install_id)
207
# Uninstall
208
uninstall(app_install_id)
209
210
# Upload new Install
211
upload_adam_app(args.ext_file)
212
213
installed_application = list_applications(args.app_name)
214
print(f"Installed application: {installed_application}")
215
if installed_application:
216
app_install_id = installed_application["appInfo"]["installId"]
217
# Start Application
218
start(app_install_id)
219
220