- fix the problem with the
This commit is contained in:
@@ -17,6 +17,7 @@ import requests
|
||||
from socket import timeout
|
||||
from crontab import CronTab
|
||||
from pathlib import Path
|
||||
from utils_cache import InfoCache
|
||||
|
||||
start_system_time = time.time()
|
||||
start_monotonic_time = time.monotonic()
|
||||
@@ -149,6 +150,32 @@ class Phone:
|
||||
self.attributes = dict()
|
||||
self.audio_id = 0
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
'id': self.identifier,
|
||||
'name': self.name,
|
||||
'role': self.role,
|
||||
'attr': self.attributes,
|
||||
'audio_id': self.audio_id
|
||||
}
|
||||
|
||||
def make(d: dict):
|
||||
r = Phone()
|
||||
|
||||
r.identifier = d['id']
|
||||
r.name = d['name']
|
||||
r.role = d['role']
|
||||
if 'attr' in d:
|
||||
r.attr = d['attr']
|
||||
else:
|
||||
r.attr = None
|
||||
|
||||
if 'audio_id' in d:
|
||||
r.audio_id = d['audio_id']
|
||||
else:
|
||||
r.audio_id = None
|
||||
|
||||
return r
|
||||
|
||||
class QualtestBackend:
|
||||
address: str
|
||||
@@ -165,34 +192,31 @@ class QualtestBackend:
|
||||
return self.__phone
|
||||
|
||||
|
||||
def preload(self):
|
||||
self.__phone = self.load_phone()
|
||||
def preload(self, cache_dir: Path):
|
||||
self.__phone = self.load_phone(cache_dir)
|
||||
|
||||
|
||||
def upload_report(self, report, files) -> str:
|
||||
def upload_report(self, report, cache: InfoCache) -> (str, bool):
|
||||
# UUID string as result
|
||||
result = None
|
||||
result = (None, False)
|
||||
|
||||
# Log about upload attempt
|
||||
utils.log_verbose(f"Uploading to {self.address} files {files} and report: {json.dumps(report, indent=4)}")
|
||||
|
||||
# POST will be sent to args.qualtest_server with args.qualtest_instance ID
|
||||
json_content = json.dumps(report, indent=4).encode('utf8')
|
||||
|
||||
# Find URL for uploading
|
||||
utils.log_verbose(f"Uploading to {self.address} report: {json.dumps(report, indent=4)}")
|
||||
url = utils.join_host_and_path(self.address, "/probes/")
|
||||
|
||||
try:
|
||||
# Step 1 - upload result record
|
||||
req = urllib.request.Request(url,
|
||||
data=json_content,
|
||||
headers={'content-type': 'application/json'})
|
||||
response = urllib.request.urlopen(req, timeout=utils.NETWORK_TIMEOUT)
|
||||
result = response.read().decode('utf8')
|
||||
utils.log_verbose(f"Response (probe ID): {result}")
|
||||
utils.log_verbose(f"Upload to {self.address} finished.")
|
||||
r = requests.post(url=url, json=report, timeout=utils.NETWORK_TIMEOUT)
|
||||
utils.log_verbose(f"Upload report finished. Response (probe ID): {r.content}")
|
||||
if r.status_code != 200:
|
||||
raise RuntimeError(f'Server returned code {r.status_code} and content {r.content}')
|
||||
|
||||
result = (r.content.decode().strip(), True)
|
||||
except Exception as e:
|
||||
utils.log_error(f"Upload to {self.address} finished with error.", err=e)
|
||||
utils.log_error(f"Upload report to {self.address} finished with error.", err=e)
|
||||
|
||||
# Backup probe result
|
||||
probe_id = cache.add_report(report)
|
||||
result = (probe_id, False)
|
||||
|
||||
return result
|
||||
|
||||
@@ -216,8 +240,7 @@ class QualtestBackend:
|
||||
if response.status_code != 200:
|
||||
utils.log_error(f"Upload audio to {self.address} finished with error {response.status_code}", None)
|
||||
else:
|
||||
utils.log_verbose(f"Response (audio ID): {response.text}")
|
||||
utils.log_verbose(f"Upload audio to {self.address} finished.")
|
||||
utils.log_verbose(f"Upload audio finished. Response (audio ID): {response.text}")
|
||||
result = True
|
||||
except Exception as e:
|
||||
utils.log_error(f"Upload audio to {self.address} finished with error.", err=e)
|
||||
@@ -248,7 +271,8 @@ class QualtestBackend:
|
||||
return None
|
||||
|
||||
|
||||
def load_phone(self) -> dict:
|
||||
def load_phone(self, cache: InfoCache) -> dict:
|
||||
result = None
|
||||
try:
|
||||
# Build query for both V1 & V2 API
|
||||
instance = urllib.parse.urlencode({"phone_id": self.instance, "phone_name": self.instance})
|
||||
@@ -257,16 +281,21 @@ class QualtestBackend:
|
||||
url = utils.join_host_and_path(self.address, "/phones/?") + instance
|
||||
|
||||
# Get response from server
|
||||
response = urllib.request.urlopen(url, timeout=utils.NETWORK_TIMEOUT)
|
||||
if response.getcode() != 200:
|
||||
utils.log_error("Failed to get task list. Error code: %s" % response.getcode())
|
||||
return None
|
||||
|
||||
result: Phone = Phone()
|
||||
try:
|
||||
response = urllib.request.urlopen(url, timeout=utils.NETWORK_TIMEOUT)
|
||||
if response.getcode() != 200:
|
||||
utils.log_error("Failed to get task list. Error code: %s" % response.getcode())
|
||||
return None
|
||||
except Exception as e:
|
||||
utils.log_error(f'Problem when loading the phone definition.')
|
||||
return cache.get_phone(self.instance)
|
||||
|
||||
# Get possible list of phones
|
||||
phones = json.loads(response.read().decode())
|
||||
if len(phones) == 0:
|
||||
return result
|
||||
return None
|
||||
|
||||
# But use first one
|
||||
phone = phones[0]
|
||||
|
||||
attr_dict = dict()
|
||||
@@ -290,6 +319,7 @@ class QualtestBackend:
|
||||
if 'sip_useproxy' not in attr_dict:
|
||||
attr_dict['sip_useproxy'] = True
|
||||
|
||||
result = Phone()
|
||||
result.attributes = attr_dict
|
||||
result.identifier = phone['id']
|
||||
result.name = phone['instance']
|
||||
|
||||
Reference in New Issue
Block a user