Skip to content

Commit

Permalink
Fix inability to resume timelapses
Browse files Browse the repository at this point in the history
  • Loading branch information
kizniche committed Jun 15, 2022
1 parent 5d34af5 commit ed33333
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 77 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Bugfixes

- Fix inability to resume timelapses
- Fix "Could not determine measurement from returned value" error for Atlas Scientific inputs

### Features
Expand Down
6 changes: 0 additions & 6 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,6 @@ Parameters:
- **setting** - Which option to set. Options are: "setpoint", "method", "integrator", "derivator", "kp", "ki", or "kd".
- **value** - The value to set.
### refresh_daemon_camera_settings()
**refresh_daemon_camera_settings**\ ()
Refresh the camera settings stored in the running daemon from the database values.
### refresh_daemon_conditional_settings()
**refresh_daemon_conditional_settings**\ (*unique_id*)
Expand Down
3 changes: 0 additions & 3 deletions mycodo/mycodo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ def controller_activate(self, controller_id):
def controller_deactivate(self, controller_id):
return self.proxy().controller_deactivate(controller_id)

def refresh_daemon_camera_settings(self):
return self.proxy().refresh_daemon_camera_settings()

def refresh_daemon_conditional_settings(self, unique_id):
return self.proxy().refresh_daemon_conditional_settings(unique_id)

Expand Down
92 changes: 30 additions & 62 deletions mycodo/mycodo_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ def __init__(self, debug):
self.timer_upgrade = time.time() + 120
self.timer_upgrade_message = time.time()

# Update camera settings
self.camera = []
self.refresh_daemon_camera_settings()

# Update Misc settings
self.output_usage_report_gen = None
self.output_usage_report_span = None
Expand Down Expand Up @@ -709,16 +705,6 @@ def pid_set(self, pid_id, setting, value):
self.logger.exception(message)


def refresh_daemon_camera_settings(self):
try:
self.logger.debug("Refreshing camera settings")
self.camera = db_retrieve_table_daemon(Camera, entry='all')
except Exception as except_msg:
self.camera = []
message = f"Could not read camera table: {except_msg}"
self.logger.exception(message)


def refresh_daemon_conditional_settings(self, unique_id):
try:
return self.controller['Conditional'][unique_id].refresh_settings()
Expand Down Expand Up @@ -1050,53 +1036,39 @@ def check_mycodo_upgrade_exists(self, now):


def check_all_timelapses(self, now):
try:
if not self.camera:
return
for each_camera in self.camera:
with session_scope(MYCODO_DB_PATH) as new_session:
for each_camera in new_session.query(Camera).all():
try:
self.timelapse_check(each_camera, now)
if (each_camera.timelapse_started and
now > each_camera.timelapse_end_time):
each_camera.timelapse_started = False
each_camera.timelapse_paused = False
each_camera.timelapse_start_time = None
each_camera.timelapse_end_time = None
each_camera.timelapse_interval = None
each_camera.timelapse_next_capture = None
each_camera.timelapse_capture_number = None
new_session.commit()
self.logger.debug(f"Camera {each_camera.id}: End of time-lapse.")
elif ((each_camera.timelapse_started and not each_camera.timelapse_paused) and
now > each_camera.timelapse_next_capture):
# Ensure next capture is greater than now (in case of power failure/reboot)
capture_now = each_camera.timelapse_next_capture
while now > each_camera.timelapse_next_capture:
# Update last capture and image number to latest before capture
each_camera.timelapse_next_capture += each_camera.timelapse_interval
each_camera.timelapse_capture_number += 1
new_session.commit()
if abs(now - capture_now) < 60:
# Only capture if close to timelapse capture time
# This prevents an unscheduled timelapse capture upon resume.
self.logger.debug(f"Camera {each_camera.id}: Capturing time-lapse image")
capture_image = threading.Thread(
target=camera_record,
args=('timelapse', each_camera.unique_id,))
capture_image.start()
except Exception:
self.logger.exception("Could not execute timelapse")
except Exception:
self.logger.exception("Timelapse ERROR")


