-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadtemp.py
46 lines (36 loc) · 1018 Bytes
/
readtemp.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb as mdb
import commands
import os
logfile = '/var/log/pcsensor.log'
errfile = '/var/log/pcsensor.err'
if os.path.exists(logfile):
openmethod = 'a'
else:
openmethod = 'w'
logfile = open(logfile, openmethod)
if os.path.exists(errfile):
openmethod = 'a'
else:
openmethod = 'w'
errfile = open(errfile, openmethod)
output = commands.getoutput('/usr/local/bin/pcsensor')
cols = output.split()
output = output + '\n'
try:
con = mdb.connect('DBHOST', 'DBUSER', 'DMPASS', 'DB');
cur = con.cursor()
sql = "INSERT INTO `readings` (`ID`, `datetime`, `temp`) VALUES (NULL, '" + cols[0].replace('/','-') + " " + cols[1] +"', '"+cols[3][:-1]+"')"
cur.execute(sql)
con.commit()
logfile.write(output)
except mdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
errfile.write(output)
sys.exit(1)
finally:
if con:
con.close()
errfile.close()
logfile.close()