You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import os
  2. import json
  3. from modules import utils
  4. from flask import Flask, request
  5. from flask_restful import Resource, Api
  6. app = Flask(__name__)
  7. api = Api(app)
  8. db_path = os.path.join(app.root_path, 'databases', 'users.json')
  9. with open(db_path, 'r') as f:
  10. users = json.load(f)
  11. class Resources(Resource):
  12. def post(self):
  13. """
  14. POST Data:
  15. user_id=<id>&type=<electricity|water>
  16. :return:
  17. """
  18. user = utils.find_by_id(users.values(), request.form['user_id'])
  19. if request.form["type"] == "electricity":
  20. usage = user['daily_electricity_usage']
  21. ideal = user['ideal_electricity_usage']
  22. diff = [abs(a-b) for a in usage for b in ideal]
  23. sum_diff = sum(diff)
  24. point = 10 - (sum_diff * 0.0001)
  25. eff = point * 10
  26. bill = ( user['electricity_bill'] * eff)/100
  27. return {
  28. 'daily_electricity_usage': usage,
  29. 'ideal_electricity_usage': ideal,
  30. 'points': point,
  31. 'efficiency': eff,
  32. 'bill': bill
  33. }
  34. elif request.form["type"] == "water":
  35. usage = user['daily_water_usage']
  36. ideal = user['ideal_water_usage']
  37. diff = [abs(a-b) for a in usage for b in ideal]
  38. sum_diff = sum(diff)
  39. point = 10 - (sum_diff * 0.0001)
  40. eff = point * 10
  41. bill = (user['water_bill'] * eff)/100
  42. return {
  43. 'daily_water_usage': usage,
  44. 'ideal_water_usage': ideal,
  45. 'point': point,
  46. 'efficiency': eff,
  47. 'bill': bill
  48. }
  49. if __name__ == '__main__':
  50. api.add_resource(Resources, '/resources', '/resources/')
  51. app.run(host='0.0.0.0', port=5000)