💾 Archived View for going-flying.com › files › vfdserver.py captured on 2020-09-24 at 00:47:23.
-=-=-=-=-=-=-
#!/usr/bin/env python3 ''' vfdserver (c) 2020 Matthew J. Ernisse <matt@going-flying.com> All Rights Reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ''' import datetime import http import http.server import serial import sys import urllib.parse vfdSerial = None class SerialController(object): def __init__(self, serialPort): self.serial = serial.Serial( port=serialPort, baudrate=115200, timeout=1.0, write_timeout=1.0 ) def write(self, s): if type(s) is not str: raise ValueError('message MUST be str') now = datetime.datetime.now() dateStr = now.strftime('%Y-%m-%d @%H:%M:%S') cmd = f'MSG 1 {dateStr}\r\n'.encode('utf-8') self.serial.write(cmd) cmd = f'MSG 2 {s}\r\n'.encode('utf-8') self.serial.write(cmd) class VFDHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): req = urllib.parse.urlparse(self.path) if req.path != '/update': response = 'Not Found'.encode('utf-8') self.send_response(http.HTTPStatus.NOT_FOUND) self.send_header( 'Content-Type', 'text/plain' ) self.end_headers() self.wfile.write(response) self.log_request(404, 0) return qs = urllib.parse.parse_qs(req.query) print(qs) if not 'msg' in qs.keys(): response = 'Invalid Request Data'.encode('utf-8') self.send_response(http.HTTPStatus.BAD_REQUEST) self.send_header( 'Content-Type', 'text/plain' ) self.end_headers() self.wfile.write(response) self.log_request(400, 0) return try: vfdSerial.write(qs['msg'][0]) except Exception as e: response = str(e).encode('utf-8') self.send_response( http.HTTPStatus.INTERNAL_SERVER_ERROR ) self.send_header( 'Content-Type', 'text/plain' ) self.end_headers() self.wfile.write(response) self.log_request(500, 0) return self.send_response(http.HTTPStatus.NO_CONTENT) self.end_headers() self.log_request(204, 0) if __name__ == '__main__': vfdSerial = SerialController(sys.argv[1]) server = http.server.ThreadingHTTPServer( ('0.0.0.0', 80), VFDHandler ) server.serve_forever()