7 from pprint import pprint
19 c.execute("SELECT s.id sid,p.id pid FROM sensors s,st_parameters p where s.st_id=p.st_id and p.id>=0")
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(sid,pid,fromDate=Yesterday(),toDate=Today()):
41 c.execute("SELECT id,timestamp,value FROM sensor_values WHERE sensor_id=%s and parameter_id=%s and timestamp>=%s AND timestamp<%s",[sid,pid,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,value):
50 command="UPDATE sensor_values SET value={} WHERE id='{}'".format(value,id)
54 print "No connection to DB"
57 def ProcessTable(sid,pid):
62 data=GetData(sid,pid,Today(),Tomorrow())
70 sValue=np.array(sValue)
72 sValueFilt=scipy.signal.medfilt(sValue,5)
74 sValueDiff=abs(sValue-sValueFilt)
76 avg=np.mean(sValueDiff)
78 print "Average="+str(avg)
80 for i in range(0,len(sTime)-1):
81 if sValueDiff[i]>avg*filterThreshold:
82 print "fixing %s : %5.2f %5.2f %5.2f" % (sTime[i],sValue[i],sValueFilt[i],sValueDiff[i])
83 FixRecord(sID[i],sValueFilt[i])
87 if len(sys.argv)==2 and sys.argv[1]=='current':
95 cfg = ConfigParser.RawConfigParser(allow_no_value=True)
96 cfg.readfp(open('/etc/weathermon.conf'))
97 dbhost = cfg.get("mysql","host")
98 dbuser = cfg.get("mysql","user")
99 dbpasswd = cfg.get("mysql","passwd")
100 dbdb = cfg.get("mysql","db")
102 filterWindow = int(cfg.get("filter","window"))
103 filterThreshold = float(cfg.get("filter","threshold"))
107 print "Error reading configuration file"
112 database = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,db=dbdb,use_unicode=True)
113 database.set_character_set('utf8')
114 c = database.cursor()
115 c.execute('SET NAMES utf8;')
121 print "Error connecting database"
126 for sid,pid in tables:
128 print "Processing sensor %d, parameter %d " % (sid,pid)
130 ProcessTable(sid,pid)