# Buggy code
def page_lcd():
    """Display LCD output settings"""
    lcd = LCD.query.all()
    pid = PID.query.all()
    relay = Relay.query.all()
    sensor = Sensor.query.all()

    display_order = csv_to_list_of_int(DisplayOrder.query.first().lcd)

    form_add_lcd = flaskforms.LCDAdd()
    form_mod_lcd = flaskforms.LCDMod()

    if request.method == "POST":
        if not flaskutils.user_has_permission("edit_controllers"):
            return redirect(url_for("general_routes.home"))

        if form_add_lcd.add.data:
            flaskutils.lcd_add(form_add_lcd.quantity.data)
        elif form_mod_lcd.save.data:
            flaskutils.lcd_mod(form_mod_lcd)
        elif form_mod_lcd.delete.data:
            flaskutils.lcd_del(form_mod_lcd.lcd_id.data)
        elif form_mod_lcd.reorder_up.data:
            flaskutils.lcd_reorder(form_mod_lcd.lcd_id.data, display_order, "up")
        elif form_mod_lcd.reorder_down.data:
            flaskutils.lcd_reorder(form_mod_lcd.lcd_id.data, display_order, "down")
        elif form_mod_lcd.activate.data:
            flaskutils.lcd_activate(form_mod_lcd.lcd_id.data)
        elif form_mod_lcd.deactivate.data:
            flaskutils.lcd_deactivate(form_mod_lcd.lcd_id.data)
        elif form_mod_lcd.reset_flashing.data:
            flaskutils.lcd_reset_flashing(form_mod_lcd.lcd_id.data)
        return redirect("/lcd")

    return render_template(
        "pages/lcd.html",
        lcd=lcd,
        measurements=MEASUREMENTS,
        pid=pid,
        relay=relay,
        sensor=sensor,
        displayOrder=display_order,
        form_add_lcd=form_add_lcd,
        form_mod_lcd=form_mod_lcd,
    )


# Traceback (IOError)
2017-06-07 11:03:18,941 - mycodo.sensors.raspi - ERROR - RaspberryPiCPUTemp.get_measurement() method raised IOError: [Errno 24] Too many open files: '/sys/class/thermal/thermal_zone0/temp'
2017-06-07 11:03:20,138 - mycodo.pid_1 - ERROR - Run Error: (sqlite3.OperationalError) unable to open database file

========= Remote Traceback (1) =========
Traceback (most recent call last):
File "/var/www/mycodo/env/local/lib/python2.7/site-packages/rpyc/core/protocol.py", line 329, in _dispatch_request
File "/var/www/mycodo/env/local/lib/python2.7/site-packages/rpyc/core/protocol.py", line 583, in _handle_call
File "/var/www/mycodo/mycodo/mycodo_daemon.py", line 201, in exposed_relay_off
File "/var/www/mycodo/mycodo/mycodo_daemon.py", line 553, in relay_off
File "/var/www/mycodo/mycodo/controller_relay.py", line 348, in relay_on_off
self.check_conditionals(relay_id, duration)
File "/var/www/mycodo/mycodo/controller_relay.py", line 397, in check_conditionals
for each_conditional in conditionals.all():

# Fixed code
def page_lcd():
    """Display LCD output settings"""
    lcd = LCD.query.all()
    pid = PID.query.all()
    relay = Relay.query.all()
    sensor = Sensor.query.all()

    display_order = csv_to_list_of_int(DisplayOrder.query.first().lcd)

    form_add_lcd = flaskforms.LCDAdd()
    form_mod_lcd = flaskforms.LCDMod()

    measurements = MEASUREMENTS

    # Add custom measurement and units to list (From linux command sensor)
    for each_sensor in sensor:
        if (
            each_sensor.cmd_measurement
            and each_sensor.cmd_measurement not in MEASUREMENTS
        ):
            if each_sensor.cmd_measurement and each_sensor.cmd_measurement_units:
                measurements.update({"LinuxCommand": [each_sensor.cmd_measurement]})

    if request.method == "POST":
        if not flaskutils.user_has_permission("edit_controllers"):
            return redirect(url_for("general_routes.home"))

        if form_add_lcd.add.data:
            flaskutils.lcd_add(form_add_lcd.quantity.data)
        elif form_mod_lcd.save.data:
            flaskutils.lcd_mod(form_mod_lcd)
        elif form_mod_lcd.delete.data:
            flaskutils.lcd_del(form_mod_lcd.lcd_id.data)
        elif form_mod_lcd.reorder_up.data:
            flaskutils.lcd_reorder(form_mod_lcd.lcd_id.data, display_order, "up")
        elif form_mod_lcd.reorder_down.data:
            flaskutils.lcd_reorder(form_mod_lcd.lcd_id.data, display_order, "down")
        elif form_mod_lcd.activate.data:
            flaskutils.lcd_activate(form_mod_lcd.lcd_id.data)
        elif form_mod_lcd.deactivate.data:
            flaskutils.lcd_deactivate(form_mod_lcd.lcd_id.data)
        elif form_mod_lcd.reset_flashing.data:
            flaskutils.lcd_reset_flashing(form_mod_lcd.lcd_id.data)
        return redirect("/lcd")

    return render_template(
        "pages/lcd.html",
        lcd=lcd,
        measurements=measurements,
        pid=pid,
        relay=relay,
        sensor=sensor,
        displayOrder=display_order,
        form_add_lcd=form_add_lcd,
        form_mod_lcd=form_mod_lcd,
    )