Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

def precal_table(tagpath,kWh):

tagid=system.db.runQuery('select id from sqlth_te where tagpath = "' +


tagpath + '/kwh" and retired is null',"sgg_EMS").getValueAt(0,0)
date=system.date.now()

#---------------Monthly------------------#

month="'"+str(system.date.format(date,"yyyy-MM-01 00:00:00"))+"'"
readquery="select Time,Value, Maximum, Minimum from Monthly where tagid=? and
time= "+month
val=system.db.runPrepQuery(readquery,[tagid],"sgg_EMS")
row_count=val.getRowCount()
if row_count==0:
insertquery=" insert into Monthly(tagid, time,
Value,Maximum,Minimum,Average,StdDev) values( " +str(tagid)+" , "+month+ ", "+
str(kWh) + ", " + str(kWh) + ", " + str(kWh) + ", " + str(kWh) + ", " + str(0)+" )"
system.db.runUpdateQuery(insertquery,"sgg_EMS")
else:
newvalue=val.getValueAt(0,1)+kWh
Max=val.getValueAt(0,2)
Min=val.getValueAt(0,3)
if Max is None:
Max=0
if Min is None:
Min=0
if kWh>Max:
Max=kWh
if kWh<Min:
Min=kWh
n=system.date.minutesBetween(val.getValueAt(0,0),date)/5
# if n==0:
# n=1
Avg=newvalue/(n+1)
updatequery="update Monthly set Value = "+str(newvalue)+", Maximum =
"+str(Max)+", Minimum = "+str(Min)+", Average = "+str(Avg)+" where tagid=
"+str(tagid)+" and time= "+ month
system.db.runUpdateQuery(updatequery,"sgg_EMS")

#----------------Daily----------------------#

day="'"+str(system.date.format(date,"yyyy-MM-dd 00:00:00"))+"'"
readquery="select Time,Value, Maximum, Minimum from Daily where tagid=? and
time= "+day
val=system.db.runPrepQuery(readquery,[tagid],"sgg_EMS")
row_count=val.getRowCount()
if row_count==0:
insertquery=" insert into Daily(tagid, time,
Value,Maximum,Minimum,Average,StdDev) values( " +str(tagid)+" , "+day+ ", "+
str(kWh) + ", " + str(kWh) + ", " + str(kWh) + ", " + str(kWh) + ", " + str(0)+" )"
system.db.runUpdateQuery(insertquery,"sgg_EMS")
else:
newvalue=val.getValueAt(0,1)+kWh
Max=val.getValueAt(0,2)
Min=val.getValueAt(0,3)
if Max is None:
Max=0
if Min is None:
Min=0
if kWh>Max:
Max=kWh
if kWh<Min:
Min=kWh
n=system.date.minutesBetween(val.getValueAt(0,0),date)/5
# if n==0:
# n=1
Avg=newvalue/(n+1)
updatequery="update Daily set Value = "+str(newvalue)+", Maximum =
"+str(Max)+", Minimum = "+str(Min)+", Average = "+str(Avg)+" where tagid=
"+str(tagid)+" and time= "+ day
system.db.runUpdateQuery(updatequery,"sgg_EMS")

#----------------Hourly----------------------#
hour ="'"+str(system.date.format(date,"yyyy-MM-dd HH:00:00"))+"'"
readquery="select Time,Value, Maximum, Minimum from Hourly where tagid=? and
time= "+hour
val=system.db.runPrepQuery(readquery,[tagid],"sgg_EMS")
row_count=val.getRowCount()
if row_count==0:
insertquery=" insert into Hourly(tagid, time,
Value,Maximum,Minimum,Average,StdDev) values( " +str(tagid)+" , "+hour+ ", "+
str(kWh) + ", " + str(kWh) + ", " + str(kWh) + ", " + str(kWh) + ", " + str(0)+" )"
system.db.runUpdateQuery(insertquery,"sgg_EMS")
else:
newvalue=val.getValueAt(0,1)+kWh
Max=val.getValueAt(0,2)
Min=val.getValueAt(0,3)
if Max is None:
Max = 0
if Min is None:
Min = 0
if kWh>Max:
Max=kWh
if kWh<Min:
Min=kWh
n=system.date.minutesBetween(val.getValueAt(0,0),date)/5
# if n==0:
# n=1
Avg=newvalue/(n+1)
updatequery="update Hourly set Value = "+str(newvalue)+", Maximum =
"+str(Max)+", Minimum = "+str(Min)+", Average = "+str(Avg)+" where tagid=
"+str(tagid)+" and time= "+ hour
system.db.runUpdateQuery(updatequery,"sgg_EMS")

You might also like