Guest User

Untitled

a guest
Nov 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. from flask import Flask
  2. from flask import request
  3. from flask import json
  4.  
  5. app = Flask(__name__)
  6.  
  7. high_voltage_file_dir = ''
  8. threshold_file_dir = ''
  9. alarm_file_dir = ''
  10.  
  11. data_file_dir = ''
  12.  
  13.  
  14. def set_file_content(file_dir, content):
  15. f = open(file_dir, "w")
  16. f.write(content)
  17. f.close()
  18.  
  19.  
  20. def read_file_content(file_dir):
  21. f = open(file_dir, "r")
  22. return f.read()
  23.  
  24.  
  25. @app.route("/config", methods=['POST'])
  26. def config():
  27. # JSON ile veri alinir.
  28. data = request.json
  29.  
  30. # JSON veri icinden dataKey secilir.
  31. data_key = data['dataKey']
  32.  
  33. # JSON veri icinden dataValue secilir.
  34. data_value = data['value']
  35.  
  36. if data_key == "HighVoltage":
  37. set_file_content(high_voltage_file_dir, data_value)
  38. elif data_key == "Threshold":
  39. set_file_content(threshold_file_dir, data_value)
  40. elif data_key == "Alarm":
  41. set_file_content(alarm_file_dir, data_value)
  42.  
  43. response_data = {"Status": "SUCCESS", "Data": data}
  44.  
  45. return app.response_class(
  46. response=json.dumps(response_data),
  47. status=200,
  48. mimetype='application/json'
  49. )
  50.  
  51.  
  52. @app.route("/data", methods=['GET'])
  53. def get_data():
  54. # JSON ile veri alinir.
  55. data_type = request.args['dataType']
  56. if data_type == "v":
  57. data = read_file_content(data_file_dir)
  58. response_data = {"Status": "SUCCESS", "Data": data}
  59.  
  60. return app.response_class(
  61. response=json.dumps(response_data),
  62. status=200,
  63. mimetype='application/json'
  64. )
  65.  
  66. return app.response_class(
  67. status=204,
  68. mimetype='application/json'
  69. )
  70.  
  71. # FLASK_APP=server.py python -m flask run --host=0.0.0.0
Add Comment
Please, Sign In to add comment