-
Notifications
You must be signed in to change notification settings - Fork 0
/
confighandler.py
executable file
·79 lines (61 loc) · 1.94 KB
/
confighandler.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
# Config file handler
import ConfigParser
import mysql.connector
import GLOBALS
Config = ConfigParser.ConfigParser()
def write_new_config(path):
if path == "":
print 'Path cannot be empty'
else:
cfgfile = open(path,'w')
# Setpoints. Used by main program to determine temperature constraints
Config.add_section('SetPoint')
Config.set('SetPoint','temperature','21.0')
Config.set('SetPoint','deadband','1.0')
# GPIO pins in use
Config.add_section('GPIO')
Config.set('GPIO','fridgepin','')
Config.set('GPIO','heaterpin','')
# Add temperature sensors
Config.add_section('TempSensor')
Config.set('TempSensor','path','/sys/devices/w1/bus/devices/28-00000XXXXXXX/w1_slave')
# Controller settings
Config.add_section('Control')
Config.set('Control','checkintervalseconds','60')
Config.set('Control','enabled','true')
# Database settings
Config.add_section('Database')
Config.set('Database','EnableLogging','False')
Config.set('Database','hostname','localhost')
Config.set('Database','username','username')
Config.set('Database','password','password')
Config.set('Database','dbname','dbname')
Config.write(cfgfile)
cfgfile.close()
def settemperature(path, temp):
# update in local control variable
float(temp)
Config.read(path)
# obtain file lock so no other thread can modify
GLOBALS.ConfigLock.acquire()
cfgfile = open(path, 'w')
Config.set('SetPoint','temperature',temp)
Config.write(cfgfile)
cfgfile.close()
# release file lock
GLOBALS.ConfigLock.release()
def setdeadband(path, temp):
float(temp)
Config.read(path)
# obtain file lock so no other thread can modify
GLOBALS.ConfigLock.acquire()
cfgfile = open(path, 'w')
Config.set('SetPoint','deadband',temp)
Config.write(cfgfile)
cfgfile.close()
# release file lock
GLOBALS.ConfigLock.release()
if __name__ == "__main__":
print 'Writing new config file...'
write_new_config("config.ini")
print 'Write successful.'