myCar.py 6.1 KB

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