#!/usr/bin/python3 import configparser import lib.config import lib.app_control # we need to initially load the configuration file. # This is loaded via a local symlink myCar_config = configparser.ConfigParser() myCar_config.read('data/etc/myCar.conf') # fill in config values with defaults myCar_config = lib.config.myCar_read_config_defaults(myCar_config) import atexit atexit.register(lib.app_control.myCar_exit, myCar_config) from flask import Flask from flask import url_for from flask import render_template from flask import request from flask import redirect app = Flask(__name__) @app.route('/') def myCar_root(): return render_template('index.html') @app.route('/bluetooth/controller') def myCar_bluetooth_controller(): # this method will return available bluetooth devices as a list for the web template output import bluew #return len(bluew.controllers()) return render_template('bluetooth/controller.html', bt_ctrl=bluew.controllers()) @app.route('/bluetooth/devices') def myCar_bluetooth_devices(): # this method will return available bluetooth devices as a list for the web template output import bluew #return len(bluew.controllers()) bt_ctrl_powered = False for dev in bluew.controllers(): if dev.Powered == True: bt_ctrl_powered = True if bt_ctrl_powered == False: return render_template('bluetooth/controller.html', bt_ctrl=bluew.controllers(), bt_error="All Bluetooth Adapters are powered off") else: return render_template('bluetooth/devices.html', bt_dev=bluew.devices()) @app.route('/bluetooth/connect', methods=['POST']) def myCar_bluetooth_connect(): # this method will connect to a bt device in the following order: # - pair # - trust # - connect #try: # request #except NameError: # request = None try: if request is not None: import bluew #try: bt_dst_mac = request.form['bt_dst_mac'] # pair bluew.pair(bt_dst_mac) # trust bluew.trust(bt_dst_mac) # connect bluew.connect(bt_dst_mac) retstr = 'connected to '.join(bt_dst_mac) else: ret = 'Mac Address not provided' except NameError: ret = render_template('bluetooth/devices.html', bt_dev=bluew.devices(),bt_error="No MAC provided to connect to") except bluew.errors.DeviceNotAvailable: ret = render_template('bluetooth/devices.html', bt_dev=bluew.devices(),bt_error='Bluetooth device ' + bt_dst_mac +' is not available') return ret @app.route('/conf') def myCar_conf(): # this method reads the configuration file # !!! beware due to stupidity in my mind this method also holds the # !!! default vaules when the config file is not present return render_template('conf/main.html', config=myCar_config, sections=myCar_config.sections()) @app.route('/confsave', methods=['POST']) def myCar_conf_save(): # this method saves the configuration settings when posted, then redirects to context /conf if request is not None and request.form['configsend'] == 'save': myCar_config['paths'] = { 'app' : request.form['[paths][app]'], 'log' : request.form['[paths][log]'], 'record' : request.form['[paths][record]'] } myCar_config['connections'] = { 'bt_dev' : request.form['[connections][bt_dev]'], 'gps_dev' : request.form['[connections][gps_dev]'], 'wifi_pwd' : request.form['[connections][wifi_pwd]'] } lib.config.myCar_save_config(myCar_config) return redirect(url_for('myCar_conf'), code=302)