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.

119 lines
3.0KB

  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. cbpi.app.logger.info("Device %s Found (Family: 28/10, Thermometer on GPIO4 (w1))" % dirname)
  18. arr.append(dirname)
  19. return arr
  20. except:
  21. return []
  22. class myThread (threading.Thread):
  23. value = 0
  24. def __init__(self, sensor_name):
  25. threading.Thread.__init__(self)
  26. self.value = 0
  27. self.sensor_name = sensor_name
  28. self.runnig = True
  29. def shutdown(self):
  30. pass
  31. def stop(self):
  32. self.runnig = False
  33. def run(self):
  34. while self.runnig:
  35. try:
  36. app.logger.info("READ TEMP")
  37. ## Test Mode
  38. if self.sensor_name is None:
  39. return
  40. with open('/sys/bus/w1/devices/w1_bus_master1/%s/w1_slave' % self.sensor_name, 'r') as content_file:
  41. content = content_file.read()
  42. if (content.split('\n')[0].split(' ')[11] == "YES"):
  43. temp = float(content.split("=")[-1]) / 1000 # temp in Celcius
  44. self.value = temp
  45. except:
  46. pass
  47. time.sleep(4)
  48. @cbpi.sensor
  49. class ONE_WIRE_SENSOR(SensorPassive):
  50. sensor_name = Property.Select("Sensor", getSensors(), description="The OneWire sensor address.")
  51. offset = Property.Number("Offset", True, 0, description="Offset which is added to the received sensor data. Positive and negative values are both allowed.")
  52. def init(self):
  53. self.t = myThread(self.sensor_name)
  54. def shudown():
  55. shudown.cb.shutdown()
  56. shudown.cb = self.t
  57. self.t.start()
  58. def stop(self):
  59. try:
  60. self.t.stop()
  61. except:
  62. pass
  63. def read(self):
  64. if self.get_config_parameter("unit", "C") == "C":
  65. self.data_received(round(self.t.value + self.offset_value(), 2))
  66. else:
  67. self.data_received(round(9.0 / 5.0 * self.t.value + 32 + self.offset_value(), 2))
  68. @cbpi.try_catch(0)
  69. def offset_value(self):
  70. return float(self.offset)
  71. @classmethod
  72. def init_global(self):
  73. try:
  74. call(["modprobe", "w1-gpio"])
  75. call(["modprobe", "w1-therm"])
  76. except Exception as e:
  77. pass
  78. @blueprint.route('/<int:t>', methods=['GET'])
  79. def set_temp(t):
  80. global temp
  81. temp = t
  82. return ('', 204)
  83. @cbpi.initalizer()
  84. def init(cbpi):
  85. cbpi.app.register_blueprint(blueprint, url_prefix='/api/one_wire')