forked from BrewPi/brewpi-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperatureProfile.py
80 lines (65 loc) · 2.9 KB
/
temperatureProfile.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
# Copyright 2012 BrewPi/Elco Jacobs.
# This file is part of BrewPi.
# BrewPi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# BrewPi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with BrewPi. If not, see <http://www.gnu.org/licenses/>.
import time
import csv
import sys
import BrewPiUtil as util
# also defined in brewpi.py. TODO: move to shared import
def logMessage(message):
print >> sys.stderr, time.strftime("%b %d %Y %H:%M:%S ") + message
def getNewTemp(scriptPath):
temperatureReader = csv.reader( open(util.addSlash(scriptPath) + 'settings/tempProfile.csv', 'rb'),
delimiter=',', quoting=csv.QUOTE_ALL)
temperatureReader.next() # discard the first row, which is the table header
prevTemp = None
nextTemp = None
interpolatedTemp = -99
prevDate = None
nextDate = None
now = time.mktime(time.localtime()) # get current time in seconds since epoch
for row in temperatureReader:
dateString = row[0]
try:
date = time.mktime(time.strptime(dateString, "%Y-%m-%dT%H:%M:%S"))
except ValueError:
continue # skip dates that cannot be parsed
try:
temperature = float(row[1])
except ValueError:
if row[1].strip() == '':
# cell is left empty, this is allowed to disable temperature control in part of the profile
temperature = None
else:
# invalid number string, skip this row
continue
prevTemp = nextTemp
nextTemp = temperature
prevDate = nextDate
nextDate = date
timeDiff = now - nextDate
if timeDiff < 0:
if prevDate is None:
interpolatedTemp = nextTemp # first set point is in the future
break
else:
if prevTemp is None or nextTemp is None:
# When the previous or next temperature is an empty cell, disable temperature control.
# This is useful to stop temperature control after a while or to not start right away.
interpolatedTemp = None
else:
interpolatedTemp = ((now - prevDate) / (nextDate - prevDate) * (nextTemp - prevTemp) + prevTemp)
interpolatedTemp = round(interpolatedTemp, 2)
break
if interpolatedTemp == -99: # all set points in the past
interpolatedTemp = nextTemp
return interpolatedTemp