1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #!/usr/bin/python
import pyodbc
import time
import OpenOPC
import datetime
#Connect to sql databse on this computer
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=MONSERV;DATABASE=MABASE;UID=MONUSER;PWD=MONPASS')
cursor = cnxn.cursor()
while True:
#Launch stored procedure to get the information if we are in the concerned period for curtailment
cursor.execute("EXEC MAPROCEDURE")
rc = cursor.fetchval()
#print rc
#Current_date_time and format
now = datetime.datetime.now()
now_format = now.strftime("%Y-%m-%d %H:%M")
now_format = str(now_format)
#Get the current minute
minute_maj = time.strftime("%M")
if minute_maj[-1:] == '0' and minute[-1:] == '9':
#Case of end of 10min point
nv_10min = 1
minute = minute_maj
#Get 10min average on signal from DB
cursor.execute("SELECT MONFIELD FROM MATABLE ")
avg = cursor.fetchval()
avg = ("%.2f" % avg)
avg = str(avg)
#Erase results in the realtime table
cursor.execute("DELETE FROM MATABLE")
cnxn.commit()
#Insert into AVG table the result for the 10min point
cursor.execute("INSERT INTO MATABLE VALUES('" + now_format + "','MAVALUE'," + avg + ")")
cnxn.commit()
else:
#Case of pending 10min point
nv_10min = 0
minute = minute_maj
#Connect to OPC server
opc = OpenOPC.client()
opc.connect('OPCSERVER')
#Get value of current windspeed/round it/Send it to DB
value = opc['SIGNAL']
value = ("%.2f" % value)
value = str(value)
cursor.execute("INSERT INTO MATABLE VALUES('MAVALUE'," + value + ")")
cnxn.commit()
opc.close() |
Partager