· 5 years ago · Jun 28, 2020, 08:08 PM
1#! /usr/bin/env python
2# -*- coding: utf-8 -*-
3from flask import Flask, flash, render_template, request, redirect, session, send_file, send_from_directory
4from flask_socketio import SocketIO, send, emit
5from bson.objectid import ObjectId
6from pymodm.connection import connect
7from pymongo.write_concern import WriteConcern
8from pymodm import EmbeddedMongoModel, MongoModel, fields
9from werkzeug.utils import secure_filename
10import datetime
11import uuid
12import functools
13import csv
14import os
15
16# from app_models import User, Equipment, Error, Failure, Repair, Service, Сoefficients, Telemetry
17
18SECRET_KEY = str(uuid.uuid4())
19app = Flask(__name__)
20app.secret_key = SECRET_KEY
21
22# print (os.path.dirname(os.path.abspath(__file__)) + "/react_app")
23
24app.config["REACT_HTML"] = os.path.dirname(
25 os.path.abspath(__file__)) + "/react_apps"
26socketio = SocketIO(app, cors_allowed_origins="*", static_url_path=(
27 os.path.dirname(os.path.abspath(__file__)) + "/react_apps"))
28
29
30@app.route('/')
31def index():
32 return render_template('index.html')
33
34
35# @app.route('/react')
36# def indexReact():
37# return render_template('app.html')
38
39
40@app.route('/react')
41def get_csv():
42 try:
43 return send_from_directory(app.config["REACT_HTML"], filename="index.html",)
44 except FileNotFoundError:
45 abort(404)
46
47
48strJSON = '[{"name":"aaaa aaa","userID":"ID223","group":"8381","timePoint":"1.1.1 1: 1"},{"name":"bb bbbbb","userID":"ID343","group":"8302","timePoint":"2.2.2 2: 2"},{"name":"ccccccc cc","userID":"ID843","group":"8302","timePoint":"2.1.2 1: 2"},{"name":"dd dddd dd","userID":"ID845","group":"8382","timePoint":"1.2.1 2: 1"},{"name":"ccccccc cc","userID":"ID843","group":"8302","timePoint":"2.1.2 1: 2"},{"name":"dd dddd dd","userID":"ID845","group":"8382","timePoint":"1.2.1 2: 1"}]'
49
50
51@socketio.on('req full')
52def handle_my_custom_event1(json):
53 print('full send: ' + strJSON)
54 emit("res full", [
55 {
56 "name": "aaaa aaa",
57 "userID": "ID223",
58 "group": "8381",
59 "timePoint": "1.1.1 1:1"
60 },
61 {
62 "name": "bb bbbbb",
63 "userID": "ID343",
64 "group": "8302",
65 "timePoint": "2.2.2 2:2"
66 },
67 {
68 "name": "ccccccc cc",
69 "userID": "ID843",
70 "group": "8302",
71 "timePoint": "2.1.2 1:2"
72 },
73 {
74 "name": "dd dddd dd",
75 "userID": "ID845",
76 "group": "8382",
77 "timePoint": "1.2.1 2:1"
78 },
79 {
80 "name": "ccccccc cc",
81 "userID": "ID843",
82 "group": "8302",
83 "timePoint": "2.1.2 1:2"
84 },
85 {
86 "name": "dd dddd dd",
87 "userID": "ID845",
88 "group": "8382",
89 "timePoint": "1.2.1 2:1"
90 }
91 ])
92
93
94@socketio.on('add')
95def handle_my_custom_event2(json):
96 print('received json: ' + str(json))
97 emit("added", json)
98
99
100@socketio.on('delete')
101def handle_my_custom_event3(json):
102 print('received json: ' + str(json))
103 emit("added", json)
104
105
106@socketio.on('move')
107def handle_my_custom_event4(json):
108 print('received json: ' + str(json))
109 emit("added", json)
110
111
112def main():
113 app.run(host='0.0.0.0')
114
115
116if __name__ == '__main__':
117 app.run(host='0.0.0.0')
118 socketio.run(app, host="localhost")