2021-02-02 21:22:59 +01:00
# -*- coding: utf-8 -*-
import asyncio
import random
import re
import random
from aiohttp import web
from cbpi . api import *
import os , re , threading , time
2021-02-06 00:40:55 +01:00
from subprocess import call
2021-02-02 21:22:59 +01:00
2021-02-06 00:40:55 +01:00
def getSensors ( ) :
try :
arr = [ ]
for dirname in os . listdir ( ' /sys/bus/w1/devices ' ) :
if ( dirname . startswith ( " 28 " ) or dirname . startswith ( " 10 " ) ) :
arr . append ( dirname )
return arr
except :
2021-02-06 14:11:30 +01:00
return [ ]
2021-02-06 00:40:55 +01:00
2021-02-02 21:22:59 +01:00
2021-02-06 00:40:55 +01:00
class ReadThread ( threading . Thread ) :
2021-02-02 21:22:59 +01:00
2021-02-06 00:40:55 +01:00
value = 0
2021-02-02 21:22:59 +01:00
def __init__ ( self , sensor_name ) :
threading . Thread . __init__ ( self )
self . value = 0
self . sensor_name = sensor_name
self . runnig = True
def shutdown ( self ) :
pass
def stop ( self ) :
self . runnig = False
def run ( self ) :
while self . runnig :
2021-02-06 00:40:55 +01:00
try :
if self . sensor_name is None :
return
with open ( ' /sys/bus/w1/devices/w1_bus_master1/ %s /w1_slave ' % self . sensor_name , ' r ' ) as content_file :
content = content_file . read ( )
if ( content . split ( ' \n ' ) [ 0 ] . split ( ' ' ) [ 11 ] == " YES " ) :
temp = float ( content . split ( " = " ) [ - 1 ] ) / 1000 # temp in Celcius
self . value = temp
2021-02-02 21:22:59 +01:00
except :
pass
2021-02-16 20:37:51 +01:00
2021-02-02 21:22:59 +01:00
time . sleep ( 1 )
2021-02-06 00:40:55 +01:00
@parameters ( [ Property . Select ( label = " Sensor " , options = getSensors ( ) ) , Property . Select ( label = " Interval " , options = [ 1 , 5 , 10 , 30 , 60 ] , description = " Interval in Seconds " ) ] )
2021-02-02 21:22:59 +01:00
class OneWire ( CBPiSensor ) :
def __init__ ( self , cbpi , id , props ) :
super ( OneWire , self ) . __init__ ( cbpi , id , props )
self . value = 0
async def start ( self ) :
await super ( ) . start ( )
2021-02-06 00:40:55 +01:00
self . name = self . props . get ( " Sensor " )
self . interval = self . props . get ( " Interval " , 60 )
self . t = ReadThread ( self . name )
self . t . daemon = True
2021-02-02 21:22:59 +01:00
def shudown ( ) :
shudown . cb . shutdown ( )
shudown . cb = self . t
self . t . start ( )
async def stop ( self ) :
try :
self . t . stop ( )
2021-02-06 00:40:55 +01:00
self . running = False
2021-02-02 21:22:59 +01:00
except :
pass
async def run ( self ) :
2021-02-16 20:37:51 +01:00
while True :
2021-02-06 00:40:55 +01:00
self . value = self . t . value
2021-02-16 20:37:51 +01:00
self . log_data ( self . value )
2021-02-02 21:22:59 +01:00
self . push_update ( self . value )
2021-02-06 00:40:55 +01:00
await asyncio . sleep ( self . interval )
2021-02-02 21:22:59 +01:00
def get_state ( self ) :
return dict ( value = self . value )
def setup ( cbpi ) :
cbpi . plugin . register ( " OneWire " , OneWire )
2021-02-06 00:40:55 +01:00
try :
# Global Init
call ( [ " modprobe " , " w1-gpio " ] )
call ( [ " modprobe " , " w1-therm " ] )
except Exception as e :
pass