forked from Techzune/PiMon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
120 lines (94 loc) · 3.95 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
import os
import sys
import time
import redis
import logging
from logging.handlers import RotatingFileHandler
from flask import Flask, render_template, Response, send_from_directory
from flask_assets import Environment, Bundle
import json
# import function to start reading/writing from serial port
import arduinoPoller
red = redis.StrictRedis()
dirName = '/tmp/logFiles'
if not(os.path.exists(dirName)):
os.makedirs(dirName)
logger = logging.getLogger()
handler = RotatingFileHandler(filename=os.path.join(dirName, 'PimonGeneral.log'),
maxBytes=(1024*1024),
backupCount=1)
formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s", datefmt="%m/%d/%Y %I:%M:%S")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
unoLogger = logging.getLogger('Arduino')
handler = RotatingFileHandler(filename=os.path.join(dirName, 'ArduinoDataStream.log'),
maxBytes=(1024*1024),
backupCount=1)
formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s", datefmt="%m/%d/%Y %I:%M:%S")
handler.setFormatter(formatter)
unoLogger.addHandler(handler)
unoLogger.setLevel(logging.DEBUG)
piLogger = logging.getLogger('Pi')
handler = RotatingFileHandler(filename=os.path.join(dirName, 'PiDataStream.log'),
maxBytes=(1024*1024),
backupCount=1)
formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s", datefmt="%m/%d/%Y %I:%M:%S")
handler.setFormatter(formatter)
piLogger.addHandler(handler)
piLogger.setLevel(logging.DEBUG)
def create_app():
# use error as stdout
sys.stdout = sys.stderr
# create Flask application
app = Flask(__name__)
# render SCSS
scss = Bundle('site.scss', filters='libsass', output='site.css')
assets = Environment(app)
assets.url = app.static_url_path
assets.register('scss_all', scss)
# Spin up polling thread
arduinoPoller.setupArduinoPolling(0.333)
# route to favicon
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico')
@app.route('/')
def home():
"""Page: home of site"""
return render_template('index.html')
@app.route("/stream/console")
def console_stream():
"""Stream: continuous pipeline stream
This stream is connected to by client via Javascript to constantly download messages (Responses) from the server.
See ./templates/index.html for Javascript code.
"""
def events():
while True:
logger.info('Getting redis data streams')
arduinoData = str(red.get('msg'), 'utf-8')
if (arduinoData != None):
arduinoData = json.loads(arduinoData)
# Record each data item to the log file
for key in arduinoData:
unoLogger.info(str(key) + ' : ' + str(arduinoData[key]))
monitorData = get_pi_logs(arduinoData)
# Add a terminator so that messages do not collide in the JS
yield json.dumps(monitorData) + '\n'
arduinoPoller.keepPollAlive()
time.sleep(.1)
return Response(events(), mimetype='text/plain')
return app
def get_pi_logs(dataDictionary):
logger.info('Getting pi logs')
piLogStreams = red.keys(pattern='Pi*')
for streamName in piLogStreams:
streamName = str(streamName, 'utf-8')
dataDictionary[streamName] = str(red.get(streamName), 'utf-8')
# Record each data item to the log file
piLogger.info(str(streamName) + ' : ' + str(dataDictionary[streamName]))
return dataDictionary
if __name__ == '__main__':
from waitress import serve
logger.info('Starting server')
serve(create_app(), host='0.0.0.0', port='80')