-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgasmeter_http_server.py
executable file
·146 lines (127 loc) · 5.36 KB
/
gasmeter_http_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
#!/usr/bin/python3
from http.server import BaseHTTPRequestHandler,HTTPServer
import logging
import logging.handlers #Needed for Syslog
import sys
from pathlib import Path
# Can call the camera Python script asynchronously using subprocess
# or synchronously (i.e. directly). For now call directly.
#import subprocess
import gasmeter_PiCamera
#Configure the port number the http server will listen on
PORT_NUMBER = 8080
#Configure file name of camera image (without filename extension)
GASMETER_IMAGE_NAME = "gasmeter_last" #Note: file name extension (.jpg) is in URL request
#Configure directory that http server will go looking for files:
GASMETER_IMAGE_DIRECTORY = "/run/shm" #Leave off last "/".
#Configure the file where CPU Temperature can be queried for
CPU_TEMP_DIRECTORY = "/sys/class/thermal/thermal_zone0/temp"
# Setup Logger for this server.
# If you want to see output to the Console, set CONSOLE = 1
# Otherwise logs go to local syslog server.
# If you want debug level output, then change the last lines
# _LOGGER.setLevel from NOTSET to DEBUG. Can also set to INFO.
CONSOLE = 0
_LOGGER = logging.getLogger(__name__)
if CONSOLE:
formatter = \
logging.Formatter('%(message)s')
handler1 = logging.StreamHandler(sys.stdout)
handler1.setFormatter(formatter)
handler1.setLevel(logging.NOTSET)
#handler1.setLevel(logging.DEBUG)
_LOGGER.addHandler(handler1)
else:
formatter2 = \
logging.Formatter('%(levelname)s %(asctime)s %(filename)s - %(message)s')
handler2 = logging.handlers.SysLogHandler(address = '/dev/log')
handler2.setFormatter(formatter2)
handler2.setLevel(logging.NOTSET)
_LOGGER.addHandler(handler2)
#LOGGER.setLevel(logging.DEBUG)
_LOGGER.setLevel(logging.INFO)
#_LOGGER.setLevel(logging.NOTSET)
#End of Configs
#Handle an incoming request
class myHandler(BaseHTTPRequestHandler):
"""Handle the incoming HTTP GET or POST Request."""
def process_incoming(self):
"""Process the incoming request."""
_LOGGER.info("Received Path: %s", self.path)
_LOGGER.debug("Received The Following Headers: ")
_LOGGER.debug("%s", self.headers)
if (self.path.find("/api/capture_image") !=-1 ):
_LOGGER.info("Running gasmeter_PiCamera .....")
#p1 = subprocess.Popen(["/usr/bin/python3","/home/pi/gasmeter_PiCamera.py"] )
gasmeter_PiCamera.main()
_LOGGER.debug("Done.")
self.send_response(200)
#Let's return something even if null.
mimetype='text/html'
self.send_header('Content-type',mimetype)
self.end_headers()
self.wfile.write(str.encode('Capturing Image...Image Captured.')) #encode string to bytes
elif (self.path.find( "/api/cpu_temp" ) !=-1 ):
_LOGGER.info("Getting CPU Temperature.....")
self.send_response(200)
mimetype='text/html'
self.send_header('Content-type',mimetype)
self.end_headers()
with open(CPU_TEMP_DIRECTORY,'r') as f:
f_cpu_temp = f.read()
cpu_temp_str = str(float(f_cpu_temp)/1000) #Pi returns 34704. Convert to 34.704
self.wfile.write(str.encode(cpu_temp_str)) #encode string to bytes
elif (self.path.find( GASMETER_IMAGE_NAME ) !=-1 ):
_LOGGER.info("Retrieving " + self.path)
filename = GASMETER_IMAGE_DIRECTORY + self.path #path starts with "/"
if( Path(filename).is_file() ):
self.send_response(200)
#return something even if null.
if self.path.endswith(".jpg"):
mimetype='image/jpeg'
elif self.path.endswith(".png"):
mimetype='image/png'
else:
mimetype='image/jpg'
self.send_header('Content-type',mimetype)
self.end_headers()
with open(filename, 'rb') as file_handle:
self.wfile.write(file_handle.read())
else:
self.send_error(404,'The requested file was not found: %s' % self.path)
else:
self.send_error(404,'The URL request could not be processed: %s' % self.path)
return
def do_GET(self):
"""Process incoming GET requests."""
_LOGGER.debug("Received a GET Request")
return self.process_incoming()
def do_POST(self):
"""Process incoming POST requests."""
return self.process_incoming()
def log_message(self, format, *args):
"""
Remove this method if you want to see
normal http.server output, otherwise
override the http.server output with
your own logger.
"""
_LOGGER.debug("%s - - [%s] %s\n" %
(self.client_address[0],
self.log_date_time_string(),
format%args))
return
#Main Program
try:
#Create and startup the server and define the handler to manage
# the incoming http requests.
server = HTTPServer(('', PORT_NUMBER), myHandler)
_LOGGER.info("Started http server on port: %s", PORT_NUMBER)
#Run forever
server.serve_forever()
except KeyboardInterrupt:
_LOGGER.info(' received, shutting down the server')
finally:
#print("Closing the socket. We're Done!")
_LOGGER.info("Closing the socket. We're Done!")
server.socket.close()