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.

114 lines
2.7KB

  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. offset = Property.Number("Offset", True, 0)
  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 + float(self.offset), 2))
  65. else:
  66. self.data_received(round(9.0 / 5.0 * self.t.value + 32 + float(self.offset), 2))
  67. @classmethod
  68. def init_global(self):
  69. try:
  70. call(["modprobe", "w1-gpio"])
  71. call(["modprobe", "w1-therm"])
  72. except Exception as e:
  73. pass
  74. @blueprint.route('/<int:t>', methods=['GET'])
  75. def set_temp(t):
  76. global temp
  77. temp = t
  78. return ('', 204)
  79. @cbpi.initalizer()
  80. def init(cbpi):
  81. cbpi.app.logger.info("INITIALIZE ONE WIRE MODULE")
  82. cbpi.app.register_blueprint(blueprint, url_prefix='/api/one_wire')