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.

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