def timelapse_check(self, camera, now):
"""If time-lapses are active, take photo at predefined periods."""
if (camera.timelapse_started and
now > camera.timelapse_end_time):
with session_scope(MYCODO_DB_PATH) as new_session:
mod_camera = new_session.query(Camera).filter(
Camera.unique_id == camera.unique_id).first()
mod_camera.timelapse_started = False
mod_camera.timelapse_paused = False
mod_camera.timelapse_start_time = None
mod_camera.timelapse_end_time = None
mod_camera.timelapse_interval = None
mod_camera.timelapse_next_capture = None
mod_camera.timelapse_capture_number = None
new_session.commit()
self.refresh_daemon_camera_settings()
self.logger.debug(f"Camera {camera.id}: End of time-lapse.")
elif ((camera.timelapse_started and not camera.timelapse_paused) and
now > camera.timelapse_next_capture):
# Ensure next capture is greater than now (in case of power failure/reboot)
next_capture = camera.timelapse_next_capture
while now > next_capture:
# Update last capture and image number to latest before capture
next_capture += camera.timelapse_interval
with session_scope(MYCODO_DB_PATH) as new_session:
mod_camera = new_session.query(Camera).filter(Camera.unique_id == camera.unique_id).first()
mod_camera.timelapse_next_capture = next_capture
mod_camera.timelapse_capture_number += 1
new_session.commit()
self.refresh_daemon_camera_settings()
self.logger.debug(f"Camera {camera.id}: Capturing time-lapse image")
# Capture image
path, filename = camera_record('timelapse', camera.unique_id)
if not path or not filename:
self.logger.error(f"{camera.id}: Could not acquire time-lapse image.")


def generate_usage_report(self):
Expand Down Expand Up @@ -1236,10 +1208,6 @@ def pid_set(self, pid_id, setting, value):
"""Set PID setting."""
return self.mycodo.pid_set(pid_id, setting, value)

def refresh_daemon_camera_settings(self, ):
"""Instruct the daemon to refresh the camera settings."""
return self.mycodo.refresh_daemon_camera_settings()

def refresh_daemon_conditional_settings(self, unique_id):
"""Instruct the daemon to refresh a conditional's settings."""
return self.mycodo.refresh_daemon_conditional_settings(unique_id)
Expand Down
4 changes: 0 additions & 4 deletions mycodo/mycodo_flask/routes_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,12 @@ def page_camera():
mod_camera.timelapse_next_capture = now
mod_camera.timelapse_capture_number = 0
db.session.commit()
control.refresh_daemon_camera_settings()
elif form_camera.pause_timelapse.data:
mod_camera.timelapse_paused = True
db.session.commit()
control.refresh_daemon_camera_settings()
elif form_camera.resume_timelapse.data:
mod_camera.timelapse_paused = False
db.session.commit()
control.refresh_daemon_camera_settings()
elif form_camera.stop_timelapse.data:
mod_camera.timelapse_started = False
mod_camera.timelapse_start_time = None
Expand All @@ -262,7 +259,6 @@ def page_camera():
mod_camera.timelapse_next_capture = None
mod_camera.timelapse_capture_number = None
db.session.commit()
control.refresh_daemon_camera_settings()
elif form_camera.start_stream.data:
if mod_camera.timelapse_started:
flash(gettext(
Expand Down
2 changes: 0 additions & 2 deletions mycodo/mycodo_flask/utils/utils_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ def camera_mod(form_camera):

if not messages["error"]:
db.session.commit()
control = DaemonControl()
control.refresh_daemon_camera_settings()
messages["success"].append("Camera settings saved")
except Exception as except_msg:
logger.exception(1)
Expand Down

1 comment on commit ed33333

@kizniche
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Radical DIY Forum. There might be relevant details there:

https://forum.radicaldiy.com/t/timelapse-resume-not-working/1012/3

Please sign in to comment.