-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathrun_server.py
146 lines (92 loc) · 3.81 KB
/
run_server.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
import os
import time
import datetime
from PIL import Image
from http.server import HTTPServer, BaseHTTPRequestHandler
import secrets
import socket
from weather_landscape import WeatherLandscape
SERV_IPADDR = "0.0.0.0"
SERV_PORT = 3355
EINKFILENAME = "test.bmp"
USERFILENAME = "test1.bmp"
FAVICON = "favicon.ico"
FILETOOOLD_SEC = 60*10
WEATHER = WeatherLandscape()
class WeatherLandscapeServer(BaseHTTPRequestHandler):
def do_GET_sendfile(self,filepath:str,mimo:str):
try:
f = open(filepath, "rb")
databytes = f.read()
f.close()
except Exception as e:
databytes = None
print("File read error '%s' :%s" % (filepath,str(e)))
if (databytes!=None):
self.send_response(200)
self.send_header("Content-type", mimo)
else:
self.send_response(404)
self.end_headers()
if (databytes!=None):
self.wfile.write(databytes)
def do_GET(self):
if self.path == '/':
self.path = '/index.html'
print("GET:",self.path)
if (self.path.startswith('/'+FAVICON)):
self.do_GET_sendfile(FAVICON,"image/ico")
return
if (self.path.startswith('/index.html')):
self.send_response(200)
self.end_headers()
self.wfile.write(bytes(self.IndexHtml(), 'utf-8'))
return
if (self.path.startswith('/'+EINKFILENAME)) or (self.path.startswith('/'+USERFILENAME)):
self.CreateWeatherImages()
file_name = WEATHER.TmpFilePath(self.path[1:])
self.do_GET_sendfile(file_name ,"image/bmp")
return
print("Path not accessible:",self.path)
self.send_response(403)
self.end_headers()
def IsFileTooOld(self, filename):
return (not os.path.isfile(filename)) or ( (time.time() - os.stat(filename).st_mtime) > FILETOOOLD_SEC )
def CreateWeatherImages(self):
user_file_name = WEATHER.TmpFilePath(USERFILENAME)
eink_file_name = WEATHER.TmpFilePath(EINKFILENAME)
if not self.IsFileTooOld(user_file_name):
return
img = WEATHER.MakeImage()
img.save(user_file_name)
img = img.rotate(-90, expand=True)
img = img.transpose(Image.FLIP_TOP_BOTTOM)
img.save(eink_file_name)
def IndexHtml(self):
body = '<h1>Weather as Landscape</h1>'
body+='<p>Place: '+("%.4f" % secrets.OWM_LAT) +' , '+("%.4f" % secrets.OWM_LON)+'</p>'
body+='<p><img src="'+USERFILENAME+'" alt="Weather" "></p>'
body+='<p>ESP32 URL: <span id="eink"></span></p>'
body+='<script> document.getElementById("eink").innerHTML = window.location+"'+EINKFILENAME+'" ;</script>'
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Weather as Landscape</title>
</head>
<body> """ + body + """
</body>
</html>"""
#todo: implement support for multiple network interfaces
def get_my_ips():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8', 80))
yield s.getsockname()[0]
finally:
s.close()
httpd = HTTPServer((SERV_IPADDR,SERV_PORT),WeatherLandscapeServer)
for ip in get_my_ips():
print(r"Serving at http://%s:%i/" % (ip,SERV_PORT))
httpd.serve_forever()