6 from pprint import pprint
18 c.execute("SELECT * FROM Items WHERE ItemName like %s",[name])
21 print "No connection to DB"
25 dt = datetime.datetime.now()
26 d_truncated = datetime.date(dt.year, dt.month, dt.day)
31 return dt + datetime.timedelta(days=1)
35 return dt - datetime.timedelta(days=1)
37 def GetData(id,fromDate=Yesterday(),toDate=Today()):
40 c.execute("SELECT * FROM Item"+str(id).strip()+" WHERE Time>=%s AND Time<%s",[fromDate.strftime('%Y-%m-%d %H:%M:%S'),toDate.strftime('%Y-%m-%d %H:%M:%S')])
43 print "No connection to DB"
46 def FixRecord(id,date,value):
49 command="UPDATE Item"+str(id).strip()+" SET Value={} WHERE Time='{}'".format(value,date.strftime('%Y-%m-%d %H:%M:%S'))
53 print "No connection to DB"
58 cfg = ConfigParser.RawConfigParser(allow_no_value=True)
59 cfg.readfp(open('/etc/openhab-db.conf'))
60 dbhost = cfg.get("mysql","host")
61 dbuser = cfg.get("mysql","user")
62 dbpasswd = cfg.get("mysql","passwd")
63 dbdb = cfg.get("mysql","db")
65 itemTemplate = cfg.get("openhab","template")
67 filterWindow = int(cfg.get("filter","window"))
68 filterThreshold = float(cfg.get("filter","threshold"))
72 print "Error reading configuration file"
77 database = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,db=dbdb,use_unicode=True)
78 database.set_character_set('utf8')
80 c.execute('SET NAMES utf8;')
86 print "Error connecting database"
89 tables = GetTables(itemTemplate)
91 for id,name in tables:
93 print "Processing: "+name
100 sValue.append(rec[1])
101 sValue=np.array(sValue)
103 sValueFilt=scipy.signal.medfilt(sValue,5)
105 sValueDiff=abs(sValue-sValueFilt)
107 avg=np.mean(sValueDiff)
109 for i in range(0,len(sTime)-1):
110 if sValueDiff[i]>avg*filterThreshold:
111 print "fixing %s : %5.2f %5.2f %5.2f" % (sTime[i],sValue[i],sValueFilt[i],sValueDiff[i])
112 FixRecord(id,sTime[i],sValueFilt[i])