import psycopg2             #Needed to communicate with any PostgreSQL database
import urllib         #Needed to load the contents of a webpage into a variable
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:
   # 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
   kastmpz = 99.99
   kashumz = -1
   kasldrz = -1
   kaswatz = -1
   kasclkz = "2000-01-01 00:00:01"
   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]
      if row[2]:                       #Meaning this station is set to be activ
         link = "http://" + row[3]
         f = urllib.urlopen(link)
         myfile = f.read()
         datalist = myfile.split(' ')
         kasclkz = datalist[0] + " " + datalist[1]
         kastmpz = datalist[2]
         kashumz = datalist[3]
         kasldrz = datalist[4]
         kaswatz = datalist[5]
         print "    " , kastmpz, "    " , kashumz,"    " , kasldrz, "    " , kaswatz, "    " , kasclkz
   #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 ASC""")                 #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
                 , kasclk
                 ) VALUES (%s, %s, %s, %s, %s, %s, %s)""",(nextkey, stampz, kastmpz, kashumz, kasldrz, kaswatz, kasclkz))
   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
   time.sleep(300)   #Just wait 5 minutes before performing this read out again
