7 from pprint import pprint
19 c.execute("SELECT * FROM Items WHERE ItemName like %s",[name])
22 print "No connection to DB"
26 dt = datetime.datetime.now()
27 d_truncated = datetime.date(dt.year, dt.month, dt.day)
32 return dt + datetime.timedelta(days=1)
36 return dt - datetime.timedelta(days=1)
38 def GetData(id,fromDate=Yesterday(),toDate=Today()):
41 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')])
44 print "No connection to DB"
47 def FixRecord(id,date,value):
50 command="UPDATE Item"+str(id).strip()+" SET Value={} WHERE Time='{}'".format(value,date.strftime('%Y-%m-%d %H:%M:%S'))
54 print "No connection to DB"
62 data=GetData(id,Today(),Tomorrow())
68 sValue=np.array(sValue)
70 sValueFilt=scipy.signal.medfilt(sValue,5)
72 sValueDiff=abs(sValue-sValueFilt)
74 avg=np.mean(sValueDiff)
76 for i in range(0,len(sTime)-1):
77 if sValueDiff[i]>avg*filterThreshold:
78 print "fixing %s : %5.2f %5.2f %5.2f" % (sTime[i],sValue[i],sValueFilt[i],sValueDiff[i])
79 FixRecord(id,sTime[i],sValueFilt[i])
83 if len(sys.argv)==2 and sys.argv[1]=='current':
91 cfg = ConfigParser.RawConfigParser(allow_no_value=True)
92 cfg.readfp(open('/etc/openhab-db.conf'))
93 dbhost = cfg.get("mysql","host")
94 dbuser = cfg.get("mysql","user")
95 dbpasswd = cfg.get("mysql","passwd")
96 dbdb = cfg.get("mysql","db")
98 itemTemplate = cfg.get("openhab","template")
100 filterWindow = int(cfg.get("filter","window"))
101 filterThreshold = float(cfg.get("filter","threshold"))
105 print "Error reading configuration file"
110 database = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,db=dbdb,use_unicode=True)
111 database.set_character_set('utf8')
112 c = database.cursor()
113 c.execute('SET NAMES utf8;')
119 print "Error connecting database"
122 tables = GetTables(itemTemplate)
124 for id,name in tables:
126 print "Processing: "+name