Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask
- from flask import request
- from flask import json
- app = Flask(__name__)
- high_voltage_file_dir = ''
- threshold_file_dir = ''
- alarm_file_dir = ''
- data_file_dir = ''
- def set_file_content(file_dir, content):
- f = open(file_dir, "w")
- f.write(content)
- f.close()
- def read_file_content(file_dir):
- f = open(file_dir, "r")
- return f.read()
- @app.route("/config", methods=['POST'])
- def config():
- # JSON ile veri alinir.
- data = request.json
- # JSON veri icinden dataKey secilir.
- data_key = data['dataKey']
- # JSON veri icinden dataValue secilir.
- data_value = data['value']
- if data_key == "HighVoltage":
- set_file_content(high_voltage_file_dir, data_value)
- elif data_key == "Threshold":
- set_file_content(threshold_file_dir, data_value)
- elif data_key == "Alarm":
- set_file_content(alarm_file_dir, data_value)
- response_data = {"Status": "SUCCESS", "Data": data}
- return app.response_class(
- response=json.dumps(response_data),
- status=200,
- mimetype='application/json'
- )
- @app.route("/data", methods=['GET'])
- def get_data():
- # JSON ile veri alinir.
- data_type = request.args['dataType']
- if data_type == "v":
- data = read_file_content(data_file_dir)
- response_data = {"Status": "SUCCESS", "Data": data}
- return app.response_class(
- response=json.dumps(response_data),
- status=200,
- mimetype='application/json'
- )
- return app.response_class(
- status=204,
- mimetype='application/json'
- )
- # FLASK_APP=server.py python -m flask run --host=0.0.0.0
Add Comment
Please, Sign In to add comment