Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

178 lignes
5.1KB

  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.core.core import cbpi
  6. class LogView(FlaskView):
  7. _log_directory = "./logs"
  8. @route('/', methods=['GET'])
  9. def get_all_logfiles(self):
  10. """
  11. Get a list of all Log Files
  12. ---
  13. tags:
  14. - logs
  15. responses:
  16. 200:
  17. description: List of all log files
  18. """
  19. result = []
  20. for filename in os.listdir(self._log_directory):
  21. if filename.endswith(".log"):
  22. result.append(filename)
  23. return json.dumps(result)
  24. @route('/actions')
  25. def actions(self):
  26. """
  27. Get a list of all brewing actions
  28. ---
  29. tags:
  30. - logs
  31. responses:
  32. 200:
  33. description: List of all log files
  34. """
  35. filename = "./logs/action.log"
  36. if os.path.isfile(filename) == False:
  37. return
  38. import csv
  39. array = []
  40. with open(filename, 'rb') as f:
  41. reader = csv.reader(f)
  42. for row in reader:
  43. try:
  44. array.append([int((datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S") - datetime.datetime(1970, 1, 1)).total_seconds()) * 1000, row[1]])
  45. except:
  46. pass
  47. return json.dumps(array)
  48. @route('/<file>', methods=["DELETE"])
  49. def clearlog(self, file):
  50. """
  51. Delete a log file by name
  52. ---
  53. tags:
  54. - logs
  55. parameters:
  56. - in: path
  57. name: file
  58. schema:
  59. type: string
  60. required: true
  61. description: File name
  62. responses:
  63. 204:
  64. description: Log deleted
  65. """
  66. if not self.check_filename(file):
  67. return ('File Not Found', 404)
  68. filename = "./logs/%s" % file
  69. if os.path.isfile(filename) == True:
  70. os.remove(filename)
  71. cbpi.notify("log deleted succesfully", "")
  72. return ('', 204)
  73. else:
  74. cbpi.notify("Failed to delete log", "", type="danger")
  75. return ('', 404)
  76. def read_log_as_json(self, type, id):
  77. filename = "./logs/%s_%s.log" % (type, id)
  78. if os.path.isfile(filename) == False:
  79. return
  80. import csv
  81. array = []
  82. with open(filename, 'rb') as f:
  83. reader = csv.reader(f)
  84. for row in reader:
  85. try:
  86. 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])])
  87. except:
  88. pass
  89. return array
  90. def convert_chart_data_to_json(self, chart_data):
  91. return {"name": chart_data["name"], "data": self.read_log_as_json(chart_data["data_type"], chart_data["data_id"])}
  92. @route('/<t>/<int:id>', methods=["POST"])
  93. def get_logs_as_json(self, t, id):
  94. """
  95. Get Log as json
  96. ---
  97. tags:
  98. - logs
  99. parameters:
  100. - in: path
  101. name: id
  102. schema:
  103. type: string
  104. required: true
  105. description: id of the file
  106. responses:
  107. 200:
  108. description: Log File Data
  109. """
  110. data = request.json
  111. result = []
  112. if t == "s":
  113. name = cbpi.cache.get("sensors").get(id).name
  114. result.append({"name": name, "data": self.read_log_as_json("sensor", id)})
  115. if t == "k":
  116. kettle = cbpi.cache.get("kettle").get(id)
  117. result = map(self.convert_chart_data_to_json, cbpi.brewing.get_controller(kettle.logic).get("class").chart(kettle))
  118. if t == "f":
  119. fermenter = cbpi.cache.get("fermenter").get(id)
  120. result = map(self.convert_chart_data_to_json, cbpi.fermentation.get_controller(fermenter.logic).get("class").chart(fermenter))
  121. return json.dumps(result)
  122. @route('/download/<file>')
  123. @cbpi.nocache
  124. def download(self, file):
  125. """
  126. Download a log file by name
  127. ---
  128. tags:
  129. - logs
  130. parameters:
  131. - in: path
  132. name: file
  133. schema:
  134. type: string
  135. required: true
  136. description: filename
  137. responses:
  138. 200:
  139. description: Log file downloaded
  140. """
  141. if not self.check_filename(file):
  142. return ('File Not Found', 404)
  143. return send_from_directory('../../logs', file, as_attachment=True, attachment_filename=file)
  144. def check_filename(self, name):
  145. import re
  146. pattern = re.compile('^([A-Za-z0-9-_])+.log$')
  147. return True if pattern.match(name) else False
  148. @cbpi.addon.core.initializer()
  149. def init(cbpi):
  150. """
  151. Initializer for the message module
  152. :param app: the flask app
  153. :return: None
  154. """
  155. LogView.register(cbpi._app, route_base='/api/logs')