Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
helgeerbe committed May 17, 2023
2 parents e37ff86 + 29e2b10 commit a98ccbe
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 50 deletions.
58 changes: 47 additions & 11 deletions src/picframe/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import logging
import time
import signal
import sys
import ssl


def make_date(txt):
Expand Down Expand Up @@ -45,6 +47,8 @@ def __init__(self, model, viewer):
self.__logger.info('creating an instance of Controller')
self.__model = model
self.__viewer = viewer
self.__http_config = self.__model.get_http_config()
self.__mqtt_config = self.__model.get_mqtt_config()
self.__paused = False
self.__force_navigate = False
self.__next_tm = 0
Expand All @@ -58,7 +62,8 @@ def __init__(self, model, viewer):
self.__location_filter = ''
self.__tags_filter = ''
self.__interface_peripherals = None
self.__shutdown_complete = False
self.__interface_mqtt = None
self.__interface_http = None

@property
def paused(self):
Expand All @@ -73,7 +78,8 @@ def paused(self, val: bool):
self.__paused = val
pic = self.__model.get_current_pics()[0] # only refresh left text
self.__viewer.reset_name_tm(pic, val, side=0, pair=self.__model.get_current_pics()[1] is not None)
self.publish_state()
if self.__mqtt_config['use_mqtt']:
self.publish_state()

def next(self):
self.__next_tm = 0
Expand Down Expand Up @@ -160,7 +166,8 @@ def display_is_on(self):
def display_is_on(self, on_off):
self.paused = not on_off
self.__viewer.display_is_on = on_off
self.publish_state()
if self.__mqtt_config['use_mqtt']:
self.publish_state()

@property
def clock_is_on(self):
Expand All @@ -179,7 +186,8 @@ def shuffle(self, val: bool):
self.__model.shuffle = val
self.__model.force_reload()
self.__next_tm = 0
self.publish_state()
if self.__mqtt_config['use_mqtt']:
self.publish_state()

@property
def fade_time(self):
Expand Down Expand Up @@ -210,7 +218,8 @@ def brightness(self):
@brightness.setter
def brightness(self, val):
self.__viewer.set_brightness(float(val))
self.publish_state()
if self.__mqtt_config['use_mqtt']:
self.publish_state()

@property
def matting_images(self):
Expand Down Expand Up @@ -315,30 +324,57 @@ def loop(self): # TODO exit loop gracefully and call image_cache.stop()
else:
field_name = self.__model.EXIF_TO_FIELD[key]
image_attr[key] = pics[0].__dict__[field_name] # TODO nicer using namedtuple for Pic
self.publish_state(pics[0].fname, image_attr)
if self.__mqtt_config['use_mqtt']:
self.publish_state(pics[0].fname, image_attr)
self.__model.pause_looping = self.__viewer.is_in_transition()
(loop_running, skip_image) = self.__viewer.slideshow_is_running(pics, time_delay, fade_time, self.__paused)
if not loop_running:
break
if skip_image:
self.__next_tm = 0
self.__interface_peripherals.check_input()
self.__shutdown_complete = True

def start(self):
self.__viewer.slideshow_start()
from picframe.interface_peripherals import InterfacePeripherals
self.__interface_peripherals = InterfacePeripherals(self.__model, self.__viewer, self)

# start mqtt
if self.__mqtt_config['use_mqtt']:
from picframe import interface_mqtt
try:
self.__interface_mqtt = interface_mqtt.InterfaceMQTT(self, self.__mqtt_config)
self.__interface_mqtt.start()
except Exception:
self.__logger.error("Can't initialize mqtt. Stopping picframe")
sys.exit(1)

# start http server
if self.__http_config['use_http']:
from picframe import interface_http
model_config = self.__model.get_model_config()
self.__interface_http = interface_http.InterfaceHttp(self,
self.__http_config['path'],
model_config['pic_dir'],
model_config['no_files_img'],
self.__http_config['port']) # TODO: Implement TLS
if self.__http_config['use_ssl']:
self.__interface_http.socket = ssl.wrap_socket(
self.__interface_http.socket,
keyfile=self.__http_config['keyfile'],
certfile=self.__http_config['certfile'],
server_side=True)

def stop(self):
self.keep_looping = False
self.__interface_peripherals.stop()
while not self.__shutdown_complete:
time.sleep(0.05) # block until main loop has stopped
if self.__interface_mqtt:
self.__interface_mqtt.stop()
if self.__interface_http:
self.__interface_http.stop()
self.__model.stop_image_chache() # close db tidily (blocks till closed)
self.__viewer.slideshow_stop() # do this last

def __signal_handler(self, sig, frame):
print('You pressed Ctrl-c!')
self.__shutdown_complete = True
self.stop()
self.keep_looping = False
2 changes: 1 addition & 1 deletion src/picframe/interface_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def on_message(self, client, userdata, message): # noqa: C901

# stop loops and end program
elif message.topic == self.__device_id + "/stop":
self.__controller.stop()
self.__controller.keep_looping = False

def publish_state(self, image=None, image_attr=None):
sensor_topic_head = "homeassistant/sensor/" + self.__device_id
Expand Down
9 changes: 3 additions & 6 deletions src/picframe/interface_peripherals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import sys
import time
import typing
from picframe.model import Model
from picframe.viewer_display import ViewerDisplay
from picframe.controller import Controller
import numpy as np
import pi3d

Expand All @@ -26,9 +23,9 @@ class InterfacePeripherals:

def __init__(
self,
model: type[Model],
viewer: type[ViewerDisplay],
controller: type[Controller],
model,
viewer,
controller,
) -> None:
logger.info("creating an instance of InterfacePeripherals")

Expand Down
34 changes: 2 additions & 32 deletions src/picframe/start.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import logging
import sys
import argparse
import os
import ssl
import locale
import sys
from distutils.dir_util import copy_tree

from picframe import model, viewer_display, controller, interface_http, __version__
from picframe import model, viewer_display, controller, __version__

PICFRAME_DATA_DIR = 'picframe_data'

Expand Down Expand Up @@ -134,36 +133,7 @@ def main():
v = viewer_display.ViewerDisplay(m.get_viewer_config())
c = controller.Controller(m, v)
c.start()

mqtt_config = m.get_mqtt_config()
if mqtt_config['use_mqtt']:
from picframe import interface_mqtt
try:
mqtt = interface_mqtt.InterfaceMQTT(c, mqtt_config)
mqtt.start()
except Exception:
logger.error("Can't initialize mqtt. Stopping picframe")
sys.exit(1)

http_config = m.get_http_config()
model_config = m.get_model_config()
if http_config['use_http']:
server = interface_http.InterfaceHttp(c,
http_config['path'],
model_config['pic_dir'],
model_config['no_files_img'],
http_config['port'])
if http_config['use_ssl']:
server.socket = ssl.wrap_socket(
server.socket,
keyfile=http_config['keyfile'],
certfile=http_config['certfile'],
server_side=True)
c.loop()
if mqtt_config['use_mqtt']:
mqtt.stop()
if http_config['use_http']: # TODO objects living in multiple threads issue at shutdown!
server.stop()
c.stop()


Expand Down

0 comments on commit a98ccbe

Please sign in to comment.