#!/usr/bin/python

import MySQLdb
import ConfigParser
import sys

from pprint import pprint
import datetime

import numpy as np

import scipy.signal

global database

def GetTables():
  if database:
    c = database.cursor()
    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")
    return c.fetchall()
  else:
    print "No connection to DB"
    exit()

def Today():
  dt = datetime.datetime.now()
  d_truncated = datetime.date(dt.year, dt.month, dt.day)
  return d_truncated
  
def Tomorrow():
  dt = Today()
  return dt + datetime.timedelta(days=1)

def Yesterday():
  dt = Today()
  return dt - datetime.timedelta(days=1)

def Prehistoric():
  dt = datetime.date(2000,01,01)
  return dt

def GetData(sid,pid,fromDate=Yesterday(),toDate=Today()):
  if database:
    c = database.cursor()
    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')])
    return c.fetchall()
  else:
    print "No connection to DB"
    exit()

def FixRecord(id,value):
  if database:
    c = database.cursor()
    command="UPDATE sensor_values  SET value={} WHERE id='{}'".format(value,id)
    c.execute(command)
  else:
    print "No connection to DB"
    exit()

def ProcessTable(sid,pid):

  if process_all:
    data=GetData(sid,pid,Prehistoric(),Today())
  elif not current:
    data=GetData(sid,pid)
  else:
    data=GetData(sid,pid,Today(),Tomorrow())  

  if not data:
    return

  sID=[]
  sTime=[]
  sValue=[]
  for rec in data:
    sID.append(rec[0])
    sTime.append(rec[1])
    sValue.append(rec[2])
  sValue=np.array(sValue)

  sValueFilt=scipy.signal.medfilt(sValue,5)

  sValueDiff=abs(sValue-sValueFilt)
  
  avg=np.mean(sValueDiff)

  for i in range(0,len(sTime)-1):
    if sValueDiff[i]>avg*filterThreshold:
      print "fixing %s : %5.2f %5.2f %5.2f" % (sTime[i],sValue[i],sValueFilt[i],sValueDiff[i])
      FixRecord(sID[i],sValueFilt[i])      

  database.commit()

if len(sys.argv)==2 and sys.argv[1]=='current':
  current=True
else:
  current=False

if len(sys.argv)==2 and sys.argv[1]=='all':
  process_all=True
else:
  process_all=False

try:

  cfg = ConfigParser.RawConfigParser(allow_no_value=True)
  cfg.readfp(open('/etc/weathermon.conf'))
  dbhost = cfg.get("mysql","host")
  dbuser = cfg.get("mysql","user")
  dbpasswd = cfg.get("mysql","passwd")
  dbdb = cfg.get("mysql","db")

  filterWindow = int(cfg.get("filter","window"))
  filterThreshold = float(cfg.get("filter","threshold"))
   
except:

  print "Error reading configuration file"
  exit()

try:

  database = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,db=dbdb,use_unicode=True)
  database.set_character_set('utf8')
  c = database.cursor()
  c.execute('SET NAMES utf8;')

  print "Connected..."

except:

  print "Error connecting database"
  exit()

tables = GetTables()

for sid,pid in tables:

  print "Processing sensor %d, parameter %d " % (sid,pid)

  ProcessTable(sid,pid)

print "Processed "
  