import psycopg2             #Needed to communicate with any PostgreSQL database
import urllib         #Needed to load the contents of a webpage into a variable
import urllib2                   #URLLIB2 seems to function better for requests https://docs.python.org/2/howto/urllib2.html
import datetime                #Needed to create a timestamp for the datarecord
import time           #Needed to be able to perform the request every 5 minutes


while True:                                 #Meaning this loop will run forever
   # This is a list of all the data that will be written into the database when
   # stations are not activated. All stations must be asked in one sequence.
   now = datetime.datetime.now()                          #Read the system time
   stampz    = now.strftime("%Y-%m-%d %H:%M:%S")   #Format to a database format

   proptmpz   = 99.99
   prophumz   = -1
   propklokz  = "2003-01-01 00:00:01"
   proprel1z  = -1
   proprel2z  = -1
   proprel3z  = -1
   proprel4z  = -1

   kastmpz   = 99.99
   kashumz   = -1
   kasldrz   = -1
   kaswatz   = -1
   kasklokz  = "2003-01-01 00:00:01"

   tuintmpz  = 99.99
   tuinldrz  = -1
   tuinwatz  = -1
   tuinklokz = "2003-01-01 00:00:01"

   voorpir1z = -1
   voortmp1z = 99.99
   voorldr1z = -1
   voorhum1z = -1
   voorprg1z = -1

   mq2z = -1
   mq3z = -1
   mq5z = -1
   mq9z = -1


   conn = psycopg2.connect(host="localhost",database="robotigs", 
      user="richard", password="Ha-8-H+")        #Create a database connection

   cur1 = conn.cursor()                       #Create a thread to the database
   cur1.execute("""SELECT *  
                     FROM florastations 
                 ORDER BY name""")                     #Define database action
   rows = cur1.fetchall()                             #Perform database action

   print "\n" + stampz
   for row in rows:
      print " ",  row[3], " ",  row[2], " ", row[1],  " ",  row[4]




   #Now we have read all stations we can write the data into the database
   #First determin the first available free key in the file florameas
   cur2 = conn.cursor()
   cur2.execute("""SELECT mesid  
                     FROM florameas 
                 ORDER BY mesid DESC
                    LIMIT 1""")                         #Define database action
   rows2 = cur2.fetchall()	           #Read the latest entry of this table
   nextkey = 1                     #Just to cath an empty table of measurements
   for row2 in rows2:
      nextkey = row2[0]
      nextkey += 1
   #Now we can create a new measurement in the database table florameas
   cur2.execute(""" INSERT INTO florameas (
                   mesid
                 , stamp
                 , kastmp
                 , kashum
                 , kasldr
                 , kaswat
                 , kasklok
                 , tuintmp
                 , tuinldr
                 , tuinwat
                 , tuinklok
                 , proptmp
                 , prophum
                 , propklok
                 , proprel1
                 , proprel2
                 , proprel3
                 , proprel4
                 , voorpir1
                 , voortmp1
                 , voorldr1
                 , voorhum1
                 , mq5
                 , mq7
                 , mq8
                 , mq135
                 ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",(nextkey, stampz, kastmpz, kashumz, kasldrz, kaswatz, kasklokz, tuintmpz, tuinldrz, tuinwatz, tuinklokz, proptmpz, prophumz, propklokz, proprel1z, proprel2z, proprel3z, proprel4z, voorpir1z, voortmp1z, voorldr1z, voorhum1z, mq2z, mq3z, mq5z, mq9z))
   conn.commit()

   cur2.close()                              #Close this thread to the database
   cur1.close()                              #Close this thread to the database
   conn.close()                          #Close this connection to the database
   print "We gaan 5 minuten slapen"
   time.sleep(300)   #Just wait 5 minutes before performing this read out again
