From Corrupt Butterfly, 3 Weeks ago, written in Plain Text.
  1. from flask import Flask, render_template, flash, jsonify, request, send_file, url_for,redirect,send_from_directory
  2. from flask_socketio import SocketIO, emit
  3. from fetch import get_data
  4. import threading
  5. from time import sleep, time
  6. import datetime
  7. from random import randint
  8. import json
  9. import os
  10.  
  11. ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
  12. app = Flask(__name__)
  13. app.config['SECRET_KEY'] = 'it is a secret'
  14. socketio = SocketIO(app)
  15.  
  16. data = []
  17. class Worker:
  18.     def __init__(self):
  19.         self.thread = threading.Thread(target=self.run)
  20.         self.stop_event = threading.Event()
  21.    
  22.     def run(self):
  23.         while not self.stop_event.is_set():
  24.             with app.test_request_context('/'):
  25.                 data_point = {'azimuth': randint(0,99)/10, 'elevation': randint(0,99)/10, 'x': randint(0, 99)/10, 'y': randint(0,99)/10, 'z': randint(0,99)/10, 'timestamp': time()*1000}
  26.                 emit('data', data_point, namespace="/test", broadcast=True)
  27.                 data.append(data_point)
  28.  
  29.             print('worker looping')
  30.             sleep(2)
  31.        
  32.         print('worker exiting')
  33.  
  34.     def start(self):
  35.         self.thread.start()
  36.  
  37.     def stop(self):
  38.         self.stop_event.set()
  39.    
  40.     def join(self):
  41.         return self.thread.join()
  42.  
  43.  
  44.  
  45. x = 0
  46. worker = None
  47.  
  48. @app.route('/')
  49. def homepage():
  50.     return render_template("index.html")
  51.  
  52. @app.route("/downloads/<path:filename>", methods=['GET', 'POST'])
  53. def download(filename):
  54.     try:
  55.         s = os.path.join(ROOT_DIR, "storage")
  56.         print(s)
  57.         return send_from_directory(directory=s, filename=filename)
  58.     except Exception as e:
  59.         return(str(e))
  60.  
  61.  
  62. @socketio.on('connect', namespace='/test')
  63. def test_connect():
  64.     #global socketio
  65.     print("fucking connected")
  66.  
  67. @socketio.on("start", namespace="/test")
  68. def start(msg):
  69.     global worker
  70.     worker = Worker()
  71.     print("starting worker")
  72.     worker.start()
  73.  
  74. @socketio.on("stop", namespace="/test")
  75. def stop(msg):
  76.     global worker
  77.     global data
  78.     if not worker is None:
  79.         worker.stop()
  80.         worker.join()
  81.         print("worker exited gracefully")
  82.         print(data)
  83.         filename = "data"+datetime.datetime.fromtimestamp(time()).strftime('%Y-%m-%d%H:%M:%S')+'.json'
  84.         with open(ROOT_DIR+"/storage/"+filename+'.json', "w") as outfile:
  85.             json.dump(data, outfile)
  86.         download(filename)
  87.  
  88. @socketio.on("reset", namespace="/test")
  89. def reset(msg):
  90.     global data
  91.     data = []
  92.  
  93. @socketio.on('disconnect', namespace='/test')
  94. def test_disconnect():
  95.     print('Client disconnected')
  96.  
  97.  
  98. if __name__ == '__main__':
  99.     socketio.run(app)
  100.  
captcha