myCar.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. app = Flask(__name__)
  35. @app.route('/')
  36. def myCar_root():
  37. return render_template('index.html', bt_ctrl_ispowered=lib.bluetooth.bt_ctrl_ispowered())
  38. @app.route('/bluetooth/controller')
  39. def myCar_bluetooth_controller():
  40. return render_template('bluetooth/controller.html', bt_ctrl=lib.bluetooth.bt_getControllers())
  41. @app.route('/bluetooth/devices')
  42. def myCar_bluetooth_devices():
  43. if lib.bluetooth.bt_ctrl_ispowered == False:
  44. return render_template('bluetooth/controller.html', bt_ctrl=lib.bluetooth.bt_getControllers(), bt_error="All Bluetooth Adapters are powered off")
  45. else:
  46. return render_template('bluetooth/devices.html', bt_dev=lib.bluetooth.bt_getDevices())
  47. @app.route('/bluetooth/connect', methods=['POST'])
  48. def myCar_bluetooth_connect():
  49. # this method will connect to a bt device in the following order:
  50. # - pair
  51. # - trust
  52. # - connect
  53. #try:
  54. # request
  55. #except NameError:
  56. # request = None
  57. try:
  58. if request is not None:
  59. import bluew
  60. #try:
  61. bt_dst_mac = request.form['bt_dst_mac']
  62. logging.info('Bluetooth trying to connect to ' + bt_dst_mac)
  63. # pair
  64. bluew.pair(bt_dst_mac)
  65. # trust
  66. bluew.trust(bt_dst_mac)
  67. # connect
  68. bluew.connect(bt_dst_mac)
  69. retstr = 'connected to '.join(bt_dst_mac)
  70. else:
  71. ret = 'Mac Address not provided'
  72. except NameError:
  73. ret = render_template('bluetooth/devices.html', bt_dev=bluew.devices(),bt_error="No MAC provided to connect to")
  74. except bluew.errors.DeviceNotAvailable:
  75. ret = render_template('bluetooth/devices.html', bt_dev=bluew.devices(),bt_error='Bluetooth device ' + bt_dst_mac +' is not available')
  76. return ret
  77. @app.route('/conf')
  78. def myCar_conf():
  79. # this method reads the configuration file
  80. # !!! beware due to stupidity in my mind this method also holds the
  81. # !!! default vaules when the config file is not present
  82. return render_template('conf/main.html', config=myCar_config, sections=myCar_config.sections())
  83. @app.route('/confsave', methods=['POST'])
  84. def myCar_conf_save():
  85. # this method saves the configuration settings when posted, then redirects to context /conf
  86. if request is not None and request.form['configsend'] == 'save':
  87. myCar_config['paths'] = { 'app' : request.form['[paths][app]'],
  88. 'log' : request.form['[paths][log]'],
  89. 'record' : request.form['[paths][record]'],
  90. 'export' : request.form['[paths][export]'] }
  91. # create the paths
  92. lib.config.myCar_save_pathcreate(myCar_config['paths'].get('app'),myCar_config['paths'].get('log'))
  93. lib.config.myCar_save_pathcreate(myCar_config['paths'].get('app'),myCar_config['paths'].get('record'))
  94. lib.config.myCar_save_pathcreate(myCar_config['paths'].get('app'),myCar_config['paths'].get('export'))
  95. myCar_config['connections'] = { 'bt_dev' : request.form['[connections][bt_dev]'],
  96. 'gps_dev' : request.form['[connections][gps_dev]'],
  97. 'wifi_pwd' : request.form['[connections][wifi_pwd]'] }
  98. myCar_config['startup'] = { 'bt_connect' : request.form['[startup][bt_connect]'],
  99. 'autorecord' : request.form['[startup][autorecord]'] }
  100. myCar_config['metrics'] = { 'temperature' : request.form['[metrics][temperature]'],
  101. 'speed' : request.form['[metrics][speed]'] }
  102. lib.config.myCar_save_config(myCar_config)
  103. return redirect(url_for('myCar_conf'), code=302)
  104. @app.route('/gps')
  105. def myCar_gps():
  106. # dummy implementation before code is ready
  107. return render_template('empty.html')
  108. @app.route('/wifi')
  109. def myCar_wifi():
  110. # dummy implementation before code is ready
  111. return render_template('empty.html')
  112. @app.route('/system')
  113. def myCar_system():
  114. # dummy implementation before code is ready
  115. return render_template('empty.html')
  116. @app.route('/live')
  117. def myCar_live():
  118. # dummy implementation before code is ready
  119. return render_template('empty.html')
  120. @app.route('/record')
  121. def myCar_record():
  122. # dummy implementation before code is ready
  123. # - first readout the available files
  124. # - then readout the trips from the files
  125. import lib.recordings
  126. flist = []
  127. fpflist = lib.recordings.myCar_rec_files(myCar_config['paths'].get('app') + '/' + myCar_config['paths'].get('record'))
  128. for f in fpflist:
  129. flist.add(str(f).strip(myCar_config['paths'].get('app') + '/' + myCar_config['paths'].get('record')))
  130. return render_template('recordings/overview.html',files=flist, fpfiles=fpflist)