#!/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 app = Flask(__name__) @app.route('/') def myCar_root(): return 'myCar project Page' @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', methods=['POST']) #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