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.

118 line
2.9KB

  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(), description="The OneWire sensor address.")
  50. offset = Property.Number("Offset", True, 0, description="Offset which is added to the received sensor data. Positive and negative values are both allowed.")
  51. def init(self):
  52. self.t = myThread(self.sensor_name)
  53. def shudown():
  54. shudown.cb.shutdown()
  55. shudown.cb = self.t
  56. self.t.start()
  57. def stop(self):
  58. try:
  59. self.t.stop()
  60. except:
  61. pass
  62. def read(self):
  63. if self.get_config_parameter("unit", "C") == "C":
  64. self.data_received(round(self.t.value + self.offset_value(), 2))
  65. else:
  66. self.data_received(round(9.0 / 5.0 * self.t.value + 32 + self.offset_value(), 2))
  67. @cbpi.try_catch(0)
  68. def offset_value(self):
  69. return float(self.offset)
  70. @classmethod
  71. def init_global(self):
  72. try:
  73. call(["modprobe", "w1-gpio"])
  74. call(["modprobe", "w1-therm"])
  75. except Exception as e:
  76. pass
  77. @blueprint.route('/<int:t>', methods=['GET'])
  78. def set_temp(t):
  79. global temp
  80. temp = t
  81. return ('', 204)
  82. @cbpi.initalizer()
  83. def init(cbpi):
  84. cbpi.app.register_blueprint(blueprint, url_prefix='/api/one_wire')