-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
205 lines (170 loc) · 5.07 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import time
import os
import subprocess
import json
from flask import Flask, request, send_from_directory
from flask_cors import CORS
import ink_display as ink
import logging.config
from PIL import Image
from WeatherData import WeatherData
app = Flask(__name__)
CORS(app)
logging.basicConfig(level=logging.DEBUG)
ink = ink.InkDisplay()
cwd = os.getcwd()
image = r"/assets/images/test/raspilogo.bmp"
font = "Font.ttc"
wd = WeatherData()
DEBUG = True
def setup_logging() -> None:
config_file = "logging.json"
with open(config_file) as f:
config = json.load(f)
logging.config.dictConfig(config)
if DEBUG:
LOGGER = logging.getLogger(__name__)
setup_logging()
LOGGER.info("Logger started")
@app.route("/favicon.ico")
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico')
@app.route("/")
def home():
return "Nothing here yet"
@app.route("/test", methods=["GET"])
def test() -> str:
try:
# INIT/CLEAR
clean(sleep=False)
# DISPLAY IMAGE
ink.display_image(image)
time.sleep(5)
ink.clear()
# CREATE DRAW
ink.draw_image = ink.blank_image()
draw = ink.draw(ink.draw_image)
ink.draw_text(
(5, 0), text="hello", font=font, size=24, color="#FF0000", draw=draw
)
ink.draw_text(
(5, 30), text="world", font=font, size=16, color="#FF0000", draw=draw
)
LOGGER.info("drawing draw image")
draw.line([(5, 170), (80, 245)], fill="#0000FF")
ink.display_draw(ink.draw_image)
time.sleep(5)
ink.clear()
# CREATE NEW DRAW
ink.draw_image = ink.blank_image()
draw = ink.draw(ink.draw_image)
ink.draw_text(
(5, 0),
text="goodbye world",
font=font,
size=36,
color="#00FF00",
draw=draw,
)
ink.display_draw(ink.draw_image)
time.sleep(5)
ink.clear()
# SLEEP
reset()
clean(sleep=True)
except IOError as e:
LOGGER.exception(e)
return "Success"
@app.route("/text", methods=["GET"])
def display_text() -> str:
"""Creates text on an image
Returns:
str: "Success"
"""
try:
text = str(request.args["text"])
color = "#" + str(request.args["color"])
pos = tuple([int(i) for i in str(request.args["pos"]).split(",")])
size = int(request.args["size"])
center = str(request.args["center"]).lower()
if center != "false":
px = size / 72 * 96
px_total = len(text) * px
pos = (pos[0] - px_total / 4, pos[1] - px / 2.66)
if DEBUG:
LOGGER.info("Displaying text: %s", text)
LOGGER.info("Displaying color: %s", color)
LOGGER.info("Displaying position: %s", pos)
LOGGER.info("Displaying size: %s", size)
LOGGER.info("Displaying center: %s", center)
# draw_image = ink.blank_image()
draw = ink.draw(ink.draw_image)
ink.draw_text(pos, text=text, font=font,
size=size, color=color, draw=draw)
# ink.display_draw(ink.draw_image)
except IOError as e:
return LOGGER.info(e)
return "Success"
@app.route("/display", methods=["GET"])
def display() -> str:
ink.init()
ink.display_draw(ink.draw_image)
ink.sleep()
return "Success"
@app.route("/reset", methods=["GET"])
def reset() -> str:
ink.draw_image = ink.blank_image()
return "Success"
@app.route("/ip", methods=["GET"])
def get_ip() -> str:
ip = subprocess.check_output(
[
"sh",
"get_ip.sh",
]
).decode("utf-8")
clean(False)
draw = ink.draw(ink.draw_image)
ink.draw_text((200, 240), text=ip, font=font,
size=64, color="#000000", draw=draw)
ink.display_draw(ink.draw_image)
ink.sleep()
return ip
@app.route("/upload_image", methods=["GET", "POST"])
def display_upload_image() -> str:
clean(False)
if request.method == "GET":
if DEBUG:
LOGGER.info("Displaying image: %s", image)
if request.method == "POST":
r = request.files["image"]
image_name = r.filename
post_image = Image.open(r)
save_loc = f"{cwd}/assets/images/upload/{image_name}"
if os.path.exists(save_loc):
os.remove(save_loc)
post_image.save(save_loc)
image = save_loc
if DEBUG:
LOGGER.info("Displaying image from POST: %s", image_name)
ink.display_image(image)
ink.sleep()
return "Success"
@app.route("/update_weather", methods=["GET"])
def update_weather():
clean(False)
wd.get_response()
wd.create_forecast()
ink.display_image(cwd + "/assets/images/weather_forecast/forecast.png")
ink.sleep()
return "Success"
@app.route("/clear", methods=["GET"])
def clean(sleep: bool = True) -> str:
ink.init()
reset()
ink.clear()
if sleep:
ink.sleep()
return "Success"
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=80)