Skip to content

Commit

Permalink
Merge pull request #3 from netz39/pictures
Browse files Browse the repository at this point in the history
Add picture delivery for state.png
  • Loading branch information
penguineer authored Nov 2, 2024
2 parents e939c6b + c15a95d commit 07e8324
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 4 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ COPY requirements.txt /
RUN pip install -r requirements.txt

COPY src/*.py /
COPY assets/ /assets/

COPY --from=install /git-version.txt /

Expand Down
Binary file added assets/closed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 119 additions & 0 deletions assets/closed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/open.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 119 additions & 0 deletions assets/open.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/PictureManager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class PictureManager:
def __init__(self, is_open_func, open_image_path, closed_image_path):
self.is_open_func = is_open_func
self.open_image_path = open_image_path
self.closed_image_path = closed_image_path

def get_image(self):
if self.is_open_func():
with open(self.open_image_path, "rb") as image_file:
return image_file.read()
else:
with open(self.closed_image_path, "rb") as image_file:
return image_file.read()
3 changes: 3 additions & 0 deletions src/SpaceApiEntry.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ def set_is_open(self, is_open):

def set_lastchange(self, lastchange):
self.data["state"]["lastchange"] = lastchange

def is_open(self):
return self.data["state"]["open"]
27 changes: 23 additions & 4 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

import json

from src.SpaceApiEntry import SpaceApiEntry
from src.SpaceStatusObserver import SpaceStatusObserver
from PictureManager import PictureManager
from SpaceApiEntry import SpaceApiEntry
from SpaceStatusObserver import SpaceStatusObserver

startup_timestamp = datetime.now()

Expand Down Expand Up @@ -84,12 +85,24 @@ def get(self):
self.finish()


def make_app(observer):
class PictureHandler(tornado.web.RequestHandler, ABC):
# noinspection PyAttributeOutsideInit
def initialize(self, picture_manager):
self.picture_manager = picture_manager

def get(self):
self.set_header("Content-Type", "image/png")
self.write(self.picture_manager.get_image())
self.finish()


def make_app(observer, picture_manager):
version_path = r"/v[0-9]"
return tornado.web.Application([
(version_path + r"/health", HealthHandler),
(version_path + r"/oas3", Oas3Handler),
(r"/", SpaceAPIHandler, dict(observer=observer)),
(r"/state.png", PictureHandler, dict(picture_manager=picture_manager)),
])


Expand Down Expand Up @@ -121,7 +134,13 @@ def main():
)
observer.start()

app = make_app(observer)
picture_manager = PictureManager(
is_open_func=observer.space_api_entry.is_open,
open_image_path="../assets/open.png",
closed_image_path="../assets/closed.png"
)

app = make_app(observer, picture_manager)
sockets = tornado.netutil.bind_sockets(arg_port, '')
server = tornado.httpserver.HTTPServer(app)
server.add_sockets(sockets)
Expand Down

0 comments on commit 07e8324

Please sign in to comment.