Skip to content

Commit

Permalink
Fix various HTTPCameraAgent issues (#733)
Browse files Browse the repository at this point in the history
* catch ReadTimeoutError

* fix pm.sleep and file writing
  • Loading branch information
davidvng authored Aug 26, 2024
1 parent f0fecae commit 6f357f2
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions socs/agents/http_camera/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ocs import ocs_agent, site_config
from ocs.ocs_twisted import Pacemaker, TimeoutLock
# Disable unverified HTTPS warnings (https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings)
from urllib3.exceptions import InsecureRequestWarning
from urllib3.exceptions import InsecureRequestWarning, ReadTimeoutError

requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)

Expand Down Expand Up @@ -90,6 +90,7 @@ def acq(self, session, params=None):

self.is_streaming = True
while self.is_streaming:
pm.sleep()
# Use UTC
timestamp = time.time()
data = {}
Expand All @@ -112,7 +113,7 @@ def acq(self, session, params=None):
"password": camera['password']}}}]
try:
resp = requests.post(login_url, data=json.dumps(login_payload), verify=False)
except requests.exceptions.RequestException as e:
except (requests.exceptions.RequestException, ReadTimeoutError) as e:
self.log.error(f'{e}')
self.log.info("Unable to get response from camera.")
connected = False
Expand Down Expand Up @@ -159,7 +160,7 @@ def acq(self, session, params=None):
elif camera['brand'] == 'acti':
response = requests.get(url, params=payload, stream=True, timeout=5)
connected = True
except requests.exceptions.RequestException as e:
except (requests.exceptions.RequestException, ReadTimeoutError) as e:
self.log.error(f'{e}')
self.log.info("Unable to get response from camera.")
connected = False
Expand All @@ -171,6 +172,9 @@ def acq(self, session, params=None):
# Write screenshot to file and update latest file
with open(filename, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
# Ensure all data is written to the disk before copying to latest
out_file.flush()
os.fsync(out_file.fileno())
self.log.debug(f"Wrote {ctime}.jpg to /{camera['location']}/{ctime_dir}.")
shutil.copy2(filename, latest_filename)
self.log.debug(f"Updated latest.jpg in /{camera['location']}.")
Expand All @@ -197,7 +201,6 @@ def acq(self, session, params=None):

if params['test_mode']:
break
pm.sleep()

return True, "Finished Recording"

Expand Down

0 comments on commit 6f357f2

Please sign in to comment.