myCar.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/python3
  2. import configparser
  3. import lib.config
  4. import lib.app_control
  5. # we need to initially load the configuration file.
  6. # This is loaded via a local symlink
  7. myCar_config = configparser.ConfigParser()
  8. myCar_config.read('data/etc/myCar.conf')
  9. # fill in config values with defaults
  10. myCar_config = lib.config.myCar_read_config_defaults(myCar_config)
  11. import atexit
  12. atexit.register(lib.app_control.myCar_exit, myCar_config)
  13. from flask import Flask
  14. from flask import url_for
  15. from flask import render_template
  16. from flask import request
  17. app = Flask(__name__)
  18. @app.route('/')
  19. def myCar_root():
  20. return 'myCar project Page'
  21. @app.route('/bluetooth/controller')
  22. def myCar_bluetooth_controller():
  23. # this method will return available bluetooth devices as a list for the web template output
  24. import bluew
  25. #return len(bluew.controllers())
  26. return render_template('bluetooth/controller.html', bt_ctrl=bluew.controllers())
  27. @app.route('/bluetooth/devices')
  28. def myCar_bluetooth_devices():
  29. # this method will return available bluetooth devices as a list for the web template output
  30. import bluew
  31. #return len(bluew.controllers())
  32. bt_ctrl_powered = False
  33. for dev in bluew.controllers():
  34. if dev.Powered == True:
  35. bt_ctrl_powered = True
  36. if bt_ctrl_powered == False:
  37. return render_template('bluetooth/controller.html', bt_ctrl=bluew.controllers(), bt_error="All Bluetooth Adapters are powered off")
  38. else:
  39. return render_template('bluetooth/devices.html', bt_dev=bluew.devices())
  40. @app.route('/bluetooth/connect', methods=['POST'])
  41. def myCar_bluetooth_connect():
  42. # this method will connect to a bt device in the following order:
  43. # - pair
  44. # - trust
  45. # - connect
  46. #try:
  47. # request
  48. #except NameError:
  49. # request = None
  50. try:
  51. if request is not None:
  52. import bluew
  53. #try:
  54. bt_dst_mac = request.form['bt_dst_mac']
  55. # pair
  56. bluew.pair(bt_dst_mac)
  57. # trust
  58. bluew.trust(bt_dst_mac)
  59. # connect
  60. bluew.connect(bt_dst_mac)
  61. retstr = 'connected to '.join(bt_dst_mac)
  62. else:
  63. ret = 'Mac Address not provided'
  64. except NameError:
  65. ret = render_template('bluetooth/devices.html', bt_dev=bluew.devices(),bt_error="No MAC provided to connect to")
  66. except bluew.errors.DeviceNotAvailable:
  67. ret = render_template('bluetooth/devices.html', bt_dev=bluew.devices(),bt_error='Bluetooth device ' + bt_dst_mac +' is not available')
  68. return ret
  69. #@app.route('/conf', methods=['POST'])
  70. #def myCar_conf():
  71. # this method reads the configuration file
  72. # !!! beware due to stupidity in my mind this method also holds the
  73. # !!! default vaules when the config file is not present