3
0

myCar.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/python3
  2. import logging
  3. import configparser
  4. import lib.config
  5. import lib.app_control
  6. import lib.bluetooth
  7. # we need to initially load the configuration file.
  8. # This is loaded via a local symlink
  9. myCar_config = configparser.ConfigParser()
  10. myCar_config.read('data/etc/myCar.conf')
  11. # fill in config values with defaults
  12. myCar_config = lib.config.myCar_read_config_defaults(myCar_config)
  13. import atexit
  14. atexit.register(lib.app_control.myCar_exit, myCar_config)
  15. # lets start with log handling here
  16. import logging, os
  17. from pathlib import Path
  18. # first check, if the logentry is a directory
  19. if Path(myCar_config['paths'].get('log')).is_dir():
  20. # now check if we can write ther:
  21. myCar_logfile=myCar_config['paths'].get('log') + '/' + 'myCar_Application.log'
  22. try:
  23. fp = open(myCar_logfile , 'w')
  24. except PermissionError:
  25. logging.warning('directory ' + myCar_logfile + ' not writeable')
  26. else:
  27. fp.close()
  28. logging.basicConfig(filename=myCar_logfile,level=logging.INFO)
  29. from flask import Flask
  30. from flask import url_for
  31. from flask import render_template
  32. from flask import request
  33. from flask import redirect
  34. from flask import abort
  35. app = Flask(__name__)
  36. @app.route('/')
  37. def myCar_root():
  38. return render_template('index.html', bt_ctrl_ispowered=lib.bluetooth.bt_ctrl_ispowered())
  39. @app.route('/bluetooth/controller')
  40. def myCar_bluetooth_controller():
  41. return render_template('bluetooth/controller.html', bt_ctrl=lib.bluetooth.bt_getControllers())
  42. @app.route('/bluetooth/devices')
  43. def myCar_bluetooth_devices():
  44. if lib.bluetooth.bt_ctrl_ispowered == False:
  45. return render_template('bluetooth/controller.html', bt_ctrl=lib.bluetooth.bt_getControllers(), bt_error="All Bluetooth Adapters are powered off")
  46. else:
  47. return render_template('bluetooth/devices.html', bt_dev=lib.bluetooth.bt_getDevices())
  48. @app.route('/bluetooth/connect', methods=['POST'])
  49. def myCar_bluetooth_connect():
  50. # this method will connect to a bt device in the following order:
  51. # - pair
  52. # - trust
  53. # - connect
  54. #try:
  55. # request
  56. #except NameError:
  57. # request = None
  58. try:
  59. if request is not None:
  60. import bluew
  61. #try:
  62. bt_dst_mac = request.form['bt_dst_mac']
  63. logging.info('Bluetooth trying to connect to ' + bt_dst_mac)
  64. # pair
  65. bluew.pair(bt_dst_mac)
  66. # trust
  67. bluew.trust(bt_dst_mac)
  68. # connect
  69. bluew.connect(bt_dst_mac)
  70. retstr = 'connected to '.join(bt_dst_mac)
  71. else:
  72. ret = 'Mac Address not provided'
  73. except NameError:
  74. ret = render_template('bluetooth/devices.html', bt_dev=bluew.devices(),bt_error="No MAC provided to connect to")
  75. except bluew.errors.DeviceNotAvailable:
  76. ret = render_template('bluetooth/devices.html', bt_dev=bluew.devices(),bt_error='Bluetooth device ' + bt_dst_mac +' is not available')
  77. return ret
  78. @app.route('/conf')
  79. def myCar_conf():
  80. # this method reads the configuration file
  81. # !!! beware due to stupidity in my mind this method also holds the
  82. # !!! default vaules when the config file is not present
  83. return render_template('conf/main.html', config=myCar_config, sections=myCar_config.sections())
  84. @app.route('/confsave', methods=['POST'])
  85. def myCar_conf_save():
  86. # this method saves the configuration settings when posted, then redirects to context /conf
  87. if request is not None and request.form['configsend'] == 'save':
  88. myCar_config['paths'] = { 'app' : request.form['[paths][app]'],
  89. 'log' : request.form['[paths][log]'],
  90. 'record' : request.form['[paths][record]'],
  91. 'export' : request.form['[paths][export]'] }
  92. # create the paths
  93. lib.config.myCar_save_pathcreate(myCar_config['paths'].get('app'),myCar_config['paths'].get('log'))
  94. lib.config.myCar_save_pathcreate(myCar_config['paths'].get('app'),myCar_config['paths'].get('record'))
  95. lib.config.myCar_save_pathcreate(myCar_config['paths'].get('app'),myCar_config['paths'].get('export'))
  96. myCar_config['connections'] = { 'bt_dev' : request.form['[connections][bt_dev]'],
  97. 'gps_dev' : request.form['[connections][gps_dev]'],
  98. 'wifi_pwd' : request.form['[connections][wifi_pwd]'] }
  99. myCar_config['startup'] = { 'bt_connect' : request.form['[startup][bt_connect]'],
  100. 'autorecord' : request.form['[startup][autorecord]'] }
  101. myCar_config['metrics'] = { 'temperature' : request.form['[metrics][temperature]'],
  102. 'speed' : request.form['[metrics][speed]'] }
  103. lib.config.myCar_save_config(myCar_config)
  104. return redirect(url_for('myCar_conf'), code=302)
  105. @app.route('/gps')
  106. def myCar_gps():
  107. # dummy implementation before code is ready
  108. return render_template('empty.html')
  109. @app.route('/wifi')
  110. def myCar_wifi():
  111. # dummy implementation before code is ready
  112. return render_template('empty.html')
  113. @app.route('/system')
  114. def myCar_system():
  115. # dummy implementation before code is ready
  116. return render_template('empty.html')
  117. @app.route('/live')
  118. def myCar_live():
  119. # dummy implementation before code is ready
  120. return render_template('empty.html')
  121. @app.route('/record')
  122. def myCar_record():
  123. # dummy implementation before code is ready
  124. # - first readout the available files
  125. # - then readout the trips from the files
  126. import lib.recordings
  127. flist = []
  128. fpflist = lib.recordings.myCar_rec_files(myCar_config['paths'].get('app') + '/' + myCar_config['paths'].get('record'))
  129. for f in fpflist:
  130. flist.add(str(f).strip(myCar_config['paths'].get('app') + '/' + myCar_config['paths'].get('record')))
  131. return render_template('recordings/overview.html',files=flist, fpfiles=fpflist)
  132. # error handling
  133. @app.errorhandler(404)
  134. def page_not_found(error):
  135. return render_template('404/error.html'), 404