<commit_before>from flask import Flask, request, jsonify
import serial
import re

DEVICE = '/dev/ttyATH0'

SPEED = 115200

COMMANDS = {
        "power": {"on": "POW=ON", "off": "POW=OFF", "status": "POW=?"},
        "blank": {"on": "BLANK=ON", "off": "BLANK=OFF", "status": "BLANK=?"},
        "model": {"status": "MODELNAME=?"},
        "menu":  {
                    "on": "MENU=ON",
                    "off": "MENU=OFF",
                    "up": "UP",
                    "down": "DOWN",
                    "right": "RIGHT",
                    "left": "LEFT",
                    "enter": "ENTER"
                }
}

app = Flask(__name__, static_url_path='/')
ser = serial.Serial(DEVICE, SPEED, timeout=1)

def read_serial(command):
    """Send a command via serial and return the answer"""
    ser_command = "\r*%s#\r" % command
    app.logger.debug(ser_command)
    ser.write(ser_command)
    out = ser.readlines()
    app.logger.debug(out)
    m = re.search("(\w+)=(\w+)", out[-1])
    answer = m.group(2)
    return answer

def write_serial(command):
    """Send a command via serial"""
    ser_command = "\r*%s#\r" % command
    app.logger.debug(ser_command)
    ser.write(ser_command)

@app.route('/power', methods=['GET'])
@app.route('/power/<status>', methods=['PUT'])
def power(status=None):
    if request.method == 'GET':
        answer = read_serial(COMMANDS["power"]["status"])
        return jsonify({"status": answer.lower()})
    else:
        if status == 'on':
            write_serial(COMMANDS["power"]["on"])
            return jsonify({"status": "ok"})
        elif status == 'off':
            write_serial(COMMANDS["power"]["off"])
            return jsonify({"status": "ok"})

@app.route('/blank', methods=['GET'])
@app.route('/blank/<status>', methods=['PUT'])
def blank(status=None):
    if request.method == 'GET':
        answer = read_serial(COMMANDS["blank"]["status"])
        return jsonify({"status": answer.lower()})
    else:
        if status == 'on':
            write_serial(COMMANDS["blank"]["on"])
            return jsonify({"status": "ok"})
        elif status == 'off':
            write_serial(COMMANDS["blank"]["off"])
            return jsonify({"status": "ok"})

@app.route('/menu/<action>', methods=['PUT'])
def menu(action=None):
    if action in COMMANDS["menu"]:
        write_serial(COMMANDS["menu"][action])
        return jsonify({"status": "ok"})

@app.route('/modelname', methods=['GET'])
def modelname(status=None):
    if request.method == 'GET':
        answer = read_serial(COMMANDS["modelname"]['status'])
        return jsonify({"status": answer.lower()})

if __name__ == "__main__":
    app.run(host='0.0.0.0')

ser.close()
<commit_msg>Now serving static files on port 80<commit_after>from flask import Flask, request, jsonify, send_file
import serial
import re

DEVICE = '/dev/ttyATH0'

SPEED = 115200

COMMANDS = {
        "power": {"on": "POW=ON", "off": "POW=OFF", "status": "POW=?"},
        "blank": {"on": "BLANK=ON", "off": "BLANK=OFF", "status": "BLANK=?"},
        "model": {"status": "MODELNAME=?"},
        "menu":  {
                    "on": "MENU=ON",
                    "off": "MENU=OFF",
                    "up": "UP",
                    "down": "DOWN",
                    "right": "RIGHT",
                    "left": "LEFT",
                    "enter": "ENTER"
                }
}

app = Flask(__name__, static_url_path='')
ser = serial.Serial(DEVICE, SPEED, timeout=1)

def read_serial(command):
    """Send a command via serial and return the answer"""
    ser_command = "\r*%s#\r" % command
    ser.write(ser_command)
    out = ser.readlines()
    m = re.search("(\w+)=(\w+)", out[-1])
    answer = m.group(2)
    return answer

def write_serial(command):
    """Send a command via serial"""
    ser_command = "\r*%s#\r" % command
    ser.write(ser_command)

@app.route("/")
def index():
    return send_file("static/index.html")

@app.route('/power', methods=['GET'])
@app.route('/power/<status>', methods=['PUT'])
def power(status=None):
    if request.method == 'GET':
        answer = read_serial(COMMANDS["power"]["status"])
        return jsonify({"status": answer.lower()})
    else:
        if status == 'on':
            write_serial(COMMANDS["power"]["on"])
            return jsonify({"status": "ok"})
        elif status == 'off':
            write_serial(COMMANDS["power"]["off"])
            return jsonify({"status": "ok"})

@app.route('/blank', methods=['GET'])
@app.route('/blank/<status>', methods=['PUT'])
def blank(status=None):
    if request.method == 'GET':
        answer = read_serial(COMMANDS["blank"]["status"])
        return jsonify({"status": answer.lower()})
    else:
        if status == 'on':
            write_serial(COMMANDS["blank"]["on"])
            return jsonify({"status": "ok"})
        elif status == 'off':
            write_serial(COMMANDS["blank"]["off"])
            return jsonify({"status": "ok"})

@app.route('/menu/<action>', methods=['PUT'])
def menu(action=None):
    if action in COMMANDS["menu"]:
        write_serial(COMMANDS["menu"][action])
        return jsonify({"status": "ok"})

@app.route('/modelname', methods=['GET'])
def modelname(status=None):
    if request.method == 'GET':
        answer = read_serial(COMMANDS["modelname"]['status'])
        return jsonify({"status": answer.lower()})

if __name__ == "__main__":
    app.run(host='0.0.0.0', port='80')

ser.close()

{'fraction_non_alphanumeric': 0.20239190432382706, 'fraction_numerical': 0.005887764489420423, 'mean_word_length': 2.736082474226804, 'pattern_counts': {'":': 54, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3887159', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1774, 'n_words': 368}