Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

113 строки
2.6KB

  1. # -*- coding: utf-8 -*-
  2. import os
  3. from subprocess import Popen, PIPE, call
  4. from modules import cbpi, app
  5. from modules.core.hardware import SensorPassive
  6. import json
  7. import os, re, threading, time
  8. from flask import Blueprint, render_template, request
  9. from modules.core.props import Property
  10. blueprint = Blueprint('one_wire', __name__)
  11. temp = 22
  12. def getSensors():
  13. try:
  14. arr = []
  15. for dirname in os.listdir('/sys/bus/w1/devices'):
  16. if (dirname.startswith("28") or dirname.startswith("10")):
  17. arr.append(dirname)
  18. return arr
  19. except:
  20. return []
  21. class myThread (threading.Thread):
  22. value = 0
  23. def __init__(self, sensor_name):
  24. threading.Thread.__init__(self)
  25. self.value = 0
  26. self.sensor_name = sensor_name
  27. self.runnig = True
  28. def shutdown(self):
  29. pass
  30. def stop(self):
  31. self.runnig = False
  32. def run(self):
  33. while self.runnig:
  34. try:
  35. app.logger.info("READ TEMP")
  36. ## Test Mode
  37. if self.sensor_name is None:
  38. return
  39. with open('/sys/bus/w1/devices/w1_bus_master1/%s/w1_slave' % self.sensor_name, 'r') as content_file:
  40. content = content_file.read()
  41. if (content.split('\n')[0].split(' ')[11] == "YES"):
  42. temp = float(content.split("=")[-1]) / 1000 # temp in Celcius
  43. self.value = temp
  44. except:
  45. pass
  46. time.sleep(4)
  47. @cbpi.sensor
  48. class ONE_WIRE_SENSOR(SensorPassive):
  49. sensor_name = Property.Select("Sensor", getSensors())
  50. def init(self):
  51. self.t = myThread(self.sensor_name)
  52. def shudown():
  53. shudown.cb.shutdown()
  54. shudown.cb = self.t
  55. self.t.start()
  56. def stop(self):
  57. try:
  58. self.t.stop()
  59. except:
  60. pass
  61. def read(self):
  62. if self.get_config_parameter("unit", "C") == "C":
  63. self.data_received(round(self.t.value, 2))
  64. else:
  65. self.data_received(round(9.0 / 5.0 * self.t.value + 32, 2))
  66. @classmethod
  67. def init_global(self):
  68. try:
  69. call(["modprobe", "w1-gpio"])
  70. call(["modprobe", "w1-therm"])
  71. except Exception as e:
  72. pass
  73. @blueprint.route('/<int:t>', methods=['GET'])
  74. def set_temp(t):
  75. global temp
  76. temp = t
  77. return ('', 204)
  78. @cbpi.initalizer()
  79. def init(cbpi):
  80. cbpi.app.logger.info("INITIALIZE ONE WIRE MODULE")
  81. cbpi.app.register_blueprint(blueprint, url_prefix='/api/one_wire')