Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

110 lines
3.6KB

  1. import datetime
  2. import os
  3. from flask import Blueprint, request, send_from_directory, json
  4. from flask_classy import FlaskView, route
  5. from modules import cbpi
  6. class LogView(FlaskView):
  7. @route('/', methods=['GET'])
  8. def get_all_logfiles(self):
  9. result = []
  10. for filename in os.listdir("./logs"):
  11. if filename.endswith(".log"):
  12. result.append(filename)
  13. return json.dumps(result)
  14. @route('/actions')
  15. def actions(self):
  16. filename = "./logs/action.log"
  17. if os.path.isfile(filename) == False:
  18. return
  19. import csv
  20. array = []
  21. with open(filename, 'rb') as f:
  22. reader = csv.reader(f)
  23. for row in reader:
  24. try:
  25. array.append([int((datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S") - datetime.datetime(1970, 1, 1)).total_seconds()) * 1000, row[1]])
  26. except:
  27. pass
  28. return json.dumps(array)
  29. @route('/<file>', methods=["DELETE"])
  30. def clearlog(self, file):
  31. """
  32. Overload delete method to shutdown sensor before delete
  33. :param id: sensor id
  34. :return: HTTP 204
  35. """
  36. if not self.check_filename(file):
  37. return ('File Not Found', 404)
  38. filename = "./logs/%s" % file
  39. if os.path.isfile(filename) == True:
  40. os.remove(filename)
  41. cbpi.notify("log deleted succesfully", "")
  42. else:
  43. cbpi.notify("Failed to delete log", "", type="danger")
  44. return ('', 204)
  45. def read_log_as_json(self, type, id):
  46. filename = "./logs/%s_%s.log" % (type, id)
  47. if os.path.isfile(filename) == False:
  48. return
  49. import csv
  50. array = []
  51. with open(filename, 'rb') as f:
  52. reader = csv.reader(f)
  53. for row in reader:
  54. try:
  55. array.append([int((datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S") - datetime.datetime(1970, 1, 1)).total_seconds()) * 1000, float(row[1])])
  56. except:
  57. pass
  58. return array
  59. def convert_chart_data_to_json(self, chart_data):
  60. return {"name": chart_data["name"], "data": self.read_log_as_json(chart_data["data_type"], chart_data["data_id"])}
  61. @route('/<t>/<int:id>', methods=["POST"])
  62. def get_logs_as_json(self, t, id):
  63. data = request.json
  64. result = []
  65. if t == "s":
  66. name = cbpi.cache.get("sensors").get(id).name
  67. result.append({"name": name, "data": self.read_log_as_json("sensor", id)})
  68. if t == "k":
  69. kettle = cbpi.cache.get("kettle").get(id)
  70. result = list(map(self.convert_chart_data_to_json, cbpi.get_controller(kettle.logic).get("class").chart(kettle)))
  71. if t == "f":
  72. fermenter = cbpi.cache.get("fermenter").get(id)
  73. result = list(map(self.convert_chart_data_to_json, cbpi.get_fermentation_controller(fermenter.logic).get("class").chart(fermenter)))
  74. return json.dumps(result)
  75. @route('/download/<file>')
  76. @cbpi.nocache
  77. def download(self, file):
  78. if not self.check_filename(file):
  79. return ('File Not Found', 404)
  80. return send_from_directory('../logs', file, as_attachment=True, attachment_filename=file)
  81. def check_filename(self, name):
  82. import re
  83. pattern = re.compile('^([A-Za-z0-9-_])+.log$')
  84. return True if pattern.match(name) else False
  85. @cbpi.initalizer()
  86. def init(app):
  87. """
  88. Initializer for the message module
  89. :param app: the flask app
  90. :return: None
  91. """
  92. LogView.register(cbpi.app, route_base='/api/logs')