#!/usr/bin/env python3 """The MIT License Copyright (c) 2016 Arti Zirk Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from pprint import pprint import sys import json import random import base64 import os.path from html import escape as html_escape from collections import deque from urllib.parse import parse_qs import serial max_nr_messages = 25 html = """ Temperature

Temperature


Source code, Good luck! """ temps = deque(maxlen=10) def stream(dev): with serial.Serial(dev, timeout=1) as s: while True: temperature = s.readline().decode().strip() if not temperature: continue print("temperature", temperature) temps.append(temperature) def application(env, start_response): if env["REQUEST_METHOD"] == "POST": try: l = int(env.get('CONTENT_LENGTH', 0)) except KeyError: start_response('411 Length Required', [('Content-Type', 'application/json')]) return [json.dumps({"error":{"code":411, "message": "Content-Length header missing"}}).encode()] if l > 150: start_response('413 Payload Too Large', [('Content-Type', 'application/json')]) return [json.dumps({"error":{"code":413, "message": "Payload Too Large"}}).encode()] message = env['wsgi.input'].read(l).decode() if not message: start_response('400 Bad Request', [('Content-Type', 'application/json')]) return [json.dumps({"error":{"code":400, "message": "message not provided"}}).encode()] try: message = json.loads(message)["message"] except json.decoder.JSONDecodeError: pass except KeyError: start_response('400 Bad Request', [('Content-Type', 'application/json')]) return [json.dumps({"error":{"code":400, "message": "message key missing from json payload data"}}).encode()] if message.startswith("message="): message = parse_qs(message)["message"][0] messages.appendleft(html_escape(message.strip())) #start_response("200 OK", [('Content-Type', 'application/json')]) if env["PATH_INFO"] == "/temps.json": start_response('200 OK', [('Content-Type', 'application/json')]) return [json.dumps(list(temps)).encode()] if env["PATH_INFO"] == "/favicon.ico": start_response('200 OK', [('Content-Type', 'image/x-icon')]) return [] elif env["PATH_INFO"] == "/smoothie.js": start_response('200 OK', [('Content-Type', 'application/javascript')]) with open("smoothie.js", "rb") as f: return [f.read()] elif env["PATH_INFO"] == "/gauge.min.js": start_response('200 OK', [('Content-Type', 'application/javascript')]) with open("gauge.min.js", "rb") as f: return [f.read()] elif env["PATH_INFO"] == "/fetch.js": start_response('200 OK', [('Content-Type', 'application/javascript')]) with open("fetch.js", "rb") as f: return [f.read()] elif env["PATH_INFO"] == "/main.js": start_response('200 OK', [('Content-Type', 'application/javascript')]) with open("main.js", "rb") as f: return [f.read()] start_response('200 OK', [('Content-Type', 'text/html')]) return [html.format().encode()] if __name__ == "__main__": if len(sys.argv) < 2: print("Usage: python3 main.py /dev/") exit(1) ser_dev = sys.argv[1] if not os.path.exists(ser_dev): print("{} is not a valid tty device".format(ser_dev)) exit(1) import threading t = threading.Thread(target=stream, args=[ser_dev]) # classifying as a daemon, so they will die when the main dies t.daemon = True # begins, must come after daemon definition t.start() from wsgiref.simple_server import make_server httpd = make_server('0.0.0.0', 8080, application) print("Serving on http://0.0.0.0:8080/") httpd.serve_forever()