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)
53 print "No connection to DB"
56 def ProcessTable(sid,pid):
61 data=GetData(sid,pid,Today(),Tomorrow())
73 sValue=np.array(sValue)
75 sValueFilt=scipy.signal.medfilt(sValue,5)
77 sValueDiff=abs(sValue-sValueFilt)
79 avg=np.mean(sValueDiff)
81 for i in range(0,len(sTime)-1):
82 if sValueDiff[i]>avg*filterThreshold:
83 print "fixing %s : %5.2f %5.2f %5.2f" % (sTime[i],sValue[i],sValueFilt[i],sValueDiff[i])
84 FixRecord(sID[i],sValueFilt[i])
88 if len(sys.argv)==2 and sys.argv[1]=='current':
96 cfg = ConfigParser.RawConfigParser(allow_no_value=True)
97 cfg.readfp(open('/etc/weathermon.conf'))
98 dbhost = cfg.get("mysql","host")
99 dbuser = cfg.get("mysql","user")
100 dbpasswd = cfg.get("mysql","passwd")
101 dbdb = cfg.get("mysql","db")
103 filterWindow = int(cfg.get("filter","window"))
104 filterThreshold = float(cfg.get("filter","threshold"))
108 print "Error reading configuration file"
113 database = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,db=dbdb,use_unicode=True)
114 database.set_character_set('utf8')
115 c = database.cursor()
116 c.execute('SET NAMES utf8;')
122 print "Error connecting database"
127 for sid,pid in tables:
129 print "Processing sensor %d, parameter %d " % (sid,pid)
131 ProcessTable(sid,pid)