-
Notifications
You must be signed in to change notification settings - Fork 0
/
livetemp.py
89 lines (71 loc) · 2.04 KB
/
livetemp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import threading
import time
import json
class LiveTemp(threading.Thread):
def __init__(self,config):
threading.Thread.__init__(self)
self.tempDir = '/sys/bus/w1/devices/'
self._stop = threading.Event()
self._pause = False
self.curTemp = {}
self.interval = 3
self.oldtime = 0
self.config = config
self.liveDataLength = 100
def stop(self):
self._stop.set()
def updateConfig(self,config):
self.config = config
def setInterval(self,TD):
self.interval = TD
def setLDL(self,LDL):
self.liveDataLength = LDL
def pause(self):
self._pause = True
def resume(self):
self._pause = False
def stopped(self):
return self._stop.isSet()
def paused(self):
return self._pause
def getCurTemp(self):
return self.curTemp
def run(self):
while not self._stop.isSet():
if not self.paused():
newtime = time.time()
if newtime >= self.interval+self.oldtime:
self.oldtime = newtime
liveDataWrite = {"timestamp":time.time()}
for sensor in self.config['sensors']:
try:
f = open(self.tempDir + sensor['sensorAddress'] + "/w1_slave", 'r')
except IOError as e:
print "Error: " + sensor['sensorName'] + " does not exist."
return;
lines=f.readlines()
f.close()
crcLine=lines[0]
tempLine=lines[1]
result_list = tempLine.split("=")
temp = float(result_list[-1])/1000
temp = (temp*9/5) + 32
temp = temp + sensor["correctionFactor"]
temp = float("%.2f" % temp)
if crcLine.find("NO") > -1:
temp = ''
else:
liveDataWrite[str(sensor['sensorName'])] = temp
if len(liveDataWrite) > 1:
try:
liveData = json.loads(open(self.config['storage']+'live.dat').read())
except:
liveData = []
while len(liveData) > self.liveDataLength:
liveData.pop(0)
if len(liveData) == self.liveDataLength:
liveData.pop(0)
liveData.append(liveDataWrite)
self.curTemp = liveDataWrite
with open(self.config['storage']+'live.dat','w') as outfile:
json.dump(liveData,outfile)