myCar.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. from flask import redirect
  18. app = Flask(__name__)
  19. @app.route('/')
  20. def myCar_root():
  21. return render_template('index.html')
  22. @app.route('/bluetooth/controller')
  23. def myCar_bluetooth_controller():
  24. # this method will return available bluetooth devices as a list for the web template output
  25. import bluew
  26. #return len(bluew.controllers())
  27. return render_template('bluetooth/controller.html', bt_ctrl=bluew.controllers())
  28. @app.route('/bluetooth/devices')
  29. def myCar_bluetooth_devices():
  30. # this method will return available bluetooth devices as a list for the web template output
  31. import bluew
  32. #return len(bluew.controllers())
  33. bt_ctrl_powered = False
  34. for dev in bluew.controllers():
  35. if dev.Powered == True:
  36. bt_ctrl_powered = True
  37. if bt_ctrl_powered == False:
  38. return render_template('bluetooth/controller.html', bt_ctrl=bluew.controllers(), bt_error="All Bluetooth Adapters are powered off")
  39. else:
  40. return render_template('bluetooth/devices.html', bt_dev=bluew.devices())
  41. @app.route('/bluetooth/connect', methods=['POST'])
  42. def myCar_bluetooth_connect():
  43. # this method will connect to a bt device in the following order:
  44. # - pair
  45. # - trust
  46. # - connect
  47. #try:
  48. # request
  49. #except NameError:
  50. # request = None
  51. try:
  52. if request is not None:
  53. import bluew
  54. #try:
  55. bt_dst_mac = request.form['bt_dst_mac']
  56. # pair
  57. bluew.pair(bt_dst_mac)
  58. # trust
  59. bluew.trust(bt_dst_mac)
  60. # connect
  61. bluew.connect(bt_dst_mac)
  62. retstr = 'connected to '.join(bt_dst_mac)
  63. else:
  64. ret = 'Mac Address not provided'
  65. except NameError:
  66. ret = render_template('bluetooth/devices.html', bt_dev=bluew.devices(),bt_error="No MAC provided to connect to")
  67. except bluew.errors.DeviceNotAvailable:
  68. ret = render_template('bluetooth/devices.html', bt_dev=bluew.devices(),bt_error='Bluetooth device ' + bt_dst_mac +' is not available')
  69. return ret
  70. @app.route('/conf')
  71. def myCar_conf():
  72. # this method reads the configuration file
  73. # !!! beware due to stupidity in my mind this method also holds the
  74. # !!! default vaules when the config file is not present
  75. return render_template('conf/main.html', config=myCar_config, sections=myCar_config.sections())
  76. @app.route('/confsave', methods=['POST'])
  77. def myCar_conf_save():
  78. # this method saves the configuration settings when posted, then redirects to context /conf
  79. if request is not None and request.form['configsend'] == 'save':
  80. myCar_config['paths'] = { 'app' : request.form['[paths][app]'],
  81. 'log' : request.form['[paths][log]'],
  82. 'record' : request.form['[paths][record]'] }
  83. myCar_config['connections'] = { 'bt_dev' : request.form['[connections][bt_dev]'],
  84. 'gps_dev' : request.form['[connections][gps_dev]'],
  85. 'wifi_pwd' : request.form['[connections][wifi_pwd]'] }
  86. lib.config.myCar_save_config(myCar_config)
  87. return redirect(url_for('myCar_conf'), code=302)